From 5cb5bbdb55f05bdc872bb2e0cf7b15c1b3edb911 Mon Sep 17 00:00:00 2001 From: Krishna Acondy Date: Wed, 30 Jun 2021 15:19:12 +0100 Subject: [PATCH] fix(execution): refresh tokens before fetching results --- src/SASViyaApiClient.ts | 83 +++++++++++++++++++---------------------- 1 file changed, 39 insertions(+), 44 deletions(-) diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index 6f1a972..e33c613 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -290,16 +290,14 @@ export class SASViyaApiClient { printPid = false, variables?: MacroVar ): Promise { - const { access_token } = authConfig || {} + let access_token = (authConfig || {}).access_token + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) + } + const logger = process.logger || console try { - const headers: any = { - 'Content-Type': 'application/json' - } - - if (access_token) headers.Authorization = `Bearer ${access_token}` - let executionSessionId: string const session = await this.sessionManager @@ -443,6 +441,10 @@ export class SASViyaApiClient { throw prefixMessage(err, 'Error while polling job status. ') }) + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) + } + const { result: currentJob } = await this.requestClient .get( `/compute/sessions/${executionSessionId}/jobs/${postedJob.id}`, @@ -886,7 +888,10 @@ export class SASViyaApiClient { printPid = false, variables?: MacroVar ) { - let { access_token } = authConfig || {} + let access_token = (authConfig || {}).access_token + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) + } if (isRelativePath(sasJob) && !this.rootFolderName) { throw new Error( @@ -913,12 +918,6 @@ export class SASViyaApiClient { ) } - const headers: any = { 'Content-Type': 'application/json' } - - if (!!access_token) { - headers.Authorization = `Bearer ${access_token}` - } - const jobToExecute = jobFolder?.find((item) => item.name === jobName) if (!jobToExecute) { @@ -985,7 +984,10 @@ export class SASViyaApiClient { data?: any, authConfig?: AuthConfig ) { - let { access_token } = authConfig || {} + let access_token = (authConfig || {}).access_token + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) + } if (isRelativePath(sasJob) && !this.rootFolderName) { throw new Error( 'Relative paths cannot be used without specifying a root folder name.' @@ -1145,21 +1147,9 @@ export class SASViyaApiClient { let POLL_INTERVAL = 300 let MAX_POLL_COUNT = 1000 let MAX_ERROR_COUNT = 5 - let { access_token, refresh_token, client, secret } = authConfig || {} - if (access_token && refresh_token) { - if ( - client && - secret && - refresh_token && - (isAccessTokenExpiring(access_token) || - isRefreshTokenExpiring(refresh_token)) - ) { - ;({ access_token, refresh_token } = await this.refreshTokens( - client, - secret, - refresh_token - )) - } + let access_token = (authConfig || {}).access_token + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) } if (pollOptions) { @@ -1213,20 +1203,8 @@ export class SASViyaApiClient { postedJobState === 'pending' || postedJobState === 'unavailable' ) { - if (access_token && refresh_token) { - if ( - client && - secret && - refresh_token && - (isAccessTokenExpiring(access_token) || - isRefreshTokenExpiring(refresh_token)) - ) { - ;({ access_token, refresh_token } = await this.refreshTokens( - client, - secret, - refresh_token - )) - } + if (authConfig) { + ;({ access_token } = await this.getTokens(authConfig)) } if (stateLink) { @@ -1510,4 +1488,21 @@ export class SASViyaApiClient { return movedFolder } + + private async getTokens(authConfig: AuthConfig): Promise { + const logger = process.logger || console + let { access_token, refresh_token, client, secret } = authConfig + if ( + isAccessTokenExpiring(access_token) || + isRefreshTokenExpiring(refresh_token) + ) { + logger.info('Refreshing access and refresh tokens.') + ;({ access_token, refresh_token } = await this.refreshTokens( + client, + secret, + refresh_token + )) + } + return { access_token, refresh_token, client, secret } + } }