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-2943] Add check for state in handleRedirectCallback #841

Merged
merged 4 commits into from
Nov 18, 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
69 changes: 50 additions & 19 deletions __tests__/Auth0Client/handleRedirectCallback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import {

import {
TEST_ACCESS_TOKEN,
TEST_AUDIENCE,
TEST_CLIENT_ID,
TEST_CODE,
TEST_CODE_CHALLENGE,
TEST_CODE_VERIFIER,
TEST_ENCODED_STATE,
TEST_ID_TOKEN,
TEST_NONCE,
TEST_REDIRECT_URI,
TEST_REFRESH_TOKEN
TEST_REFRESH_TOKEN,
TEST_SCOPES
} from '../constants';

import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
Expand Down Expand Up @@ -138,9 +141,7 @@ describe('Auth0Client', () => {
const auth0 = setup();

jest.spyOn(auth0['transactionManager'], 'remove');

await loginWithRedirect(auth0);

expect(auth0['transactionManager'].remove).toHaveBeenCalledWith();
});

Expand Down Expand Up @@ -194,11 +195,13 @@ describe('Auth0Client', () => {
it('should throw an error if there is no transaction', async () => {
const auth0 = setup();
let error;

try {
await auth0.handleRedirectCallback('test?foo=bar');
} catch (e) {
error = e;
}

expect(error).toBeDefined();
expect(error.message).toBe('Invalid state');
});
Expand Down Expand Up @@ -249,6 +252,48 @@ describe('Auth0Client', () => {
expect.objectContaining({ oauthTokenScope: 'openid profile email' })
);
});

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

await expect(async () => {
await loginWithRedirect(
auth0,
{},
{
authorize: {
state: 'random-state',
code: 'TEST_CODE'
}
}
);
}).rejects.toEqual(new Error('Invalid state'));
});

it('should not validate the state if there is no state in the transaction', async () => {
const auth0 = setup();

mockFetch.mockResolvedValueOnce(
fetchResponse(true, {
id_token: TEST_ID_TOKEN,
refresh_token: TEST_REFRESH_TOKEN,
access_token: TEST_ACCESS_TOKEN,
expires_in: 86400
})
);

auth0['transactionManager'].create({
audience: TEST_AUDIENCE,
nonce: TEST_NONCE,
scope: TEST_SCOPES,
redirect_uri: TEST_REDIRECT_URI,
code_verifier: TEST_CODE_VERIFIER
// no state
});

// should not throw
await auth0.handleRedirectCallback();
});
});

it('calls oauth/token without redirect uri if not set in transaction', async () => {
Expand All @@ -270,8 +315,7 @@ describe('Auth0Client', () => {
const auth0 = setup();
delete auth0['options']['redirect_uri'];

await auth0.loginWithRedirect();
await auth0.handleRedirectCallback();
await loginWithRedirect(auth0);

expect(mockFetch.mock.calls[0][0]).toBe('https://auth0_domain/oauth/token');

Expand Down Expand Up @@ -299,8 +343,7 @@ describe('Auth0Client', () => {
useFormData: true
});

await auth0.loginWithRedirect();
await auth0.handleRedirectCallback();
await loginWithRedirect(auth0);

assertPostFn(mockFetch)(
'https://auth0_domain/oauth/token',
Expand Down Expand Up @@ -412,17 +455,5 @@ describe('Auth0Client', () => {
'There are no query params available for parsing.'
);
});

it('should throw an error if there is no transaction', async () => {
const auth0 = setup();
let error;
try {
await auth0.handleRedirectCallback('#test?foo=bar');
} catch (e) {
error = e;
}
expect(error).toBeDefined();
expect(error.message).toBe('Invalid state');
});
});
});
5 changes: 3 additions & 2 deletions __tests__/transaction-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import TransactionManager from '../src/transaction-manager';
import { SessionStorage } from '../src/storage';
import { TEST_CLIENT_ID } from './constants';
import { TEST_CLIENT_ID, TEST_STATE } from './constants';
import { mocked } from 'ts-jest/utils';

const TRANSACTION_KEY_PREFIX = 'a0.spajs.txs';
Expand All @@ -11,7 +11,8 @@ const transaction = {
appState: 'appStateIn',
scope: 'scopeIn',
audience: ' audienceIn',
redirect_uri: 'http://localhost'
redirect_uri: 'http://localhost',
state: TEST_STATE
};

const transactionJson = JSON.stringify(transaction);
Expand Down
Loading