Skip to content

Commit

Permalink
Interceptor bind client (#237)
Browse files Browse the repository at this point in the history
* Add tests for interceptor

* Bind client to getSigningKey

Authored-by: Erik Fried <[email protected]>
  • Loading branch information
erikfried authored Mar 23, 2021
1 parent f5270dd commit dd3a05c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wrappers/interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const retrieveSigningKeys = require('../utils').retrieveSigningKeys;
* external cache, or provided object before falling back to the jwksUri endpoint
*/
function getKeysInterceptor(client, { getKeysInterceptor } = options) {
const getSigningKey = client.getSigningKey;
const getSigningKey = client.getSigningKey.bind(client);

return async (kid) => {
const keys = await getKeysInterceptor();
Expand Down
43 changes: 43 additions & 0 deletions tests/interceptor.tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const nock = require('nock');
const { expect } = require('chai');

const { x5cSingle, x5cMultiple } = require('./keys');
const { JwksClient } = require('../src/JwksClient');

describe('JwksClient (interceptor)', () => {
const jwksHost = 'http://my-authz-server';

beforeEach(() => {
nock.cleanAll();
});

describe('#getSigningKeys', () => {
it('should prefer key from interceptor', async () => {
const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`,
getKeysInterceptor: () => Promise.resolve(x5cSingle.keys)
});

nock(jwksHost)
.get('/.well-known/jwks.json')
.replyWithError('Call to jwksUri not expected');

const key = await client.getSigningKey('NkFCNEE1NDFDNTQ5RTQ5OTE1QzRBMjYyMzY0NEJCQTJBMjJBQkZCMA');
expect(key.kid).to.equal('NkFCNEE1NDFDNTQ5RTQ5OTE1QzRBMjYyMzY0NEJCQTJBMjJBQkZCMA');
});

it('should fallback to fetch from jwksUri', async () => {
const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`,
getKeysInterceptor: () => Promise.resolve([])
});

nock(jwksHost)
.get('/.well-known/jwks.json')
.reply(200, x5cMultiple);

const key = await client.getSigningKey('RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg');
expect(key.kid).to.equal('RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg');
});
});
});

0 comments on commit dd3a05c

Please sign in to comment.