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

[SDK-1634] Pass custom options to the token endpoint #465

Merged
merged 6 commits into from
May 19, 2020
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
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,
adamjmcgrath marked this conversation as resolved.
Show resolved Hide resolved
timeoutInSeconds,
...customOptions
} = options;

const tokenResult = await oauthToken(
{
...customOptions,
adamjmcgrath marked this conversation as resolved.
Show resolved Hide resolved
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(
adamjmcgrath marked this conversation as resolved.
Show resolved Hide resolved
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