Use request handlers

This commit is contained in:
Yarmo Mackenbach 2020-10-26 21:18:17 +01:00
parent bb58fc3614
commit 8d44b4f9bc
2 changed files with 31 additions and 16 deletions

View file

@ -14,8 +14,6 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const validUrl = require('valid-url') const validUrl = require('valid-url')
const bent = require('bent')
const req = bent('GET')
const serviceproviders = require('./serviceproviders') const serviceproviders = require('./serviceproviders')
const claimVerification = require('./claimVerification') const claimVerification = require('./claimVerification')
const utils = require('./utils') const utils = require('./utils')
@ -41,20 +39,12 @@ const verify = async (uri, fingerprint, opts) => {
res = null res = null
if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) { if (spData.customRequestHandler instanceof Function) {
res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri) proofData = spData.customRequestHandler(spData, opts)
} else if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) {
switch (spData.proof.format) { proofData = serviceproviders.directRequestHandler(spData)
case 'json': } else {
proofData = await res.json() proofData = serviceproviders.proxyRequestHandler(spData)
break
case 'text':
proofData = await res.text()
break
default:
throw new Error('No specified proof data format')
break
}
} }
claimHasBeenVerified = claimVerification.run(proofData, spData) claimHasBeenVerified = claimVerification.run(proofData, spData)

View file

@ -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 See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
const bent = require('bent')
const req = bent('GET')
const list = [ const list = [
'dns', 'dns',
'xmpp', 'xmpp',
@ -50,6 +53,28 @@ const match = (uri, opts) => {
return matches 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.list = list
exports.data = data exports.data = data
exports.match = match exports.match = match
exports.directRequestHandler = directRequestHandler
exports.proxyRequestHandler = proxyRequestHandler