Commit 2e3cc6c9 authored by Robert Knight's avatar Robert Knight

Rename `tokenGetter` => `getAccessToken`

The previous name was a vestige from the days of Angular 1.x in the
client. Use a clearer name instead.
parent 593bb287
...@@ -210,7 +210,7 @@ export default function api(apiRoutes, auth, store) { ...@@ -210,7 +210,7 @@ export default function api(apiRoutes, auth, store) {
function apiCall(route) { function apiCall(route) {
return createAPICall(links, route, { return createAPICall(links, route, {
getAccessToken: auth.tokenGetter, getAccessToken: auth.getAccessToken,
getClientId, getClientId,
onRequestStarted: store.apiRequestStarted, onRequestStarted: store.apiRequestStarted,
onRequestFinished: store.apiRequestFinished, onRequestFinished: store.apiRequestFinished,
......
...@@ -16,7 +16,7 @@ import { resolve } from '../util/url'; ...@@ -16,7 +16,7 @@ import { resolve } from '../util/url';
* Authorization service. * Authorization service.
* *
* This service is responsible for acquiring access tokens for making API * This service is responsible for acquiring access tokens for making API
* requests and making them available via the `tokenGetter()` method. * requests and making them available via the `getAccessToken()` method.
* *
* Access tokens are acquired via the OAuth authorization flow, loading valid * Access tokens are acquired via the OAuth authorization flow, loading valid
* tokens from a previous session or, on some websites, by exchanging a grant * tokens from a previous session or, on some websites, by exchanging a grant
...@@ -124,7 +124,7 @@ export class AuthService extends TinyEmitter { ...@@ -124,7 +124,7 @@ export class AuthService extends TinyEmitter {
$window.addEventListener('storage', ({ key }) => { $window.addEventListener('storage', ({ key }) => {
if (key === storageKey()) { if (key === storageKey()) {
// Reset cached token information. Tokens will be reloaded from storage // Reset cached token information. Tokens will be reloaded from storage
// on the next call to `tokenGetter()`. // on the next call to `getAccessToken()`.
tokenInfoPromise = null; tokenInfoPromise = null;
this.emit('oauthTokensChanged'); this.emit('oauthTokensChanged');
} }
...@@ -196,7 +196,7 @@ export class AuthService extends TinyEmitter { ...@@ -196,7 +196,7 @@ export class AuthService extends TinyEmitter {
* *
* @return {Promise<string|null>} The API access token or `null` if not logged in. * @return {Promise<string|null>} The API access token or `null` if not logged in.
*/ */
const tokenGetter = async () => { const getAccessToken = async () => {
// Step 1: Determine how to get an access token, depending on the login // Step 1: Determine how to get an access token, depending on the login
// method being used. // method being used.
if (!tokenInfoPromise) { if (!tokenInfoPromise) {
...@@ -239,8 +239,8 @@ export class AuthService extends TinyEmitter { ...@@ -239,8 +239,8 @@ export class AuthService extends TinyEmitter {
// Step 3: Re-fetch the token if it is no longer valid // Step 3: Re-fetch the token if it is no longer valid
if (origToken !== tokenInfoPromise) { if (origToken !== tokenInfoPromise) {
// A token refresh has been initiated via a call to `refreshAccessToken` // A token refresh has been initiated via a call to `refreshAccessToken`
// below since `tokenGetter()` was called. // below since `getAccessToken()` was called.
return tokenGetter(); return getAccessToken();
} }
if (Date.now() > token.expiresAt) { if (Date.now() > token.expiresAt) {
...@@ -254,7 +254,7 @@ export class AuthService extends TinyEmitter { ...@@ -254,7 +254,7 @@ export class AuthService extends TinyEmitter {
persist: !usingGrantToken, persist: !usingGrantToken,
}); });
return tokenGetter(); return getAccessToken();
} }
// Step 4: If the token was valid, return it // Step 4: If the token was valid, return it
...@@ -305,6 +305,6 @@ export class AuthService extends TinyEmitter { ...@@ -305,6 +305,6 @@ export class AuthService extends TinyEmitter {
// TODO - Convert these to ordinary class methods. // TODO - Convert these to ordinary class methods.
this.login = login; this.login = login;
this.logout = logout; this.logout = logout;
this.tokenGetter = tokenGetter; this.getAccessToken = getAccessToken;
} }
} }
...@@ -280,7 +280,7 @@ export default function groups( ...@@ -280,7 +280,7 @@ export default function groups(
] = await Promise.all([ ] = await Promise.all([
api.profile.groups.read({ expand: expandParam }), api.profile.groups.read({ expand: expandParam }),
api.groups.list(listParams), api.groups.list(listParams),
auth.tokenGetter(), auth.getAccessToken(),
directLinkedAnnApi, directLinkedAnnApi,
directLinkedGroupApi, directLinkedGroupApi,
]); ]);
......
...@@ -125,7 +125,7 @@ export default function Streamer(store, auth, groups, session, settings) { ...@@ -125,7 +125,7 @@ export default function Streamer(store, auth, groups, session, settings) {
} }
return auth return auth
.tokenGetter() .getAccessToken()
.then(function (token) { .then(function (token) {
let url; let url;
if (token) { if (token) {
......
...@@ -65,7 +65,7 @@ describe('sidebar.services.api', function () { ...@@ -65,7 +65,7 @@ describe('sidebar.services.api', function () {
routes: sinon.stub().returns(Promise.resolve(routes)), routes: sinon.stub().returns(Promise.resolve(routes)),
}; };
fakeAuth = { fakeAuth = {
tokenGetter: sinon.stub().returns(Promise.resolve('faketoken')), getAccessToken: sinon.stub().returns(Promise.resolve('faketoken')),
}; };
fakeStore = { fakeStore = {
apiRequestStarted: sinon.stub(), apiRequestStarted: sinon.stub(),
...@@ -235,7 +235,7 @@ describe('sidebar.services.api', function () { ...@@ -235,7 +235,7 @@ describe('sidebar.services.api', function () {
}); });
it('omits Authorization header if no access token is available', () => { it('omits Authorization header if no access token is available', () => {
fakeAuth.tokenGetter.returns(Promise.resolve(null)); fakeAuth.getAccessToken.returns(Promise.resolve(null));
expectCall('get', 'profile'); expectCall('get', 'profile');
return api.profile.read().then(() => { return api.profile.read().then(() => {
const [, options] = fetchMock.lastCall(); const [, options] = fetchMock.lastCall();
......
...@@ -109,7 +109,7 @@ describe('AuthService', function () { ...@@ -109,7 +109,7 @@ describe('AuthService', function () {
it('configures an OAuthClient correctly', () => { it('configures an OAuthClient correctly', () => {
// Call a method which will trigger construction of the `OAuthClient`. // Call a method which will trigger construction of the `OAuthClient`.
return auth.tokenGetter().then(() => { return auth.getAccessToken().then(() => {
assert.deepEqual(fakeClient.config, { assert.deepEqual(fakeClient.config, {
clientId: 'the-client-id', clientId: 'the-client-id',
tokenEndpoint: 'https://hypothes.is/api/token', tokenEndpoint: 'https://hypothes.is/api/token',
...@@ -119,7 +119,7 @@ describe('AuthService', function () { ...@@ -119,7 +119,7 @@ describe('AuthService', function () {
}); });
}); });
describe('#tokenGetter', function () { describe('#getAccessToken', function () {
const successfulTokenResponse = Promise.resolve({ const successfulTokenResponse = Promise.resolve({
accessToken: 'firstAccessToken', accessToken: 'firstAccessToken',
refreshToken: 'firstRefreshToken', refreshToken: 'firstRefreshToken',
...@@ -129,7 +129,7 @@ describe('AuthService', function () { ...@@ -129,7 +129,7 @@ describe('AuthService', function () {
it('exchanges the grant token for an access token if provided', function () { it('exchanges the grant token for an access token if provided', function () {
fakeClient.exchangeGrantToken.returns(successfulTokenResponse); fakeClient.exchangeGrantToken.returns(successfulTokenResponse);
return auth.tokenGetter().then(token => { return auth.getAccessToken().then(token => {
assert.calledWith(fakeClient.exchangeGrantToken, 'a.jwt.token'); assert.calledWith(fakeClient.exchangeGrantToken, 'a.jwt.token');
assert.equal(token, 'firstAccessToken'); assert.equal(token, 'firstAccessToken');
}); });
...@@ -142,7 +142,7 @@ describe('AuthService', function () { ...@@ -142,7 +142,7 @@ describe('AuthService', function () {
}); });
function assertThatAccessTokenPromiseWasRejectedAnd(func) { function assertThatAccessTokenPromiseWasRejectedAnd(func) {
return auth.tokenGetter().then(function onResolved() { return auth.getAccessToken().then(function onResolved() {
assert(false, 'The Promise should have been rejected'); assert(false, 'The Promise should have been rejected');
}, func); }, func);
} }
...@@ -170,10 +170,10 @@ describe('AuthService', function () { ...@@ -170,10 +170,10 @@ describe('AuthService', function () {
it('should cache tokens for future use', function () { it('should cache tokens for future use', function () {
fakeClient.exchangeGrantToken.returns(successfulTokenResponse); fakeClient.exchangeGrantToken.returns(successfulTokenResponse);
return auth return auth
.tokenGetter() .getAccessToken()
.then(function () { .then(function () {
fakeClient.exchangeGrantToken.reset(); fakeClient.exchangeGrantToken.reset();
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(function (token) { .then(function (token) {
assert.equal(token, 'firstAccessToken'); assert.equal(token, 'firstAccessToken');
...@@ -182,7 +182,7 @@ describe('AuthService', function () { ...@@ -182,7 +182,7 @@ describe('AuthService', function () {
}); });
// If an access token request has already been made but is still in // If an access token request has already been made but is still in
// flight when tokenGetter() is called again, then it should just return // flight when getAccessToken() is called again, then it should just return
// the pending Promise for the first request again (and not send a second // the pending Promise for the first request again (and not send a second
// concurrent HTTP request). // concurrent HTTP request).
it('should not make two concurrent access token requests', function () { it('should not make two concurrent access token requests', function () {
...@@ -193,9 +193,9 @@ describe('AuthService', function () { ...@@ -193,9 +193,9 @@ describe('AuthService', function () {
}) })
); );
// The first time tokenGetter() is called it makes an `exchangeGrantToken` // The first time getAccessToken() is called it makes an `exchangeGrantToken`
// call and caches the resulting Promise. // call and caches the resulting Promise.
const tokens = [auth.tokenGetter(), auth.tokenGetter()]; const tokens = [auth.getAccessToken(), auth.getAccessToken()];
// Resolve the initial request for an access token in exchange for a JWT. // Resolve the initial request for an access token in exchange for a JWT.
respond({ respond({
...@@ -210,7 +210,7 @@ describe('AuthService', function () { ...@@ -210,7 +210,7 @@ describe('AuthService', function () {
it('should not attempt to exchange a grant token if none was provided', function () { it('should not attempt to exchange a grant token if none was provided', function () {
fakeSettings.services = [{ authority: 'publisher.org' }]; fakeSettings.services = [{ authority: 'publisher.org' }];
return auth.tokenGetter().then(function (token) { return auth.getAccessToken().then(function (token) {
assert.notCalled(fakeClient.exchangeGrantToken); assert.notCalled(fakeClient.exchangeGrantToken);
assert.equal(token, null); assert.equal(token, null);
}); });
...@@ -222,7 +222,7 @@ describe('AuthService', function () { ...@@ -222,7 +222,7 @@ describe('AuthService', function () {
); );
function callTokenGetter() { function callTokenGetter() {
const tokenPromise = auth.tokenGetter(); const tokenPromise = auth.getAccessToken();
fakeClient.refreshToken.returns( fakeClient.refreshToken.returns(
Promise.resolve({ Promise.resolve({
...@@ -243,12 +243,12 @@ describe('AuthService', function () { ...@@ -243,12 +243,12 @@ describe('AuthService', function () {
return callTokenGetter() return callTokenGetter()
.then(expireAccessToken) .then(expireAccessToken)
.then(() => auth.tokenGetter()) .then(() => auth.getAccessToken())
.then(token => assert.equal(token, 'secondAccessToken')) .then(token => assert.equal(token, 'secondAccessToken'))
.then(assertRefreshTokenWasUsed('firstRefreshToken')); .then(assertRefreshTokenWasUsed('firstRefreshToken'));
}); });
// It only sends one refresh request, even if tokenGetter() is called // It only sends one refresh request, even if getAccessToken() is called
// multiple times and the refresh response hasn't come back yet. // multiple times and the refresh response hasn't come back yet.
it('does not send more than one refresh request', function () { it('does not send more than one refresh request', function () {
fakeClient.exchangeGrantToken.returns( fakeClient.exchangeGrantToken.returns(
...@@ -258,7 +258,7 @@ describe('AuthService', function () { ...@@ -258,7 +258,7 @@ describe('AuthService', function () {
// Perform an initial token fetch which will exchange the JWT grant for an // Perform an initial token fetch which will exchange the JWT grant for an
// access token. // access token.
return auth return auth
.tokenGetter() .getAccessToken()
.then(() => { .then(() => {
// Expire the access token to trigger a refresh request on the next // Expire the access token to trigger a refresh request on the next
// token fetch. // token fetch.
...@@ -271,7 +271,10 @@ describe('AuthService', function () { ...@@ -271,7 +271,10 @@ describe('AuthService', function () {
); );
// Request an auth token multiple times. // Request an auth token multiple times.
const tokens = Promise.all([auth.tokenGetter(), auth.tokenGetter()]); const tokens = Promise.all([
auth.getAccessToken(),
auth.getAccessToken(),
]);
// Finally, respond to the refresh request. // Finally, respond to the refresh request.
respond({ respond({
...@@ -297,7 +300,7 @@ describe('AuthService', function () { ...@@ -297,7 +300,7 @@ describe('AuthService', function () {
it('logs the user out', function () { it('logs the user out', function () {
expireAccessToken(); expireAccessToken();
return auth.tokenGetter(token => { return auth.getAccessToken(token => {
assert.equal(token, null); assert.equal(token, null);
}); });
}); });
...@@ -319,7 +322,7 @@ describe('AuthService', function () { ...@@ -319,7 +322,7 @@ describe('AuthService', function () {
].forEach(({ authority, grantToken, expectedToken }) => { ].forEach(({ authority, grantToken, expectedToken }) => {
it(`should not persist access tokens if a grant token (${grantToken}) was provided`, () => { it(`should not persist access tokens if a grant token (${grantToken}) was provided`, () => {
fakeSettings.services = [{ authority, grantToken }]; fakeSettings.services = [{ authority, grantToken }];
return auth.tokenGetter().then(() => { return auth.getAccessToken().then(() => {
assert.notCalled(fakeLocalStorage.setObject); assert.notCalled(fakeLocalStorage.setObject);
}); });
}); });
...@@ -329,7 +332,7 @@ describe('AuthService', function () { ...@@ -329,7 +332,7 @@ describe('AuthService', function () {
Promise.resolve(successfulTokenResponse) Promise.resolve(successfulTokenResponse)
); );
fakeSettings.services = [{ authority, grantToken }]; fakeSettings.services = [{ authority, grantToken }];
return auth.tokenGetter().then(token => { return auth.getAccessToken().then(token => {
assert.equal(token, expectedToken); assert.equal(token, expectedToken);
assert.notCalled(fakeLocalStorage.getObject); assert.notCalled(fakeLocalStorage.getObject);
}); });
...@@ -341,7 +344,7 @@ describe('AuthService', function () { ...@@ -341,7 +344,7 @@ describe('AuthService', function () {
return login() return login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(() => { .then(() => {
assert.calledWith(fakeLocalStorage.setObject, TOKEN_KEY, { assert.calledWith(fakeLocalStorage.setObject, TOKEN_KEY, {
...@@ -362,7 +365,7 @@ describe('AuthService', function () { ...@@ -362,7 +365,7 @@ describe('AuthService', function () {
refreshToken: 'secondRefreshToken', refreshToken: 'secondRefreshToken',
}) })
); );
return auth.tokenGetter(); return auth.getAccessToken();
} }
it('persists refreshed tokens to storage', () => { it('persists refreshed tokens to storage', () => {
...@@ -371,7 +374,7 @@ describe('AuthService', function () { ...@@ -371,7 +374,7 @@ describe('AuthService', function () {
// 1. Perform initial token exchange. // 1. Perform initial token exchange.
return login() return login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(() => { .then(() => {
// 2. Refresh access token. // 2. Refresh access token.
...@@ -393,7 +396,7 @@ describe('AuthService', function () { ...@@ -393,7 +396,7 @@ describe('AuthService', function () {
]; ];
return auth return auth
.tokenGetter() .getAccessToken()
.then(() => { .then(() => {
return expireAndRefreshAccessToken(); return expireAndRefreshAccessToken();
}) })
...@@ -411,7 +414,7 @@ describe('AuthService', function () { ...@@ -411,7 +414,7 @@ describe('AuthService', function () {
expiresAt: 123, expiresAt: 123,
}); });
return auth.tokenGetter().then(token => { return auth.getAccessToken().then(token => {
assert.equal(token, 'foo'); assert.equal(token, 'foo');
}); });
}); });
...@@ -436,7 +439,7 @@ describe('AuthService', function () { ...@@ -436,7 +439,7 @@ describe('AuthService', function () {
// Fetch the token again from the service and check that it gets // Fetch the token again from the service and check that it gets
// refreshed. // refreshed.
return auth.tokenGetter().then(token => { return auth.getAccessToken().then(token => {
assert.equal(token, 'secondToken'); assert.equal(token, 'secondToken');
assert.calledWith(fakeLocalStorage.setObject, TOKEN_KEY, { assert.calledWith(fakeLocalStorage.setObject, TOKEN_KEY, {
accessToken: 'secondToken', accessToken: 'secondToken',
...@@ -466,7 +469,7 @@ describe('AuthService', function () { ...@@ -466,7 +469,7 @@ describe('AuthService', function () {
it('ignores invalid tokens in storage', () => { it('ignores invalid tokens in storage', () => {
fakeSettings.services = []; fakeSettings.services = [];
fakeLocalStorage.getObject.withArgs('foo').returns(data); fakeLocalStorage.getObject.withArgs('foo').returns(data);
return auth.tokenGetter().then(token => { return auth.getAccessToken().then(token => {
assert.equal(token, null); assert.equal(token, null);
}); });
}); });
...@@ -496,14 +499,14 @@ describe('AuthService', function () { ...@@ -496,14 +499,14 @@ describe('AuthService', function () {
it('reloads tokens from storage', () => { it('reloads tokens from storage', () => {
return login() return login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(token => { .then(token => {
assert.equal(token, 'firstAccessToken'); assert.equal(token, 'firstAccessToken');
notifyStoredTokenChange(); notifyStoredTokenChange();
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(token => { .then(token => {
assert.equal(token, 'storedAccessToken'); assert.equal(token, 'storedAccessToken');
...@@ -542,7 +545,7 @@ describe('AuthService', function () { ...@@ -542,7 +545,7 @@ describe('AuthService', function () {
return auth return auth
.login() .login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(() => { .then(() => {
// 2. Verify that auth code is exchanged for access & refresh tokens. // 2. Verify that auth code is exchanged for access & refresh tokens.
...@@ -567,7 +570,7 @@ describe('AuthService', function () { ...@@ -567,7 +570,7 @@ describe('AuthService', function () {
return auth return auth
.login() .login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.catch(err => { .catch(err => {
assert.equal(err.message, expectedErr.message); assert.equal(err.message, expectedErr.message);
...@@ -583,7 +586,7 @@ describe('AuthService', function () { ...@@ -583,7 +586,7 @@ describe('AuthService', function () {
return login() return login()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(token => { .then(token => {
assert.notEqual(token, null); assert.notEqual(token, null);
...@@ -594,7 +597,7 @@ describe('AuthService', function () { ...@@ -594,7 +597,7 @@ describe('AuthService', function () {
return auth return auth
.logout() .logout()
.then(() => { .then(() => {
return auth.tokenGetter(); return auth.getAccessToken();
}) })
.then(token => { .then(token => {
assert.equal(token, null); assert.equal(token, null);
......
...@@ -48,7 +48,7 @@ describe('sidebar/services/groups', function () { ...@@ -48,7 +48,7 @@ describe('sidebar/services/groups', function () {
beforeEach(function () { beforeEach(function () {
fakeAuth = { fakeAuth = {
tokenGetter: sinon.stub().returns('1234'), getAccessToken: sinon.stub().returns('1234'),
}; };
fakeMetadata = { fakeMetadata = {
...@@ -563,7 +563,7 @@ describe('sidebar/services/groups', function () { ...@@ -563,7 +563,7 @@ describe('sidebar/services/groups', function () {
); );
// The user is logged out. // The user is logged out.
fakeAuth.tokenGetter.returns(null); fakeAuth.getAccessToken.returns(null);
return svc.load().then(groups => { return svc.load().then(groups => {
const groupIds = groups.map(g => g.id); const groupIds = groups.map(g => g.id);
...@@ -627,7 +627,7 @@ describe('sidebar/services/groups', function () { ...@@ -627,7 +627,7 @@ describe('sidebar/services/groups', function () {
); );
// The user is logged out. // The user is logged out.
fakeAuth.tokenGetter.returns(null); fakeAuth.getAccessToken.returns(null);
return svc.load().then(groups => { return svc.load().then(groups => {
const linkedToGroupShown = groups.some(g => g.id === 'out-of-scope'); const linkedToGroupShown = groups.some(g => g.id === 'out-of-scope');
...@@ -656,7 +656,7 @@ describe('sidebar/services/groups', function () { ...@@ -656,7 +656,7 @@ describe('sidebar/services/groups', function () {
Promise.resolve({ name: 'Public', id: '__world__' }) Promise.resolve({ name: 'Public', id: '__world__' })
); );
fakeAuth.tokenGetter.returns(null); fakeAuth.getAccessToken.returns(null);
return svc.load().then(groups => { return svc.load().then(groups => {
const publicGroupShown = groups.some(g => g.id === '__world__'); const publicGroupShown = groups.some(g => g.id === '__world__');
...@@ -690,7 +690,7 @@ describe('sidebar/services/groups', function () { ...@@ -690,7 +690,7 @@ describe('sidebar/services/groups', function () {
groups.push({ name: 'BioPub', id: 'biopub' }); groups.push({ name: 'BioPub', id: 'biopub' });
} }
fakeAuth.tokenGetter.returns(loggedIn ? '1234' : null); fakeAuth.getAccessToken.returns(loggedIn ? '1234' : null);
fakeApi.groups.list.returns(Promise.resolve(groups)); fakeApi.groups.list.returns(Promise.resolve(groups));
return svc.load().then(groups => { return svc.load().then(groups => {
......
...@@ -93,7 +93,7 @@ describe('Streamer', function () { ...@@ -93,7 +93,7 @@ describe('Streamer', function () {
beforeEach(function () { beforeEach(function () {
fakeAuth = { fakeAuth = {
tokenGetter: function () { getAccessToken: function () {
return Promise.resolve('dummy-access-token'); return Promise.resolve('dummy-access-token');
}, },
}; };
...@@ -208,7 +208,7 @@ describe('Streamer', function () { ...@@ -208,7 +208,7 @@ describe('Streamer', function () {
}); });
it('should not include credentials in the URL if the client has no access token', function () { it('should not include credentials in the URL if the client has no access token', function () {
fakeAuth.tokenGetter = function () { fakeAuth.getAccessToken = function () {
return Promise.resolve(null); return Promise.resolve(null);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment