From d8a14abac381ef1cb82d0234bc4c8d11a913f769 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Thu, 10 Dec 2020 23:22:10 +0100 Subject: [PATCH] Improve proxy policy handling, new fallback policy --- docs/api.md | 2 +- src/claims.js | 60 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 53 insertions(+), 9 deletions(-) diff --git a/docs/api.md b/docs/api.md index 0584d90..10b2958 100644 --- a/docs/api.md +++ b/docs/api.md @@ -19,7 +19,7 @@ Verifies the identity behind the provided **uri** using the **fingerprint**. | Name | Type | Default value | Description | | ----------------- | ------- | -------------------- | ------------------------------------------------------------------- | | 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 | When the `proxyPolicy` option is to `adaptive`, the chosen strategy is diff --git a/src/claims.js b/src/claims.js index a3993dd..8d1dd41 100644 --- a/src/claims.js +++ b/src/claims.js @@ -149,7 +149,8 @@ const verify = async (input, fingerprint, opts) => { }) } - const uri = input + const uri = input.replace(/^\s+|\s+$/g, '') + let verifErrors = [] if (!fingerprint) { fingerprint = null @@ -171,6 +172,7 @@ const verify = async (input, fingerprint, opts) => { if ('returnMatchesOnly' in opts && opts.returnMatchesOnly) { return spMatches } + let claimVerificationDone = false, claimVerificationResult, sp, @@ -178,6 +180,7 @@ const verify = async (input, fingerprint, opts) => { res, proofData, spData + while (!claimVerificationDone && iSp < spMatches.length) { spData = spMatches[iSp] spData.claim.fingerprint = fingerprint @@ -185,14 +188,52 @@ const verify = async (input, fingerprint, opts) => { res = null if (spData.customRequestHandler instanceof Function) { - proofData = await spData.customRequestHandler(spData, opts) - } else if ( - !spData.proof.useProxy || - ('proxyPolicy' in opts && !opts.useProxyWhenNeeded) - ) { - proofData = await serviceproviders.directRequestHandler(spData, opts) + try { + proofData = await spData.customRequestHandler(spData, opts) + } catch (e) { + verifErrors.push('custom_request_handler_failed') + } } else { - proofData = await serviceproviders.proxyRequestHandler(spData, opts) + switch (opts.proxyPolicy) { + case 'adaptive': + if (spData.proof.useProxy) { + try { + 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) { @@ -201,6 +242,8 @@ const verify = async (input, fingerprint, opts) => { if (claimVerificationResult.errors.length == 0) { claimVerificationDone = true } + } else { + verifErrors.push('unsuccessful_claim_verification') } iSp++ @@ -214,6 +257,7 @@ const verify = async (input, fingerprint, opts) => { return { isVerified: claimVerificationResult.isVerified, + errors: verifErrors, serviceproviderData: spData, } }