From 5d77bbba8badd085a886c79940561b6de671c243 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Thu, 1 Jul 2021 09:10:32 +0100 Subject: [PATCH] fix(auth): use token functions from utils library --- src/SASViyaApiClient.ts | 17 +++++++++++------ src/utils/auth.ts | 35 ----------------------------------- src/utils/index.ts | 1 - 3 files changed, 11 insertions(+), 42 deletions(-) delete mode 100644 src/utils/auth.ts diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index e33c613..068e780 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -3,9 +3,7 @@ import { isRelativePath, isUri, isUrl, - fetchLogByChunks, - isAccessTokenExpiring, - isRefreshTokenExpiring + fetchLogByChunks } from './utils' import * as NodeFormData from 'form-data' import { @@ -27,11 +25,18 @@ import { import { formatDataForRequest } from './utils/formatDataForRequest' import { SessionManager } from './SessionManager' import { ContextManager } from './ContextManager' -import { timestampToYYYYMMDDHHMMSS } from '@sasjs/utils/time' -import { Logger, LogLevel } from '@sasjs/utils/logger' +import { + timestampToYYYYMMDDHHMMSS, + isAccessTokenExpiring, + isRefreshTokenExpiring, + Logger, + LogLevel, + SasAuthResponse, + MacroVar, + AuthConfig +} from '@sasjs/utils' import { isAuthorizeFormRequired } from './auth/isAuthorizeFormRequired' import { RequestClient } from './request/RequestClient' -import { SasAuthResponse, MacroVar, AuthConfig } from '@sasjs/utils/types' import { prefixMessage } from '@sasjs/utils/error' import * as mime from 'mime' diff --git a/src/utils/auth.ts b/src/utils/auth.ts deleted file mode 100644 index 8cbff9b..0000000 --- a/src/utils/auth.ts +++ /dev/null @@ -1,35 +0,0 @@ -import jwtDecode from 'jwt-decode' - -/** - * Checks if the Access Token is expired or is expiring in 1 hour. A default Access Token - * lasts 12 hours. If the Access Token expires, the Refresh Token is used to fetch a new - * Access Token. In the case that the Refresh Token is expired, 1 hour is enough to let - * most jobs finish. - * @param {string} token- token string that will be evaluated - */ -export function isAccessTokenExpiring(token: string): boolean { - if (!token) { - return true - } - const payload = jwtDecode<{ exp: number }>(token) - const timeToLive = payload.exp - new Date().valueOf() / 1000 - - return timeToLive <= 60 * 60 // 1 hour -} - -/** - * Checks if the Refresh Token is expired or expiring in 30 secs. A default Refresh Token - * lasts 30 days. Once the Refresh Token expires, the user must re-authenticate (provide - * credentials in a browser to obtain an authorisation code). 30 seconds is enough time - * to make a request for a final Access Token. - * @param {string} token- token string that will be evaluated - */ -export function isRefreshTokenExpiring(token?: string): boolean { - if (!token) { - return true - } - const payload = jwtDecode<{ exp: number }>(token) - const timeToLive = payload.exp - new Date().valueOf() / 1000 - - return timeToLive <= 30 // 30 seconds -} diff --git a/src/utils/index.ts b/src/utils/index.ts index 88d9f16..3f6ec1d 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,4 @@ export * from './asyncForEach' -export * from './auth' export * from './compareTimestamps' export * from './convertToCsv' export * from './isRelativePath'