v1.1.2: DRY key check loop

This commit is contained in:
AJ ONeal 2019-02-25 15:58:02 -07:00
parent 6c071af9cd
commit 6d5c9d0097
2 changed files with 22 additions and 50 deletions

View File

@ -111,64 +111,36 @@ keyfetch.oidcJwks = function (iss) {
}); });
}); });
}; };
keyfetch.oidcJwk = function (id, iss) { function checkId(id) {
// TODO [2] DRY this up a bit return function (results) {
return keyfetch._checkCache(id, iss).then(function (hit) { var result = results.some(function (result) {
if (hit) { // we already checked iss above
return Promise.resolve(hit); 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 result;
return keyfetch.oidcJwks(iss).then(function (results) { };
var result = results.some(function (result) { }
// we already checked iss above keyfetch.oidcJwk = function (id, iss) {
return result.jwk.kid === id || result.thumbprint === id; return keyfetch._checkCache(id, iss).then(function (hit) {
})[0]; if (hit) { return hit; }
return keyfetch.oidcJwks(iss).then(checkId(id));
if (!result) {
throw new Error("No JWK found by kid or thumbprint '" + id + "'");
}
return result;
});
}); });
}; };
keyfetch.wellKnownJwk = function (id, iss) { keyfetch.wellKnownJwk = function (id, iss) {
// TODO [2] DRY this up a bit
return keyfetch._checkCache(id, iss).then(function (hit) { return keyfetch._checkCache(id, iss).then(function (hit) {
if (hit) { if (hit) { return hit; }
return Promise.resolve(hit); return keyfetch.wellKnownJwks(iss).then(checkId(id));
}
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;
});
}); });
}; };
keyfetch.jwk = function (id, jwksUrl) { keyfetch.jwk = function (id, jwksUrl) {
// TODO [2] DRY this up a bit
return keyfetch._checkCache(id, jwksUrl).then(function (hit) { return keyfetch._checkCache(id, jwksUrl).then(function (hit) {
if (hit) { if (hit) { return hit; }
return Promise.resolve(hit); return keyfetch.jwks(jwksUrl).then(checkId(id));
}
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;
});
}); });
}; };
keyfetch._checkCache = function (id, iss) { keyfetch._checkCache = function (id, iss) {

View File

@ -31,5 +31,5 @@
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },
"version": "1.1.1" "version": "1.1.2"
} }