@sasjs/adapter 3.10.10 → 3.10.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ export declare class InvalidSASjsCsrfError extends Error {
2
+ constructor();
3
+ }
package/package.json CHANGED
@@ -87,5 +87,5 @@
87
87
  "https": "1.0.0",
88
88
  "tough-cookie": "4.0.0"
89
89
  },
90
- "version": "3.10.10"
90
+ "version": "3.10.11"
91
91
  }
@@ -223,9 +223,17 @@ export class AuthManager {
223
223
 
224
224
  private async getNewLoginForm() {
225
225
  if (this.serverType === ServerType.Sasjs) {
226
- // server will be sending CSRF cookie,
226
+ // server will be sending CSRF token in response,
227
+ // need to save in cookie so that,
227
228
  // http client will use it automatically
228
- return this.requestClient.get('/', undefined)
229
+ return this.requestClient.get('/', undefined).then(({ result }) => {
230
+ const cookie =
231
+ /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(
232
+ result as string
233
+ )?.[1]
234
+
235
+ if (cookie) document.cookie = cookie
236
+ })
229
237
  }
230
238
 
231
239
  const { result: formResponse } = await this.requestClient.get<string>(
@@ -19,7 +19,7 @@ import {
19
19
  parseSourceCode,
20
20
  createAxiosInstance
21
21
  } from '../utils'
22
- import { InvalidCsrfError } from '../types/errors/InvalidCsrfError'
22
+ import { InvalidSASjsCsrfError } from '../types/errors/InvalidSASjsCsrfError'
23
23
 
24
24
  export interface HttpClient {
25
25
  get<T>(
@@ -499,12 +499,20 @@ export class RequestClient implements HttpClient {
499
499
  throw e
500
500
  }
501
501
 
502
- if (e instanceof InvalidCsrfError) {
503
- // Fetching root will inject CSRF token in cookie
502
+ if (e instanceof InvalidSASjsCsrfError) {
503
+ // Fetching root and creating CSRF cookie
504
504
  await this.httpClient
505
505
  .get('/', {
506
506
  withCredentials: true
507
507
  })
508
+ .then((response) => {
509
+ const cookie =
510
+ /<script>document.cookie = '(XSRF-TOKEN=.*; Max-Age=86400; SameSite=Strict; Path=\/;)'<\/script>/.exec(
511
+ response.data
512
+ )?.[1]
513
+
514
+ if (cookie) document.cookie = cookie
515
+ })
508
516
  .catch((err) => {
509
517
  throw prefixMessage(err, 'Error while re-fetching CSRF token.')
510
518
  })
@@ -615,7 +623,7 @@ export const throwIfError = (response: AxiosResponse) => {
615
623
  typeof response.data === 'string' &&
616
624
  response.data.toLowerCase() === 'invalid csrf token!'
617
625
  ) {
618
- throw new InvalidCsrfError()
626
+ throw new InvalidSASjsCsrfError()
619
627
  }
620
628
  break
621
629
  case 401:
@@ -0,0 +1,9 @@
1
+ export class InvalidSASjsCsrfError extends Error {
2
+ constructor() {
3
+ const message = 'Invalid CSRF token!'
4
+
5
+ super(`Auth error: ${message}`)
6
+ this.name = 'InvalidSASjsCsrfError'
7
+ Object.setPrototypeOf(this, InvalidSASjsCsrfError.prototype)
8
+ }
9
+ }
@@ -0,0 +1,3 @@
1
+ export declare class InvalidSASjsCsrfError extends Error {
2
+ constructor();
3
+ }
@@ -1,3 +0,0 @@
1
- export declare class InvalidCsrfError extends Error {
2
- constructor();
3
- }
@@ -1,9 +0,0 @@
1
- export class InvalidCsrfError extends Error {
2
- constructor() {
3
- const message = 'Invalid CSRF token!'
4
-
5
- super(`Auth error: ${message}`)
6
- this.name = 'InvalidCsrfError'
7
- Object.setPrototypeOf(this, InvalidCsrfError.prototype)
8
- }
9
- }
@@ -1,3 +0,0 @@
1
- export declare class InvalidCsrfError extends Error {
2
- constructor();
3
- }