diff --git a/src/index.js b/src/index.js index 863d6db..a0cb6cf 100644 --- a/src/index.js +++ b/src/index.js @@ -14,8 +14,6 @@ See the License for the specific language governing permissions and limitations under the License. */ const validUrl = require('valid-url') -const bent = require('bent') -const req = bent('GET') const serviceproviders = require('./serviceproviders') const claimVerification = require('./claimVerification') const utils = require('./utils') @@ -41,20 +39,12 @@ const verify = async (uri, fingerprint, opts) => { res = null - if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) { - res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri) - - switch (spData.proof.format) { - case 'json': - proofData = await res.json() - break - case 'text': - proofData = await res.text() - break - default: - throw new Error('No specified proof data format') - break - } + if (spData.customRequestHandler instanceof Function) { + proofData = spData.customRequestHandler(spData, opts) + } else if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) { + proofData = serviceproviders.directRequestHandler(spData) + } else { + proofData = serviceproviders.proxyRequestHandler(spData) } claimHasBeenVerified = claimVerification.run(proofData, spData) diff --git a/src/serviceproviders.js b/src/serviceproviders.js index 680c398..5ce99b4 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -13,6 +13,9 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +const bent = require('bent') +const req = bent('GET') + const list = [ 'dns', 'xmpp', @@ -50,6 +53,28 @@ const match = (uri, opts) => { return matches } +const directRequestHandler = (spData) => { + const res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri) + + switch (spData.proof.format) { + case 'json': + return await res.json() + break + case 'text': + return await res.text() + break + default: + throw new Error('No specified proof data format') + break + } +} + +const proxyRequestHandler = (spData) => { + return null +} + exports.list = list exports.data = data exports.match = match +exports.directRequestHandler = directRequestHandler +exports.proxyRequestHandler = proxyRequestHandler