From 86aa366ccc00dc31a23c19a4d51657776aa7bf57 Mon Sep 17 00:00:00 2001 From: Yarmo Mackenbach Date: Fri, 6 Nov 2020 00:43:32 +0100 Subject: [PATCH] Remove dotenv and better handle options --- package.json | 1 - src/index.js | 10 +++++++--- src/serviceproviders.js | 6 +++--- src/utils.js | 8 ++------ yarn.lock | 5 ----- 5 files changed, 12 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 6b40e51..c2bc2cd 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,6 @@ "dependencies": { "bent": "^7.3.12", "browserify": "^17.0.0", - "dotenv": "^8.2.0", "valid-url": "^1.0.9" }, "devDependencies": { diff --git a/src/index.js b/src/index.js index b8da4c3..83f87e2 100644 --- a/src/index.js +++ b/src/index.js @@ -22,6 +22,10 @@ const verify = async (uri, fingerprint, opts) => { if (!fingerprint) { fingerprint = null } if (!opts) { opts = {} } + if (!('doipProxyHostname' in opts) || !opts.doipProxyHostname) { + opts.doipProxyHostname = 'proxy.keyoxide.org' + } + if (!validUrl.isUri(uri)) { throw new Error('Not a valid URI') } @@ -40,10 +44,10 @@ const verify = async (uri, fingerprint, opts) => { if (spData.customRequestHandler instanceof Function) { proofData = await spData.customRequestHandler(spData, opts) - } else if (!spData.proof.useProxy || 'useProxyWhenNeeded' in opts && !opts.useProxyWhenNeeded) { - proofData = await serviceproviders.directRequestHandler(spData) + } else if (!spData.proof.useProxy || 'proxyPolicy' in opts && !opts.useProxyWhenNeeded) { + proofData = await serviceproviders.directRequestHandler(spData, opts) } else { - proofData = await serviceproviders.proxyRequestHandler(spData) + proofData = await serviceproviders.proxyRequestHandler(spData, opts) } if (proofData) { diff --git a/src/serviceproviders.js b/src/serviceproviders.js index 32a32df..d36bf04 100644 --- a/src/serviceproviders.js +++ b/src/serviceproviders.js @@ -64,7 +64,7 @@ const match = (uri, opts) => { return matches } -const directRequestHandler = async (spData) => { +const directRequestHandler = async (spData, opts) => { const res = await req(spData.proof.fetch ? spData.proof.fetch : spData.proof.uri, null, { Accept: 'application/json' }) switch (spData.proof.format) { @@ -80,9 +80,9 @@ const directRequestHandler = async (spData) => { } } -const proxyRequestHandler = async (spData) => { +const proxyRequestHandler = async (spData, opts) => { const url = spData.proof.fetch ? spData.proof.fetch : spData.proof.uri - const res = await req(utils.generateProxyURL(spData.proof.format, url), null, { Accept: 'application/json' }) + const res = await req(utils.generateProxyURL(spData.proof.format, url, opts), null, { Accept: 'application/json' }) const json = await res.json() return json.content } diff --git a/src/utils.js b/src/utils.js index f755236..a3518bb 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,9 +1,5 @@ -require('dotenv').config() - -const DOIP_PROXY_HOSTNAME = process.env.DOIP_PROXY_HOSTNAME || 'proxy.keyoxide.org' - -const generateProxyURL = (type, url) => { - return `https://${DOIP_PROXY_HOSTNAME}/api/1/get/${type}/${encodeURIComponent(url)}` +const generateProxyURL = (type, url, opts) => { + return `https://${opts.doipProxyHostname}/api/1/get/${type}/${encodeURIComponent(url)}` } const generateClaim = (fingerprint, format) => { diff --git a/yarn.lock b/yarn.lock index 6d31534..9d1df5e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -734,11 +734,6 @@ dot-case@^3.0.3: no-case "^3.0.3" tslib "^1.10.0" -dotenv@^8.2.0: - version "8.2.0" - resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-8.2.0.tgz#97e619259ada750eea3e4ea3e26bceea5424b16a" - integrity sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw== - duplexer2@^0.1.2, duplexer2@~0.1.0, duplexer2@~0.1.2: version "0.1.4" resolved "https://registry.yarnpkg.com/duplexer2/-/duplexer2-0.1.4.tgz#8b12dab878c0d69e3e7891051662a32fc6bddcc1"