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

Add 'lock' to prevent getTokenSilently to be invoked in parallel #238

Merged
merged 3 commits into from
Oct 23, 2019
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
7 changes: 7 additions & 0 deletions __mocks__/browser-tabs-lock/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export const acquireLockMock = jest.fn();
export const releaseLockMock = jest.fn();

export default class FakeLock {
acquireLock = acquireLockMock;
releaseLock = releaseLockMock;
}
39 changes: 38 additions & 1 deletion __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
jest.mock('browser-tabs-lock');
jest.mock('../src/jwt');
jest.mock('../src/storage');
jest.mock('../src/cache');
Expand All @@ -9,6 +10,7 @@ import createAuth0Client from '../src/index';
import { AuthenticationError } from '../src/errors';
import version from '../src/version';
import { DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS } from '../src/constants';
const GET_TOKEN_SILENTLY_LOCK_KEY = 'auth0.lock.getTokenSilently';

const TEST_DOMAIN = 'test.auth0.com';
const TEST_CLIENT_ID = 'test-client-id';
Expand Down Expand Up @@ -49,6 +51,7 @@ const setup = async (options = {}) => {
save: require('../src/storage').save,
remove: require('../src/storage').remove
};
const lock = require('browser-tabs-lock');
const cache = getInstance('../src/cache');
const tokenVerifier = require('../src/jwt').verify;
const transactionManager = getInstance('../src/transaction-manager');
Expand Down Expand Up @@ -84,7 +87,15 @@ const setup = async (options = {}) => {
aud: TEST_CLIENT_ID
}
});
return { auth0, storage, cache, tokenVerifier, transactionManager, utils };
return {
auth0,
storage,
cache,
tokenVerifier,
transactionManager,
utils,
lock
};
};

describe('Auth0', () => {
Expand Down Expand Up @@ -945,6 +956,19 @@ describe('Auth0', () => {

expect(token).toBe(TEST_ACCESS_TOKEN);
});
it('acquires and releases lock when there is a cache', async () => {
const { auth0, cache, lock } = await setup();
cache.get.mockReturnValue({ access_token: TEST_ACCESS_TOKEN });

await auth0.getTokenSilently();
expect(lock.acquireLockMock).toHaveBeenCalledWith(
GET_TOKEN_SILENTLY_LOCK_KEY,
5000
);
expect(lock.releaseLockMock).toHaveBeenCalledWith(
GET_TOKEN_SILENTLY_LOCK_KEY
);
});
it('continues method execution when there is no cache available', async () => {
const { auth0, utils } = await setup();

Expand Down Expand Up @@ -1122,6 +1146,19 @@ describe('Auth0', () => {
{ daysUntilExpire: 1 }
);
});
it('acquires and releases lock', async () => {
const { auth0, lock } = await setup();

await auth0.getTokenSilently(defaultOptionsIgnoreCacheTrue);

expect(lock.acquireLockMock).toHaveBeenCalledWith(
GET_TOKEN_SILENTLY_LOCK_KEY,
5000
);
expect(lock.releaseLockMock).toHaveBeenCalledWith(
GET_TOKEN_SILENTLY_LOCK_KEY
);
});
});
});
describe('getTokenWithPopup()', async () => {
Expand Down
19 changes: 12 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"wait-on": "^3.3.0"
},
"dependencies": {
"browser-tabs-lock": "^1.1.9",
"core-js": "^3.2.1",
"es-cookie": "^1.2.0",
"fast-text-encoding": "^1.0.0",
Expand Down
9 changes: 9 additions & 0 deletions src/Auth0Client.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import Lock from 'browser-tabs-lock';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you mean:

import Lock from 'auth0-lock';

😆

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geez. can you imagine that? 😝


import {
getUniqueScopes,
createQueryParams,
Expand All @@ -20,6 +22,9 @@ import * as ClientStorage from './storage';
import { DEFAULT_POPUP_CONFIG_OPTIONS } from './constants';
import version from './version';

const lock = new Lock();
const GET_TOKEN_SILENTLY_LOCK_KEY = 'auth0.lock.getTokenSilently';

/**
* Auth0 SDK for Single Page Applications using [Authorization Code Grant Flow with PKCE](https://auth0.com/docs/api-auth/tutorials/authorization-code-grant-pkce).
*/
Expand Down Expand Up @@ -295,12 +300,15 @@ export default class Auth0Client {
}
) {
options.scope = getUniqueScopes(this.DEFAULT_SCOPE, options.scope);

await lock.acquireLock(GET_TOKEN_SILENTLY_LOCK_KEY, 5000);
if (!options.ignoreCache) {
const cache = this.cache.get({
scope: options.scope,
audience: options.audience || 'default'
});
if (cache) {
lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY);
return cache.access_token;
}
}
Expand Down Expand Up @@ -346,6 +354,7 @@ export default class Auth0Client {
};
this.cache.save(cacheEntry);
ClientStorage.save('auth0.is.authenticated', true, { daysUntilExpire: 1 });
lock.releaseLock(GET_TOKEN_SILENTLY_LOCK_KEY);
return authResult.access_token;
}

Expand Down
8 changes: 5 additions & 3 deletions static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@
});
});
$('#getToken').click(function() {
auth0.getTokenSilently().then(function(token) {
alert(token);
Promise.all([
auth0.getTokenSilently({ ignoreCache: true }),
auth0.getTokenSilently({ ignoreCache: true })
]).then(function([token1, token2]) {
console.log(token1, token2);
});
});

$('#getTokenPopup').click(function() {
auth0
.getTokenWithPopup({
Expand Down