Skip to content

Commit

Permalink
2.3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
obabichev committed Nov 24, 2021
1 parent db94503 commit 765f28b
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-token-auth",
"version": "2.3.3",
"version": "2.3.6",
"description": "React Token Auth",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/isTokenExpired.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('isTokenExpired', () => {

it('hardcoded token is expired', () => {
const token =
'header.eyJlbWFpbCI6IlRlc3RAZ21haWwuY29tMiIsInN1YiI6IjEzIiwiaWF0IjoxNjM3NzQ3OTg3LCJleHAiOjE2Mzc3NDgwNDd9.sign';
expect(isTokenExpired(token)).toBeTruthy();
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6IkpzYWRrZmphZGpraGZAcXdlcXdlcyIsInN1YiI6IjE1IiwiaWF0IjoxNjM3NzUxMjM5LCJleHAiOjE2Mzc3NTEyOTl9.ZjcVgbVvoZrIZHzjIckYgFwY5rnlyxlHGvGNHg_CRRk';
expect(isTokenExpired(token, 5000)).toBeTruthy();
});
});
14 changes: 7 additions & 7 deletions src/createAsyncAuthProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const createAsyncAuthProvider = <Session>({
expirationThresholdMillisec = 5000,
debug = false,
}: IAsyncAuthProviderConfig<Session>): IAsyncAuthProvider<Session> => {
const { log } = createLogger(debug);
const logger = createLogger(debug);
const listenersContainer = createListenersContainer();

const tokenProvider = createAsyncTokenProvider<Session>({
Expand All @@ -52,7 +52,7 @@ export const createAsyncAuthProvider = <Session>({

let _session: Maybe<Session> = null;
const updateSession = async (session: Maybe<Session>) => {
log('updateSession', 'session', session);
logger.log('updateSession', 'session', session);
await tokenProvider.setToken(session);
_session = session;
listenersContainer.notify();
Expand All @@ -77,19 +77,19 @@ export const createAsyncAuthProvider = <Session>({

const getSession = async () => {
const accessToken = extractAccessToken(getSessionState(), getAccessToken);
log('getSession', 'accessToken', accessToken);
log('getSession', 'tokenUpdater', tokenUpdater);
logger.log('getSession', 'accessToken', accessToken);
logger.log('getSession', 'tokenUpdater', tokenUpdater);
if (accessToken) {
log(
logger.log(
'getSession',
'isTokenExpired(accessToken, expirationThresholdMillisec)',
isTokenExpired(accessToken, expirationThresholdMillisec),
isTokenExpired(accessToken, expirationThresholdMillisec, logger),
);
}

if (_session && tokenUpdater && accessToken && isTokenExpired(accessToken, expirationThresholdMillisec)) {
const updatedSession = await tokenUpdater.updateToken(_session);
log('getSession', 'updatedSession', accessToken);
logger.log('getSession', 'updatedSession', accessToken);
await updateSession(updatedSession);
}

Expand Down
18 changes: 13 additions & 5 deletions src/isTokenExpired.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { SimpleLogger } from './logger';
import { Maybe, TokenString } from './types';

export const isTokenExpired = (token: TokenString, thresholdMillisec?: number) =>
isTimestampExpired(jwtExp(token), thresholdMillisec);
export const isTokenExpired = (token: TokenString, thresholdMillisec?: number, logger?: SimpleLogger) =>
isTimestampExpired(jwtExp(token, logger), thresholdMillisec, logger);

const jwtExp = (token: string): number | null => {
const jwtExp = (token: string, logger?: SimpleLogger): number | null => {
const split = token.split('.');
logger?.log('jwtExp', 'split', split);

if (split.length < 2) {
return null;
}

try {
const middlePart = Buffer.from(token.split('.')[1], 'base64').toString();
const middlePart = atob(token.split('.')[1]);
// const middlePart = Buffer.from(token.split('.')[1], 'base64').toString();
logger?.log('jwtExp', 'middlePart', middlePart);
const jwt = JSON.parse(middlePart);
logger?.log('jwtExp', 'jwt', jwt);
if (jwt && jwt.exp && Number.isFinite(jwt.exp)) {
return jwt.exp * 1000;
} else {
Expand All @@ -23,10 +28,13 @@ const jwtExp = (token: string): number | null => {
}
};

const isTimestampExpired = (exp: Maybe<number>, thresholdMillisec?: number) => {
const isTimestampExpired = (exp: Maybe<number>, thresholdMillisec?: number, logger?: SimpleLogger) => {
logger?.log('isTimestampExpired', 'exp', exp);
if (!exp) {
return false;
}

logger?.log('isTimestampExpired', 'Date.now()', Date.now());
logger?.log('isTimestampExpired', '(thresholdMillisec ?? 0', thresholdMillisec ?? 0);
return Date.now() > exp - (thresholdMillisec ?? 0);
};
6 changes: 5 additions & 1 deletion src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const createLogger = (debug?: boolean) => {
export interface SimpleLogger {
log: (name: string, message: string, ...objs: any[]) => void;
}

export const createLogger = (debug?: boolean): SimpleLogger => {
const log = (name: string, message: string, ...objs: any[]) => {
if (debug) {
// tslint:disable-next-line:no-console
Expand Down

0 comments on commit 765f28b

Please sign in to comment.