Skip to content

Commit

Permalink
Reduce transaction cookie size (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
luisrudge authored Jun 23, 2019
1 parent ce49809 commit 1de0ef3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 36 deletions.
19 changes: 10 additions & 9 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,15 +328,16 @@ describe('Auth0', () => {
const { auth0, transactionManager } = await setup();

await auth0.loginWithRedirect(REDIRECT_OPTIONS);
expect(transactionManager.create).toHaveBeenCalledWith({
appState: TEST_APP_STATE,
audience: 'default',
code_challenge: TEST_BASE64_ENCODED_STRING,
code_verifier: TEST_RANDOM_STRING,
nonce: TEST_RANDOM_STRING,
scope: TEST_SCOPES,
state: TEST_ENCODED_STATE
});
expect(transactionManager.create).toHaveBeenCalledWith(
TEST_ENCODED_STATE,
{
appState: TEST_APP_STATE,
audience: 'default',
code_verifier: TEST_RANDOM_STRING,
nonce: TEST_RANDOM_STRING,
scope: TEST_SCOPES
}
);
});
it('calls `window.location.assign` with the correct url', async () => {
const { auth0 } = await setup();
Expand Down
35 changes: 17 additions & 18 deletions __tests__/transaction-manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import TransactionManager from '../src/transaction-manager';

const COOKIE_KEY = 'Auth0.spa-js.transactions.';
const COOKIE_KEY = 'a0.spajs.txs.';

const stateIn = 'stateIn';
const transaction = {
state: 'stateIn',
nonce: 'nonceIn',
code_verifier: 'code_verifierIn',
code_challenge: 'code_challengeIn',
appState: 'appStateIn',
scope: 'scopeIn',
audience: ' audienceIn'
Expand All @@ -22,8 +21,8 @@ describe('transaction manager', () => {
describe('constructor', () => {
it('loads transactions from localStorage (per key)', () => {
getStorageMock().getAllKeys.mockReturnValue([
'Auth0.spa-js.transactions.key1',
'Auth0.spa-js.transactions.key2'
'a0.spajs.txs.key1',
'a0.spajs.txs.key2'
]);
tm = new TransactionManager();
expect(getStorageMock().getAllKeys).toHaveBeenCalled();
Expand All @@ -46,36 +45,36 @@ describe('transaction manager', () => {
tm = new TransactionManager();
});
it('`create` creates the transaction', () => {
tm.create(transaction);
expect(tm.get(transaction.state)).toMatchObject(transaction);
tm.create(stateIn, transaction);
expect(tm.get(stateIn)).toMatchObject(transaction);
});
it('`create` saves the transaction in the storage', () => {
tm.create(transaction);
tm.create(stateIn, transaction);
expect(getStorageMock().save).toHaveBeenCalledWith(
`Auth0.spa-js.transactions.${transaction.state}`,
`a0.spajs.txs.${stateIn}`,
transaction,
{
daysUntilExpire: 1
}
);
});
it('`get` without a transaction should return undefined', () => {
expect(tm.get(transaction.state)).toBeUndefined();
expect(tm.get(stateIn)).toBeUndefined();
});
it('`get` with a transaction should return the transaction', () => {
tm.create(transaction);
expect(tm.get(transaction.state)).toMatchObject(transaction);
tm.create(stateIn, transaction);
expect(tm.get(stateIn)).toMatchObject(transaction);
});
it('`remove` removes the transaction', () => {
tm.create(transaction);
tm.remove(transaction.state);
expect(tm.get(transaction.state)).toBeUndefined();
tm.create(stateIn, transaction);
tm.remove(stateIn);
expect(tm.get(stateIn)).toBeUndefined();
});
it('`remove` removes transaction from storage', () => {
tm.create(transaction);
tm.remove(transaction.state);
tm.create(stateIn, transaction);
tm.remove(stateIn);
expect(getStorageMock().remove).toHaveBeenLastCalledWith(
`Auth0.spa-js.transactions.${transaction.state}`
`a0.spajs.txs.${stateIn}`
);
});
});
Expand Down
4 changes: 1 addition & 3 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,9 @@ export default class Auth0Client {
redirect_uri
);
const url = this._authorizeUrl(params);
this.transactionManager.create({
state: stateIn,
this.transactionManager.create(stateIn, {
nonce: nonceIn,
code_verifier,
code_challenge: code_challenge,
appState,
scope: params.scope,
audience: params.audience || 'default'
Expand Down
10 changes: 4 additions & 6 deletions src/transaction-manager.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import * as ClientStorage from './storage';

const COOKIE_KEY = 'Auth0.spa-js.transactions.';
const COOKIE_KEY = 'a0.spajs.txs.';
const getTransactionKey = (state: string) => `${COOKIE_KEY}${state}`;

interface Transaction {
state: string;
nonce: string;
scope: string;
audience: string;
appState?: any;
code_verifier: string;
code_challenge: string;
}
interface Transactions {
[key: string]: Transaction;
Expand All @@ -26,9 +24,9 @@ export default class TransactionManager {
this.transactions[state] = ClientStorage.get<Transaction>(k);
});
}
public create(transaction: Transaction) {
this.transactions[transaction.state] = transaction;
ClientStorage.save(getTransactionKey(transaction.state), transaction, {
public create(state: string, transaction: Transaction) {
this.transactions[state] = transaction;
ClientStorage.save(getTransactionKey(state), transaction, {
daysUntilExpire: 1
});
}
Expand Down

0 comments on commit 1de0ef3

Please sign in to comment.