diff --git a/src/SASViyaApiClient.ts b/src/SASViyaApiClient.ts index d45db0c..de4acab 100644 --- a/src/SASViyaApiClient.ts +++ b/src/SASViyaApiClient.ts @@ -20,6 +20,7 @@ export class SASViyaApiClient { private serverUrl: string, private rootFolderName: string, private contextName: string, + private setCsrfToken: (csrfToken: CsrfToken) => void, private rootFolderMap = new Map() ) { if (!rootFolderName) { @@ -28,7 +29,7 @@ export class SASViyaApiClient { } private csrfToken: CsrfToken | null = null; private rootFolder: Folder | null = null; - private sessionManager = new SessionManager(this.serverUrl, this.contextName); + private sessionManager = new SessionManager(this.serverUrl, this.contextName, this.setCsrfToken); /** * Returns a map containing the directory structure in the currently set root folder. @@ -1025,8 +1026,9 @@ export class SASViyaApiClient { return `/folders/folders/${folder.id}`; } - setCsrfToken = (csrfToken: CsrfToken) => { + setCsrfTokenLocal = (csrfToken: CsrfToken) => { this.csrfToken = csrfToken; + this.setCsrfToken(csrfToken); }; private async request( @@ -1040,6 +1042,6 @@ export class SASViyaApiClient { [this.csrfToken.headerName]: this.csrfToken.value, }; } - return await makeRequest(url, options, this.setCsrfToken, contentType); + return await makeRequest(url, options, this.setCsrfTokenLocal, contentType); } } diff --git a/src/SASjs.ts b/src/SASjs.ts index dc808c0..a48f55b 100644 --- a/src/SASjs.ts +++ b/src/SASjs.ts @@ -20,6 +20,7 @@ import { SASjsRequest, SASjsWaitingRequest, ServerType, + CsrfToken, } from "./types"; import { SASViyaApiClient } from "./SASViyaApiClient"; import { SAS9ApiClient } from "./SAS9ApiClient"; @@ -46,8 +47,7 @@ export default class SASjs { private jobsPath: string = ""; private logoutUrl: string = ""; private loginUrl: string = ""; - private _csrf: string | null = null; - private _csrfHeader: string | null = null; + private csrfToken: CsrfToken | null = null; private retryCount: number = 0; private sasjsRequests: SASjsRequest[] = []; private sasjsWaitingRequests: SASjsWaitingRequest[] = []; @@ -239,7 +239,7 @@ export default class SASjs { * */ public getCsrf() { - return this._csrf; + return this.csrfToken?.value; } /** @@ -452,7 +452,8 @@ export default class SASjs { sasApiClient = new SASViyaApiClient( serverUrl, appLoc, - this.sasjsConfig.contextName + this.sasjsConfig.contextName, + this.setCsrfToken ); } else if (this.sasjsConfig.serverType === ServerType.SAS9) { sasApiClient = new SAS9ApiClient(serverUrl); @@ -517,7 +518,7 @@ export default class SASjs { return JSON.parse(response!.result); }) .catch((e) => { - if (e && e.includes(401)) { + if (e && e.status === 401) { if (loginRequiredCallback) loginRequiredCallback(true); sasjsWaitingRequest.requestPromise.resolve = resolve; sasjsWaitingRequest.requestPromise.reject = reject; @@ -698,8 +699,8 @@ export default class SASjs { reject({ MESSAGE: errorMsg }); } const headers: any = {}; - if (this._csrfHeader && this._csrf) { - headers[this._csrfHeader] = this._csrf; + if (this.csrfToken) { + headers[this.csrfToken.headerName] = this.csrfToken.value; } fetch(apiUrl, { method: "POST", @@ -714,8 +715,10 @@ export default class SASjs { if (tokenHeader) { const token = response.headers.get(tokenHeader); - this._csrfHeader = tokenHeader; - this._csrf = token; + this.csrfToken = { + headerName: tokenHeader, + value: token || '' + } } } } @@ -810,6 +813,10 @@ export default class SASjs { return sasjsWaitingRequest.requestPromise.promise; } + private setCsrfToken = (csrfToken: CsrfToken) => { + this.csrfToken = csrfToken; + }; + private async resendWaitingRequests() { for (const sasjsWaitingRequest of this.sasjsWaitingRequests) { this.request( @@ -832,8 +839,8 @@ export default class SASjs { private getRequestParams(): any { const requestParams: any = {}; - if (this._csrf) { - requestParams["_csrf"] = this._csrf; + if (this.csrfToken) { + requestParams["_csrf"] = this.csrfToken.value; } if (this.sasjsConfig.debug) { @@ -1070,7 +1077,8 @@ export default class SASjs { this.sasViyaApiClient = new SASViyaApiClient( this.sasjsConfig.serverUrl, this.sasjsConfig.appLoc, - this.sasjsConfig.contextName + this.sasjsConfig.contextName, + this.setCsrfToken ); } if (this.sasjsConfig.serverType === ServerType.SAS9) { diff --git a/src/SessionManager.ts b/src/SessionManager.ts index a11d609..1a20b96 100644 --- a/src/SessionManager.ts +++ b/src/SessionManager.ts @@ -4,7 +4,11 @@ import { asyncForEach, makeRequest } from "./utils"; const MAX_SESSION_COUNT = 1; export class SessionManager { - constructor(private serverUrl: string, private contextName: string) {} + constructor( + private serverUrl: string, + private contextName: string, + private setCsrfToken: (csrfToken: CsrfToken) => void + ) {} private sessions: Session[] = []; private currentContext: Context | null = null; private csrfToken: CsrfToken | null = null; @@ -132,7 +136,10 @@ export class SessionManager { return await makeRequest( url, options, - (token) => (this.csrfToken = token), + (token) => { + this.csrfToken = token; + this.setCsrfToken(token); + }, contentType ); }