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-1789] Add custom initial options to the 2 getToken methods #524

Merged
merged 2 commits into from
Jul 14, 2020
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
37 changes: 29 additions & 8 deletions __tests__/Auth0Client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,10 @@ describe('Auth0Client', () => {
});

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

await login(auth0, true);

Expand All @@ -644,34 +647,51 @@ describe('Auth0Client', () => {
state: 'MTIz'
});

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

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

expect(
(<any>utils.runIframe).mock.calls[0][0].includes(
'customParam=hello%20world'
'custom_param=hello%20world&another_custom_param=bar'
)
).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',
custom_param: 'hello world',
another_custom_param: 'bar',
code_verifier: '123'
});
});

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

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

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

mockFetch.mockResolvedValue(
fetchResponse(true, {
id_token: 'my_id_token',
refresh_token: 'my_refresh_token',
Expand All @@ -684,15 +704,16 @@ describe('Auth0Client', () => {

const access_token = await auth0.getTokenSilently({
ignoreCache: true,
customParam: 'hello world'
custom_param: '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'
custom_param: 'hello world',
another_custom_param: 'bar'
});

expect(access_token).toEqual('my_access_token');
Expand Down
30 changes: 30 additions & 0 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,38 @@ const cacheFactory = (location: string) => {
*/
const isIE11 = () => /Trident.*rv:11\.0/.test(navigator.userAgent);

/**
* @ignore
*/
const getCustomInitialOptions = (
options: Auth0ClientOptions
): BaseLoginOptions => {
const {
advancedOptions,
audience,
auth0Client,
authorizeTimeoutInSeconds,
cacheLocation,
client_id,
domain,
issuer,
leeway,
max_age,
redirect_uri,
scope,
useRefreshTokens,
...customParams
} = options;
return customParams;
};

/**
* Auth0 SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce).
*/
export default class Auth0Client {
private cache: ICache;
private transactionManager: TransactionManager;
private customOptions: BaseLoginOptions;
private domainUrl: string;
private tokenIssuer: string;
private defaultScope: string;
Expand Down Expand Up @@ -137,6 +163,8 @@ export default class Auth0Client {
) {
this.worker = new TokenWorker();
}

this.customOptions = getCustomInitialOptions(options);
}

private _url(path) {
Expand Down Expand Up @@ -719,6 +747,7 @@ export default class Auth0Client {

const tokenResult = await oauthToken(
{
...this.customOptions,
...customOptions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it's worth renaming the local customOptions here, alongside this.customOptions, it could be confusing as to why they're both there. Or do you feel the reference to this is enough to differentiate them?

baseUrl: this.domainUrl,
client_id: this.options.client_id,
Expand Down Expand Up @@ -780,6 +809,7 @@ export default class Auth0Client {
try {
tokenResult = await oauthToken(
{
...this.customOptions,
...customOptions,
baseUrl: this.domainUrl,
client_id: this.options.client_id,
Expand Down