@versini/auth-common 2.10.0 → 2.11.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 +16 -1
- package/dist/index.js +120 -101
- package/package.json +3 -3
package/dist/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ declare const JWT: {
|
|
|
15
15
|
USER_ID_KEY: string;
|
|
16
16
|
TOKEN_ID_KEY: string;
|
|
17
17
|
NONCE_KEY: string;
|
|
18
|
+
USERNAME_KEY: string;
|
|
18
19
|
ISSUER: string;
|
|
19
20
|
};
|
|
20
21
|
declare const JWT_PUBLIC_KEY = "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7\nw5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5\ni1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle\naMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+\nl0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE\nsjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81\nawIDAQAB\n-----END PUBLIC KEY-----";
|
|
@@ -58,4 +59,18 @@ declare function pkceChallengePair(length?: number): Promise<{
|
|
|
58
59
|
*/
|
|
59
60
|
declare function verifyChallenge(code_verifier: string, expectedChallenge: string): Promise<boolean>;
|
|
60
61
|
|
|
61
|
-
|
|
62
|
+
type HeadersLike = Record<string, unknown> & {
|
|
63
|
+
authorization?: string;
|
|
64
|
+
"content-type"?: string;
|
|
65
|
+
cookie?: string;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* Get a Bearer Token from a request.
|
|
69
|
+
*
|
|
70
|
+
* @param headers An object containing the request headers, usually `req.headers`.
|
|
71
|
+
* @param clientId The client ID to use.
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
declare const getToken: (headers: HeadersLike, clientId: string) => string;
|
|
75
|
+
|
|
76
|
+
export { API_TYPE, AUTH_TYPES, HEADERS, JWT, JWT_PUBLIC_KEY, TOKEN_EXPIRATION, decodeToken, generateCodeChallenge, getToken, pkceChallengePair, verifyAndExtractToken, verifyChallenge };
|
package/dist/index.js
CHANGED
|
@@ -1,29 +1,30 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/auth-common v2.
|
|
2
|
+
@versini/auth-common v2.11.0
|
|
3
3
|
© 2024 gizmette.com
|
|
4
4
|
*/
|
|
5
5
|
try {
|
|
6
6
|
window.__VERSINI_AUTH_COMMON__ || (window.__VERSINI_AUTH_COMMON__ = {
|
|
7
|
-
version: "2.
|
|
8
|
-
buildTime: "
|
|
7
|
+
version: "2.11.0",
|
|
8
|
+
buildTime: "07/07/2024 10:25 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 Qe = {
|
|
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
|
+
}, Xe = {
|
|
21
21
|
CLIENT_ID: "X-Auth-ClientId"
|
|
22
22
|
}, N = {
|
|
23
23
|
ALG: "RS256",
|
|
24
24
|
USER_ID_KEY: "sub",
|
|
25
25
|
TOKEN_ID_KEY: "__raw",
|
|
26
26
|
NONCE_KEY: "_nonce",
|
|
27
|
+
USERNAME_KEY: "username",
|
|
27
28
|
ISSUER: "gizmette.com"
|
|
28
29
|
}, ne = `-----BEGIN PUBLIC KEY-----
|
|
29
30
|
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
|
|
@@ -33,15 +34,15 @@ aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
|
|
|
33
34
|
l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
|
|
34
35
|
sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
|
|
35
36
|
awIDAQAB
|
|
36
|
-
-----END PUBLIC KEY-----`,
|
|
37
|
+
-----END PUBLIC KEY-----`, Ze = {
|
|
37
38
|
ACCESS: "5m",
|
|
38
39
|
ID: "90d",
|
|
39
40
|
REFRESH: "90d"
|
|
40
|
-
},
|
|
41
|
+
}, je = {
|
|
41
42
|
AUTHENTICATE: "authenticate",
|
|
42
43
|
CODE: "code",
|
|
43
44
|
LOGOUT: "logout"
|
|
44
|
-
}, x = crypto,
|
|
45
|
+
}, x = crypto, z = (e) => e instanceof CryptoKey, v = new TextEncoder(), C = new TextDecoder();
|
|
45
46
|
function ae(...e) {
|
|
46
47
|
const t = e.reduce((a, { length: i }) => a + i, 0), r = new Uint8Array(t);
|
|
47
48
|
let n = 0;
|
|
@@ -54,7 +55,7 @@ const oe = (e) => {
|
|
|
54
55
|
for (let n = 0; n < t.length; n++)
|
|
55
56
|
r[n] = t.charCodeAt(n);
|
|
56
57
|
return r;
|
|
57
|
-
},
|
|
58
|
+
}, b = (e) => {
|
|
58
59
|
let t = e;
|
|
59
60
|
t instanceof Uint8Array && (t = C.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
60
61
|
try {
|
|
@@ -63,7 +64,7 @@ const oe = (e) => {
|
|
|
63
64
|
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
64
65
|
}
|
|
65
66
|
};
|
|
66
|
-
class
|
|
67
|
+
class A extends Error {
|
|
67
68
|
static get code() {
|
|
68
69
|
return "ERR_JOSE_GENERIC";
|
|
69
70
|
}
|
|
@@ -72,7 +73,7 @@ class b extends Error {
|
|
|
72
73
|
super(t), this.code = "ERR_JOSE_GENERIC", this.name = this.constructor.name, (r = Error.captureStackTrace) == null || r.call(Error, this, this.constructor);
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
|
-
class
|
|
76
|
+
class h extends A {
|
|
76
77
|
static get code() {
|
|
77
78
|
return "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
78
79
|
}
|
|
@@ -80,7 +81,7 @@ class f extends b {
|
|
|
80
81
|
super(t), this.code = "ERR_JWT_CLAIM_VALIDATION_FAILED", this.claim = n, this.reason = a, this.payload = r;
|
|
81
82
|
}
|
|
82
83
|
}
|
|
83
|
-
class $ extends
|
|
84
|
+
class $ extends A {
|
|
84
85
|
static get code() {
|
|
85
86
|
return "ERR_JWT_EXPIRED";
|
|
86
87
|
}
|
|
@@ -88,7 +89,7 @@ class $ extends b {
|
|
|
88
89
|
super(t), this.code = "ERR_JWT_EXPIRED", this.claim = n, this.reason = a, this.payload = r;
|
|
89
90
|
}
|
|
90
91
|
}
|
|
91
|
-
class ie extends
|
|
92
|
+
class ie extends A {
|
|
92
93
|
constructor() {
|
|
93
94
|
super(...arguments), this.code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
94
95
|
}
|
|
@@ -96,7 +97,7 @@ class ie extends b {
|
|
|
96
97
|
return "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
97
98
|
}
|
|
98
99
|
}
|
|
99
|
-
class w extends
|
|
100
|
+
class w extends A {
|
|
100
101
|
constructor() {
|
|
101
102
|
super(...arguments), this.code = "ERR_JOSE_NOT_SUPPORTED";
|
|
102
103
|
}
|
|
@@ -104,7 +105,7 @@ class w extends b {
|
|
|
104
105
|
return "ERR_JOSE_NOT_SUPPORTED";
|
|
105
106
|
}
|
|
106
107
|
}
|
|
107
|
-
class u extends
|
|
108
|
+
class u extends A {
|
|
108
109
|
constructor() {
|
|
109
110
|
super(...arguments), this.code = "ERR_JWS_INVALID";
|
|
110
111
|
}
|
|
@@ -112,7 +113,7 @@ class u extends b {
|
|
|
112
113
|
return "ERR_JWS_INVALID";
|
|
113
114
|
}
|
|
114
115
|
}
|
|
115
|
-
class S extends
|
|
116
|
+
class S extends A {
|
|
116
117
|
constructor() {
|
|
117
118
|
super(...arguments), this.code = "ERR_JWT_INVALID";
|
|
118
119
|
}
|
|
@@ -120,7 +121,7 @@ class S extends b {
|
|
|
120
121
|
return "ERR_JWT_INVALID";
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
|
-
class ce extends
|
|
124
|
+
class ce extends A {
|
|
124
125
|
constructor() {
|
|
125
126
|
super(...arguments), this.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED", this.message = "signature verification failed";
|
|
126
127
|
}
|
|
@@ -134,7 +135,7 @@ function m(e, t = "algorithm.name") {
|
|
|
134
135
|
function T(e, t) {
|
|
135
136
|
return e.name === t;
|
|
136
137
|
}
|
|
137
|
-
function
|
|
138
|
+
function O(e) {
|
|
138
139
|
return parseInt(e.name.slice(4), 10);
|
|
139
140
|
}
|
|
140
141
|
function se(e) {
|
|
@@ -168,7 +169,7 @@ function ue(e, t, ...r) {
|
|
|
168
169
|
if (!T(e.algorithm, "HMAC"))
|
|
169
170
|
throw m("HMAC");
|
|
170
171
|
const n = parseInt(t.slice(2), 10);
|
|
171
|
-
if (
|
|
172
|
+
if (O(e.algorithm.hash) !== n)
|
|
172
173
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
173
174
|
break;
|
|
174
175
|
}
|
|
@@ -178,7 +179,7 @@ function ue(e, t, ...r) {
|
|
|
178
179
|
if (!T(e.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
179
180
|
throw m("RSASSA-PKCS1-v1_5");
|
|
180
181
|
const n = parseInt(t.slice(2), 10);
|
|
181
|
-
if (
|
|
182
|
+
if (O(e.algorithm.hash) !== n)
|
|
182
183
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
183
184
|
break;
|
|
184
185
|
}
|
|
@@ -188,7 +189,7 @@ function ue(e, t, ...r) {
|
|
|
188
189
|
if (!T(e.algorithm, "RSA-PSS"))
|
|
189
190
|
throw m("RSA-PSS");
|
|
190
191
|
const n = parseInt(t.slice(2), 10);
|
|
191
|
-
if (
|
|
192
|
+
if (O(e.algorithm.hash) !== n)
|
|
192
193
|
throw m(`SHA-${n}`, "algorithm.hash");
|
|
193
194
|
break;
|
|
194
195
|
}
|
|
@@ -212,7 +213,7 @@ function ue(e, t, ...r) {
|
|
|
212
213
|
}
|
|
213
214
|
de(e, r);
|
|
214
215
|
}
|
|
215
|
-
function
|
|
216
|
+
function G(e, t, ...r) {
|
|
216
217
|
var n;
|
|
217
218
|
if (r.length > 2) {
|
|
218
219
|
const a = r.pop();
|
|
@@ -221,11 +222,11 @@ function z(e, t, ...r) {
|
|
|
221
222
|
r.length === 2 ? e += `one of type ${r[0]} or ${r[1]}.` : e += `of type ${r[0]}.`;
|
|
222
223
|
return t == null ? e += ` Received ${t}` : typeof t == "function" && t.name ? e += ` Received function ${t.name}` : typeof t == "object" && t != null && (n = t.constructor) != null && n.name && (e += ` Received an instance of ${t.constructor.name}`), e;
|
|
223
224
|
}
|
|
224
|
-
const L = (e, ...t) =>
|
|
225
|
+
const L = (e, ...t) => G("Key must be ", e, ...t);
|
|
225
226
|
function Y(e, t, ...r) {
|
|
226
|
-
return
|
|
227
|
+
return G(`Key for the ${e} algorithm must be `, t, ...r);
|
|
227
228
|
}
|
|
228
|
-
const Q = (e) =>
|
|
229
|
+
const Q = (e) => z(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", _ = ["CryptoKey"], le = (...e) => {
|
|
229
230
|
const t = e.filter(Boolean);
|
|
230
231
|
if (t.length === 0 || t.length === 1)
|
|
231
232
|
return !0;
|
|
@@ -244,11 +245,11 @@ const Q = (e) => G(e) ? !0 : (e == null ? void 0 : e[Symbol.toStringTag]) === "K
|
|
|
244
245
|
}
|
|
245
246
|
return !0;
|
|
246
247
|
};
|
|
247
|
-
function
|
|
248
|
+
function fe(e) {
|
|
248
249
|
return typeof e == "object" && e !== null;
|
|
249
250
|
}
|
|
250
|
-
function
|
|
251
|
-
if (!
|
|
251
|
+
function P(e) {
|
|
252
|
+
if (!fe(e) || Object.prototype.toString.call(e) !== "[object Object]")
|
|
252
253
|
return !1;
|
|
253
254
|
if (Object.getPrototypeOf(e) === null)
|
|
254
255
|
return !0;
|
|
@@ -257,7 +258,7 @@ function _(e) {
|
|
|
257
258
|
t = Object.getPrototypeOf(t);
|
|
258
259
|
return Object.getPrototypeOf(e) === t;
|
|
259
260
|
}
|
|
260
|
-
const
|
|
261
|
+
const he = (e, t) => {
|
|
261
262
|
if (e.startsWith("RS") || e.startsWith("PS")) {
|
|
262
263
|
const { modulusLength: r } = t.algorithm;
|
|
263
264
|
if (typeof r != "number" || r < 2048)
|
|
@@ -345,7 +346,7 @@ const me = async (e) => {
|
|
|
345
346
|
e.key_ops ?? r
|
|
346
347
|
], a = { ...e };
|
|
347
348
|
return delete a.alg, delete a.use, x.subtle.importKey("jwk", a, ...n);
|
|
348
|
-
}, X = (e) =>
|
|
349
|
+
}, X = (e) => b(e);
|
|
349
350
|
let W, J;
|
|
350
351
|
const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j = async (e, t, r, n) => {
|
|
351
352
|
let a = e.get(t);
|
|
@@ -439,29 +440,29 @@ const Z = (e) => (e == null ? void 0 : e[Symbol.toStringTag]) === "KeyObject", j
|
|
|
439
440
|
}
|
|
440
441
|
return x.subtle.importKey(t, c, i, !1, o);
|
|
441
442
|
}, ge = (e, t, r) => we(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
|
|
442
|
-
async function
|
|
443
|
+
async function Ae(e, t, r) {
|
|
443
444
|
if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
|
|
444
445
|
throw new TypeError('"spki" must be SPKI formatted string');
|
|
445
446
|
return ge(e, t);
|
|
446
447
|
}
|
|
447
|
-
const
|
|
448
|
+
const R = (e) => e == null ? void 0 : e[Symbol.toStringTag], be = (e, t) => {
|
|
448
449
|
if (!(t instanceof Uint8Array)) {
|
|
449
450
|
if (!Q(t))
|
|
450
|
-
throw new TypeError(Y(e, t, ...
|
|
451
|
+
throw new TypeError(Y(e, t, ..._, "Uint8Array"));
|
|
451
452
|
if (t.type !== "secret")
|
|
452
|
-
throw new TypeError(`${
|
|
453
|
+
throw new TypeError(`${R(t)} instances for symmetric algorithms must be of type "secret"`);
|
|
453
454
|
}
|
|
454
455
|
}, Ce = (e, t, r) => {
|
|
455
456
|
if (!Q(t))
|
|
456
|
-
throw new TypeError(Y(e, t, ...
|
|
457
|
+
throw new TypeError(Y(e, t, ..._));
|
|
457
458
|
if (t.type === "secret")
|
|
458
|
-
throw new TypeError(`${
|
|
459
|
+
throw new TypeError(`${R(t)} instances for asymmetric algorithms must not be of type "secret"`);
|
|
459
460
|
if (t.algorithm && r === "verify" && t.type === "private")
|
|
460
|
-
throw new TypeError(`${
|
|
461
|
+
throw new TypeError(`${R(t)} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
461
462
|
if (t.algorithm && r === "encrypt" && t.type === "private")
|
|
462
|
-
throw new TypeError(`${
|
|
463
|
+
throw new TypeError(`${R(t)} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
463
464
|
}, ve = (e, t, r) => {
|
|
464
|
-
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ?
|
|
465
|
+
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? be(e, t) : Ce(e, t, r);
|
|
465
466
|
};
|
|
466
467
|
function Te(e, t, r, n, a) {
|
|
467
468
|
if (a.crit !== void 0 && (n == null ? void 0 : n.crit) === void 0)
|
|
@@ -488,7 +489,7 @@ const Ie = (e, t) => {
|
|
|
488
489
|
if (t)
|
|
489
490
|
return new Set(t);
|
|
490
491
|
};
|
|
491
|
-
function
|
|
492
|
+
function Re(e, t) {
|
|
492
493
|
const r = `SHA-${e.slice(-3)}`;
|
|
493
494
|
switch (e) {
|
|
494
495
|
case "HS256":
|
|
@@ -513,20 +514,20 @@ function Pe(e, t) {
|
|
|
513
514
|
throw new w(`alg ${e} is not supported either by JOSE or your javascript runtime`);
|
|
514
515
|
}
|
|
515
516
|
}
|
|
516
|
-
async function
|
|
517
|
-
if (t = await Ee.normalizePublicKey(t, e),
|
|
517
|
+
async function _e(e, t, r) {
|
|
518
|
+
if (t = await Ee.normalizePublicKey(t, e), z(t))
|
|
518
519
|
return ue(t, e, r), t;
|
|
519
520
|
if (t instanceof Uint8Array) {
|
|
520
521
|
if (!e.startsWith("HS"))
|
|
521
|
-
throw new TypeError(L(t, ...
|
|
522
|
+
throw new TypeError(L(t, ..._));
|
|
522
523
|
return x.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [r]);
|
|
523
524
|
}
|
|
524
|
-
throw new TypeError(L(t, ...
|
|
525
|
+
throw new TypeError(L(t, ..._, "Uint8Array"));
|
|
525
526
|
}
|
|
526
|
-
const
|
|
527
|
-
const a = await
|
|
528
|
-
|
|
529
|
-
const i =
|
|
527
|
+
const Pe = async (e, t, r, n) => {
|
|
528
|
+
const a = await _e(e, t, "verify");
|
|
529
|
+
he(e, a);
|
|
530
|
+
const i = Re(e, a.algorithm);
|
|
530
531
|
try {
|
|
531
532
|
return await x.subtle.verify(i, a, r, n);
|
|
532
533
|
} catch {
|
|
@@ -534,7 +535,7 @@ const _e = async (e, t, r, n) => {
|
|
|
534
535
|
}
|
|
535
536
|
};
|
|
536
537
|
async function xe(e, t, r) {
|
|
537
|
-
if (!
|
|
538
|
+
if (!P(e))
|
|
538
539
|
throw new u("Flattened JWS must be an object");
|
|
539
540
|
if (e.protected === void 0 && e.header === void 0)
|
|
540
541
|
throw new u('Flattened JWS must have either of the "protected" or "header" members');
|
|
@@ -544,13 +545,13 @@ async function xe(e, t, r) {
|
|
|
544
545
|
throw new u("JWS Payload missing");
|
|
545
546
|
if (typeof e.signature != "string")
|
|
546
547
|
throw new u("JWS Signature missing or incorrect type");
|
|
547
|
-
if (e.header !== void 0 && !
|
|
548
|
+
if (e.header !== void 0 && !P(e.header))
|
|
548
549
|
throw new u("JWS Unprotected Header incorrect type");
|
|
549
550
|
let n = {};
|
|
550
551
|
if (e.protected)
|
|
551
552
|
try {
|
|
552
|
-
const
|
|
553
|
-
n = JSON.parse(C.decode(
|
|
553
|
+
const K = b(e.protected);
|
|
554
|
+
n = JSON.parse(C.decode(K));
|
|
554
555
|
} catch {
|
|
555
556
|
throw new u("JWS Protected Header is invalid");
|
|
556
557
|
}
|
|
@@ -577,18 +578,18 @@ async function xe(e, t, r) {
|
|
|
577
578
|
let p = !1;
|
|
578
579
|
typeof t == "function" && (t = await t(n, e), p = !0), ve(c, t, "verify");
|
|
579
580
|
const g = ae(v.encode(e.protected ?? ""), v.encode("."), typeof e.payload == "string" ? v.encode(e.payload) : e.payload);
|
|
580
|
-
let
|
|
581
|
+
let f;
|
|
581
582
|
try {
|
|
582
|
-
|
|
583
|
+
f = b(e.signature);
|
|
583
584
|
} catch {
|
|
584
585
|
throw new u("Failed to base64url decode the signature");
|
|
585
586
|
}
|
|
586
|
-
if (!await
|
|
587
|
+
if (!await Pe(c, t, f, g))
|
|
587
588
|
throw new ce();
|
|
588
589
|
let y;
|
|
589
590
|
if (o)
|
|
590
591
|
try {
|
|
591
|
-
y =
|
|
592
|
+
y = b(e.payload);
|
|
592
593
|
} catch {
|
|
593
594
|
throw new u("Failed to base64url decode the payload");
|
|
594
595
|
}
|
|
@@ -597,7 +598,7 @@ async function xe(e, t, r) {
|
|
|
597
598
|
const l = { payload: y };
|
|
598
599
|
return e.protected !== void 0 && (l.protectedHeader = n), e.header !== void 0 && (l.unprotectedHeader = e.header), p ? { ...l, key: t } : l;
|
|
599
600
|
}
|
|
600
|
-
async function
|
|
601
|
+
async function Ke(e, t, r) {
|
|
601
602
|
if (e instanceof Uint8Array && (e = C.decode(e)), typeof e != "string")
|
|
602
603
|
throw new u("Compact JWS must be a string or Uint8Array");
|
|
603
604
|
const { 0: n, 1: a, 2: i, length: o } = e.split(".");
|
|
@@ -606,7 +607,7 @@ async function Oe(e, t, r) {
|
|
|
606
607
|
const c = await xe({ payload: a, protected: n, signature: i }, t, r), s = { payload: c.payload, protectedHeader: c.protectedHeader };
|
|
607
608
|
return typeof t == "function" ? { ...s, key: c.key } : s;
|
|
608
609
|
}
|
|
609
|
-
const
|
|
610
|
+
const Oe = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te * 24, We = H * 7, Je = H * 365.25, De = /^(\+|\-)? ?(\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
611
|
const t = De.exec(e);
|
|
611
612
|
if (!t || t[4] && t[1])
|
|
612
613
|
throw new TypeError("Invalid time period format");
|
|
@@ -649,75 +650,75 @@ const Ke = (e) => Math.floor(e.getTime() / 1e3), ee = 60, te = ee * 60, H = te *
|
|
|
649
650
|
break;
|
|
650
651
|
}
|
|
651
652
|
return t[1] === "-" || t[4] === "ago" ? -a : a;
|
|
652
|
-
},
|
|
653
|
+
}, V = (e) => e.toLowerCase().replace(/^application\//, ""), He = (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
654
|
let n;
|
|
654
655
|
try {
|
|
655
656
|
n = JSON.parse(C.decode(t));
|
|
656
657
|
} catch {
|
|
657
658
|
}
|
|
658
|
-
if (!
|
|
659
|
+
if (!P(n))
|
|
659
660
|
throw new S("JWT Claims Set must be a top-level JSON object");
|
|
660
661
|
const { typ: a } = r;
|
|
661
|
-
if (a && (typeof e.typ != "string" ||
|
|
662
|
-
throw new
|
|
662
|
+
if (a && (typeof e.typ != "string" || V(e.typ) !== V(a)))
|
|
663
|
+
throw new h('unexpected "typ" JWT header value', n, "typ", "check_failed");
|
|
663
664
|
const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: p } = r, g = [...i];
|
|
664
665
|
p !== void 0 && g.push("iat"), s !== void 0 && g.push("aud"), c !== void 0 && g.push("sub"), o !== void 0 && g.push("iss");
|
|
665
666
|
for (const l of new Set(g.reverse()))
|
|
666
667
|
if (!(l in n))
|
|
667
|
-
throw new
|
|
668
|
+
throw new h(`missing required "${l}" claim`, n, l, "missing");
|
|
668
669
|
if (o && !(Array.isArray(o) ? o : [o]).includes(n.iss))
|
|
669
|
-
throw new
|
|
670
|
+
throw new h('unexpected "iss" claim value', n, "iss", "check_failed");
|
|
670
671
|
if (c && n.sub !== c)
|
|
671
|
-
throw new
|
|
672
|
+
throw new h('unexpected "sub" claim value', n, "sub", "check_failed");
|
|
672
673
|
if (s && !He(n.aud, typeof s == "string" ? [s] : s))
|
|
673
|
-
throw new
|
|
674
|
-
let
|
|
674
|
+
throw new h('unexpected "aud" claim value', n, "aud", "check_failed");
|
|
675
|
+
let f;
|
|
675
676
|
switch (typeof r.clockTolerance) {
|
|
676
677
|
case "string":
|
|
677
|
-
|
|
678
|
+
f = k(r.clockTolerance);
|
|
678
679
|
break;
|
|
679
680
|
case "number":
|
|
680
|
-
|
|
681
|
+
f = r.clockTolerance;
|
|
681
682
|
break;
|
|
682
683
|
case "undefined":
|
|
683
|
-
|
|
684
|
+
f = 0;
|
|
684
685
|
break;
|
|
685
686
|
default:
|
|
686
687
|
throw new TypeError("Invalid clockTolerance option type");
|
|
687
688
|
}
|
|
688
|
-
const { currentDate: U } = r, y =
|
|
689
|
+
const { currentDate: U } = r, y = Oe(U || /* @__PURE__ */ new Date());
|
|
689
690
|
if ((n.iat !== void 0 || p) && typeof n.iat != "number")
|
|
690
|
-
throw new
|
|
691
|
+
throw new h('"iat" claim must be a number', n, "iat", "invalid");
|
|
691
692
|
if (n.nbf !== void 0) {
|
|
692
693
|
if (typeof n.nbf != "number")
|
|
693
|
-
throw new
|
|
694
|
-
if (n.nbf > y +
|
|
695
|
-
throw new
|
|
694
|
+
throw new h('"nbf" claim must be a number', n, "nbf", "invalid");
|
|
695
|
+
if (n.nbf > y + f)
|
|
696
|
+
throw new h('"nbf" claim timestamp check failed', n, "nbf", "check_failed");
|
|
696
697
|
}
|
|
697
698
|
if (n.exp !== void 0) {
|
|
698
699
|
if (typeof n.exp != "number")
|
|
699
|
-
throw new
|
|
700
|
-
if (n.exp <= y -
|
|
700
|
+
throw new h('"exp" claim must be a number', n, "exp", "invalid");
|
|
701
|
+
if (n.exp <= y - f)
|
|
701
702
|
throw new $('"exp" claim timestamp check failed', n, "exp", "check_failed");
|
|
702
703
|
}
|
|
703
704
|
if (p) {
|
|
704
|
-
const l = y - n.iat,
|
|
705
|
-
if (l -
|
|
705
|
+
const l = y - n.iat, K = typeof p == "number" ? p : k(p);
|
|
706
|
+
if (l - f > K)
|
|
706
707
|
throw new $('"iat" claim timestamp check failed (too far in the past)', n, "iat", "check_failed");
|
|
707
|
-
if (l < 0 -
|
|
708
|
-
throw new
|
|
708
|
+
if (l < 0 - f)
|
|
709
|
+
throw new h('"iat" claim timestamp check failed (it should be in the past)', n, "iat", "check_failed");
|
|
709
710
|
}
|
|
710
711
|
return n;
|
|
711
712
|
};
|
|
712
713
|
async function Ne(e, t, r) {
|
|
713
714
|
var o;
|
|
714
|
-
const n = await
|
|
715
|
+
const n = await Ke(e, t, r);
|
|
715
716
|
if ((o = n.protectedHeader.crit) != null && o.includes("b64") && n.protectedHeader.b64 === !1)
|
|
716
717
|
throw new S("JWTs MUST NOT use unencoded payload");
|
|
717
718
|
const i = { payload: Ue(n.protectedHeader, n.payload, r), protectedHeader: n.protectedHeader };
|
|
718
719
|
return typeof t == "function" ? { ...i, key: n.key } : i;
|
|
719
720
|
}
|
|
720
|
-
const $e =
|
|
721
|
+
const $e = b;
|
|
721
722
|
function Le(e) {
|
|
722
723
|
if (typeof e != "string")
|
|
723
724
|
throw new S("JWTs must use Compact JWS serialization, JWT must be a string");
|
|
@@ -740,20 +741,20 @@ function Le(e) {
|
|
|
740
741
|
} catch {
|
|
741
742
|
throw new S("Failed to parse the decoded payload as JSON");
|
|
742
743
|
}
|
|
743
|
-
if (!
|
|
744
|
+
if (!P(a))
|
|
744
745
|
throw new S("Invalid JWT Claims Set");
|
|
745
746
|
return a;
|
|
746
747
|
}
|
|
747
|
-
const
|
|
748
|
+
const et = async (e) => {
|
|
748
749
|
try {
|
|
749
|
-
const t = N.ALG, n = await
|
|
750
|
+
const t = N.ALG, n = await Ae(ne, t);
|
|
750
751
|
return await Ne(e, n, {
|
|
751
752
|
issuer: N.ISSUER
|
|
752
753
|
});
|
|
753
754
|
} catch {
|
|
754
755
|
return;
|
|
755
756
|
}
|
|
756
|
-
},
|
|
757
|
+
}, tt = (e) => {
|
|
757
758
|
try {
|
|
758
759
|
return Le(e);
|
|
759
760
|
} catch {
|
|
@@ -766,11 +767,11 @@ for (var D = 0; D < 256; ++D)
|
|
|
766
767
|
function Me(e, t = 0) {
|
|
767
768
|
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
769
|
}
|
|
769
|
-
var I,
|
|
770
|
-
function
|
|
770
|
+
var I, ke = new Uint8Array(16);
|
|
771
|
+
function Ve() {
|
|
771
772
|
if (!I && (I = typeof crypto < "u" && crypto.getRandomValues && crypto.getRandomValues.bind(crypto), !I))
|
|
772
773
|
throw new Error("crypto.getRandomValues() not supported. See https://github.com/uuidjs/uuid#getrandomvalues-not-supported");
|
|
773
|
-
return I(
|
|
774
|
+
return I(ke);
|
|
774
775
|
}
|
|
775
776
|
var Be = typeof crypto < "u" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
776
777
|
const B = {
|
|
@@ -780,7 +781,7 @@ function F(e, t, r) {
|
|
|
780
781
|
if (B.randomUUID && !t && !e)
|
|
781
782
|
return B.randomUUID();
|
|
782
783
|
e = e || {};
|
|
783
|
-
var n = e.random || (e.rng ||
|
|
784
|
+
var n = e.random || (e.rng || Ve)();
|
|
784
785
|
return n[6] = n[6] & 15 | 64, n[8] = n[8] & 63 | 128, Me(n);
|
|
785
786
|
}
|
|
786
787
|
const q = globalThis.crypto, Fe = (e) => `${F()}${F()}`.slice(0, e), qe = (e) => btoa(
|
|
@@ -794,7 +795,7 @@ async function re(e) {
|
|
|
794
795
|
const t = new TextEncoder().encode(e), r = await q.subtle.digest("SHA-256", t);
|
|
795
796
|
return qe(r).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
796
797
|
}
|
|
797
|
-
async function
|
|
798
|
+
async function rt(e) {
|
|
798
799
|
const t = e || 43;
|
|
799
800
|
if (t < 43 || t > 128)
|
|
800
801
|
throw `Expected a length between 43 and 128. Received ${e}.`;
|
|
@@ -804,19 +805,37 @@ async function je(e) {
|
|
|
804
805
|
code_challenge: n
|
|
805
806
|
};
|
|
806
807
|
}
|
|
807
|
-
async function
|
|
808
|
+
async function nt(e, t) {
|
|
808
809
|
return t === await re(e);
|
|
809
810
|
}
|
|
811
|
+
const ze = /^Bearer (.+)$/i, Ge = (e) => {
|
|
812
|
+
if (typeof e.authorization != "string")
|
|
813
|
+
return;
|
|
814
|
+
const t = e.authorization.match(ze);
|
|
815
|
+
if (t)
|
|
816
|
+
return t[1];
|
|
817
|
+
}, Ye = (e, t) => {
|
|
818
|
+
const r = e.cookie, n = new RegExp(`auth.${t}=(.+?)(?:;|$)`);
|
|
819
|
+
if (typeof r != "string")
|
|
820
|
+
return;
|
|
821
|
+
const a = r.match(n);
|
|
822
|
+
if (a)
|
|
823
|
+
return a[1];
|
|
824
|
+
}, at = (e, t) => {
|
|
825
|
+
const r = Ge(e), n = Ye(e, t);
|
|
826
|
+
return !n && !r ? "" : n || r;
|
|
827
|
+
};
|
|
810
828
|
export {
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
829
|
+
je as API_TYPE,
|
|
830
|
+
Qe as AUTH_TYPES,
|
|
831
|
+
Xe as HEADERS,
|
|
814
832
|
N as JWT,
|
|
815
833
|
ne as JWT_PUBLIC_KEY,
|
|
816
|
-
|
|
817
|
-
|
|
834
|
+
Ze as TOKEN_EXPIRATION,
|
|
835
|
+
tt as decodeToken,
|
|
818
836
|
re as generateCodeChallenge,
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
et as
|
|
837
|
+
at as getToken,
|
|
838
|
+
rt as pkceChallengePair,
|
|
839
|
+
et as verifyAndExtractToken,
|
|
840
|
+
nt as verifyChallenge
|
|
822
841
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/auth-common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.11.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"test": "vitest run"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"jose": "5.6.
|
|
35
|
+
"jose": "5.6.3",
|
|
36
36
|
"uuid": "10.0.0"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "e6e92b161c834eba2f655e9e6b80bdd7b5cb2316"
|
|
39
39
|
}
|