Unverified Commit 5c242e46 authored by Robert Knight's avatar Robert Knight Committed by Sean Hammond

Remove the cookie-based authentication service

parent 68e6524b
'use strict';
var NULL_TOKEN = Promise.resolve(null);
/**
* Service for fetching and caching access tokens for the Hypothesis API.
*/
// @ngInject
function auth($http, jwtHelper, settings) {
var cachedToken = NULL_TOKEN;
/**
* Fetch a new API token for the current logged-in user.
*
* The user is authenticated using their session cookie.
*
* @return {Promise<string>} - A promise for a new JWT token.
*/
function fetchToken() {
var tokenUrl = new URL('token', settings.apiUrl).href;
return $http.get(tokenUrl, {}).then(function (response) {
return response.data;
});
}
/**
* Fetch or return a cached JWT API token for the current user.
*
* @return {Promise<string>} - A promise for a JWT API token for the current
* user.
*/
function tokenGetter() {
return cachedToken.then(function (token) {
if (!token || jwtHelper.isTokenExpired(token)) {
cachedToken = fetchToken();
return cachedToken;
} else {
return token;
}
});
}
function clearCache() {
cachedToken = NULL_TOKEN;
}
return {
clearCache: clearCache,
tokenGetter: tokenGetter,
};
}
module.exports = auth;
'use strict';
var auth = require('../auth');
describe('auth', function () {
var fakeHttp;
var fakeJwtHelper;
var fakeSettings;
var fakeTokens = ['token-one', 'token-two'];
var fakeTokenIndex;
beforeEach(function () {
fakeTokenIndex = 0;
fakeHttp = {
get: sinon.spy(function (url, config) {
assert.equal(url, 'https://test.hypothes.is/api/token');
assert.deepEqual(config, {});
var result = {status: 200, data: fakeTokens[fakeTokenIndex]};
++fakeTokenIndex;
return Promise.resolve(result);
}),
};
fakeJwtHelper = {isTokenExpired: sinon.stub()};
fakeSettings = {
apiUrl: 'https://test.hypothes.is/api/',
};
});
function authFactory() {
return auth(fakeHttp, fakeJwtHelper, fakeSettings);
}
describe('#tokenGetter', function () {
it('should fetch and return a new token', function () {
var auth = authFactory();
return auth.tokenGetter().then(function (token) {
assert.called(fakeHttp.get);
assert.equal(token, fakeTokens[0]);
});
});
it('should cache tokens for future use', function () {
var auth = authFactory();
return auth.tokenGetter().then(function () {
return auth.tokenGetter();
}).then(function (token) {
assert.calledOnce(fakeHttp.get);
assert.equal(token, fakeTokens[0]);
});
});
it('should refresh expired tokens', function () {
var auth = authFactory();
return auth.tokenGetter().then(function () {
fakeJwtHelper.isTokenExpired = function () {
return true;
};
return auth.tokenGetter();
}).then(function (token) {
assert.calledTwice(fakeHttp.get);
assert.equal(token, fakeTokens[1]);
});
});
});
describe('#clearCache', function () {
it('should remove existing cached tokens', function () {
var auth = authFactory();
return auth.tokenGetter().then(function () {
auth.clearCache();
return auth.tokenGetter();
}).then(function (token) {
assert.calledTwice(fakeHttp.get);
assert.equal(token, fakeTokens[1]);
});
});
});
});
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