Skip to content

Commit

Permalink
Fix: Management of clients when injecting access token (#775)
Browse files Browse the repository at this point in the history
Setting client ID to defined value if absent

Co-authored-by: Will Vedder <will.vedder@okta.com>
Co-authored-by: Ewan Harris <ewanharris93@gmail.com>
  • Loading branch information
3 people committed Mar 31, 2023
1 parent ec4c9b9 commit 1e8feb6
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/tools/auth0/handlers/clients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default class ClientHandler extends DefaultAPIHandler {

// Always filter out the client we are using to access Auth0 Management API
// As it could cause problems if it gets deleted or updated etc
const currentClient = this.config('AUTH0_CLIENT_ID');
const currentClient = this.config('AUTH0_CLIENT_ID') || '';

const filterClients = (list) => {
if (excludedClients.length) {
Expand Down
6 changes: 5 additions & 1 deletion src/tools/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export default async function deploy(
// Setup log level
log.level = process.env.AUTH0_DEBUG === 'true' ? 'debug' : 'info';

log.info('Getting access token for ' + config('AUTH0_CLIENT_ID') + '/' + config('AUTH0_DOMAIN'));
log.info(
`Getting access token for ${
config('AUTH0_CLIENT_ID') !== undefined ? `${config('AUTH0_CLIENT_ID')}/` : ''
}${config('AUTH0_DOMAIN')}`
);

const auth0 = new Auth0(client, assets, config);

Expand Down
75 changes: 69 additions & 6 deletions test/tools/auth0/handlers/clients.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,15 @@ describe('#clients handler', () => {
expect(params).to.be.an('undefined');
return Promise.resolve([]);
},
getAll: () => [
{ client_id: 'client1', name: 'existingClient' },
{ client_id: 'client2', name: 'existingClient2' },
],
getAll: () => Promise.resolve([]),
},
pool,
};

const assets = {
clients: [{ name: 'excludedClient' }, { name: 'existingClient' }],
clients: [{ name: 'Client 1' }, { name: 'Client 2' }],
exclude: {
clients: ['excludedClient', 'existingClient', 'existingClient2'],
clients: ['Client 1', 'Client 2'],
},
};

Expand Down Expand Up @@ -285,5 +282,71 @@ describe('#clients handler', () => {

await stageFn.apply(handler, [{ clients: [] }]);
});

it('should process clients even if AUTH0_CLIENT_ID is not defined', async () => {
let wasCreateCalled = false;
let wasUpdateCalled = false;
let wasDeleteCalled = false;
const auth0 = {
clients: {
create: function (data) {
wasCreateCalled = true;
(() => expect(this).to.not.be.undefined)();
expect(data).to.be.an('object');
expect(data.name).to.equal('Client 3');
return Promise.resolve(data);
},
update: function (data) {
wasUpdateCalled = true;
(() => expect(this).to.not.be.undefined)();
expect(data).to.be.an('object');
expect(data.client_id).to.equal('client-1');
return Promise.resolve(data);
},
delete: function (data) {
wasDeleteCalled = true;
(() => expect(this).to.not.be.undefined)();
expect(data).to.be.an('object');
expect(data.client_id).to.equal('client-2');
return Promise.resolve(data);
},
getAll: () => [
{
client_id: 'client-1',
name: 'Client 1',
},
{
client_id: 'client-2',
name: 'Client 2',
},
],
},
pool,
};

const handler = new clients.default({
client: auth0,
config: (key) =>
({
// Notably omitted is AUTH0_CLIENT_ID which
AUTH0_ACCESS_TOKEN:
'some-fake-access-token-which-is-why-AUTH0_CLIENT_ID-does-not-exists',
AUTH0_ALLOW_DELETE: true,
}[key]),
});
const stageFn = Object.getPrototypeOf(handler).processChanges;

await stageFn.apply(handler, [
{
clients: [{ name: 'Client 1' }, { name: 'Client 3' }],
},
]);
// eslint-disable-next-line no-unused-expressions
expect(wasCreateCalled).to.be.true;
// eslint-disable-next-line no-unused-expressions
expect(wasUpdateCalled).to.be.true;
// eslint-disable-next-line no-unused-expressions
expect(wasDeleteCalled).to.be.true;
});
});
});

0 comments on commit 1e8feb6

Please sign in to comment.