1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-14 23:50:06 +00:00

Merge pull request #712 from sasjs/issue-711

When expires csrf token, re-fetch and empty webout fix
This commit is contained in:
Allan Bowe
2022-05-23 14:03:42 +03:00
committed by GitHub
3 changed files with 38 additions and 2 deletions

View File

@@ -172,7 +172,7 @@ export class WebJobExecutor extends BaseJobExecutor {
const resObj = res const resObj = res
if (this.serverType === ServerType.Sasjs) { if (this.serverType === ServerType.Sasjs) {
if (res.result._webout < 1) if (res.result._webout.length < 1)
throw new JobExecutionError( throw new JobExecutionError(
0, 0,
'Job execution failed', 'Job execution failed',

View File

@@ -19,6 +19,7 @@ import {
parseSourceCode, parseSourceCode,
createAxiosInstance createAxiosInstance
} from '../utils' } from '../utils'
import { InvalidCsrfError } from '../types/errors/InvalidCsrfError'
export interface HttpClient { export interface HttpClient {
get<T>( get<T>(
@@ -498,6 +499,24 @@ export class RequestClient implements HttpClient {
throw e throw e
} }
if (e instanceof InvalidCsrfError) {
// Fetching root will inject CSRF token in cookie
await this.httpClient
.get('/', {
withCredentials: true
})
.catch((err) => {
throw prefixMessage(err, 'Error while re-fetching CSRF token.')
})
return await callback().catch((err: any) => {
throw prefixMessage(
err,
'Error while executing callback in handleError. '
)
})
}
if (response?.status === 403 || response?.status === 449) { if (response?.status === 403 || response?.status === 449) {
this.parseAndSetCsrfToken(response) this.parseAndSetCsrfToken(response)
@@ -584,9 +603,17 @@ export class RequestClient implements HttpClient {
export const throwIfError = (response: AxiosResponse) => { export const throwIfError = (response: AxiosResponse) => {
switch (response.status) { switch (response.status) {
case 400: case 400:
if (typeof response.data === 'object') { if (
typeof response.data === 'object' &&
response.data.error === 'invalid_grant'
) {
// In SASVIYA when trying to get access token, if auth code is wrong status code will be 400 so in such case we return login required error.
throw new LoginRequiredError(response.data) throw new LoginRequiredError(response.data)
} }
if (response.data.toLowerCase() === 'invalid csrf token!') {
throw new InvalidCsrfError()
}
break break
case 401: case 401:
if (typeof response.data === 'object') { if (typeof response.data === 'object') {

View File

@@ -0,0 +1,9 @@
export class InvalidCsrfError extends Error {
constructor() {
const message = 'Invalid CSRF token!'
super(`Auth error: ${message}`)
this.name = 'InvalidCsrfError'
Object.setPrototypeOf(this, InvalidCsrfError.prototype)
}
}