Improve proxy policy handling, new fallback policy

This commit is contained in:
Yarmo Mackenbach 2020-12-10 23:22:10 +01:00
parent eca111aaa7
commit d8a14abac3
2 changed files with 53 additions and 9 deletions

View file

@ -19,7 +19,7 @@ Verifies the identity behind the provided **uri** using the **fingerprint**.
| Name | Type | Default value | Description | | Name | Type | Default value | Description |
| ----------------- | ------- | -------------------- | ------------------------------------------------------------------- | | ----------------- | ------- | -------------------- | ------------------------------------------------------------------- |
| returnMatchesOnly | boolean | false | only return matching service providers, do not attempt verification | | returnMatchesOnly | boolean | false | only return matching service providers, do not attempt verification |
| proxyPolicy | string | 'adaptive' | when to use a proxy ['adaptive', 'always', 'never'] | | proxyPolicy | string | 'adaptive' | when to use a proxy ['adaptive', 'fallback', 'always', 'never'] |
| doipProxyHostname | string | 'proxy.keyoxide.org' | the hostname of the proxy server | | doipProxyHostname | string | 'proxy.keyoxide.org' | the hostname of the proxy server |
When the `proxyPolicy` option is to `adaptive`, the chosen strategy is When the `proxyPolicy` option is to `adaptive`, the chosen strategy is

View file

@ -149,7 +149,8 @@ const verify = async (input, fingerprint, opts) => {
}) })
} }
const uri = input const uri = input.replace(/^\s+|\s+$/g, '')
let verifErrors = []
if (!fingerprint) { if (!fingerprint) {
fingerprint = null fingerprint = null
@ -171,6 +172,7 @@ const verify = async (input, fingerprint, opts) => {
if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) {
return spMatches return spMatches
} }
let claimVerificationDone = false, let claimVerificationDone = false,
claimVerificationResult, claimVerificationResult,
sp, sp,
@ -178,6 +180,7 @@ const verify = async (input, fingerprint, opts) => {
res, res,
proofData, proofData,
spData spData
while (!claimVerificationDone && iSp < spMatches.length) { while (!claimVerificationDone && iSp < spMatches.length) {
spData = spMatches[iSp] spData = spMatches[iSp]
spData.claim.fingerprint = fingerprint spData.claim.fingerprint = fingerprint
@ -185,14 +188,52 @@ const verify = async (input, fingerprint, opts) => {
res = null res = null
if (spData.customRequestHandler instanceof Function) { if (spData.customRequestHandler instanceof Function) {
try {
proofData = await spData.customRequestHandler(spData, opts) proofData = await spData.customRequestHandler(spData, opts)
} else if ( } catch (e) {
!spData.proof.useProxy || verifErrors.push('custom_request_handler_failed')
('proxyPolicy' in opts && !opts.useProxyWhenNeeded) }
) {
proofData = await serviceproviders.directRequestHandler(spData, opts)
} else { } else {
switch (opts.proxyPolicy) {
case 'adaptive':
if (spData.proof.useProxy) {
try {
proofData = await serviceproviders.proxyRequestHandler(spData, opts) proofData = await serviceproviders.proxyRequestHandler(spData, opts)
} catch(er) {}
} else {
try {
proofData = await serviceproviders.directRequestHandler(spData, opts)
} catch(er) {}
if (!proofData) {
try {
proofData = await serviceproviders.proxyRequestHandler(spData, opts)
} catch(er) {}
}
}
break;
case 'fallback':
try {
proofData = await serviceproviders.directRequestHandler(spData, opts)
} catch(er) {}
if (!proofData) {
try {
proofData = await serviceproviders.proxyRequestHandler(spData, opts)
} catch(er) {}
}
break;
case 'always':
try {
proofData = await serviceproviders.proxyRequestHandler(spData, opts)
} catch(er) {}
break;
case 'never':
try {
proofData = await serviceproviders.directRequestHandler(spData, opts)
} catch(er) {}
break;
default:
verifErrors.push('invalid_proxy_policy')
}
} }
if (proofData) { if (proofData) {
@ -201,6 +242,8 @@ const verify = async (input, fingerprint, opts) => {
if (claimVerificationResult.errors.length == 0) { if (claimVerificationResult.errors.length == 0) {
claimVerificationDone = true claimVerificationDone = true
} }
} else {
verifErrors.push('unsuccessful_claim_verification')
} }
iSp++ iSp++
@ -214,6 +257,7 @@ const verify = async (input, fingerprint, opts) => {
return { return {
isVerified: claimVerificationResult.isVerified, isVerified: claimVerificationResult.isVerified,
errors: verifErrors,
serviceproviderData: spData, serviceproviderData: spData,
} }
} }