Skip to content

Commit

Permalink
[SDK-1634] Pass custom options to the token endpoint (#465)
Browse files Browse the repository at this point in the history
* Passed custom options to refresh token call

* Added custom params support for token endpoint (iframe)

* Added timeoutInSeconds to exclusion list when getting tokens

* Added generic dictionary type to TokenEndpointOptions

* Readded scope concatenation to _getTokenUsingRefreshToken

* Added timeoutInSeconds to exclusion list in _getTokenUsingRefreshToken
  • Loading branch information
Steve Hobbs authored May 19, 2020
1 parent 703a6c3 commit 3f279a4
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
64 changes: 64 additions & 0 deletions __tests__/Auth0Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,4 +542,68 @@ describe('Auth0Client', () => {
expect(access_token).toEqual('my_access_token');
expect(utils.runIframe).not.toHaveBeenCalled();
});

it('sends custom options through to the token endpoint when using an iframe', async () => {
const auth0 = setup();

await login(auth0, true);

jest.spyOn(<any>utils, 'runIframe').mockResolvedValue({
access_token: 'my_access_token',
state: 'MTIz'
});

await auth0.getTokenSilently({
ignoreCache: true,
customParam: 'hello world'
});

expect(
(<any>utils.runIframe).mock.calls[0][0].includes(
'customParam=hello%20world'
)
).toBe(true);

expect(JSON.parse(mockFetch.mock.calls[1][1].body)).toEqual({
redirect_uri: 'my_callback_url',
client_id: 'auth0_client_id',
grant_type: 'authorization_code',
customParam: 'hello world',
code_verifier: '123'
});
});

it('sends custom options through to the token endpoint when using refresh tokens', async () => {
const auth0 = setup({
useRefreshTokens: true
});

await login(auth0, true, { refresh_token: 'a_refresh_token' });

mockFetch.mockResolvedValueOnce(
fetchResponse(true, {
id_token: 'my_id_token',
refresh_token: 'my_refresh_token',
access_token: 'my_access_token',
expires_in: 86400
})
);

expect(utils.runIframe).not.toHaveBeenCalled();

const access_token = await auth0.getTokenSilently({
ignoreCache: true,
customParam: 'hello world'
});

expect(JSON.parse(mockFetch.mock.calls[1][1].body)).toEqual({
redirect_uri: 'my_callback_url',
client_id: 'auth0_client_id',
grant_type: 'refresh_token',
refresh_token: 'a_refresh_token',
customParam: 'hello world'
});

expect(access_token).toEqual('my_access_token');
});
});
23 changes: 22 additions & 1 deletion src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,18 @@ export default class Auth0Client {
throw new Error('Invalid state');
}

const {
scope,
audience,
redirect_uri,
ignoreCache,
timeoutInSeconds,
...customOptions
} = options;

const tokenResult = await oauthToken(
{
...customOptions,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
code_verifier,
Expand All @@ -695,7 +705,7 @@ export default class Auth0Client {
): Promise<any> {
options.scope = getUniqueScopes(
this.defaultScope,
this.scope,
this.options.scope,
options.scope
);

Expand All @@ -718,9 +728,19 @@ export default class Auth0Client {
window.location.origin;

let tokenResult;

const {
scope,
audience,
ignoreCache,
timeoutInSeconds,
...customOptions
} = options;

try {
tokenResult = await oauthToken(
{
...customOptions,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
grant_type: 'refresh_token',
Expand All @@ -737,6 +757,7 @@ export default class Auth0Client {
}
throw e;
}

const decodedToken = this._verifyIdToken(tokenResult.id_token);

return {
Expand Down
1 change: 1 addition & 0 deletions src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ export interface TokenEndpointOptions {
client_id: string;
grant_type: string;
timeout?: number;
[key: string]: any;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,10 @@ <h3>Last error</h3>
var _self = this;

_self.auth0
.getTokenSilently({ ignoreCache: !_self.useCache })
.getTokenSilently({
ignoreCache: !_self.useCache,
aCustomOption: 'hello world'
})
.then(function (token) {
_self.access_tokens.push(token);
_self.error = null;
Expand Down

0 comments on commit 3f279a4

Please sign in to comment.