From 460575b462170c198c6fcdc8d96b08155197944f Mon Sep 17 00:00:00 2001 From: Mihajlo Date: Fri, 28 May 2021 15:05:44 +0200 Subject: [PATCH] fix: when sas fails with verifying credentials, resend request with new csrf token --- src/ContextManager.ts | 4 +-- src/SASViyaApiClient.ts | 23 +++++++-------- src/SessionManager.ts | 5 +--- src/auth/AuthManager.ts | 45 ++++++++++++++++++++--------- src/job-execution/WebJobExecutor.ts | 6 ++-- src/request/RequestClient.ts | 11 ++++--- 6 files changed, 50 insertions(+), 44 deletions(-) diff --git a/src/ContextManager.ts b/src/ContextManager.ts index 3c15ff5..eec0dff 100644 --- a/src/ContextManager.ts +++ b/src/ContextManager.ts @@ -314,9 +314,7 @@ export class ContextManager { contextId: string, accessToken?: string ): Promise { - const { - result: context - } = await this.requestClient + const { result: context } = await this.requestClient .get( `${this.serverUrl}/compute/contexts/${contextId}`, accessToken diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index fff248a..d688807 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -579,16 +579,15 @@ export class SASViyaApiClient { } } - const { - result: createFolderResponse - } = await this.requestClient.post( - `/folders/folders?parentFolderUri=${parentFolderUri}`, - { - name: folderName, - type: 'folder' - }, - accessToken - ) + const { result: createFolderResponse } = + await this.requestClient.post( + `/folders/folders?parentFolderUri=${parentFolderUri}`, + { + name: folderName, + type: 'folder' + }, + accessToken + ) // update folder map with newly created folder. await this.populateFolderMap( @@ -860,9 +859,7 @@ export class SASViyaApiClient { throw new Error(`URI of job definition was not found.`) } - const { - result: jobDefinition - } = await this.requestClient + const { result: jobDefinition } = await this.requestClient .get( `${this.serverUrl}${jobDefinitionLink.href}`, accessToken diff --git a/src/SessionManager.ts b/src/SessionManager.ts index 3c780dd..2120471 100644 --- a/src/SessionManager.ts +++ b/src/SessionManager.ts @@ -91,10 +91,7 @@ export class SessionManager { } private async createAndWaitForSession(accessToken?: string) { - const { - result: createdSession, - etag - } = await this.requestClient + const { result: createdSession, etag } = await this.requestClient .post( `${this.serverUrl}/compute/contexts/${ this.currentContext!.id diff --git a/src/auth/AuthManager.ts b/src/auth/AuthManager.ts index d917b76..82fa4dd 100644 --- a/src/auth/AuthManager.ts +++ b/src/auth/AuthManager.ts @@ -44,21 +44,13 @@ export class AuthManager { } } - for (const key in loginForm) { - loginParams[key] = loginForm[key] - } - const loginParamsStr = serialize(loginParams) + let loginResponse = await this.sendLoginRequest(loginForm, loginParams) - const { result: loginResponse } = await this.requestClient.post( - this.loginUrl, - loginParamsStr, - undefined, - 'text/plain', - { - 'Content-Type': 'application/x-www-form-urlencoded', - Accept: '*/*' - } - ) + if (isCredentialsVerifyError(loginResponse)) { + let newLoginForm = await this.getLoginForm(loginResponse) + + loginResponse = await this.sendLoginRequest(newLoginForm, loginParams) + } let loggedIn = isLogInSuccess(loginResponse) @@ -77,6 +69,26 @@ export class AuthManager { } } + private async sendLoginRequest(loginForm: any, loginParams: any) { + for (const key in loginForm) { + loginParams[key] = loginForm[key] + } + const loginParamsStr = serialize(loginParams) + + const { result: loginResponse } = await this.requestClient.post( + this.loginUrl, + loginParamsStr, + undefined, + 'text/plain', + { + 'Content-Type': 'application/x-www-form-urlencoded', + Accept: '*/*' + } + ) + + return loginResponse + } + /** * Checks whether a session is active, or login is required. * @returns - a promise which resolves with an object containing two values - a boolean `isLoggedIn`, and a string `userName`. @@ -168,5 +180,10 @@ export class AuthManager { } } +const isCredentialsVerifyError = (response: string): boolean => + /An error occurred while the system was verifying your credentials. Please enter your credentials again./gm.test( + response + ) + const isLogInSuccess = (response: string): boolean => /You have signed in/gm.test(response) diff --git a/src/job-execution/WebJobExecutor.ts b/src/job-execution/WebJobExecutor.ts index 816c6d7..61f215c 100644 --- a/src/job-execution/WebJobExecutor.ts +++ b/src/job-execution/WebJobExecutor.ts @@ -71,10 +71,8 @@ export class WebJobExecutor extends BaseJobExecutor { } else { // param based approach try { - const { - formData: newFormData, - requestParams: params - } = generateTableUploadForm(formData, data) + const { formData: newFormData, requestParams: params } = + generateTableUploadForm(formData, data) formData = newFormData requestParams = { ...requestParams, ...params } } catch (e) { diff --git a/src/request/RequestClient.ts b/src/request/RequestClient.ts index c55060b..c38d506 100644 --- a/src/request/RequestClient.ts +++ b/src/request/RequestClient.ts @@ -214,9 +214,8 @@ export class RequestClient implements HttpClient { const headers = this.getHeaders(accessToken, 'application/json') if (this.fileUploadCsrfToken?.value) { - headers[ - this.fileUploadCsrfToken.headerName - ] = this.fileUploadCsrfToken.value + headers[this.fileUploadCsrfToken.headerName] = + this.fileUploadCsrfToken.value } try { @@ -333,9 +332,9 @@ export class RequestClient implements HttpClient { } private parseCsrfToken = (response: AxiosResponse): CsrfToken | undefined => { - const tokenHeader = (response.headers[ - 'x-csrf-header' - ] as string)?.toLowerCase() + const tokenHeader = ( + response.headers['x-csrf-header'] as string + )?.toLowerCase() if (tokenHeader) { const token = response.headers[tokenHeader]