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 missing invalid state errors with Generic Error #1102

Merged
merged 1 commit into from
May 30, 2023
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
16 changes: 13 additions & 3 deletions __tests__/Auth0Client/getTokenSilently.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1835,9 +1835,19 @@ describe('Auth0Client', () => {
})
);

await expect(auth0.getTokenSilently()).rejects.toThrowError(
'Invalid state'
);
let error;

try {
await auth0.getTokenSilently();
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toBe('Invalid state');
expect(error.error).toBe('state_mismatch');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(GenericError);
});

it('saves into cache', async () => {
Expand Down
18 changes: 14 additions & 4 deletions __tests__/Auth0Client/loginWithPopup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
DEFAULT_AUTH0_CLIENT,
DEFAULT_POPUP_CONFIG_OPTIONS
} from '../../src/constants';
import { GenericError } from '../../src/errors';

jest.mock('es-cookie');
jest.mock('../../src/jwt');
Expand Down Expand Up @@ -476,16 +477,25 @@ describe('Auth0Client', () => {

it('throws error if state from popup response is different from the provided state', async () => {
const auth0 = setup();
let error;

await expect(
loginWithPopup(auth0, undefined, undefined, {
try {
await loginWithPopup(auth0, undefined, undefined, {
authorize: {
response: {
state: 'other-state'
}
}
})
).rejects.toThrowError('Invalid state');
});
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toBe('Invalid state');
expect(error.error).toBe('state_mismatch');
expect(error).toBeInstanceOf(Error);
expect(error).toBeInstanceOf(GenericError);
});

it('calls `tokenVerifier.verify` with the `issuer` from in the oauth/token response', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ export class Auth0Client {
});

if (params.state !== codeResult.state) {
throw new Error('Invalid state');
throw new GenericError('state_mismatch', 'Invalid state');
}

const organizationId =
Expand Down Expand Up @@ -898,7 +898,7 @@ export class Auth0Client {
const codeResult = await runIframe(url, this.domainUrl, authorizeTimeout);

if (stateIn !== codeResult.state) {
throw new Error('Invalid state');
throw new GenericError('state_mismatch', 'Invalid state');
}

const tokenResult = await this._requestToken(
Expand Down