Skip to content

Commit

Permalink
Merge pull request panva#202 from slavab89/cache-clear-single
Browse files Browse the repository at this point in the history
add clear single client from cache
  • Loading branch information
panva authored Jan 26, 2018
2 parents 9689f6f + 1369050 commit 36de2a0
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Available [Client Metadata][client-metadata] is validated as defined by the spec

Note: each oidc-provider caches the clients once they are loaded. When your adapter-stored client
configuration changes you should either reload your processes or trigger a cache clear
(`provider.Client.cacheClear()`).
(`provider.Client.cacheClear()` or `provider.Client.cacheClear(id)`).

**via Provider interface**
To add pre-established clients use the `initialize` method on a oidc-provider instance. This accepts
Expand Down
18 changes: 13 additions & 5 deletions lib/models/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,19 @@ module.exports = function getClient(provider) {
return undefined;
}

static cacheClear() {
cache.forEach((client) => {
if (client.noManage) return;
cache.del(client.clientId);
});
static cacheClear(id) {
if (id) {
if (cache.has(id)) {
const found = cache.get(id);
if (found.noManage) return;
cache.del(id);
}
} else {
cache.forEach((client) => {
if (client.noManage) return;
cache.del(client.clientId);
});
}
}

static needsSecret(metadata) {
Expand Down
48 changes: 48 additions & 0 deletions test/configuration/clients.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,54 @@ describe('provider.Client', () => {
expect(await this.provider.Client.find('client')).not.to.be.ok;
});

it('keeps the fixed client in cache given id', async function () {
expect(await this.provider.Client.find('fixed')).to.be.ok;
this.provider.Client.cacheClear('fixed');
expect(await this.provider.Client.find('fixed')).to.be.ok;
});

it('removes the adapter backed client from cache given id', async function () {
await i(this.provider).clientAdd({
client_id: 'client',
client_secret: 'secret',
redirect_uris: ['https://client.example.com/cb'],
});
expect(await this.provider.Client.find('client')).to.be.ok;
this.provider.Client.cacheClear('client');
expect(await this.provider.Client.find('client')).not.to.be.ok;
});

it('removes only wanted adapter backed client from cache', async function () {
await i(this.provider).clientAdd({
client_id: 'client',
client_secret: 'secret',
redirect_uris: ['https://client.example.com/cb'],
});
await i(this.provider).clientAdd({
client_id: 'clientStay',
client_secret: 'secret',
redirect_uris: ['https://client.example.com/cb'],
});
expect(await this.provider.Client.find('client')).to.be.ok;
expect(await this.provider.Client.find('clientStay')).to.be.ok;
this.provider.Client.cacheClear('client');
expect(await this.provider.Client.find('client')).not.to.be.ok;
expect(await this.provider.Client.find('clientStay')).to.be.ok;
this.provider.Client.cacheClear('clientStay');
});

it('leaves adapter backed clients intact in case of not found', async function () {
await i(this.provider).clientAdd({
client_id: 'client',
client_secret: 'secret',
redirect_uris: ['https://client.example.com/cb'],
});
expect(await this.provider.Client.find('client')).to.be.ok;
this.provider.Client.cacheClear('another');
expect(await this.provider.Client.find('client')).to.be.ok;
this.provider.Client.cacheClear('client');
});

it('has a Schema class getter that can work its magic', async function () {
expect(this.provider.Client.Schema).to.be.ok;

Expand Down

0 comments on commit 36de2a0

Please sign in to comment.