Skip to content
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
12 changes: 8 additions & 4 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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|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.
Expand Down Expand Up @@ -144,15 +144,19 @@ 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 === 'function'
? options.token()
: Promise.resolve(options.token);
},
};
managerOptions.headers['Authorization'] = `Bearer ${options.token}`;
}

managerOptions.tokenProvider = this.tokenProvider;
Expand Down
28 changes: 28 additions & 0 deletions test/management/management-client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,21 @@ describe('ManagementClient', () => {
token: 'fake-token',
};

const withTokenFunctionConfig = {
domain: 'auth0-node-sdk.auth0.com',
token: () => Promise.resolve('fake-function-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
Expand Down Expand Up @@ -964,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();
});
});
});