Skip to content

Commit

Permalink
feat: distinguish between missing and invalid state (#1099)
Browse files Browse the repository at this point in the history
  • Loading branch information
frederikprijck authored May 22, 2023
1 parent ccc53a5 commit 3c588e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit 3c588e9

Please sign in to comment.