mirror of
https://github.com/sasjs/adapter.git
synced 2026-01-07 12:30:06 +00:00
fix: when sas fails with verifying credentials, resend request with new csrf token
This commit is contained in:
@@ -314,9 +314,7 @@ export class ContextManager {
|
|||||||
contextId: string,
|
contextId: string,
|
||||||
accessToken?: string
|
accessToken?: string
|
||||||
): Promise<ContextAllAttributes> {
|
): Promise<ContextAllAttributes> {
|
||||||
const {
|
const { result: context } = await this.requestClient
|
||||||
result: context
|
|
||||||
} = await this.requestClient
|
|
||||||
.get<ContextAllAttributes>(
|
.get<ContextAllAttributes>(
|
||||||
`${this.serverUrl}/compute/contexts/${contextId}`,
|
`${this.serverUrl}/compute/contexts/${contextId}`,
|
||||||
accessToken
|
accessToken
|
||||||
|
|||||||
@@ -579,9 +579,8 @@ export class SASViyaApiClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const { result: createFolderResponse } =
|
||||||
result: createFolderResponse
|
await this.requestClient.post<Folder>(
|
||||||
} = await this.requestClient.post<Folder>(
|
|
||||||
`/folders/folders?parentFolderUri=${parentFolderUri}`,
|
`/folders/folders?parentFolderUri=${parentFolderUri}`,
|
||||||
{
|
{
|
||||||
name: folderName,
|
name: folderName,
|
||||||
@@ -860,9 +859,7 @@ export class SASViyaApiClient {
|
|||||||
throw new Error(`URI of job definition was not found.`)
|
throw new Error(`URI of job definition was not found.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const {
|
const { result: jobDefinition } = await this.requestClient
|
||||||
result: jobDefinition
|
|
||||||
} = await this.requestClient
|
|
||||||
.get<JobDefinition>(
|
.get<JobDefinition>(
|
||||||
`${this.serverUrl}${jobDefinitionLink.href}`,
|
`${this.serverUrl}${jobDefinitionLink.href}`,
|
||||||
accessToken
|
accessToken
|
||||||
|
|||||||
@@ -91,10 +91,7 @@ export class SessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async createAndWaitForSession(accessToken?: string) {
|
private async createAndWaitForSession(accessToken?: string) {
|
||||||
const {
|
const { result: createdSession, etag } = await this.requestClient
|
||||||
result: createdSession,
|
|
||||||
etag
|
|
||||||
} = await this.requestClient
|
|
||||||
.post<Session>(
|
.post<Session>(
|
||||||
`${this.serverUrl}/compute/contexts/${
|
`${this.serverUrl}/compute/contexts/${
|
||||||
this.currentContext!.id
|
this.currentContext!.id
|
||||||
|
|||||||
@@ -44,21 +44,13 @@ export class AuthManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const key in loginForm) {
|
let loginResponse = await this.sendLoginRequest(loginForm, loginParams)
|
||||||
loginParams[key] = loginForm[key]
|
|
||||||
}
|
|
||||||
const loginParamsStr = serialize(loginParams)
|
|
||||||
|
|
||||||
const { result: loginResponse } = await this.requestClient.post<string>(
|
if (isCredentialsVerifyError(loginResponse)) {
|
||||||
this.loginUrl,
|
let newLoginForm = await this.getLoginForm(loginResponse)
|
||||||
loginParamsStr,
|
|
||||||
undefined,
|
loginResponse = await this.sendLoginRequest(newLoginForm, loginParams)
|
||||||
'text/plain',
|
|
||||||
{
|
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
|
||||||
Accept: '*/*'
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
let loggedIn = isLogInSuccess(loginResponse)
|
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<string>(
|
||||||
|
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.
|
* 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`.
|
* @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 =>
|
const isLogInSuccess = (response: string): boolean =>
|
||||||
/You have signed in/gm.test(response)
|
/You have signed in/gm.test(response)
|
||||||
|
|||||||
@@ -71,10 +71,8 @@ export class WebJobExecutor extends BaseJobExecutor {
|
|||||||
} else {
|
} else {
|
||||||
// param based approach
|
// param based approach
|
||||||
try {
|
try {
|
||||||
const {
|
const { formData: newFormData, requestParams: params } =
|
||||||
formData: newFormData,
|
generateTableUploadForm(formData, data)
|
||||||
requestParams: params
|
|
||||||
} = generateTableUploadForm(formData, data)
|
|
||||||
formData = newFormData
|
formData = newFormData
|
||||||
requestParams = { ...requestParams, ...params }
|
requestParams = { ...requestParams, ...params }
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -214,9 +214,8 @@ export class RequestClient implements HttpClient {
|
|||||||
const headers = this.getHeaders(accessToken, 'application/json')
|
const headers = this.getHeaders(accessToken, 'application/json')
|
||||||
|
|
||||||
if (this.fileUploadCsrfToken?.value) {
|
if (this.fileUploadCsrfToken?.value) {
|
||||||
headers[
|
headers[this.fileUploadCsrfToken.headerName] =
|
||||||
this.fileUploadCsrfToken.headerName
|
this.fileUploadCsrfToken.value
|
||||||
] = this.fileUploadCsrfToken.value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -333,9 +332,9 @@ export class RequestClient implements HttpClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private parseCsrfToken = (response: AxiosResponse): CsrfToken | undefined => {
|
private parseCsrfToken = (response: AxiosResponse): CsrfToken | undefined => {
|
||||||
const tokenHeader = (response.headers[
|
const tokenHeader = (
|
||||||
'x-csrf-header'
|
response.headers['x-csrf-header'] as string
|
||||||
] as string)?.toLowerCase()
|
)?.toLowerCase()
|
||||||
|
|
||||||
if (tokenHeader) {
|
if (tokenHeader) {
|
||||||
const token = response.headers[tokenHeader]
|
const token = response.headers[tokenHeader]
|
||||||
|
|||||||
Reference in New Issue
Block a user