@byline/admin 2.4.0 → 2.4.1

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.
Files changed (177) hide show
  1. package/dist/abilities.js +5 -24
  2. package/dist/index.js +8 -30
  3. package/dist/lib/assert-admin-actor.js +13 -74
  4. package/dist/lib/create-command.js +6 -16
  5. package/dist/modules/admin-account/commands.js +35 -24
  6. package/dist/modules/admin-account/components/change-password.d.ts +8 -0
  7. package/dist/modules/admin-account/components/change-password.js +192 -0
  8. package/dist/modules/admin-account/components/change-password.module.js +8 -0
  9. package/dist/modules/admin-account/components/change-password_module.css +27 -0
  10. package/dist/modules/admin-account/components/container.d.ts +29 -0
  11. package/dist/modules/admin-account/components/container.js +298 -0
  12. package/dist/modules/admin-account/components/container.module.js +28 -0
  13. package/dist/modules/admin-account/components/container_module.css +106 -0
  14. package/dist/modules/admin-account/components/update.d.ts +8 -0
  15. package/dist/modules/admin-account/components/update.js +207 -0
  16. package/dist/modules/admin-account/components/update.module.js +8 -0
  17. package/dist/modules/admin-account/components/update_module.css +27 -0
  18. package/dist/modules/admin-account/errors.js +14 -45
  19. package/dist/modules/admin-account/index.js +4 -34
  20. package/dist/modules/admin-account/schemas.js +25 -59
  21. package/dist/modules/admin-account/service.js +56 -61
  22. package/dist/modules/admin-permissions/abilities.js +6 -24
  23. package/dist/modules/admin-permissions/commands.js +42 -28
  24. package/dist/modules/admin-permissions/components/inspector.d.ts +4 -0
  25. package/dist/modules/admin-permissions/components/inspector.js +284 -0
  26. package/dist/modules/admin-permissions/components/inspector.module.js +56 -0
  27. package/dist/modules/admin-permissions/components/inspector_module.css +238 -0
  28. package/dist/modules/admin-permissions/dto.js +3 -16
  29. package/dist/modules/admin-permissions/errors.js +14 -27
  30. package/dist/modules/admin-permissions/index.js +6 -26
  31. package/dist/modules/admin-permissions/repository.js +1 -8
  32. package/dist/modules/admin-permissions/schemas.js +33 -70
  33. package/dist/modules/admin-permissions/service.js +88 -92
  34. package/dist/modules/admin-roles/abilities.js +8 -30
  35. package/dist/modules/admin-roles/commands.js +89 -55
  36. package/dist/modules/admin-roles/components/create.d.ts +7 -0
  37. package/dist/modules/admin-roles/components/create.js +177 -0
  38. package/dist/modules/admin-roles/components/create.module.js +8 -0
  39. package/dist/modules/admin-roles/components/create_module.css +27 -0
  40. package/dist/modules/admin-roles/components/permissions.d.ts +10 -0
  41. package/dist/modules/admin-roles/components/permissions.js +303 -0
  42. package/dist/modules/admin-roles/components/permissions.module.js +44 -0
  43. package/dist/modules/admin-roles/components/permissions_module.css +192 -0
  44. package/dist/modules/admin-roles/components/update.d.ts +8 -0
  45. package/dist/modules/admin-roles/components/update.js +166 -0
  46. package/dist/modules/admin-roles/components/update.module.js +8 -0
  47. package/dist/modules/admin-roles/components/update_module.css +27 -0
  48. package/dist/modules/admin-roles/dto.js +3 -16
  49. package/dist/modules/admin-roles/errors.js +16 -40
  50. package/dist/modules/admin-roles/index.js +6 -26
  51. package/dist/modules/admin-roles/repository.js +1 -8
  52. package/dist/modules/admin-roles/schemas.js +41 -71
  53. package/dist/modules/admin-roles/service.js +79 -82
  54. package/dist/modules/admin-users/abilities.js +9 -38
  55. package/dist/modules/admin-users/commands.js +92 -50
  56. package/dist/modules/admin-users/components/create.d.ts +8 -0
  57. package/dist/modules/admin-users/components/create.js +268 -0
  58. package/dist/modules/admin-users/components/create.module.js +10 -0
  59. package/dist/modules/admin-users/components/create_module.css +45 -0
  60. package/dist/modules/admin-users/components/roles.d.ts +11 -0
  61. package/dist/modules/admin-users/components/roles.js +148 -0
  62. package/dist/modules/admin-users/components/roles.module.js +18 -0
  63. package/dist/modules/admin-users/components/roles_module.css +75 -0
  64. package/dist/modules/admin-users/components/set-password.d.ts +8 -0
  65. package/dist/modules/admin-users/components/set-password.js +170 -0
  66. package/dist/modules/admin-users/components/set-password.module.js +9 -0
  67. package/dist/modules/admin-users/components/set-password_module.css +31 -0
  68. package/dist/modules/admin-users/components/update.d.ts +8 -0
  69. package/dist/modules/admin-users/components/update.js +254 -0
  70. package/dist/modules/admin-users/components/update.module.js +9 -0
  71. package/dist/modules/admin-users/components/update_module.css +34 -0
  72. package/dist/modules/admin-users/dto.js +3 -18
  73. package/dist/modules/admin-users/errors.js +17 -43
  74. package/dist/modules/admin-users/index.js +7 -27
  75. package/dist/modules/admin-users/repository.js +1 -8
  76. package/dist/modules/admin-users/schemas.js +44 -75
  77. package/dist/modules/admin-users/seed-super-admin.js +9 -34
  78. package/dist/modules/admin-users/service.js +76 -91
  79. package/dist/modules/auth/components/sign-in-form.d.ts +12 -0
  80. package/dist/modules/auth/components/sign-in-form.js +115 -0
  81. package/dist/modules/auth/components/sign-in-form.module.js +12 -0
  82. package/dist/modules/auth/components/sign-in-form_module.css +41 -0
  83. package/dist/modules/auth/index.js +3 -24
  84. package/dist/modules/auth/jwt-session-provider.js +179 -149
  85. package/dist/modules/auth/password.js +11 -53
  86. package/dist/modules/auth/phc.js +21 -54
  87. package/dist/modules/auth/refresh-tokens-repository.js +1 -8
  88. package/dist/modules/auth/resolve-actor.js +6 -28
  89. package/dist/services/admin-services-context.d.ts +16 -0
  90. package/dist/services/admin-services-context.js +13 -0
  91. package/dist/services/admin-services-types.d.ts +129 -0
  92. package/dist/services/admin-services-types.js +1 -0
  93. package/dist/store.js +1 -8
  94. package/dist/vendor/noble-argon2/_blake.js +277 -45
  95. package/dist/vendor/noble-argon2/_md.js +81 -136
  96. package/dist/vendor/noble-argon2/_u64.js +65 -67
  97. package/dist/vendor/noble-argon2/argon2.js +181 -342
  98. package/dist/vendor/noble-argon2/blake2.js +252 -327
  99. package/dist/vendor/noble-argon2/utils.js +110 -490
  100. package/dist/vendor/noble-argon2/utils.js.LICENSE.txt +1 -0
  101. package/package.json +89 -10
  102. package/src/abilities.ts +32 -0
  103. package/src/declarations.d.ts +4 -0
  104. package/src/index.ts +39 -0
  105. package/src/lib/assert-admin-actor.ts +90 -0
  106. package/src/lib/create-command.ts +109 -0
  107. package/src/modules/admin-account/commands.ts +76 -0
  108. package/src/modules/admin-account/components/change-password.module.css +40 -0
  109. package/src/modules/admin-account/components/change-password.tsx +232 -0
  110. package/src/modules/admin-account/components/container.module.css +158 -0
  111. package/src/modules/admin-account/components/container.tsx +229 -0
  112. package/src/modules/admin-account/components/update.module.css +40 -0
  113. package/src/modules/admin-account/components/update.tsx +263 -0
  114. package/src/modules/admin-account/errors.ts +75 -0
  115. package/src/modules/admin-account/index.ts +60 -0
  116. package/src/modules/admin-account/schemas.ts +84 -0
  117. package/src/modules/admin-account/service.ts +92 -0
  118. package/src/modules/admin-permissions/abilities.ts +46 -0
  119. package/src/modules/admin-permissions/commands.ts +103 -0
  120. package/src/modules/admin-permissions/components/inspector.module.css +326 -0
  121. package/src/modules/admin-permissions/components/inspector.tsx +298 -0
  122. package/src/modules/admin-permissions/dto.ts +28 -0
  123. package/src/modules/admin-permissions/errors.ts +57 -0
  124. package/src/modules/admin-permissions/index.ts +72 -0
  125. package/src/modules/admin-permissions/repository.ts +49 -0
  126. package/src/modules/admin-permissions/schemas.ts +128 -0
  127. package/src/modules/admin-permissions/service.ts +137 -0
  128. package/src/modules/admin-roles/abilities.ts +62 -0
  129. package/src/modules/admin-roles/commands.ts +161 -0
  130. package/src/modules/admin-roles/components/create.module.css +40 -0
  131. package/src/modules/admin-roles/components/create.tsx +218 -0
  132. package/src/modules/admin-roles/components/permissions.module.css +279 -0
  133. package/src/modules/admin-roles/components/permissions.tsx +396 -0
  134. package/src/modules/admin-roles/components/update.module.css +40 -0
  135. package/src/modules/admin-roles/components/update.tsx +218 -0
  136. package/src/modules/admin-roles/dto.ts +30 -0
  137. package/src/modules/admin-roles/errors.ts +76 -0
  138. package/src/modules/admin-roles/index.ts +81 -0
  139. package/src/modules/admin-roles/repository.ts +96 -0
  140. package/src/modules/admin-roles/schemas.ts +139 -0
  141. package/src/modules/admin-roles/service.ts +136 -0
  142. package/src/modules/admin-users/abilities.ts +76 -0
  143. package/src/modules/admin-users/commands.ts +157 -0
  144. package/src/modules/admin-users/components/create.module.css +63 -0
  145. package/src/modules/admin-users/components/create.tsx +323 -0
  146. package/src/modules/admin-users/components/roles.module.css +119 -0
  147. package/src/modules/admin-users/components/roles.tsx +172 -0
  148. package/src/modules/admin-users/components/set-password.module.css +46 -0
  149. package/src/modules/admin-users/components/set-password.tsx +199 -0
  150. package/src/modules/admin-users/components/update.module.css +49 -0
  151. package/src/modules/admin-users/components/update.tsx +328 -0
  152. package/src/modules/admin-users/dto.ts +39 -0
  153. package/src/modules/admin-users/errors.ts +84 -0
  154. package/src/modules/admin-users/index.ts +91 -0
  155. package/src/modules/admin-users/repository.ts +161 -0
  156. package/src/modules/admin-users/schemas.ts +168 -0
  157. package/src/modules/admin-users/seed-super-admin.ts +102 -0
  158. package/src/modules/admin-users/service.ts +166 -0
  159. package/src/modules/auth/components/sign-in-form.module.css +62 -0
  160. package/src/modules/auth/components/sign-in-form.tsx +132 -0
  161. package/src/modules/auth/index.ts +31 -0
  162. package/src/modules/auth/jwt-session-provider.ts +301 -0
  163. package/src/modules/auth/password.ts +94 -0
  164. package/src/modules/auth/phc.ts +121 -0
  165. package/src/modules/auth/refresh-tokens-repository.ts +74 -0
  166. package/src/modules/auth/resolve-actor.ts +42 -0
  167. package/src/services/admin-services-context.tsx +52 -0
  168. package/src/services/admin-services-types.ts +177 -0
  169. package/src/store.ts +32 -0
  170. package/src/vendor/noble-argon2/LICENSE +21 -0
  171. package/src/vendor/noble-argon2/README.md +87 -0
  172. package/src/vendor/noble-argon2/_blake.ts +58 -0
  173. package/src/vendor/noble-argon2/_md.ts +223 -0
  174. package/src/vendor/noble-argon2/_u64.ts +118 -0
  175. package/src/vendor/noble-argon2/argon2.ts +668 -0
  176. package/src/vendor/noble-argon2/blake2.ts +583 -0
  177. package/src/vendor/noble-argon2/utils.ts +849 -0
@@ -0,0 +1,668 @@
1
+ // @ts-nocheck — vendored from noble-hashes; see ./README.md
2
+ /**
3
+ * Argon2 KDF from RFC 9106. Can be used to create a key from password and salt.
4
+ * We suggest to use Scrypt. JS Argon is 2-10x slower than native code because of 64-bitness:
5
+ * * argon uses uint64, but JS doesn't have fast uint64array
6
+ * * uint64 multiplication is 1/3 of time
7
+ * * `P` function would be very nice with u64, because most of value will be in registers,
8
+ * hovewer with u32 it will require 32 registers, which is too much.
9
+ * * JS arrays do slow bound checks, so reading from `A2_BUF` slows it down
10
+ * @module
11
+ */
12
+ import { add3H, add3L, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL } from './_u64.js';
13
+ import { blake2b } from './blake2.js';
14
+ import {
15
+ anumber,
16
+ clean,
17
+ kdfInputToBytes,
18
+ nextTick,
19
+ swap32IfBE,
20
+ swap8IfBE,
21
+ u32,
22
+ u8,
23
+ type KDFInput,
24
+ type TArg,
25
+ type TRet,
26
+ } from './utils.js';
27
+
28
+ // RFC 9106 §3.1 type `y`: 0 = Argon2d, 1 = Argon2i, 2 = Argon2id. The numeric values are the
29
+ // spec-bound part here; the object keys are internal labels.
30
+ const AT = { Argond2d: 0, Argon2i: 1, Argon2id: 2 } as const;
31
+ type Types = (typeof AT)[keyof typeof AT];
32
+
33
+ // RFC 9106 sync points constant `SL = 4`, fixed by the design rather than exposed as a tuning knob.
34
+ const ARGON2_SYNC_POINTS = 4;
35
+ // Preserve Argon2's `LE32(len(X)) || X` encoding for omitted
36
+ // optional fields by emitting empty bytes.
37
+ const abytesOrZero = (buf?: TArg<KDFInput>, errorTitle = ''): TRet<Uint8Array> => {
38
+ if (buf === undefined) return Uint8Array.of();
39
+ return kdfInputToBytes(buf, errorTitle);
40
+ };
41
+
42
+ // Unsigned `u32 * u32 = { h, l }`, returned as split 64-bit halves.
43
+ function mul(a: number, b: number) {
44
+ // Split into 16-bit limbs so each partial product stays exact under `Math.imul`.
45
+ const aL = a & 0xffff;
46
+ const aH = a >>> 16;
47
+ const bL = b & 0xffff;
48
+ const bH = b >>> 16;
49
+ const ll = Math.imul(aL, bL);
50
+ const hl = Math.imul(aH, bL);
51
+ const lh = Math.imul(aL, bH);
52
+ const hh = Math.imul(aH, bH);
53
+ const carry = (ll >>> 16) + (hl & 0xffff) + lh;
54
+ const high = (hh + (hl >>> 16) + (carry >>> 16)) | 0;
55
+ const low = (carry << 16) | (ll & 0xffff);
56
+ return { h: high, l: low };
57
+ }
58
+
59
+ function mul2(a: number, b: number) {
60
+ // Double the split 64-bit product; carry from `l` is folded back into `h` via `l >>> 31`.
61
+ const { h, l } = mul(a, b);
62
+ return { h: ((h << 1) | (l >>> 31)) & 0xffff_ffff, l: (l << 1) & 0xffff_ffff };
63
+ }
64
+
65
+ // BlaMka permutation for Argon2
66
+ // `A + B + 2 * trunc(A) * trunc(B)`, where `trunc(...)` means the low 32-bit halves.
67
+ function blamka(Ah: number, Al: number, Bh: number, Bl: number) {
68
+ const { h: Ch, l: Cl } = mul2(Al, Bl);
69
+ // A + B + (2 * A * B)
70
+ const Rll = add3L(Al, Bl, Cl);
71
+ return { h: add3H(Rll, Ah, Bh, Ch), l: Rll | 0 };
72
+ }
73
+
74
+ // Temporary block buffer.
75
+ // 1024-byte block: 256 u32 = 128 interleaved low/high halves = RFC's
76
+ // 8x8 matrix of 16-byte registers.
77
+ const A2_BUF = new Uint32Array(256);
78
+
79
+ // Quarter-round over 64-bit word indices into `A2_BUF`; each index maps to adjacent low/high u32s.
80
+ function G(a: number, b: number, c: number, d: number) {
81
+ let Al = A2_BUF[2*a], Ah = A2_BUF[2*a + 1]; // prettier-ignore
82
+ let Bl = A2_BUF[2*b], Bh = A2_BUF[2*b + 1]; // prettier-ignore
83
+ let Cl = A2_BUF[2*c], Ch = A2_BUF[2*c + 1]; // prettier-ignore
84
+ let Dl = A2_BUF[2*d], Dh = A2_BUF[2*d + 1]; // prettier-ignore
85
+
86
+ // RFC 9106 Figure 19 GB rotates by 32, 24, 16, and 63 bits after each XOR step.
87
+ ({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
88
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
89
+ ({ Dh, Dl } = { Dh: rotr32H(Dh, Dl), Dl: rotr32L(Dh, Dl) });
90
+
91
+ ({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
92
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
93
+ ({ Bh, Bl } = { Bh: rotrSH(Bh, Bl, 24), Bl: rotrSL(Bh, Bl, 24) });
94
+
95
+ ({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
96
+ ({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
97
+ ({ Dh, Dl } = { Dh: rotrSH(Dh, Dl, 16), Dl: rotrSL(Dh, Dl, 16) });
98
+
99
+ ({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
100
+ ({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
101
+ ({ Bh, Bl } = { Bh: rotrBH(Bh, Bl, 63), Bl: rotrBL(Bh, Bl, 63) });
102
+
103
+ ((A2_BUF[2 * a] = Al), (A2_BUF[2 * a + 1] = Ah));
104
+ ((A2_BUF[2 * b] = Bl), (A2_BUF[2 * b + 1] = Bh));
105
+ ((A2_BUF[2 * c] = Cl), (A2_BUF[2 * c + 1] = Ch));
106
+ ((A2_BUF[2 * d] = Dl), (A2_BUF[2 * d + 1] = Dh));
107
+ }
108
+
109
+ // Argon2 permutation over 16 register indices into `A2_BUF`, not the register values themselves.
110
+ // RFC 9106 Figure 17: these arguments are the 16 `v0..v15` 64-bit word
111
+ // indices inside eight 16-byte inputs, not copied word values.
112
+ // prettier-ignore
113
+ function P(
114
+ v00: number, v01: number, v02: number, v03: number, v04: number, v05: number, v06: number, v07: number,
115
+ v08: number, v09: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number,
116
+ ) {
117
+ // RFC 9106 Figure 18: first apply GB across rows, then across columns of the 8x8 register matrix.
118
+ G(v00, v04, v08, v12);
119
+ G(v01, v05, v09, v13);
120
+ G(v02, v06, v10, v14);
121
+ G(v03, v07, v11, v15);
122
+ G(v00, v05, v10, v15);
123
+ G(v01, v06, v11, v12);
124
+ G(v02, v07, v08, v13);
125
+ G(v03, v04, v09, v14);
126
+ }
127
+
128
+ function block(x: TArg<Uint32Array>, xPos: number, yPos: number, outPos: number, needXor: boolean) {
129
+ for (let i = 0; i < 256; i++) A2_BUF[i] = x[xPos + i] ^ x[yPos + i];
130
+ // rows (8 consecutive 16-register groups)
131
+ for (let i = 0; i < 128; i += 16) {
132
+ // prettier-ignore
133
+ P(
134
+ i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7,
135
+ i + 8, i + 9, i + 10, i + 11, i + 12, i + 13, i + 14, i + 15
136
+ );
137
+ }
138
+ // columns (8 strided 16-register groups)
139
+ for (let i = 0; i < 16; i += 2) {
140
+ // prettier-ignore
141
+ P(
142
+ i, i + 1, i + 16, i + 17, i + 32, i + 33, i + 48, i + 49,
143
+ i + 64, i + 65, i + 80, i + 81, i + 96, i + 97, i + 112, i + 113
144
+ );
145
+ }
146
+
147
+ // RFC 9106 step 6: passes after the first XOR the old destination block into the new G(X, Y).
148
+ if (needXor) for (let i = 0; i < 256; i++) x[outPos + i] ^= A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
149
+ else for (let i = 0; i < 256; i++) x[outPos + i] = A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
150
+ clean(A2_BUF);
151
+ }
152
+
153
+ // Variable-Length Hash Function H'
154
+ // Returns bytes, not words; 1024-byte block callers explicitly reinterpret with `u32(...)`.
155
+ function Hp(A: TArg<Uint32Array>, dkLen: number): TRet<Uint8Array> {
156
+ const A8 = u8(A);
157
+ const T = new Uint32Array(1);
158
+ const T8 = u8(T);
159
+ // Argon2 H' prefixes dkLen as LE32; native Uint32Array writes would serialize as BE on s390x.
160
+ T[0] = swap8IfBE(dkLen);
161
+ // Fast path
162
+ if (dkLen <= 64) return blake2b.create({ dkLen }).update(T8).update(A8).digest();
163
+ const out = new Uint8Array(dkLen);
164
+ let V = blake2b.create({}).update(T8).update(A8).digest();
165
+ let pos = 0;
166
+ // RFC 9106 Figure 8: each intermediate `V_i` contributes only `W_i`, its first 32 bytes; only
167
+ // `V_{r+1}` is emitted in full at the remaining length.
168
+ out.set(V.subarray(0, 32));
169
+ pos += 32;
170
+ // Rest blocks
171
+ for (; dkLen - pos > 64; pos += 32) {
172
+ const Vh = blake2b.create({}).update(V);
173
+ Vh.digestInto(V);
174
+ Vh.destroy();
175
+ out.set(V.subarray(0, 32), pos);
176
+ }
177
+ // Last block
178
+ out.set(blake2b(V, { dkLen: dkLen - pos }), pos);
179
+ clean(V, T);
180
+ // H' is byte-oriented; returning `u32(out)` would silently drop dkLen % 4 tail bytes.
181
+ return out as TRet<Uint8Array>;
182
+ }
183
+
184
+ // Used only inside process block!
185
+ function indexAlpha(
186
+ r: number,
187
+ s: number,
188
+ laneLen: number,
189
+ segmentLen: number,
190
+ index: number,
191
+ randL: number,
192
+ sameLane: boolean = false
193
+ ) {
194
+ // RFC 9106 §3.4.2 Figures 12-13: map `J1` / `J2` into the current lane's reference area `W`.
195
+ let area: number;
196
+ if (r === 0) {
197
+ if (s === 0) area = index - 1;
198
+ else if (sameLane) area = s * segmentLen + index - 1;
199
+ else area = s * segmentLen + (index == 0 ? -1 : 0);
200
+ } else if (sameLane) area = laneLen - segmentLen + index - 1;
201
+ else area = laneLen - segmentLen + (index == 0 ? -1 : 0);
202
+ const startPos = r !== 0 && s !== ARGON2_SYNC_POINTS - 1 ? (s + 1) * segmentLen : 0;
203
+ // RFC 9106 Figure 13: `mul(randL, randL).h` is `floor(J_1^2 / 2^32)`, and the outer high-half
204
+ // multiply computes `floor(|W| * x / 2^32)` without floating-point math.
205
+ const rel = area - 1 - mul(area, mul(randL, randL).h).h;
206
+ return (startPos + rel) % laneLen;
207
+ }
208
+
209
+ /** Argon2 cost, output, and optional secret/personalization inputs. */
210
+ export type ArgonOpts = {
211
+ /** Time cost measured in iterations. */
212
+ t: number;
213
+ /** Memory cost in kibibytes. */
214
+ m: number;
215
+ /** Parallelization parameter. */
216
+ p: number;
217
+ /** Argon2 version number. Defaults to `0x13`. */
218
+ version?: number;
219
+ /** Optional secret key mixed into initialization. */
220
+ key?: KDFInput;
221
+ /** Optional personalization string or bytes. */
222
+ personalization?: KDFInput;
223
+ /** Desired output length in bytes. RFC 9106 §3.1 requires `T` in the 4..(2^32 - 1) range. */
224
+ dkLen?: number;
225
+ /** Max scheduler block time in milliseconds for the async variants. */
226
+ asyncTick?: number;
227
+ /** Maximum temporary memory budget in bytes. */
228
+ maxmem?: number;
229
+ /**
230
+ * Optional progress callback invoked during long-running derivations.
231
+ * param progress - completion fraction in the `0..1` range
232
+ */
233
+ onProgress?: (progress: number) => void;
234
+ };
235
+
236
+ // Exclusive `2^32` sentinel used by `isU32(...)`, not the inclusive maximum u32 value.
237
+ const maxUint32 = Math.pow(2, 32);
238
+ // Validate safe JS integers in `[0, 2^32 - 1]`.
239
+ function isU32(num: number) {
240
+ return Number.isSafeInteger(num) && num >= 0 && num < maxUint32;
241
+ }
242
+
243
+ function argon2Opts(opts: TArg<ArgonOpts>) {
244
+ const merged: any = {
245
+ version: 0x13,
246
+ dkLen: 32,
247
+ maxmem: maxUint32 - 1,
248
+ asyncTick: 10,
249
+ };
250
+ // Unknown keys are copied through unchanged here and later ignored unless
251
+ // destructuring consumes them.
252
+ for (let [k, v] of Object.entries(opts)) if (v !== undefined) merged[k] = v;
253
+
254
+ const { dkLen, p, m, t, version, onProgress, asyncTick } = merged;
255
+ // RFC 9106 §3.1: tag length `T` MUST be an integer number of bytes from 4 to 2^32-1.
256
+ if (!isU32(dkLen) || dkLen < 4) throw new Error('"dkLen" must be 4..');
257
+ if (!isU32(p) || p < 1 || p >= Math.pow(2, 24)) throw new Error('"p" must be 1..2^24');
258
+ if (!isU32(m)) throw new Error('"m" must be 0..2^32');
259
+ if (!isU32(t) || t < 1) throw new Error('"t" (iterations) must be 1..2^32');
260
+ if (onProgress !== undefined && typeof onProgress !== 'function')
261
+ throw new Error('"progressCb" must be a function');
262
+ anumber(asyncTick, 'asyncTick');
263
+ /*
264
+ Memory size m MUST be an integer number of kibibytes from 8*p
265
+ to 2^(32)-1. The actual number of blocks is m', which is m
266
+ rounded down to the nearest multiple of 4*p.
267
+ */
268
+ if (!isU32(m) || m < 8 * p) throw new Error('"m" (memory) must be at least 8*p bytes');
269
+ // Accept legacy `0x10` for compatibility even though RFC 9106 profiles standardize `0x13`.
270
+ if (version !== 0x10 && version !== 0x13)
271
+ throw new Error('"version" must be 0x10 or 0x13, got ' + version);
272
+ return merged;
273
+ }
274
+
275
+ function argon2Init(
276
+ password: TArg<KDFInput>,
277
+ salt: TArg<KDFInput>,
278
+ type: Types,
279
+ opts: TArg<ArgonOpts>
280
+ ) {
281
+ password = kdfInputToBytes(password, 'password');
282
+ salt = kdfInputToBytes(salt, 'salt');
283
+ if (!isU32(password.length)) throw new Error('"password" must be less of length 1..4Gb');
284
+ // RFC 9106 §3.1 only requires S <= 2^32-1 bytes and says 16 bytes is RECOMMENDED for password
285
+ // hashing; this library intentionally takes the stricter common >=8-byte salt path.
286
+ if (!isU32(salt.length) || salt.length < 8) throw new Error('"salt" must be of length 8..4Gb');
287
+ if (!Object.values(AT).includes(type)) throw new Error('"type" was invalid');
288
+ let { p, dkLen, m, t, version, key, personalization, maxmem, onProgress, asyncTick } =
289
+ argon2Opts(opts);
290
+ // Validation
291
+ key = abytesOrZero(key, 'key');
292
+ personalization = abytesOrZero(personalization, 'personalization');
293
+ // H_0 = H^(64)(LE32(p) || LE32(T) || LE32(m) || LE32(t) ||
294
+ // LE32(v) || LE32(y) || LE32(length(P)) || P ||
295
+ // LE32(length(S)) || S || LE32(length(K)) || K ||
296
+ // LE32(length(X)) || X)
297
+ const h = blake2b.create();
298
+ const BUF = new Uint32Array(1);
299
+ const BUF8 = u8(BUF);
300
+ for (let item of [p, dkLen, m, t, version, type]) {
301
+ // RFC 9106 H0 encodes these scalars as LE32, so normalize the host word before exposing bytes.
302
+ BUF[0] = swap8IfBE(item);
303
+ h.update(BUF8);
304
+ }
305
+ for (let i of [password, salt, key, personalization]) {
306
+ BUF[0] = swap8IfBE(i.length); // BUF is u32 array, this is valid once normalized to LE bytes
307
+ h.update(BUF8).update(i);
308
+ }
309
+ // Reserve two extra LE32 words after the 64-byte `H_0` so Figures 3-4 can append
310
+ // `LE32(0 or 1) || LE32(i)` in place for the lane-starting blocks.
311
+ const H0 = new Uint32Array(18);
312
+ const H0_8 = u8(H0);
313
+ h.digestInto(H0_8);
314
+ // 256 u32 = 1024 (BLOCK_SIZE), fills A2_BUF on processing
315
+
316
+ // Params
317
+ const lanes = p;
318
+ // m' = 4 * p * floor (m / 4p)
319
+ const mP = 4 * p * Math.floor(m / (ARGON2_SYNC_POINTS * p));
320
+ //q = m' / p columns
321
+ const laneLen = Math.floor(mP / p);
322
+ const segmentLen = Math.floor(laneLen / ARGON2_SYNC_POINTS);
323
+ // `maxmem` is documented in bytes; compare against the actual 1024-byte block allocation.
324
+ const memUsed = mP * 1024;
325
+ if (!isU32(maxmem)) throw new Error('"maxmem" expected <2**32, got ' + maxmem);
326
+ if (memUsed > maxmem)
327
+ throw new Error('"maxmem" limit was hit: memUsed(mP*1024)=' + memUsed + ', maxmem=' + maxmem);
328
+ const B = new Uint32Array(memUsed / 4);
329
+ // Fill first blocks
330
+ for (let l = 0; l < p; l++) {
331
+ const i = 256 * laneLen * l;
332
+ // B[i][0] = H'^(1024)(H_0 || LE32(0) || LE32(i))
333
+ H0[17] = swap8IfBE(l);
334
+ H0[16] = swap8IfBE(0);
335
+ B.set(swap32IfBE(u32(Hp(H0, 1024))), i);
336
+ // B[i][1] = H'^(1024)(H_0 || LE32(1) || LE32(i))
337
+ H0[16] = swap8IfBE(1);
338
+ B.set(swap32IfBE(u32(Hp(H0, 1024))), i + 256);
339
+ }
340
+ let perBlock = () => {};
341
+ if (onProgress) {
342
+ // The first segment of the first pass skips two preinitialized blocks per lane.
343
+ const totalBlock = t * ARGON2_SYNC_POINTS * p * segmentLen - 2 * p;
344
+ // Invoke callback if progress changes from 10.01 to 10.02
345
+ // Allows to draw smooth progress bar on up to 8K screen
346
+ const callbackPer = Math.max(Math.floor(totalBlock / 10000), 1);
347
+ let blockCnt = 0;
348
+ perBlock = () => {
349
+ blockCnt++;
350
+ if (onProgress && (!(blockCnt % callbackPer) || blockCnt === totalBlock))
351
+ onProgress(blockCnt / totalBlock);
352
+ };
353
+ }
354
+ clean(BUF, H0);
355
+ return { type, mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick };
356
+ }
357
+
358
+ function argon2Output(
359
+ B: TArg<Uint32Array>,
360
+ p: number,
361
+ laneLen: number,
362
+ dkLen: number
363
+ ): TRet<Uint8Array> {
364
+ const B_final = new Uint32Array(256);
365
+ for (let l = 0; l < p; l++)
366
+ for (let j = 0; j < 256; j++) B_final[j] ^= B[256 * (laneLen * l + laneLen - 1) + j];
367
+ // RFC 9106 steps 7-8 feed the byte string `C` into `H'^T(C)`, so normalize the xor'ed words
368
+ // back to spec byte order before `Hp(...)` reinterprets them as bytes.
369
+ const res = Hp(swap32IfBE(B_final), dkLen);
370
+ // Wipe both the xor scratch and the full working matrix once final digest bytes exist.
371
+ // JS cleanup is still only best-effort, but this local buffer is no longer needed here.
372
+ clean(B, B_final);
373
+ return res;
374
+ }
375
+
376
+ function processBlock(
377
+ B: TArg<Uint32Array>,
378
+ address: TArg<Uint32Array>,
379
+ l: number,
380
+ r: number,
381
+ s: number,
382
+ index: number,
383
+ laneLen: number,
384
+ segmentLen: number,
385
+ lanes: number,
386
+ offset: number,
387
+ prev: number,
388
+ dataIndependent: boolean,
389
+ needXor: boolean
390
+ ) {
391
+ if (offset % laneLen) prev = offset - 1;
392
+ let randL, randH;
393
+ if (dataIndependent) {
394
+ let i128 = index % 128;
395
+ // RFC 9106 §3.4.1.2: each 1024-byte address block yields 128 `(J1, J2)` pairs, so regenerate
396
+ // it whenever the segment index crosses a multiple of 128.
397
+ if (i128 === 0) {
398
+ address[256 + 12]++;
399
+ block(address, 256, 2 * 256, 0, false);
400
+ block(address, 0, 2 * 256, 0, false);
401
+ }
402
+ randL = address[2 * i128];
403
+ randH = address[2 * i128 + 1];
404
+ } else {
405
+ const T = 256 * prev;
406
+ randL = B[T];
407
+ randH = B[T + 1];
408
+ }
409
+ // Address-block path selects `J1` / `J2`, then maps them to the reference
410
+ // lane/block per RFC 9106 §3.4.
411
+ const refLane = r === 0 && s === 0 ? l : randH % lanes;
412
+ const refPos = indexAlpha(r, s, laneLen, segmentLen, index, randL, refLane == l);
413
+ const refBlock = laneLen * refLane + refPos;
414
+ // B[i][j] = G(B[i][j-1], B[l][z])
415
+ block(B, 256 * prev, 256 * refBlock, offset * 256, needXor);
416
+ }
417
+
418
+ function argon2(
419
+ type: Types,
420
+ password: TArg<KDFInput>,
421
+ salt: TArg<KDFInput>,
422
+ opts: TArg<ArgonOpts>
423
+ ): TRet<Uint8Array> {
424
+ const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock } = argon2Init(
425
+ password,
426
+ salt,
427
+ type,
428
+ opts
429
+ );
430
+ // Pre-loop setup
431
+ // [address, input, zero_block] format so we can pass single U32 to block function
432
+ const address = new Uint32Array(3 * 256);
433
+ address[256 + 6] = mP;
434
+ address[256 + 8] = t;
435
+ address[256 + 10] = type;
436
+ for (let r = 0; r < t; r++) {
437
+ // RFC 9106 step 6 applies the XOR-on-later-passes rule only for version `0x13`; legacy
438
+ // `0x10` keeps the older overwrite behavior used by the v16 test vectors.
439
+ const needXor = r !== 0 && version === 0x13;
440
+ address[256 + 0] = r;
441
+ for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
442
+ address[256 + 4] = s;
443
+ // RFC 9106 §3.4.1.3: Argon2id uses Argon2i's data-independent `J1` / `J2` generation only
444
+ // in pass 0, slices 0 and 1; Argon2i uses it in every segment.
445
+ const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
446
+ for (let l = 0; l < p; l++) {
447
+ address[256 + 2] = l;
448
+ address[256 + 12] = 0;
449
+ let startPos = 0;
450
+ if (r === 0 && s === 0) {
451
+ startPos = 2;
452
+ if (dataIndependent) {
453
+ address[256 + 12]++;
454
+ block(address, 256, 2 * 256, 0, false);
455
+ block(address, 0, 2 * 256, 0, false);
456
+ }
457
+ }
458
+ // current block postion
459
+ let offset = l * laneLen + s * segmentLen + startPos;
460
+ // previous block position
461
+ let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
462
+ for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
463
+ perBlock();
464
+ processBlock(
465
+ B,
466
+ address,
467
+ l,
468
+ r,
469
+ s,
470
+ index,
471
+ laneLen,
472
+ segmentLen,
473
+ lanes,
474
+ offset,
475
+ prev,
476
+ dataIndependent,
477
+ needXor
478
+ );
479
+ }
480
+ }
481
+ }
482
+ }
483
+ clean(address);
484
+ return argon2Output(B, p, laneLen, dkLen);
485
+ }
486
+
487
+ /**
488
+ * Argon2d GPU-resistant version.
489
+ * @param password - password or input key material
490
+ * @param salt - unique salt value
491
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
492
+ * @returns Derived key bytes.
493
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
494
+ * @example
495
+ * Derive a key with Argon2d.
496
+ * ```ts
497
+ * argon2d('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
498
+ * ```
499
+ */
500
+ export const argon2d = (
501
+ password: TArg<KDFInput>,
502
+ salt: TArg<KDFInput>,
503
+ opts: TArg<ArgonOpts>
504
+ ): TRet<Uint8Array> => argon2(AT.Argond2d, password, salt, opts);
505
+ /**
506
+ * Argon2i side-channel-resistant version.
507
+ * @param password - password or input key material
508
+ * @param salt - unique salt value
509
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
510
+ * @returns Derived key bytes.
511
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
512
+ * @example
513
+ * Derive a key with Argon2i.
514
+ * ```ts
515
+ * argon2i('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
516
+ * ```
517
+ */
518
+ export const argon2i = (
519
+ password: TArg<KDFInput>,
520
+ salt: TArg<KDFInput>,
521
+ opts: TArg<ArgonOpts>
522
+ ): TRet<Uint8Array> => argon2(AT.Argon2i, password, salt, opts);
523
+ /**
524
+ * Argon2id, combining i+d, the most popular version from RFC 9106.
525
+ * @param password - password or input key material
526
+ * @param salt - unique salt value
527
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
528
+ * @returns Derived key bytes.
529
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
530
+ * @example
531
+ * Derive a key with Argon2id.
532
+ * ```ts
533
+ * argon2id('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
534
+ * ```
535
+ */
536
+ export const argon2id = (
537
+ password: TArg<KDFInput>,
538
+ salt: TArg<KDFInput>,
539
+ opts: TArg<ArgonOpts>
540
+ ): TRet<Uint8Array> => argon2(AT.Argon2id, password, salt, opts);
541
+
542
+ async function argon2Async(
543
+ type: Types,
544
+ password: TArg<KDFInput>,
545
+ salt: TArg<KDFInput>,
546
+ opts: TArg<ArgonOpts>
547
+ ): Promise<TRet<Uint8Array>> {
548
+ const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick } =
549
+ argon2Init(password, salt, type, opts);
550
+ // Pre-loop setup
551
+ // [address, input, zero_block] format so we can pass single U32 to block function
552
+ const address = new Uint32Array(3 * 256);
553
+ address[256 + 6] = mP;
554
+ address[256 + 8] = t;
555
+ address[256 + 10] = type;
556
+ let ts = Date.now();
557
+ for (let r = 0; r < t; r++) {
558
+ // RFC 9106 step 6 applies the XOR-on-later-passes rule only for version `0x13`; legacy
559
+ // `0x10` keeps the older overwrite behavior used by the v16 test vectors.
560
+ const needXor = r !== 0 && version === 0x13;
561
+ address[256 + 0] = r;
562
+ for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
563
+ address[256 + 4] = s;
564
+ // RFC 9106 §3.4.1.3: Argon2id uses Argon2i's data-independent `J1` / `J2` generation only
565
+ // in pass 0, slices 0 and 1; Argon2i uses it in every segment.
566
+ const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
567
+ for (let l = 0; l < p; l++) {
568
+ address[256 + 2] = l;
569
+ address[256 + 12] = 0;
570
+ let startPos = 0;
571
+ if (r === 0 && s === 0) {
572
+ startPos = 2;
573
+ if (dataIndependent) {
574
+ address[256 + 12]++;
575
+ block(address, 256, 2 * 256, 0, false);
576
+ block(address, 0, 2 * 256, 0, false);
577
+ }
578
+ }
579
+ // current block postion
580
+ let offset = l * laneLen + s * segmentLen + startPos;
581
+ // previous block position
582
+ let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
583
+ for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
584
+ perBlock();
585
+ processBlock(
586
+ B,
587
+ address,
588
+ l,
589
+ r,
590
+ s,
591
+ index,
592
+ laneLen,
593
+ segmentLen,
594
+ lanes,
595
+ offset,
596
+ prev,
597
+ dataIndependent,
598
+ needXor
599
+ );
600
+ // Date.now() is not monotonic. If the clock goes backwards,
601
+ // still yield control.
602
+ const diff = Date.now() - ts;
603
+ if (!(diff >= 0 && diff < asyncTick)) {
604
+ await nextTick();
605
+ ts += diff;
606
+ }
607
+ }
608
+ }
609
+ }
610
+ }
611
+ clean(address);
612
+ return argon2Output(B, p, laneLen, dkLen);
613
+ }
614
+
615
+ /**
616
+ * Argon2d async GPU-resistant version.
617
+ * @param password - password or input key material
618
+ * @param salt - unique salt value
619
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
620
+ * @returns Promise resolving to derived key bytes.
621
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
622
+ * @example
623
+ * Derive a key with Argon2d asynchronously.
624
+ * ```ts
625
+ * await argon2dAsync('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
626
+ * ```
627
+ */
628
+ export const argon2dAsync = (
629
+ password: TArg<KDFInput>,
630
+ salt: TArg<KDFInput>,
631
+ opts: TArg<ArgonOpts>
632
+ ): Promise<TRet<Uint8Array>> => argon2Async(AT.Argond2d, password, salt, opts);
633
+ /**
634
+ * Argon2i async side-channel-resistant version.
635
+ * @param password - password or input key material
636
+ * @param salt - unique salt value
637
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
638
+ * @returns Promise resolving to derived key bytes.
639
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
640
+ * @example
641
+ * Derive a key with Argon2i asynchronously.
642
+ * ```ts
643
+ * await argon2iAsync('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
644
+ * ```
645
+ */
646
+ export const argon2iAsync = (
647
+ password: TArg<KDFInput>,
648
+ salt: TArg<KDFInput>,
649
+ opts: TArg<ArgonOpts>
650
+ ): Promise<TRet<Uint8Array>> => argon2Async(AT.Argon2i, password, salt, opts);
651
+ /**
652
+ * Argon2id async, combining i+d, the most popular version from RFC 9106.
653
+ * @param password - password or input key material
654
+ * @param salt - unique salt value
655
+ * @param opts - Argon2 cost and optional tuning parameters. See {@link ArgonOpts}.
656
+ * @returns Promise resolving to derived key bytes.
657
+ * @throws If the Argon2 input or cost parameters are invalid. {@link Error}
658
+ * @example
659
+ * Derive a key with Argon2id asynchronously.
660
+ * ```ts
661
+ * await argon2idAsync('password', 'salt1234', { t: 1, m: 8, p: 1, dkLen: 32 });
662
+ * ```
663
+ */
664
+ export const argon2idAsync = (
665
+ password: TArg<KDFInput>,
666
+ salt: TArg<KDFInput>,
667
+ opts: TArg<ArgonOpts>
668
+ ): Promise<TRet<Uint8Array>> => argon2Async(AT.Argon2id, password, salt, opts);