@data-fair/lib-node 2.7.1 → 2.8.0

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.
package/axios-auth.d.ts CHANGED
@@ -9,6 +9,7 @@ export interface AxiosAuthOptions {
9
9
  directoryUrl?: string;
10
10
  axiosOpts?: any;
11
11
  orgStorage?: boolean;
12
+ globalCookies?: boolean;
12
13
  }
13
14
  export type AxiosAuthInstance = AxiosInstance & {
14
15
  cookieJar: CookieJar;
package/axios-auth.js CHANGED
@@ -1,8 +1,12 @@
1
1
  // build axios instances with sessions on a simple-directory instance
2
- // used for integration testing
2
+ // WARNING: use for integration testing only
3
3
  import { Agent } from 'node:http'
4
4
  import { axiosBuilder, axiosInstance } from './axios.js'
5
5
  import { CookieJar } from 'tough-cookie'
6
+ const getConfigUrl = (config) => {
7
+ if (!config.url) { throw new Error('url is required') }
8
+ return new URL((config.baseURL && config.url?.startsWith('/')) ? (config.baseURL + config.url) : config.url)
9
+ }
6
10
  export async function axiosAuth (opts) {
7
11
  const body = { email: opts.email, password: opts.password }
8
12
  if (opts.org) { body.org = opts.org }
@@ -20,7 +24,7 @@ export async function axiosAuth (opts) {
20
24
  callbackUrl = callbackUrl.replace(directoryUrl + '/simple-directory', directoryUrl)
21
25
  }
22
26
  const cookieJar = new CookieJar()
23
- const origin = new URL(directoryUrl).origin
27
+ const sdOrigin = new URL(directoryUrl).origin
24
28
  try {
25
29
  await axiosInstance.get(callbackUrl, { maxRedirects: 0 })
26
30
  } catch (err) {
@@ -28,20 +32,28 @@ export async function axiosAuth (opts) {
28
32
  const redirectUrl = new URL(err.headers.location)
29
33
  const redirectError = redirectUrl.searchParams.get('error')
30
34
  if (redirectError) { throw new Error(redirectError) }
31
- for (const cookie of err.headers['set-cookie'] ?? []) { cookieJar.setCookieSync(cookie, origin) }
35
+ for (const cookie of err.headers['set-cookie'] ?? []) { cookieJar.setCookieSync(cookie, sdOrigin) }
32
36
  }
33
37
  const ax = axiosBuilder(axiosOpts, (ax) => {
34
38
  ax.interceptors.request.use((config) => {
35
- const url = (config.baseURL && config.url?.startsWith('/')) ? (config.baseURL + config.url) : config.url
36
- config.headers.Cookie = cookieJar.getCookiesSync(url ?? origin)
39
+ const url = getConfigUrl(config)
40
+ // if opts.globalCookies is true the cookie jar is used to store cookies shared accross all domains
41
+ // this behavior is useful for testing purposes if serves are not on the same domain
42
+ if (!opts.globalCookies || url.origin === sdOrigin) {
43
+ config.headers.Cookie = cookieJar.getCookiesSync(url.href)
44
+ } else {
45
+ config.headers.Cookie = cookieJar.getCookiesSync(sdOrigin)
46
+ }
37
47
  return config
38
48
  })
39
49
  ax.interceptors.response.use((res) => {
50
+ const origin = opts.globalCookies ? sdOrigin : getConfigUrl(res.config).origin
40
51
  for (const cookie of res.headers['set-cookie'] ?? []) { cookieJar.setCookieSync(cookie, origin) }
41
52
  return res
42
53
  }, async (error) => {
43
54
  const res = error.response
44
55
  if (res) {
56
+ const origin = opts.globalCookies ? sdOrigin : getConfigUrl(res.config).origin
45
57
  for (const cookie of res.headers['set-cookie'] ?? []) { cookieJar.setCookieSync(cookie, origin) }
46
58
  }
47
59
  return Promise.reject(error)
@@ -49,8 +61,8 @@ export async function axiosAuth (opts) {
49
61
  })
50
62
  ax.cookieJar = cookieJar
51
63
  ax.setOrg = (org, dep) => {
52
- cookieJar.setCookieSync(`id_token_org=${org}`, origin)
53
- cookieJar.setCookieSync(`id_token_dep=${dep ?? ''}`, origin)
64
+ cookieJar.setCookieSync(`id_token_org=${org}`, sdOrigin)
65
+ cookieJar.setCookieSync(`id_token_dep=${dep ?? ''}`, sdOrigin)
54
66
  }
55
67
  return ax
56
68
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@data-fair/lib-node",
3
- "version": "2.7.1",
3
+ "version": "2.8.0",
4
4
  "description": "Small nodejs related utility functions for the data-fair stack.",
5
5
  "main": "index.js",
6
6
  "files": [