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

Fix cached scopes when using detailed response mode #824

Merged
merged 3 commits into from
Oct 20, 2021
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
37 changes: 37 additions & 0 deletions __tests__/Auth0Client/handleRedirectCallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,50 @@ describe('Auth0Client', () => {

it('returns the transactions appState', async () => {
const auth0 = setup();

const appState = {
key: 'property'
};

const result = await loginWithRedirect(auth0, { appState });

expect(result).toBeDefined();
expect(result.appState).toBe(appState);
});

it('does not store the scope from token endpoint if none was returned', async () => {
const auth0 = setup();
const cacheSetSpy = jest.spyOn(auth0['cacheManager'], 'set');

const appState = {
key: 'property'
};

await loginWithRedirect(auth0, { appState });

expect(
Object.keys(cacheSetSpy.mock.calls[0][0]).includes('oauthTokenScope')
).toBeFalsy();
});

it('stores the scope returned from the token endpoint in the cache', async () => {
const auth0 = setup();
const cacheSetSpy = jest.spyOn(auth0['cacheManager'], 'set');

const appState = {
key: 'property'
};

await loginWithRedirect(
auth0,
{ appState },
{ token: { response: { scope: 'openid profile email' } } }
);

expect(cacheSetSpy).toHaveBeenCalledWith(
expect.objectContaining({ oauthTokenScope: 'openid profile email' })
);
});
});

it('calls oauth/token without redirect uri if not set in transaction', async () => {
Expand Down
8 changes: 7 additions & 1 deletion src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ export default class Auth0Client {
auth0Client,
cacheLocation,
advancedOptions,
detailedResponse,
...withoutClientOptions
} = this.options;

Expand Down Expand Up @@ -678,6 +679,7 @@ export default class Auth0Client {
decodedToken,
audience: transaction.audience,
scope: transaction.scope,
...(authResult.scope ? { oauthTokenScope: authResult.scope } : null),
client_id: this.options.client_id
});

Expand Down Expand Up @@ -1015,8 +1017,10 @@ export default class Auth0Client {
const code_challengeBuffer = await sha256(code_verifier);
const code_challenge = bufferToBase64UrlEncoded(code_challengeBuffer);

const { detailedResponse, ...withoutClientOptions } = options;

const params = this._getParams(
options,
withoutClientOptions,
stateIn,
nonceIn,
code_challenge,
Expand Down Expand Up @@ -1063,6 +1067,7 @@ export default class Auth0Client {
redirect_uri,
ignoreCache,
timeoutInSeconds,
detailedResponse,
...customOptions
} = options;

Expand Down Expand Up @@ -1144,6 +1149,7 @@ export default class Auth0Client {
audience,
ignoreCache,
timeoutInSeconds,
detailedResponse,
...customOptions
} = options;

Expand Down