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

distinguish between missing and invalid state #1099

Merged
merged 1 commit into from
May 22, 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
17 changes: 15 additions & 2 deletions __tests__/Auth0Client/handleRedirectCallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
} from '../constants';

import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
import { GenericError } from '../../src';

jest.mock('es-cookie');
jest.mock('../../src/jwt');
Expand Down Expand Up @@ -204,6 +205,9 @@ describe('Auth0Client', () => {

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

it('returns the transactions appState', async () => {
Expand Down Expand Up @@ -269,8 +273,9 @@ describe('Auth0Client', () => {

it('should fail with an error if the state in the transaction does not match the request', async () => {
const auth0 = setup();
let error;

await expect(async () => {
try {
await loginWithRedirect(
auth0,
{},
Expand All @@ -281,7 +286,15 @@ describe('Auth0Client', () => {
}
}
);
}).rejects.toEqual(new Error('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('should not validate the state if there is no state in the transaction', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ export class Auth0Client {
const transaction = this.transactionManager.get();

if (!transaction) {
throw new Error('Invalid state');
throw new GenericError('missing_transaction', 'Invalid state');
}

this.transactionManager.remove();
Expand All @@ -512,7 +512,7 @@ export class Auth0Client {
!transaction.code_verifier ||
(transaction.state && transaction.state !== state)
) {
throw new Error('Invalid state');
throw new GenericError('state_mismatch', 'Invalid state');
}

const organizationId = transaction.organizationId;
Expand Down