mirror of
https://github.com/elyby/accounts-frontend.git
synced 2025-05-31 14:11:58 +05:30
#246: remove outdated code from refreshTokenMiddleware
This commit is contained in:
@ -1,3 +1,4 @@
|
|||||||
|
import { getJwtPayload } from 'functions';
|
||||||
import authentication from 'services/api/authentication';
|
import authentication from 'services/api/authentication';
|
||||||
import logger from 'services/logger';
|
import logger from 'services/logger';
|
||||||
import { updateToken, logoutAll } from 'components/accounts/actions';
|
import { updateToken, logoutAll } from 'components/accounts/actions';
|
||||||
@ -14,7 +15,7 @@ import { updateToken, logoutAll } from 'components/accounts/actions';
|
|||||||
export default function refreshTokenMiddleware({dispatch, getState}) {
|
export default function refreshTokenMiddleware({dispatch, getState}) {
|
||||||
return {
|
return {
|
||||||
before(req) {
|
before(req) {
|
||||||
const {user, accounts} = getState();
|
const {accounts} = getState();
|
||||||
|
|
||||||
let refreshToken;
|
let refreshToken;
|
||||||
let token;
|
let token;
|
||||||
@ -24,18 +25,15 @@ export default function refreshTokenMiddleware({dispatch, getState}) {
|
|||||||
if (accounts.active) {
|
if (accounts.active) {
|
||||||
token = accounts.active.token;
|
token = accounts.active.token;
|
||||||
refreshToken = accounts.active.refreshToken;
|
refreshToken = accounts.active.refreshToken;
|
||||||
} else { // #legacy token
|
|
||||||
token = user.token;
|
|
||||||
refreshToken = user.refreshToken;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!token || req.options.token || isRefreshTokenRequest) {
|
if (!token || req.options.token || isRefreshTokenRequest) {
|
||||||
return req;
|
return Promise.resolve(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time dissynchronization problem
|
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time dissynchronization problem
|
||||||
const jwt = getJWTPayload(token);
|
const jwt = getJwtPayload(token);
|
||||||
|
|
||||||
if (jwt.exp - SAFETY_FACTOR < Date.now() / 1000) {
|
if (jwt.exp - SAFETY_FACTOR < Date.now() / 1000) {
|
||||||
return requestAccessToken(refreshToken, dispatch).then(() => req);
|
return requestAccessToken(refreshToken, dispatch).then(() => req);
|
||||||
@ -53,8 +51,8 @@ export default function refreshTokenMiddleware({dispatch, getState}) {
|
|||||||
|
|
||||||
catch(resp, req, restart) {
|
catch(resp, req, restart) {
|
||||||
if (resp && resp.status === 401 && !req.options.token) {
|
if (resp && resp.status === 401 && !req.options.token) {
|
||||||
const {user, accounts} = getState();
|
const {accounts} = getState();
|
||||||
const {refreshToken} = accounts.active ? accounts.active : user;
|
const {refreshToken} = accounts.active || {};
|
||||||
|
|
||||||
if (resp.message === 'Token expired' && refreshToken) {
|
if (resp.message === 'Token expired' && refreshToken) {
|
||||||
// request token and retry
|
// request token and retry
|
||||||
@ -83,16 +81,3 @@ function requestAccessToken(refreshToken, dispatch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function getJWTPayload(jwt) {
|
|
||||||
const parts = (jwt || '').split('.');
|
|
||||||
|
|
||||||
if (parts.length !== 3) {
|
|
||||||
throw new Error('Invalid jwt token');
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
return JSON.parse(atob(parts[1]));
|
|
||||||
} catch (err) {
|
|
||||||
throw new Error('Can not decode jwt token');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -61,3 +61,24 @@ export const rAF = window.requestAnimationFrame
|
|||||||
* @param {bool} [immediate=false] - whether to execute at the beginning
|
* @param {bool} [immediate=false] - whether to execute at the beginning
|
||||||
*/
|
*/
|
||||||
export debounce from 'debounce';
|
export debounce from 'debounce';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string} jwt
|
||||||
|
*
|
||||||
|
* @throws {Error} If can not decode token
|
||||||
|
*
|
||||||
|
* @return {object} - decoded jwt payload
|
||||||
|
*/
|
||||||
|
export function getJwtPayload(jwt) {
|
||||||
|
const parts = (jwt || '').split('.');
|
||||||
|
|
||||||
|
if (parts.length !== 3) {
|
||||||
|
throw new Error('Invalid jwt token');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return JSON.parse(atob(parts[1]));
|
||||||
|
} catch (err) {
|
||||||
|
throw new Error('Can not decode jwt token');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import expect from 'unexpected';
|
import expect from 'unexpected';
|
||||||
|
import sinon from 'sinon';
|
||||||
|
|
||||||
import refreshTokenMiddleware from 'components/user/middlewares/refreshTokenMiddleware';
|
import refreshTokenMiddleware from 'components/user/middlewares/refreshTokenMiddleware';
|
||||||
|
|
||||||
@ -75,9 +76,11 @@ describe('refreshTokenMiddleware', () => {
|
|||||||
const data = {url: '/refresh-token', options: {}};
|
const data = {url: '/refresh-token', options: {}};
|
||||||
const resp = middleware.before(data);
|
const resp = middleware.before(data);
|
||||||
|
|
||||||
expect(resp, 'to satisfy', data);
|
|
||||||
|
|
||||||
expect(authentication.requestToken, 'was not called');
|
return expect(resp, 'to be fulfilled with', data)
|
||||||
|
.then(() =>
|
||||||
|
expect(authentication.requestToken, 'was not called')
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not auto refresh token if options.token specified', () => {
|
it('should not auto refresh token if options.token specified', () => {
|
||||||
@ -142,40 +145,6 @@ describe('refreshTokenMiddleware', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('when token expired legacy user', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
getState.returns({
|
|
||||||
accounts: {
|
|
||||||
active: null,
|
|
||||||
available: []
|
|
||||||
},
|
|
||||||
user: {
|
|
||||||
token: expiredToken,
|
|
||||||
refreshToken
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should request new token', () => {
|
|
||||||
const data = {
|
|
||||||
url: 'foo',
|
|
||||||
options: {
|
|
||||||
headers: {}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
authentication.requestToken.returns(Promise.resolve({token: validToken}));
|
|
||||||
|
|
||||||
return middleware.before(data).then((resp) => {
|
|
||||||
expect(resp, 'to satisfy', data);
|
|
||||||
|
|
||||||
expect(authentication.requestToken, 'to have a call satisfying', [
|
|
||||||
refreshToken
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should not be applied if no token', () => {
|
it('should not be applied if no token', () => {
|
||||||
getState.returns({
|
getState.returns({
|
||||||
accounts: {
|
accounts: {
|
||||||
@ -187,9 +156,10 @@ describe('refreshTokenMiddleware', () => {
|
|||||||
const data = {url: 'foo'};
|
const data = {url: 'foo'};
|
||||||
const resp = middleware.before(data);
|
const resp = middleware.before(data);
|
||||||
|
|
||||||
expect(resp, 'to satisfy', data);
|
return expect(resp, 'to be fulfilled with', data)
|
||||||
|
.then(() =>
|
||||||
expect(authentication.requestToken, 'was not called');
|
expect(authentication.requestToken, 'was not called')
|
||||||
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -290,25 +260,5 @@ describe('refreshTokenMiddleware', () => {
|
|||||||
expect(authentication.requestToken, 'was not called');
|
expect(authentication.requestToken, 'was not called');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('legacy user.refreshToken', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
getState.returns({
|
|
||||||
accounts: {
|
|
||||||
active: null
|
|
||||||
},
|
|
||||||
user: {refreshToken}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should request new token if expired', () =>
|
|
||||||
middleware.catch(expiredResponse, {options: {}}, restart).then(() => {
|
|
||||||
expect(authentication.requestToken, 'to have a call satisfying', [
|
|
||||||
refreshToken
|
|
||||||
]);
|
|
||||||
expect(restart, 'was called');
|
|
||||||
})
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Reference in New Issue
Block a user