Fixes ACCOUNTS-5FJ. Use common getJwtPayloads function to decode jwt payloads

This commit is contained in:
ErickSkrauch 2019-05-20 16:32:07 +03:00
parent 9ee96473c2
commit afd6c3c300
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
2 changed files with 10 additions and 8 deletions

View File

@ -1,6 +1,6 @@
// @flow
import type { Account, State as AccountsState } from './reducer';
import { getJwtPayload } from 'functions';
import { getJwtPayloads } from 'functions';
import { sessionStorage } from 'services/localStorage';
import { validateToken, requestToken, logout } from 'services/api/authentication';
import { relogin as navigateToLogin } from 'components/auth/actions';
@ -116,8 +116,7 @@ export function authenticate(account: Account | {
}
function findAccountIdFromToken(token: string): number {
const encodedPayloads = token.split('.')[1];
const { sub, jti }: { sub: string, jti: number } = JSON.parse(atob(encodedPayloads));
const { sub, jti } = getJwtPayloads(token);
// sub has the format "ely|{accountId}", so we must trim "ely|" part
if (sub) {
return parseInt(sub.substr(4), 10);
@ -144,10 +143,10 @@ export function ensureToken() {
const {token} = getActiveAccount(getState()) || {};
try {
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time dissynchronization problem
const jwt = getJwtPayload(token);
const SAFETY_FACTOR = 300; // ask new token earlier to overcome time desynchronization problem
const { exp } = getJwtPayloads(token);
if (jwt.exp - SAFETY_FACTOR < Date.now() / 1000) {
if (exp - SAFETY_FACTOR < Date.now() / 1000) {
return dispatch(requestNewToken());
}
} catch (err) {

View File

@ -66,9 +66,12 @@ export { default as debounce } from 'debounce';
*
* @return {object} - decoded jwt payload
*/
export function getJwtPayload(jwt: string): Object {
export function getJwtPayloads(jwt: string): {
sub: string;
jti: number;
exp: number;
} {
const parts = (jwt || '').split('.');
if (parts.length !== 3) {
throw new Error('Invalid jwt token');
}