Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass a method as token #793

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 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|() => Promise<string>} [options.token] API access token.
KillianHmyd marked this conversation as resolved.
Show resolved Hide resolved
* @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,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'
KillianHmyd marked this conversation as resolved.
Show resolved Hide resolved
? options.token()
KillianHmyd marked this conversation as resolved.
Show resolved Hide resolved
: Promise.resolve(options.token);
},
};
managerOptions.headers['Authorization'] = `Bearer ${options.token}`;
KillianHmyd marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
11 changes: 11 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-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