Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Callback backwards compatibility #227

Merged
merged 1 commit into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/JwksClient.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const debug = require('debug');
const { retrieveSigningKeys } = require('./utils') ;
const { request, cacheSigningKey, rateLimitSigningKey, getKeysInterceptor } = require('./wrappers');
const { request, cacheSigningKey, rateLimitSigningKey, getKeysInterceptor, callbackSupport } = require('./wrappers');
const JwksError = require('./errors/JwksError');
const SigningKeyNotFoundError = require('./errors/SigningKeyNotFoundError');

Expand All @@ -25,6 +25,8 @@ class JwksClient {
if (this.options.cache) {
this.getSigningKey = cacheSigningKey(this, options);
}

this.getSigningKey = callbackSupport(this, options);
}

async getKeys() {
Expand Down
16 changes: 16 additions & 0 deletions src/wrappers/callbackSupport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { callbackify } = require('util');

const callbackSupport = (client) => {
const getSigningKey = client.getSigningKey.bind(client);

return (kid, cb) => {
if (cb) {
const callbackFunc = callbackify(getSigningKey);
return callbackFunc(kid, cb);
}

return getSigningKey(kid);
};
};

module.exports.default = callbackSupport;
3 changes: 2 additions & 1 deletion src/wrappers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ module.exports = {
request: require('./request').default,
cacheSigningKey: require('./cache').default,
rateLimitSigningKey: require('./rateLimit').default,
getKeysInterceptor: require('./interceptor').default
getKeysInterceptor: require('./interceptor').default,
callbackSupport: require('./callbackSupport').default
};
34 changes: 34 additions & 0 deletions tests/jwksClient.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,40 @@ describe('JwksClient', () => {
});

describe('#getSigningKey', () => {
describe('when a callback is supplied', () => {
it('should return the key found to the callback', (done) => {
nock(jwksHost)
.get('/.well-known/jwks.json')
.reply(200, x5cMultiple);

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`
});
const key = x5cMultiple.keys[0];

client.getSigningKey(key.kid, (err, foundKey) => {
expect(foundKey.kid).to.equal(key.kid);
done();
});
});

it('should return any errors to the callback', (done) => {
nock(jwksHost)
.get('/.well-known/jwks.json')
.reply(200, x5cMultiple);

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`
});

client.getSigningKey('123', (err) => {
expect(err).not.to.be.null;
expect(err.name).to.equal('SigningKeyNotFoundError');
done();
});
});
});

it('should return error if signing key is not found', async () => {
nock(jwksHost)
.get('/.well-known/jwks.json')
Expand Down