From 33fb6cb50d2338c1b01dbef838674f484aff67d9 Mon Sep 17 00:00:00 2001 From: KillianHmyd Date: Tue, 14 Mar 2023 12:40:24 +0100 Subject: [PATCH 1/2] feat: allow to pass a method as token --- src/management/index.js | 11 ++++++++--- test/management/management-client.tests.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/management/index.js b/src/management/index.js index e38e128526..8aff45bdd1 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -95,7 +95,7 @@ class ManagementClient { * If no token is provided domain, clientId and clientSecret (or clientAssertionSigningKey) * are required. * @param {string} options.domain ManagementClient server domain. - * @param {string} [options.token] API access token. + * @param {string|() => Promise} [options.token] API access token. * @param {string} [options.clientId] Management API Non Interactive Client Id. * @param {string} [options.clientSecret] Management API Non Interactive Client Secret. * @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT. @@ -144,12 +144,17 @@ class ManagementClient { } this.tokenProvider = new ManagementTokenProvider(config); - } else if (typeof options.token !== 'string' || options.token.length === 0) { + } else if ( + typeof options.token !== 'function' && + (typeof options.token !== 'string' || options.token.length === 0) + ) { throw new ArgumentError('Must provide a token'); } else { this.tokenProvider = { getAccessToken() { - return Promise.resolve(options.token); + return typeof options.token !== 'string' + ? options.token() + : Promise.resolve(options.token); }, }; managerOptions.headers['Authorization'] = `Bearer ${options.token}`; diff --git a/test/management/management-client.tests.js b/test/management/management-client.tests.js index 8b38001af0..65fe2ee34c 100644 --- a/test/management/management-client.tests.js +++ b/test/management/management-client.tests.js @@ -37,10 +37,21 @@ describe('ManagementClient', () => { token: 'fake-token', }; + const withTokenFunctionConfig = { + domain: 'auth0-node-sdk.auth0.com', + token: () => Promise.resolve('fake-token'), + }; + it('should expose an instance of ManagementClient when withTokenConfig is passed', () => { expect(new ManagementClient(withTokenConfig)).to.exist.to.be.an.instanceOf(ManagementClient); }); + it('should expose an instance of ManagementClient when withTokenFunctionConfig is passed', () => { + expect(new ManagementClient(withTokenFunctionConfig)).to.exist.to.be.an.instanceOf( + ManagementClient + ); + }); + it('should expose an instance of ManagementClient when withTokenProviderConfig is passed', () => { expect(new ManagementClient(withTokenProviderConfig)).to.exist.to.be.an.instanceOf( ManagementClient From 1ae988e4e3ced84a9fb7c89976d57aeb78a207dc Mon Sep 17 00:00:00 2001 From: KillianHmyd Date: Tue, 14 Mar 2023 19:44:51 +0100 Subject: [PATCH 2/2] PR-FIX --- src/management/index.js | 5 ++--- test/management/management-client.tests.js | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/management/index.js b/src/management/index.js index 8aff45bdd1..9bacac36ae 100644 --- a/src/management/index.js +++ b/src/management/index.js @@ -95,7 +95,7 @@ class ManagementClient { * If no token is provided domain, clientId and clientSecret (or clientAssertionSigningKey) * are required. * @param {string} options.domain ManagementClient server domain. - * @param {string|() => Promise} [options.token] API access token. + * @param {string|Function} [options.token] API access token. * @param {string} [options.clientId] Management API Non Interactive Client Id. * @param {string} [options.clientSecret] Management API Non Interactive Client Secret. * @param {string} [options.clientAssertionSigningKey] Private key used to sign the client assertion JWT. @@ -152,12 +152,11 @@ class ManagementClient { } else { this.tokenProvider = { getAccessToken() { - return typeof options.token !== 'string' + return typeof options.token === 'function' ? options.token() : Promise.resolve(options.token); }, }; - managerOptions.headers['Authorization'] = `Bearer ${options.token}`; } managerOptions.tokenProvider = this.tokenProvider; diff --git a/test/management/management-client.tests.js b/test/management/management-client.tests.js index 65fe2ee34c..33cc893626 100644 --- a/test/management/management-client.tests.js +++ b/test/management/management-client.tests.js @@ -39,7 +39,7 @@ describe('ManagementClient', () => { const withTokenFunctionConfig = { domain: 'auth0-node-sdk.auth0.com', - token: () => Promise.resolve('fake-token'), + token: () => Promise.resolve('fake-function-token'), }; it('should expose an instance of ManagementClient when withTokenConfig is passed', () => { @@ -975,5 +975,22 @@ describe('ManagementClient', () => { expect(headers).to.deep.equal({ 'content-type': 'application/json' }); nock.cleanAll(); }); + + it('should include the header Authorization with the token returned by the injected function', async function () { + const config = Object.assign({}, withTokenFunctionConfig); + this.client = new ManagementClient(config); + + nock('https://auth0-node-sdk.auth0.com', { + reqheaders: { + Authorization: (value) => value === 'Bearer fake-function-token', + }, + }) + .get(`/api/v2/users`) + .reply(200, { data: 'value' }); + + const { data } = await this.client.getUsers(); + expect(data).to.deep.equal('value'); + nock.cleanAll(); + }); }); });