@typekcz-nocobase-plugins/plugin-oidc-plus 1.0.2 → 1.0.3

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,91 @@
1
+ /**
2
+ * Generate secure URL-friendly unique ID.
3
+ *
4
+ * By default, the ID will have 21 symbols to have a collision probability
5
+ * similar to UUID v4.
6
+ *
7
+ * ```js
8
+ * import { nanoid } from 'nanoid'
9
+ * model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
10
+ * ```
11
+ *
12
+ * @param size Size of the ID. The default size is 21.
13
+ * @returns A random string.
14
+ */
15
+ export function nanoid(size?: number): string
16
+
17
+ /**
18
+ * Generate secure unique ID with custom alphabet.
19
+ *
20
+ * Alphabet must contain 256 symbols or less. Otherwise, the generator
21
+ * will not be secure.
22
+ *
23
+ * @param alphabet Alphabet used to generate the ID.
24
+ * @param defaultSize Size of the ID. The default size is 21.
25
+ * @returns A random string generator.
26
+ *
27
+ * ```js
28
+ * const { customAlphabet } = require('nanoid')
29
+ * const nanoid = customAlphabet('0123456789абвгдеё', 5)
30
+ * nanoid() //=> "8ё56а"
31
+ * ```
32
+ */
33
+ export function customAlphabet(
34
+ alphabet: string,
35
+ defaultSize?: number
36
+ ): (size?: number) => string
37
+
38
+ /**
39
+ * Generate unique ID with custom random generator and alphabet.
40
+ *
41
+ * Alphabet must contain 256 symbols or less. Otherwise, the generator
42
+ * will not be secure.
43
+ *
44
+ * ```js
45
+ * import { customRandom } from 'nanoid/format'
46
+ *
47
+ * const nanoid = customRandom('abcdef', 5, size => {
48
+ * const random = []
49
+ * for (let i = 0; i < size; i++) {
50
+ * random.push(randomByte())
51
+ * }
52
+ * return random
53
+ * })
54
+ *
55
+ * nanoid() //=> "fbaef"
56
+ * ```
57
+ *
58
+ * @param alphabet Alphabet used to generate a random string.
59
+ * @param size Size of the random string.
60
+ * @param random A random bytes generator.
61
+ * @returns A random string generator.
62
+ */
63
+ export function customRandom(
64
+ alphabet: string,
65
+ size: number,
66
+ random: (bytes: number) => Uint8Array
67
+ ): () => string
68
+
69
+ /**
70
+ * URL safe symbols.
71
+ *
72
+ * ```js
73
+ * import { urlAlphabet } from 'nanoid'
74
+ * const nanoid = customAlphabet(urlAlphabet, 10)
75
+ * nanoid() //=> "Uakgb_J5m9"
76
+ * ```
77
+ */
78
+ export const urlAlphabet: string
79
+
80
+ /**
81
+ * Generate an array of random bytes collected from hardware noise.
82
+ *
83
+ * ```js
84
+ * import { customRandom, random } from 'nanoid'
85
+ * const nanoid = customRandom("abcdef", 5, random)
86
+ * ```
87
+ *
88
+ * @param bytes Size of the array.
89
+ * @returns An array of random bytes.
90
+ */
91
+ export function random(bytes: number): Uint8Array
@@ -1,7 +1,15 @@
1
1
  import crypto from 'crypto'
2
+
2
3
  import { urlAlphabet } from './url-alphabet/index.js'
4
+
5
+ // It is best to make fewer, larger requests to the crypto module to
6
+ // avoid system call overhead. So, random numbers are generated in a
7
+ // pool. The pool is a Buffer that is larger than the initial random
8
+ // request size by this multiplier. The pool is enlarged if subsequent
9
+ // requests exceed the maximum buffer size.
3
10
  const POOL_SIZE_MULTIPLIER = 128
4
11
  let pool, poolOffset
12
+
5
13
  let fillPool = bytes => {
6
14
  if (!pool || pool.length < bytes) {
7
15
  pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER)
@@ -13,33 +21,65 @@ let fillPool = bytes => {
13
21
  }
14
22
  poolOffset += bytes
15
23
  }
24
+
16
25
  let random = bytes => {
17
- fillPool((bytes -= 0))
26
+ // `|=` convert `bytes` to number to prevent `valueOf` abusing and pool pollution
27
+ fillPool((bytes |= 0))
18
28
  return pool.subarray(poolOffset - bytes, poolOffset)
19
29
  }
30
+
20
31
  let customRandom = (alphabet, defaultSize, getRandom) => {
32
+ // First, a bitmask is necessary to generate the ID. The bitmask makes bytes
33
+ // values closer to the alphabet size. The bitmask calculates the closest
34
+ // `2^31 - 1` number, which exceeds the alphabet size.
35
+ // For example, the bitmask for the alphabet size 30 is 31 (00011111).
21
36
  let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
37
+ // Though, the bitmask solution is not perfect since the bytes exceeding
38
+ // the alphabet size are refused. Therefore, to reliably generate the ID,
39
+ // the random bytes redundancy has to be satisfied.
40
+
41
+ // Note: every hardware random generator call is performance expensive,
42
+ // because the system call for entropy collection takes a lot of time.
43
+ // So, to avoid additional system calls, extra bytes are requested in advance.
44
+
45
+ // Next, a step determines how many random bytes to generate.
46
+ // The number of random bytes gets decided upon the ID size, mask,
47
+ // alphabet size, and magic number 1.6 (using 1.6 peaks at performance
48
+ // according to benchmarks).
22
49
  let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
50
+
23
51
  return (size = defaultSize) => {
24
52
  let id = ''
25
53
  while (true) {
26
54
  let bytes = getRandom(step)
55
+ // A compact alternative for `for (let i = 0; i < step; i++)`.
27
56
  let i = step
28
57
  while (i--) {
58
+ // Adding `|| ''` refuses a random byte that exceeds the alphabet size.
29
59
  id += alphabet[bytes[i] & mask] || ''
30
60
  if (id.length === size) return id
31
61
  }
32
62
  }
33
63
  }
34
64
  }
65
+
35
66
  let customAlphabet = (alphabet, size = 21) =>
36
67
  customRandom(alphabet, size, random)
68
+
37
69
  let nanoid = (size = 21) => {
38
- fillPool((size -= 0))
70
+ // `|=` convert `size` to number to prevent `valueOf` abusing and pool pollution
71
+ fillPool((size |= 0))
39
72
  let id = ''
73
+ // We are reading directly from the random pool to avoid creating new array
40
74
  for (let i = poolOffset - size; i < poolOffset; i++) {
75
+ // It is incorrect to use bytes exceeding the alphabet size.
76
+ // The following mask reduces the random byte in the 0-255 value
77
+ // range to the 0-63 value range. Therefore, adding hacks, such
78
+ // as empty string fallback or magic numbers, is unneccessary because
79
+ // the bitmask trims bytes down to the alphabet size.
41
80
  id += urlAlphabet[pool[i] & 63]
42
81
  }
43
82
  return id
44
83
  }
84
+
45
85
  export { nanoid, customAlphabet, customRandom, urlAlphabet, random }
@@ -1,21 +1,34 @@
1
+ // This alphabet uses `A-Za-z0-9_-` symbols.
2
+ // The order of characters is optimized for better gzip and brotli compression.
3
+ // References to the same file (works both for gzip and brotli):
4
+ // `'use`, `andom`, and `rict'`
5
+ // References to the brotli default dictionary:
6
+ // `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
1
7
  let urlAlphabet =
2
8
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
9
+
3
10
  let customAlphabet = (alphabet, defaultSize = 21) => {
4
11
  return (size = defaultSize) => {
5
12
  let id = ''
6
- let i = size
13
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
14
+ let i = size | 0
7
15
  while (i--) {
16
+ // `| 0` is more compact and faster than `Math.floor()`.
8
17
  id += alphabet[(Math.random() * alphabet.length) | 0]
9
18
  }
10
19
  return id
11
20
  }
12
21
  }
22
+
13
23
  let nanoid = (size = 21) => {
14
24
  let id = ''
15
- let i = size
25
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
26
+ let i = size | 0
16
27
  while (i--) {
28
+ // `| 0` is more compact and faster than `Math.floor()`.
17
29
  id += urlAlphabet[(Math.random() * 64) | 0]
18
30
  }
19
31
  return id
20
32
  }
33
+
21
34
  module.exports = { nanoid, customAlphabet }
@@ -1,21 +1,34 @@
1
+ // This alphabet uses `A-Za-z0-9_-` symbols.
2
+ // The order of characters is optimized for better gzip and brotli compression.
3
+ // References to the same file (works both for gzip and brotli):
4
+ // `'use`, `andom`, and `rict'`
5
+ // References to the brotli default dictionary:
6
+ // `-26T`, `1983`, `40px`, `75px`, `bush`, `jack`, `mind`, `very`, and `wolf`
1
7
  let urlAlphabet =
2
8
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
9
+
3
10
  let customAlphabet = (alphabet, defaultSize = 21) => {
4
11
  return (size = defaultSize) => {
5
12
  let id = ''
6
- let i = size
13
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
14
+ let i = size | 0
7
15
  while (i--) {
16
+ // `| 0` is more compact and faster than `Math.floor()`.
8
17
  id += alphabet[(Math.random() * alphabet.length) | 0]
9
18
  }
10
19
  return id
11
20
  }
12
21
  }
22
+
13
23
  let nanoid = (size = 21) => {
14
24
  let id = ''
15
- let i = size
25
+ // A compact alternative for `for (var i = 0; i < step; i++)`.
26
+ let i = size | 0
16
27
  while (i--) {
28
+ // `| 0` is more compact and faster than `Math.floor()`.
17
29
  id += urlAlphabet[(Math.random() * 64) | 0]
18
30
  }
19
31
  return id
20
32
  }
33
+
21
34
  export { nanoid, customAlphabet }
@@ -1 +1 @@
1
- {"name":"nanoid","version":"3.3.4","description":"A tiny (116 bytes), secure URL-friendly unique string ID generator","keywords":["uuid","random","id","url"],"engines":{"node":"^10 || ^12 || ^13.7 || ^14 || >=15.0.1"},"author":"Andrey Sitnik <andrey@sitnik.ru>","license":"MIT","repository":"ai/nanoid","browser":{"./index.js":"./index.browser.js","./async/index.js":"./async/index.browser.js","./async/index.cjs":"./async/index.browser.cjs","./index.cjs":"./index.browser.cjs"},"react-native":"index.js","bin":"./bin/nanoid.cjs","sideEffects":false,"types":"./index.d.ts","type":"module","main":"index.cjs","module":"index.js","exports":{".":{"types":"./index.d.ts","browser":"./index.browser.js","require":"./index.cjs","import":"./index.js","default":"./index.js"},"./index.d.ts":"./index.d.ts","./package.json":"./package.json","./async/package.json":"./async/package.json","./async":{"browser":"./async/index.browser.js","require":"./async/index.cjs","import":"./async/index.js","default":"./async/index.js"},"./non-secure/package.json":"./non-secure/package.json","./non-secure":{"require":"./non-secure/index.cjs","import":"./non-secure/index.js","default":"./non-secure/index.js"},"./url-alphabet/package.json":"./url-alphabet/package.json","./url-alphabet":{"require":"./url-alphabet/index.cjs","import":"./url-alphabet/index.js","default":"./url-alphabet/index.js"}},"_lastModified":"2024-03-26T12:28:54.494Z"}
1
+ {"name":"nanoid","version":"3.3.8","description":"A tiny (116 bytes), secure URL-friendly unique string ID generator","keywords":["uuid","random","id","url"],"engines":{"node":"^10 || ^12 || ^13.7 || ^14 || >=15.0.1"},"funding":[{"type":"github","url":"https://github.com/sponsors/ai"}],"author":"Andrey Sitnik <andrey@sitnik.ru>","license":"MIT","repository":"ai/nanoid","browser":{"./index.js":"./index.browser.js","./async/index.js":"./async/index.browser.js","./async/index.cjs":"./async/index.browser.cjs","./index.cjs":"./index.browser.cjs"},"react-native":"index.js","bin":"./bin/nanoid.cjs","sideEffects":false,"types":"./index.d.ts","type":"module","main":"index.cjs","module":"index.js","exports":{".":{"browser":"./index.browser.js","require":{"types":"./index.d.cts","default":"./index.cjs"},"import":{"types":"./index.d.ts","default":"./index.js"},"default":"./index.js"},"./package.json":"./package.json","./async/package.json":"./async/package.json","./async":{"browser":"./async/index.browser.js","require":{"types":"./index.d.cts","default":"./async/index.cjs"},"import":{"types":"./index.d.ts","default":"./async/index.js"},"default":"./async/index.js"},"./non-secure/package.json":"./non-secure/package.json","./non-secure":{"require":{"types":"./index.d.cts","default":"./non-secure/index.cjs"},"import":{"types":"./index.d.ts","default":"./non-secure/index.js"},"default":"./non-secure/index.js"},"./url-alphabet/package.json":"./url-alphabet/package.json","./url-alphabet":{"require":{"types":"./index.d.cts","default":"./url-alphabet/index.cjs"},"import":{"types":"./index.d.ts","default":"./url-alphabet/index.js"},"default":"./url-alphabet/index.js"}},"_lastModified":"2025-03-05T12:36:57.578Z"}
@@ -1,3 +1,7 @@
1
+ // This alphabet uses `A-Za-z0-9_-` symbols.
2
+ // The order of characters is optimized for better gzip and brotli compression.
3
+ // Same as in non-secure/index.js
1
4
  let urlAlphabet =
2
5
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
6
+
3
7
  module.exports = { urlAlphabet }
@@ -1,3 +1,7 @@
1
+ // This alphabet uses `A-Za-z0-9_-` symbols.
2
+ // The order of characters is optimized for better gzip and brotli compression.
3
+ // Same as in non-secure/index.js
1
4
  let urlAlphabet =
2
5
  'useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict'
6
+
3
7
  export { urlAlphabet }
@@ -191,7 +191,7 @@ class BaseClient {
191
191
  authorization_signed_response_alg: 'RS256',
192
192
  response_types: ['code'],
193
193
  token_endpoint_auth_method: 'client_secret_basic',
194
- ...(this.fapi()
194
+ ...(this.fapi1()
195
195
  ? {
196
196
  grant_types: ['authorization_code', 'implicit'],
197
197
  id_token_signed_response_alg: 'PS256',
@@ -201,6 +201,13 @@ class BaseClient {
201
201
  token_endpoint_auth_method: undefined,
202
202
  }
203
203
  : undefined),
204
+ ...(this.fapi2()
205
+ ? {
206
+ id_token_signed_response_alg: 'PS256',
207
+ authorization_signed_response_alg: 'PS256',
208
+ token_endpoint_auth_method: undefined,
209
+ }
210
+ : undefined),
204
211
  ...metadata,
205
212
  };
206
213
 
@@ -221,6 +228,26 @@ class BaseClient {
221
228
  }
222
229
  }
223
230
 
231
+ if (this.fapi2()) {
232
+ if (
233
+ properties.tls_client_certificate_bound_access_tokens &&
234
+ properties.dpop_bound_access_tokens
235
+ ) {
236
+ throw new TypeError(
237
+ 'either tls_client_certificate_bound_access_tokens or dpop_bound_access_tokens must be set to true',
238
+ );
239
+ }
240
+
241
+ if (
242
+ !properties.tls_client_certificate_bound_access_tokens &&
243
+ !properties.dpop_bound_access_tokens
244
+ ) {
245
+ throw new TypeError(
246
+ 'either tls_client_certificate_bound_access_tokens or dpop_bound_access_tokens must be set to true',
247
+ );
248
+ }
249
+ }
250
+
224
251
  handleCommonMistakes(this, metadata, properties);
225
252
 
226
253
  assertSigningAlgValuesSupport('token', this.issuer, properties);
@@ -824,7 +851,7 @@ class BaseClient {
824
851
  });
825
852
  }
826
853
 
827
- if (this.fapi()) {
854
+ if (this.fapi1()) {
828
855
  if (!payload.s_hash && (tokenSet.state || state)) {
829
856
  throw new RPError({
830
857
  message: 'missing required property s_hash',
@@ -1631,9 +1658,17 @@ class BaseClient {
1631
1658
  }
1632
1659
 
1633
1660
  fapi() {
1661
+ return this.fapi1() || this.fapi2();
1662
+ }
1663
+
1664
+ fapi1() {
1634
1665
  return this.constructor.name === 'FAPI1Client';
1635
1666
  }
1636
1667
 
1668
+ fapi2() {
1669
+ return this.constructor.name === 'FAPI2Client';
1670
+ }
1671
+
1637
1672
  async validateJARM(response) {
1638
1673
  const expectedAlg = this.authorization_signed_response_alg;
1639
1674
  const { payload } = await this.validateJWT(response, expectedAlg, ['iss', 'exp', 'aud']);
@@ -92,9 +92,6 @@ async function authFor(endpoint, { clientAssertionPayload } = {}) {
92
92
  case 'private_key_jwt':
93
93
  case 'client_secret_jwt': {
94
94
  const timestamp = now();
95
- const audience = [
96
- ...new Set([this.issuer.issuer, this.issuer.token_endpoint].filter(Boolean)),
97
- ];
98
95
 
99
96
  const assertion = await clientAssertion.call(this, endpoint, {
100
97
  iat: timestamp,
@@ -102,7 +99,7 @@ async function authFor(endpoint, { clientAssertionPayload } = {}) {
102
99
  jti: random(),
103
100
  iss: this.client_id,
104
101
  sub: this.client_id,
105
- aud: audience,
102
+ aud: this.issuer.issuer,
106
103
  ...clientAssertionPayload,
107
104
  });
108
105
 
@@ -79,7 +79,7 @@ module.exports = async function request(options, { accessToken, mTLS = false, DP
79
79
  opts.headers.DPoP = await this.dpopProof(
80
80
  {
81
81
  htu: `${url.origin}${url.pathname}`,
82
- htm: options.method,
82
+ htm: options.method || 'GET',
83
83
  nonce: nonces.get(nonceKey),
84
84
  },
85
85
  DPoP,