From 6d5c9d0097b7f5ae610ac32e1e3731e8984402af Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 25 Feb 2019 15:58:02 -0700 Subject: [PATCH] v1.1.2: DRY key check loop --- keyfetch.js | 70 ++++++++++++++++------------------------------------ package.json | 2 +- 2 files changed, 22 insertions(+), 50 deletions(-) diff --git a/keyfetch.js b/keyfetch.js index 0e5b6ae..a012e2f 100644 --- a/keyfetch.js +++ b/keyfetch.js @@ -111,64 +111,36 @@ keyfetch.oidcJwks = function (iss) { }); }); }; -keyfetch.oidcJwk = function (id, iss) { - // TODO [2] DRY this up a bit - return keyfetch._checkCache(id, iss).then(function (hit) { - if (hit) { - return Promise.resolve(hit); +function checkId(id) { + return function (results) { + var result = results.some(function (result) { + // we already checked iss above + console.log(result); + return result.jwk.kid === id || result.thumbprint === id; + })[0]; + + if (!result) { + throw new Error("No JWK found by kid or thumbprint '" + id + "'"); } - - return keyfetch.oidcJwks(iss).then(function (results) { - var result = results.some(function (result) { - // we already checked iss above - return result.jwk.kid === id || result.thumbprint === id; - })[0]; - - if (!result) { - throw new Error("No JWK found by kid or thumbprint '" + id + "'"); - } - return result; - }); + return result; + }; +} +keyfetch.oidcJwk = function (id, iss) { + return keyfetch._checkCache(id, iss).then(function (hit) { + if (hit) { return hit; } + return keyfetch.oidcJwks(iss).then(checkId(id)); }); }; keyfetch.wellKnownJwk = function (id, iss) { - // TODO [2] DRY this up a bit return keyfetch._checkCache(id, iss).then(function (hit) { - if (hit) { - return Promise.resolve(hit); - } - - return keyfetch.wellKnownJwks(iss).then(function (results) { - var result = results.some(function (result) { - // we already checked iss above - return result.jwk.kid === id || result.thumbprint === id; - })[0]; - - if (!result) { - throw new Error("No JWK found by kid or thumbprint '" + id + "'"); - } - return result; - }); + if (hit) { return hit; } + return keyfetch.wellKnownJwks(iss).then(checkId(id)); }); }; keyfetch.jwk = function (id, jwksUrl) { - // TODO [2] DRY this up a bit return keyfetch._checkCache(id, jwksUrl).then(function (hit) { - if (hit) { - return Promise.resolve(hit); - } - - return keyfetch.jwks(jwksUrl).then(function (results) { - var result = results.some(function (result) { - // we already checked iss above - return result.jwk.kid === id || result.thumbprint === id; - })[0]; - - if (!result) { - throw new Error("No JWK found by kid or thumbprint '" + id + "'"); - } - return result; - }); + if (hit) { return hit; } + return keyfetch.jwks(jwksUrl).then(checkId(id)); }); }; keyfetch._checkCache = function (id, iss) { diff --git a/package.json b/package.json index d87abfb..90c8d2f 100644 --- a/package.json +++ b/package.json @@ -31,5 +31,5 @@ "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, - "version": "1.1.1" + "version": "1.1.2" }