@versini/auth-common 2.12.1 → 3.0.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/dist/index.d.ts +18 -2
- package/dist/index.js +127 -120
- package/package.json +2 -2
package/dist/index.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ declare const AUTH_TYPES: {
|
|
|
11
11
|
declare const HEADERS: {
|
|
12
12
|
CLIENT_ID: string;
|
|
13
13
|
};
|
|
14
|
+
declare const BODY: {
|
|
15
|
+
ACCESS_TOKEN: string;
|
|
16
|
+
};
|
|
14
17
|
declare const JWT: {
|
|
15
18
|
ALG: string;
|
|
16
19
|
USER_ID_KEY: string;
|
|
@@ -61,6 +64,9 @@ declare function pkceChallengePair(length?: number): Promise<{
|
|
|
61
64
|
*/
|
|
62
65
|
declare function verifyChallenge(code_verifier: string, expectedChallenge: string): Promise<boolean>;
|
|
63
66
|
|
|
67
|
+
type BodyLike = Record<string, unknown> & {
|
|
68
|
+
access_token?: string;
|
|
69
|
+
};
|
|
64
70
|
type HeadersLike = Record<string, unknown> & {
|
|
65
71
|
authorization?: string;
|
|
66
72
|
"content-type"?: string;
|
|
@@ -68,11 +74,21 @@ type HeadersLike = Record<string, unknown> & {
|
|
|
68
74
|
};
|
|
69
75
|
/**
|
|
70
76
|
* Get a Bearer Token from a request.
|
|
77
|
+
* It checks the following sources in order:
|
|
78
|
+
* 1. The `access_token` body parameter.
|
|
79
|
+
* 2. The `auth.${clientId}` cookie.
|
|
80
|
+
* 3. The `Authorization` header.
|
|
71
81
|
*
|
|
72
82
|
* @param headers An object containing the request headers, usually `req.headers`.
|
|
83
|
+
* @param body An object containing the request body, usually `req.body`.
|
|
73
84
|
* @param clientId The client ID to use.
|
|
74
85
|
*
|
|
75
86
|
*/
|
|
76
|
-
|
|
87
|
+
type GetToken = {
|
|
88
|
+
clientId: string;
|
|
89
|
+
headers: HeadersLike;
|
|
90
|
+
body?: BodyLike;
|
|
91
|
+
};
|
|
92
|
+
declare const getToken: ({ headers, body, clientId }: GetToken) => string;
|
|
77
93
|
|
|
78
|
-
export { API_TYPE, AUTH_TYPES, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
|
|
94
|
+
export { API_TYPE, AUTH_TYPES, BODY, type BodyLike, HEADERS, type HeadersLike, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,28 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/auth-common
|
|
2
|
+
@versini/auth-common v3.0.0
|
|
3
3
|
© 2024 gizmette.com
|
|
4
4
|
*/
|
|
5
5
|
try {
|
|
6
6
|
window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
|
|
7
|
-
version: "
|
|
8
|
-
buildTime: "07/
|
|
7
|
+
version: "3.0.0",
|
|
8
|
+
buildTime: "07/19/2024 09:13 AM EDT",
|
|
9
9
|
homepage: "https://github.com/aversini/auth-client",
|
|
10
10
|
license: "MIT"
|
|
11
11
|
});
|
|
12
12
|
} catch {
|
|
13
13
|
}
|
|
14
|
-
const
|
|
14
|
+
const Ze = {
|
|
15
15
|
ID_TOKEN: "id_token",
|
|
16
16
|
ACCESS_TOKEN: "token",
|
|
17
17
|
ID_AND_ACCESS_TOKEN: "id_token token",
|
|
18
18
|
CODE: "code",
|
|
19
19
|
REFRESH_TOKEN: "refresh_token",
|
|
20
20
|
PASSKEY: "passkey"
|
|
21
|
-
},
|
|
21
|
+
}, je = {
|
|
22
22
|
CLIENT_ID: "X-Auth-ClientId"
|
|
23
|
-
},
|
|
23
|
+
}, ne = {
|
|
24
|
+
ACCESS_TOKEN: "access_token"
|
|
25
|
+
}, U = {
|
|
24
26
|
ALG: "RS256",
|
|
25
27
|
USER_ID_KEY: "sub",
|
|
26
28
|
TOKEN_ID_KEY: "__raw",
|
|
@@ -28,7 +30,7 @@ const Qe = {
|
|
|
28
30
|
USERNAME_KEY: "username",
|
|
29
31
|
AUTH_TYPE_KEY: "auth_type",
|
|
30
32
|
ISSUER: "gizmette.com"
|
|
31
|
-
},
|
|
33
|
+
}, ae = `-----BEGIN PUBLIC KEY-----
|
|
32
34
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
|
|
33
35
|
w5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5
|
|
34
36
|
i1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle
|
|
@@ -36,23 +38,23 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
|
|
|
36
38
|
l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
|
|
37
39
|
sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
|
|
38
40
|
awIDAQAB
|
|
39
|
-
-----END PUBLIC KEY-----`,
|
|
41
|
+
-----END PUBLIC KEY-----`, et = {
|
|
40
42
|
ACCESS: "5m",
|
|
41
43
|
ID: "90d",
|
|
42
44
|
REFRESH: "90d"
|
|
43
|
-
},
|
|
45
|
+
}, tt = {
|
|
44
46
|
AUTHENTICATE: "authenticate",
|
|
45
47
|
CODE: "code",
|
|
46
48
|
LOGOUT: "logout"
|
|
47
49
|
}, K = crypto, q = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
|
|
48
|
-
function
|
|
50
|
+
function oe(...e) {
|
|
49
51
|
const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
|
|
50
52
|
let n = 0;
|
|
51
53
|
for (const a of e)
|
|
52
54
|
r.set(a, n), n += a.length;
|
|
53
55
|
return r;
|
|
54
56
|
}
|
|
55
|
-
const
|
|
57
|
+
const ie = (e) => {
|
|
56
58
|
const t = atob(e), r = new Uint8Array(t.length);
|
|
57
59
|
for (let n = 0; n < t.length; n++)
|
|
58
60
|
r[n] = t.charCodeAt(n);
|
|
@@ -61,7 +63,7 @@ const oe = (e) => {
|
|
|
61
63
|
let t = e;
|
|
62
64
|
t instanceof Uint8Array && (t = C.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
63
65
|
try {
|
|
64
|
-
return
|
|
66
|
+
return ie(t);
|
|
65
67
|
} catch {
|
|
66
68
|
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
67
69
|
}
|
|
@@ -91,7 +93,7 @@ class $ extends A {
|
|
|
91
93
|
super(t), this.code = "ERR_JWT_EXPIRED", this.claim = n, this.reason = a, this.payload = r;
|
|
92
94
|
}
|
|
93
95
|
}
|
|
94
|
-
class
|
|
96
|
+
class ce extends A {
|
|
95
97
|
constructor() {
|
|
96
98
|
super(...arguments), this.code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
97
99
|
}
|
|
@@ -123,7 +125,7 @@ class S extends A {
|
|
|
123
125
|
return "ERR_JWT_INVALID";
|
|
124
126
|
}
|
|
125
127
|
}
|
|
126
|
-
class
|
|
128
|
+
class se extends A {
|
|
127
129
|
constructor() {
|
|
128
130
|
super(...arguments), this.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED", this.message = "signature verification failed";
|
|
129
131
|
}
|
|
@@ -137,10 +139,10 @@ function m(e, t = "algorithm.name") {
|
|
|
137
139
|
function T(e, t) {
|
|
138
140
|
return e.name === t;
|
|
139
141
|
}
|
|
140
|
-
function
|
|
142
|
+
function x(e) {
|
|
141
143
|
return parseInt(e.name.slice(4), 10);
|
|
142
144
|
}
|
|
143
|
-
function
|
|
145
|
+
function de(e) {
|
|
144
146
|
switch (e) {
|
|
145
147
|
case "ES256":
|
|
146
148
|
return "P-256";
|
|
@@ -152,7 +154,7 @@ function se(e) {
|
|
|
152
154
|
throw new Error("unreachable");
|
|
153
155
|
}
|
|
154
156
|
}
|
|
155
|
-
function
|
|
157
|
+
function ue(e, t) {
|
|
156
158
|
if (t.length && !t.some((r) => e.usages.includes(r))) {
|
|
157
159
|
let r = "CryptoKey does not support this operation, its usages must include ";
|
|
158
160
|
if (t.length > 2) {
|
|
@@ -162,7 +164,7 @@ function de(e, t) {
|
|
|
162
164
|
throw new TypeError(r);
|
|
163
165
|
}
|
|
164
166
|
}
|
|
165
|
-
function
|
|
167
|
+
function le(e, t, ...r) {
|
|
166
168
|
switch (t) {
|
|
167
169
|
case "HS256":
|
|
168
170
|
case "HS384":
|
|
@@ -170,7 +172,7 @@ function ue(e, t, ...r) {
|
|
|
170
172
|
if (!T(e.algorithm, "HMAC"))
|
|
171
173
|
throw m("HMAC");
|
|
172
174
|
const n = parseInt(t.slice(2), 10);
|
|
173
|
-
if (
|
|
175
|
+
if (x(e.algorithm.hash) !== n)
|
|
174
176
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
175
177
|
break;
|
|
176
178
|
}
|
|
@@ -180,7 +182,7 @@ function ue(e, t, ...r) {
|
|
|
180
182
|
if (!T(e.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
181
183
|
throw m("RSASSA-PKCS1-v1_5");
|
|
182
184
|
const n = parseInt(t.slice(2), 10);
|
|
183
|
-
if (
|
|
185
|
+
if (x(e.algorithm.hash) !== n)
|
|
184
186
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
185
187
|
break;
|
|
186
188
|
}
|
|
@@ -190,7 +192,7 @@ function ue(e, t, ...r) {
|
|
|
190
192
|
if (!T(e.algorithm, "RSA-PSS"))
|
|
191
193
|
throw m("RSA-PSS");
|
|
192
194
|
const n = parseInt(t.slice(2), 10);
|
|
193
|
-
if (
|
|
195
|
+
if (x(e.algorithm.hash) !== n)
|
|
194
196
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
195
197
|
break;
|
|
196
198
|
}
|
|
@@ -204,7 +206,7 @@ function ue(e, t, ...r) {
|
|
|
204
206
|
case "ES512": {
|
|
205
207
|
if (!T(e.algorithm, "ECDSA"))
|
|
206
208
|
throw m("ECDSA");
|
|
207
|
-
const n =
|
|
209
|
+
const n = de(t);
|
|
208
210
|
if (e.algorithm.namedCurve !== n)
|
|
209
211
|
throw m(n, "algorithm.namedCurve");
|
|
210
212
|
break;
|
|
@@ -212,7 +214,7 @@ function ue(e, t, ...r) {
|
|
|
212
214
|
default:
|
|
213
215
|
throw new TypeError("CryptoKey does not support this operation");
|
|
214
216
|
}
|
|
215
|
-
|
|
217
|
+
ue(e, r);
|
|
216
218
|
}
|
|
217
219
|
function z(e, t, ...r) {
|
|
218
220
|
var n;
|
|
@@ -226,7 +228,7 @@ const L = (e, ...t) => z("Key must be ", e, ...t);
|
|
|
226
228
|
function G(e, t, ...r) {
|
|
227
229
|
return z(`Key for the ${e} algorithm must be `, t, ...r);
|
|
228
230
|
}
|
|
229
|
-
const Q = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"],
|
|
231
|
+
const Q = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", R = ["CryptoKey"], fe = (...e) => {
|
|
230
232
|
const t = e.filter(Boolean);
|
|
231
233
|
if (t.length === 0 || t.length === 1)
|
|
232
234
|
return !0;
|
|
@@ -245,11 +247,11 @@ const Q = (e) => q(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
|
|
|
245
247
|
}
|
|
246
248
|
return !0;
|
|
247
249
|
};
|
|
248
|
-
function
|
|
250
|
+
function he(e) {
|
|
249
251
|
return typeof e == "object" && e !== null;
|
|
250
252
|
}
|
|
251
253
|
function P(e) {
|
|
252
|
-
if (!
|
|
254
|
+
if (!he(e) || Object.prototype.toString.call(e) !== "[object Object]")
|
|
253
255
|
return !1;
|
|
254
256
|
if (Object.getPrototypeOf(e) === null)
|
|
255
257
|
return !0;
|
|
@@ -258,14 +260,14 @@ function P(e) {
|
|
|
258
260
|
t = Object.getPrototypeOf(t);
|
|
259
261
|
return Object.getPrototypeOf(e) === t;
|
|
260
262
|
}
|
|
261
|
-
const
|
|
263
|
+
const pe = (e, t) => {
|
|
262
264
|
if (e.startsWith("RS") || e.startsWith("PS")) {
|
|
263
265
|
const { modulusLength: r } = t.algorithm;
|
|
264
266
|
if (typeof r != "number" || r < 2048)
|
|
265
267
|
throw new TypeError(`${e} requires key modulusLength to be 2048 bits or larger`);
|
|
266
268
|
}
|
|
267
269
|
};
|
|
268
|
-
function
|
|
270
|
+
function me(e) {
|
|
269
271
|
let t, r;
|
|
270
272
|
switch (e.kty) {
|
|
271
273
|
case "RSA": {
|
|
@@ -337,10 +339,10 @@ function pe(e) {
|
|
|
337
339
|
}
|
|
338
340
|
return { algorithm: t, keyUsages: r };
|
|
339
341
|
}
|
|
340
|
-
const
|
|
342
|
+
const Se = async (e) => {
|
|
341
343
|
if (!e.alg)
|
|
342
344
|
throw new TypeError('"alg" argument is required when "jwk.alg" is not present');
|
|
343
|
-
const { algorithm: t, keyUsages: r } =
|
|
345
|
+
const { algorithm: t, keyUsages: r } = me(e), n = [
|
|
344
346
|
t,
|
|
345
347
|
e.ext ?? !1,
|
|
346
348
|
e.key_ops ?? r
|
|
@@ -352,21 +354,21 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
352
354
|
let a = e.get(t);
|
|
353
355
|
if (a != null && a[n])
|
|
354
356
|
return a[n];
|
|
355
|
-
const i = await
|
|
357
|
+
const i = await Se({ ...r, alg: n });
|
|
356
358
|
return a ? a[n] = i : e.set(t, { [n]: i }), i;
|
|
357
|
-
},
|
|
359
|
+
}, ye = (e, t) => {
|
|
358
360
|
if (Z(e)) {
|
|
359
361
|
let r = e.export({ format: "jwk" });
|
|
360
362
|
return delete r.d, delete r.dp, delete r.dq, delete r.p, delete r.q, delete r.qi, r.k ? X(r.k) : (J || (J = /* @__PURE__ */ new WeakMap()), j(J, e, r, t));
|
|
361
363
|
}
|
|
362
364
|
return e;
|
|
363
|
-
},
|
|
365
|
+
}, Ee = (e, t) => {
|
|
364
366
|
if (Z(e)) {
|
|
365
367
|
let r = e.export({ format: "jwk" });
|
|
366
368
|
return r.k ? X(r.k) : (W || (W = /* @__PURE__ */ new WeakMap()), j(W, e, r, t));
|
|
367
369
|
}
|
|
368
370
|
return e;
|
|
369
|
-
},
|
|
371
|
+
}, we = { normalizePublicKey: ye, normalizePrivateKey: Ee }, E = (e, t, r = 0) => {
|
|
370
372
|
r === 0 && (t.unshift(t.length), t.unshift(6));
|
|
371
373
|
const n = e.indexOf(t[0], r);
|
|
372
374
|
if (n === -1)
|
|
@@ -392,7 +394,7 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
392
394
|
default:
|
|
393
395
|
throw new w("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
|
|
394
396
|
}
|
|
395
|
-
},
|
|
397
|
+
}, ge = async (e, t, r, n, a) => {
|
|
396
398
|
let i, o;
|
|
397
399
|
const c = new Uint8Array(atob(r.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
|
|
398
400
|
switch (n) {
|
|
@@ -439,20 +441,20 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
439
441
|
throw new w('Invalid or unsupported "alg" (Algorithm) value');
|
|
440
442
|
}
|
|
441
443
|
return K.subtle.importKey(t, c, i, !1, o);
|
|
442
|
-
},
|
|
443
|
-
async function
|
|
444
|
+
}, Ae = (e, t, r) => ge(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
|
|
445
|
+
async function be(e, t, r) {
|
|
444
446
|
if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
|
|
445
447
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
446
|
-
return
|
|
448
|
+
return Ae(e, t);
|
|
447
449
|
}
|
|
448
|
-
const I = (e) => e == null ? void 0 : e[Symbol.toStringTag],
|
|
450
|
+
const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], Ce = (e, t) => {
|
|
449
451
|
if (!(t instanceof Uint8Array)) {
|
|
450
452
|
if (!Q(t))
|
|
451
453
|
throw new TypeError(G(e, t, ...R, "Uint8Array"));
|
|
452
454
|
if (t.type !== "secret")
|
|
453
455
|
throw new TypeError(`${I(t)} instances for symmetric algorithms must be of type "secret"`);
|
|
454
456
|
}
|
|
455
|
-
},
|
|
457
|
+
}, ve = (e, t, r) => {
|
|
456
458
|
if (!Q(t))
|
|
457
459
|
throw new TypeError(G(e, t, ...R));
|
|
458
460
|
if (t.type === "secret")
|
|
@@ -461,10 +463,10 @@ const I = (e) => e == null ? void 0 : e[Symbol.toStringTag], be = (e, t) => {
|
|
|
461
463
|
throw new TypeError(`${I(t)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
462
464
|
if (t.algorithm && r === "encrypt" && t.type === "private")
|
|
463
465
|
throw new TypeError(`${I(t)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
464
|
-
},
|
|
465
|
-
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ?
|
|
466
|
+
}, Te = (e, t, r) => {
|
|
467
|
+
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? Ce(e, t) : ve(e, t, r);
|
|
466
468
|
};
|
|
467
|
-
function
|
|
469
|
+
function _e(e, t, r, n, a) {
|
|
468
470
|
if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
|
|
469
471
|
throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
470
472
|
if (!n || n.crit === void 0)
|
|
@@ -483,13 +485,13 @@ function Te(e, t, r, n, a) {
|
|
|
483
485
|
}
|
|
484
486
|
return new Set(n.crit);
|
|
485
487
|
}
|
|
486
|
-
const
|
|
488
|
+
const Ie = (e, t) => {
|
|
487
489
|
if (t !== void 0 && (!Array.isArray(t) || t.some((r) => typeof r != "string")))
|
|
488
490
|
throw new TypeError(`"${e}" option must be an array of strings`);
|
|
489
491
|
if (t)
|
|
490
492
|
return new Set(t);
|
|
491
493
|
};
|
|
492
|
-
function
|
|
494
|
+
function Re(e, t) {
|
|
493
495
|
const r = `SHA-${e.slice(-3)}`;
|
|
494
496
|
switch (e) {
|
|
495
497
|
case "HS256":
|
|
@@ -514,9 +516,9 @@ function Ie(e, t) {
|
|
|
514
516
|
throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
|
|
515
517
|
}
|
|
516
518
|
}
|
|
517
|
-
async function
|
|
518
|
-
if (t = await
|
|
519
|
-
return
|
|
519
|
+
async function Pe(e, t, r) {
|
|
520
|
+
if (t = await we.normalizePublicKey(t, e), q(t))
|
|
521
|
+
return le(t, e, r), t;
|
|
520
522
|
if (t instanceof Uint8Array) {
|
|
521
523
|
if (!e.startsWith("HS"))
|
|
522
524
|
throw new TypeError(L(t, ...R));
|
|
@@ -524,17 +526,17 @@ async function Re(e, t, r) {
|
|
|
524
526
|
}
|
|
525
527
|
throw new TypeError(L(t, ...R, "Uint8Array"));
|
|
526
528
|
}
|
|
527
|
-
const
|
|
528
|
-
const a = await
|
|
529
|
-
|
|
530
|
-
const i =
|
|
529
|
+
const Ke = async (e, t, r, n) => {
|
|
530
|
+
const a = await Pe(e, t, "verify");
|
|
531
|
+
pe(e, a);
|
|
532
|
+
const i = Re(e, a.algorithm);
|
|
531
533
|
try {
|
|
532
534
|
return await K.subtle.verify(i, a, r, n);
|
|
533
535
|
} catch {
|
|
534
536
|
return !1;
|
|
535
537
|
}
|
|
536
538
|
};
|
|
537
|
-
async function
|
|
539
|
+
async function Oe(e, t, r) {
|
|
538
540
|
if (!P(e))
|
|
539
541
|
throw new u("Flattened JWS must be an object");
|
|
540
542
|
if (e.protected === void 0 && e.header === void 0)
|
|
@@ -550,42 +552,42 @@ async function Ke(e, t, r) {
|
|
|
550
552
|
let n = {};
|
|
551
553
|
if (e.protected)
|
|
552
554
|
try {
|
|
553
|
-
const
|
|
554
|
-
n = JSON.parse(C.decode(
|
|
555
|
+
const O = b(e.protected);
|
|
556
|
+
n = JSON.parse(C.decode(O));
|
|
555
557
|
} catch {
|
|
556
558
|
throw new u("JWS Protected Header is invalid");
|
|
557
559
|
}
|
|
558
|
-
if (!
|
|
560
|
+
if (!fe(n, e.header))
|
|
559
561
|
throw new u("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
560
562
|
const a = {
|
|
561
563
|
...n,
|
|
562
564
|
...e.header
|
|
563
|
-
}, i =
|
|
565
|
+
}, i = _e(u, /* @__PURE__ */ new Map([["b64", !0]]), r == null ? void 0 : r.crit, n, a);
|
|
564
566
|
let o = !0;
|
|
565
567
|
if (i.has("b64") && (o = n.b64, typeof o != "boolean"))
|
|
566
568
|
throw new u('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
567
569
|
const { alg: c } = a;
|
|
568
570
|
if (typeof c != "string" || !c)
|
|
569
571
|
throw new u('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
570
|
-
const s = r &&
|
|
572
|
+
const s = r && Ie("algorithms", r.algorithms);
|
|
571
573
|
if (s && !s.has(c))
|
|
572
|
-
throw new
|
|
574
|
+
throw new ce('"alg" (Algorithm) Header Parameter value not allowed');
|
|
573
575
|
if (o) {
|
|
574
576
|
if (typeof e.payload != "string")
|
|
575
577
|
throw new u("JWS Payload must be a string");
|
|
576
578
|
} else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
|
|
577
579
|
throw new u("JWS Payload must be a string or an Uint8Array instance");
|
|
578
580
|
let p = !1;
|
|
579
|
-
typeof t == "function" && (t = await t(n, e), p = !0),
|
|
580
|
-
const g =
|
|
581
|
+
typeof t == "function" && (t = await t(n, e), p = !0), Te(c, t, "verify");
|
|
582
|
+
const g = oe(v.encode(e.protected ?? ""), v.encode("."), typeof e.payload == "string" ? v.encode(e.payload) : e.payload);
|
|
581
583
|
let f;
|
|
582
584
|
try {
|
|
583
585
|
f = b(e.signature);
|
|
584
586
|
} catch {
|
|
585
587
|
throw new u("Failed to base64url decode the signature");
|
|
586
588
|
}
|
|
587
|
-
if (!await
|
|
588
|
-
throw new
|
|
589
|
+
if (!await Ke(c, t, f, g))
|
|
590
|
+
throw new se();
|
|
589
591
|
let y;
|
|
590
592
|
if (o)
|
|
591
593
|
try {
|
|
@@ -603,10 +605,10 @@ async function xe(e, t, r) {
|
|
|
603
605
|
const { 0: n, 1: a, 2: i, length: o } = e.split(".");
|
|
604
606
|
if (o !== 3)
|
|
605
607
|
throw new u("Invalid Compact JWS");
|
|
606
|
-
const c = await
|
|
608
|
+
const c = await Oe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
|
|
607
609
|
return typeof t == "function" ? { ...s, key: c.key } : s;
|
|
608
610
|
}
|
|
609
|
-
const
|
|
611
|
+
const We = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te * 24, Je = H * 7, De = H * 365.25, He = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i, k = (e) => {
|
|
610
612
|
const t = He.exec(e);
|
|
611
613
|
if (!t || t[4] && t[1])
|
|
612
614
|
throw new TypeError("Invalid time period format");
|
|
@@ -637,19 +639,19 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, D = te *
|
|
|
637
639
|
case "day":
|
|
638
640
|
case "days":
|
|
639
641
|
case "d":
|
|
640
|
-
a = Math.round(r *
|
|
642
|
+
a = Math.round(r * H);
|
|
641
643
|
break;
|
|
642
644
|
case "week":
|
|
643
645
|
case "weeks":
|
|
644
646
|
case "w":
|
|
645
|
-
a = Math.round(r *
|
|
647
|
+
a = Math.round(r * Je);
|
|
646
648
|
break;
|
|
647
649
|
default:
|
|
648
|
-
a = Math.round(r *
|
|
650
|
+
a = Math.round(r * De);
|
|
649
651
|
break;
|
|
650
652
|
}
|
|
651
653
|
return t[1] === "-" || t[4] === "ago" ? -a : a;
|
|
652
|
-
},
|
|
654
|
+
}, B = (e) => e.toLowerCase().replace(/^application\//, ""), Ne = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1, Ue = (e, t, r = {}) => {
|
|
653
655
|
let n;
|
|
654
656
|
try {
|
|
655
657
|
n = JSON.parse(C.decode(t));
|
|
@@ -658,7 +660,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, D = te *
|
|
|
658
660
|
if (!P(n))
|
|
659
661
|
throw new S("JWT Claims Set must be a top-level JSON object");
|
|
660
662
|
const { typ: a } = r;
|
|
661
|
-
if (a && (typeof e.typ != "string" ||
|
|
663
|
+
if (a && (typeof e.typ != "string" || B(e.typ) !== B(a)))
|
|
662
664
|
throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
|
|
663
665
|
const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: p } = r, g = [...i];
|
|
664
666
|
p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"), o !== void 0 && g.push("iss");
|
|
@@ -669,7 +671,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, D = te *
|
|
|
669
671
|
throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
|
|
670
672
|
if (c && n.sub !== c)
|
|
671
673
|
throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
|
|
672
|
-
if (s && !
|
|
674
|
+
if (s && !Ne(n.aud, typeof s == "string" ? [s] : s))
|
|
673
675
|
throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
|
|
674
676
|
let f;
|
|
675
677
|
switch (typeof r.clockTolerance) {
|
|
@@ -685,7 +687,7 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, D = te *
|
|
|
685
687
|
default:
|
|
686
688
|
throw new TypeError("Invalid clockTolerance option type");
|
|
687
689
|
}
|
|
688
|
-
const { currentDate:
|
|
690
|
+
const { currentDate: N } = r, y = We(N || /* @__PURE__ */ new Date());
|
|
689
691
|
if ((n.iat !== void 0 || p) && typeof n.iat != "number")
|
|
690
692
|
throw new h('"iat" claim must be a number', n, "iat", "invalid");
|
|
691
693
|
if (n.nbf !== void 0) {
|
|
@@ -701,15 +703,15 @@ const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, D = te *
|
|
|
701
703
|
throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
|
|
702
704
|
}
|
|
703
705
|
if (p) {
|
|
704
|
-
const l = y - n.iat,
|
|
705
|
-
if (l - f >
|
|
706
|
+
const l = y - n.iat, O = typeof p == "number" ? p : k(p);
|
|
707
|
+
if (l - f > O)
|
|
706
708
|
throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
|
|
707
709
|
if (l < 0 - f)
|
|
708
710
|
throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
|
|
709
711
|
}
|
|
710
712
|
return n;
|
|
711
713
|
};
|
|
712
|
-
async function
|
|
714
|
+
async function $e(e, t, r) {
|
|
713
715
|
var o;
|
|
714
716
|
const n = await xe(e, t, r);
|
|
715
717
|
if ((o = n.protectedHeader.crit) != null && o.includes("b64") && n.protectedHeader.b64 === !1)
|
|
@@ -717,8 +719,8 @@ async function Ne(e, t, r) {
|
|
|
717
719
|
const i = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
|
|
718
720
|
return typeof t == "function" ? { ...i, key: n.key } : i;
|
|
719
721
|
}
|
|
720
|
-
const
|
|
721
|
-
function
|
|
722
|
+
const Le = b;
|
|
723
|
+
function Me(e) {
|
|
722
724
|
if (typeof e != "string")
|
|
723
725
|
throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
|
|
724
726
|
const { 1: t, length: r } = e.split(".");
|
|
@@ -730,7 +732,7 @@ function Le(e) {
|
|
|
730
732
|
throw new S("JWTs must contain a payload");
|
|
731
733
|
let n;
|
|
732
734
|
try {
|
|
733
|
-
n =
|
|
735
|
+
n = Le(t);
|
|
734
736
|
} catch {
|
|
735
737
|
throw new S("Failed to base64url decode the payload");
|
|
736
738
|
}
|
|
@@ -744,46 +746,46 @@ function Le(e) {
|
|
|
744
746
|
throw new S("Invalid JWT Claims Set");
|
|
745
747
|
return a;
|
|
746
748
|
}
|
|
747
|
-
const
|
|
749
|
+
const rt = async (e) => {
|
|
748
750
|
try {
|
|
749
|
-
const t =
|
|
750
|
-
return await
|
|
751
|
-
issuer:
|
|
751
|
+
const t = U.ALG, n = await be(ae, t);
|
|
752
|
+
return await $e(e, n, {
|
|
753
|
+
issuer: U.ISSUER
|
|
752
754
|
});
|
|
753
755
|
} catch {
|
|
754
756
|
return;
|
|
755
757
|
}
|
|
756
|
-
},
|
|
758
|
+
}, nt = (e) => {
|
|
757
759
|
try {
|
|
758
|
-
return
|
|
760
|
+
return Me(e);
|
|
759
761
|
} catch {
|
|
760
762
|
return;
|
|
761
763
|
}
|
|
762
764
|
};
|
|
763
765
|
var d = [];
|
|
764
|
-
for (var
|
|
765
|
-
d.push((
|
|
766
|
-
function
|
|
766
|
+
for (var D = 0; D < 256; ++D)
|
|
767
|
+
d.push((D + 256).toString(16).slice(1));
|
|
768
|
+
function ke(e, t = 0) {
|
|
767
769
|
return (d[e[t + 0]] + d[e[t + 1]] + d[e[t + 2]] + d[e[t + 3]] + "-" + d[e[t + 4]] + d[e[t + 5]] + "-" + d[e[t + 6]] + d[e[t + 7]] + "-" + d[e[t + 8]] + d[e[t + 9]] + "-" + d[e[t + 10]] + d[e[t + 11]] + d[e[t + 12]] + d[e[t + 13]] + d[e[t + 14]] + d[e[t + 15]]).toLowerCase();
|
|
768
770
|
}
|
|
769
|
-
var _,
|
|
770
|
-
function
|
|
771
|
+
var _, Be = new Uint8Array(16);
|
|
772
|
+
function Fe() {
|
|
771
773
|
if (!_ && (_ = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !_))
|
|
772
774
|
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
773
|
-
return _(
|
|
775
|
+
return _(Be);
|
|
774
776
|
}
|
|
775
|
-
var
|
|
776
|
-
const
|
|
777
|
-
randomUUID:
|
|
777
|
+
var Ve = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
778
|
+
const F = {
|
|
779
|
+
randomUUID: Ve
|
|
778
780
|
};
|
|
779
|
-
function
|
|
780
|
-
if (
|
|
781
|
-
return
|
|
781
|
+
function V(e, t, r) {
|
|
782
|
+
if (F.randomUUID && !t && !e)
|
|
783
|
+
return F.randomUUID();
|
|
782
784
|
e = e || {};
|
|
783
|
-
var n = e.random || (e.rng ||
|
|
784
|
-
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128,
|
|
785
|
+
var n = e.random || (e.rng || Fe)();
|
|
786
|
+
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, ke(n);
|
|
785
787
|
}
|
|
786
|
-
const Y = globalThis.crypto,
|
|
788
|
+
const Y = globalThis.crypto, Ye = (e) => `${V()}${V()}`.slice(0, e), qe = (e) => btoa(
|
|
787
789
|
[...new Uint8Array(e)].map((t) => String.fromCharCode(t)).join("")
|
|
788
790
|
);
|
|
789
791
|
async function re(e) {
|
|
@@ -792,49 +794,54 @@ async function re(e) {
|
|
|
792
794
|
"crypto.subtle is available only in secure contexts (HTTPS)."
|
|
793
795
|
);
|
|
794
796
|
const t = new TextEncoder().encode(e), r = await Y.subtle.digest("SHA-256", t);
|
|
795
|
-
return
|
|
797
|
+
return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
796
798
|
}
|
|
797
|
-
async function
|
|
799
|
+
async function at(e) {
|
|
798
800
|
const t = e || 43;
|
|
799
801
|
if (t < 43 || t > 128)
|
|
800
802
|
throw `Expected a length between 43 and 128. Received ${e}.`;
|
|
801
|
-
const r =
|
|
803
|
+
const r = Ye(t), n = await re(r);
|
|
802
804
|
return {
|
|
803
805
|
code_verifier: r,
|
|
804
806
|
code_challenge: n
|
|
805
807
|
};
|
|
806
808
|
}
|
|
807
|
-
async function
|
|
809
|
+
async function ot(e, t) {
|
|
808
810
|
return t === await re(e);
|
|
809
811
|
}
|
|
810
|
-
const
|
|
812
|
+
const ze = /^Bearer (.+)$/i, Ge = (e) => {
|
|
811
813
|
if (typeof e.authorization != "string")
|
|
812
814
|
return;
|
|
813
|
-
const t = e.authorization.match(
|
|
815
|
+
const t = e.authorization.match(ze);
|
|
814
816
|
if (t)
|
|
815
817
|
return t[1];
|
|
816
|
-
},
|
|
818
|
+
}, Qe = (e, t) => {
|
|
817
819
|
const r = e.cookie, n = new RegExp(`auth.${t}=(.+?)(?:;|$)`);
|
|
818
820
|
if (typeof r != "string")
|
|
819
821
|
return;
|
|
820
822
|
const a = r.match(n);
|
|
821
823
|
if (a)
|
|
822
824
|
return a[1];
|
|
823
|
-
},
|
|
824
|
-
const
|
|
825
|
-
|
|
825
|
+
}, Xe = (e) => {
|
|
826
|
+
const t = e == null ? void 0 : e[ne.ACCESS_TOKEN];
|
|
827
|
+
if (typeof t == "string")
|
|
828
|
+
return t;
|
|
829
|
+
}, it = ({ headers: e, body: t, clientId: r }) => {
|
|
830
|
+
const n = Ge(e), a = Qe(e, r);
|
|
831
|
+
return Xe(t) || a || n || "";
|
|
826
832
|
};
|
|
827
833
|
export {
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
834
|
+
tt as API_TYPE,
|
|
835
|
+
Ze as AUTH_TYPES,
|
|
836
|
+
ne as BODY,
|
|
837
|
+
je as HEADERS,
|
|
838
|
+
U as JWT,
|
|
839
|
+
ae as JWT_PUBLIC_KEY,
|
|
840
|
+
et as TOKEN_EXPIRATION,
|
|
841
|
+
nt as decodeToken,
|
|
835
842
|
re as generateCodeChallenge,
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
843
|
+
it as getToken,
|
|
844
|
+
at as pkceChallengePair,
|
|
845
|
+
rt as verifyAndExtractToken,
|
|
846
|
+
ot as verifyChallenge
|
|
840
847
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/auth-common",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -36,5 +36,5 @@
|
|
|
36
36
|
"jose": "5.6.3",
|
|
37
37
|
"uuid": "10.0.0"
|
|
38
38
|
},
|
|
39
|
-
"gitHead": "
|
|
39
|
+
"gitHead": "ce92ce0065a9faf54d5c498ff63a195b249043af"
|
|
40
40
|
}
|