@versini/auth-common 2.1.0 → 2.2.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 +5 -1
- package/dist/index.js +603 -9
- package/package.json +5 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import * as jose from 'jose';
|
|
2
|
+
|
|
1
3
|
declare const AUTH_TYPES: {
|
|
2
4
|
ID_TOKEN: string;
|
|
3
5
|
};
|
|
@@ -9,5 +11,7 @@ declare const JWT: {
|
|
|
9
11
|
USER_ID_KEY: string;
|
|
10
12
|
ISSUER: string;
|
|
11
13
|
};
|
|
14
|
+
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-----";
|
|
15
|
+
declare const verifyAndExtractToken: (token: string, audience: string) => Promise<jose.JWTVerifyResult<jose.JWTPayload> | undefined>;
|
|
12
16
|
|
|
13
|
-
export { AUTH_TYPES, HEADERS, JWT };
|
|
17
|
+
export { AUTH_TYPES, HEADERS, JWT, JWT_PUBLIC_KEY, verifyAndExtractToken };
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,621 @@
|
|
|
1
1
|
/*!
|
|
2
|
-
@versini/auth-common v2.
|
|
2
|
+
@versini/auth-common v2.2.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: "06/
|
|
7
|
+
version: "2.2.0",
|
|
8
|
+
buildTime: "06/24/2024 05:23 PM 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 P = crypto, D = (e) => e instanceof CryptoKey, b = new TextEncoder(), g = new TextDecoder();
|
|
15
|
+
function V(...e) {
|
|
16
|
+
const t = e.reduce((a, { length: i }) => a + i, 0), n = new Uint8Array(t);
|
|
17
|
+
let r = 0;
|
|
18
|
+
for (const a of e)
|
|
19
|
+
n.set(a, r), r += a.length;
|
|
20
|
+
return n;
|
|
21
|
+
}
|
|
22
|
+
const F = (e) => {
|
|
23
|
+
const t = atob(e), n = new Uint8Array(t.length);
|
|
24
|
+
for (let r = 0; r < t.length; r++)
|
|
25
|
+
n[r] = t.charCodeAt(r);
|
|
26
|
+
return n;
|
|
27
|
+
}, R = (e) => {
|
|
28
|
+
let t = e;
|
|
29
|
+
t instanceof Uint8Array && (t = g.decode(t)), t = t.replace(/-/g, "+").replace(/_/g, "/").replace(/\s/g, "");
|
|
30
|
+
try {
|
|
31
|
+
return F(t);
|
|
32
|
+
} catch {
|
|
33
|
+
throw new TypeError("The input to be decoded is not correctly encoded.");
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
class E extends Error {
|
|
37
|
+
static get code() {
|
|
38
|
+
return "ERR_JOSE_GENERIC";
|
|
39
|
+
}
|
|
40
|
+
constructor(t) {
|
|
41
|
+
var n;
|
|
42
|
+
super(t), this.code = "ERR_JOSE_GENERIC", this.name = this.constructor.name, (n = Error.captureStackTrace) == null || n.call(Error, this, this.constructor);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class h extends E {
|
|
46
|
+
static get code() {
|
|
47
|
+
return "ERR_JWT_CLAIM_VALIDATION_FAILED";
|
|
48
|
+
}
|
|
49
|
+
constructor(t, n, r = "unspecified", a = "unspecified") {
|
|
50
|
+
super(t), this.code = "ERR_JWT_CLAIM_VALIDATION_FAILED", this.claim = r, this.reason = a, this.payload = n;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
class O extends E {
|
|
54
|
+
static get code() {
|
|
55
|
+
return "ERR_JWT_EXPIRED";
|
|
56
|
+
}
|
|
57
|
+
constructor(t, n, r = "unspecified", a = "unspecified") {
|
|
58
|
+
super(t), this.code = "ERR_JWT_EXPIRED", this.claim = r, this.reason = a, this.payload = n;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
class G extends E {
|
|
62
|
+
constructor() {
|
|
63
|
+
super(...arguments), this.code = "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
64
|
+
}
|
|
65
|
+
static get code() {
|
|
66
|
+
return "ERR_JOSE_ALG_NOT_ALLOWED";
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
class I extends E {
|
|
70
|
+
constructor() {
|
|
71
|
+
super(...arguments), this.code = "ERR_JOSE_NOT_SUPPORTED";
|
|
72
|
+
}
|
|
73
|
+
static get code() {
|
|
74
|
+
return "ERR_JOSE_NOT_SUPPORTED";
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
class d extends E {
|
|
78
|
+
constructor() {
|
|
79
|
+
super(...arguments), this.code = "ERR_JWS_INVALID";
|
|
80
|
+
}
|
|
81
|
+
static get code() {
|
|
82
|
+
return "ERR_JWS_INVALID";
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
class L extends E {
|
|
86
|
+
constructor() {
|
|
87
|
+
super(...arguments), this.code = "ERR_JWT_INVALID";
|
|
88
|
+
}
|
|
89
|
+
static get code() {
|
|
90
|
+
return "ERR_JWT_INVALID";
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
class q extends E {
|
|
94
|
+
constructor() {
|
|
95
|
+
super(...arguments), this.code = "ERR_JWS_SIGNATURE_VERIFICATION_FAILED", this.message = "signature verification failed";
|
|
96
|
+
}
|
|
97
|
+
static get code() {
|
|
98
|
+
return "ERR_JWS_SIGNATURE_VERIFICATION_FAILED";
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function p(e, t = "algorithm.name") {
|
|
102
|
+
return new TypeError(`CryptoKey does not support this operation, its ${t} must be ${e}`);
|
|
103
|
+
}
|
|
104
|
+
function A(e, t) {
|
|
105
|
+
return e.name === t;
|
|
106
|
+
}
|
|
107
|
+
function T(e) {
|
|
108
|
+
return parseInt(e.name.slice(4), 10);
|
|
109
|
+
}
|
|
110
|
+
function Y(e) {
|
|
111
|
+
switch (e) {
|
|
112
|
+
case "ES256":
|
|
113
|
+
return "P-256";
|
|
114
|
+
case "ES384":
|
|
115
|
+
return "P-384";
|
|
116
|
+
case "ES512":
|
|
117
|
+
return "P-521";
|
|
118
|
+
default:
|
|
119
|
+
throw new Error("unreachable");
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
function z(e, t) {
|
|
123
|
+
if (t.length && !t.some((n) => e.usages.includes(n))) {
|
|
124
|
+
let n = "CryptoKey does not support this operation, its usages must include ";
|
|
125
|
+
if (t.length > 2) {
|
|
126
|
+
const r = t.pop();
|
|
127
|
+
n += `one of ${t.join(", ")}, or ${r}.`;
|
|
128
|
+
} else
|
|
129
|
+
t.length === 2 ? n += `one of ${t[0]} or ${t[1]}.` : n += `${t[0]}.`;
|
|
130
|
+
throw new TypeError(n);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function Q(e, t, ...n) {
|
|
134
|
+
switch (t) {
|
|
135
|
+
case "HS256":
|
|
136
|
+
case "HS384":
|
|
137
|
+
case "HS512": {
|
|
138
|
+
if (!A(e.algorithm, "HMAC"))
|
|
139
|
+
throw p("HMAC");
|
|
140
|
+
const r = parseInt(t.slice(2), 10);
|
|
141
|
+
if (T(e.algorithm.hash) !== r)
|
|
142
|
+
throw p(`SHA-${r}`, "algorithm.hash");
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
case "RS256":
|
|
146
|
+
case "RS384":
|
|
147
|
+
case "RS512": {
|
|
148
|
+
if (!A(e.algorithm, "RSASSA-PKCS1-v1_5"))
|
|
149
|
+
throw p("RSASSA-PKCS1-v1_5");
|
|
150
|
+
const r = parseInt(t.slice(2), 10);
|
|
151
|
+
if (T(e.algorithm.hash) !== r)
|
|
152
|
+
throw p(`SHA-${r}`, "algorithm.hash");
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case "PS256":
|
|
156
|
+
case "PS384":
|
|
157
|
+
case "PS512": {
|
|
158
|
+
if (!A(e.algorithm, "RSA-PSS"))
|
|
159
|
+
throw p("RSA-PSS");
|
|
160
|
+
const r = parseInt(t.slice(2), 10);
|
|
161
|
+
if (T(e.algorithm.hash) !== r)
|
|
162
|
+
throw p(`SHA-${r}`, "algorithm.hash");
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case "EdDSA": {
|
|
166
|
+
if (e.algorithm.name !== "Ed25519" && e.algorithm.name !== "Ed448")
|
|
167
|
+
throw p("Ed25519 or Ed448");
|
|
168
|
+
break;
|
|
169
|
+
}
|
|
170
|
+
case "ES256":
|
|
171
|
+
case "ES384":
|
|
172
|
+
case "ES512": {
|
|
173
|
+
if (!A(e.algorithm, "ECDSA"))
|
|
174
|
+
throw p("ECDSA");
|
|
175
|
+
const r = Y(t);
|
|
176
|
+
if (e.algorithm.namedCurve !== r)
|
|
177
|
+
throw p(r, "algorithm.namedCurve");
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
default:
|
|
181
|
+
throw new TypeError("CryptoKey does not support this operation");
|
|
182
|
+
}
|
|
183
|
+
z(e, n);
|
|
184
|
+
}
|
|
185
|
+
function U(e, t, ...n) {
|
|
186
|
+
var r;
|
|
187
|
+
if (n.length > 2) {
|
|
188
|
+
const a = n.pop();
|
|
189
|
+
e += `one of type ${n.join(", ")}, or ${a}.`;
|
|
190
|
+
} else
|
|
191
|
+
n.length === 2 ? e += `one of type ${n[0]} or ${n[1]}.` : e += `of type ${n[0]}.`;
|
|
192
|
+
return t == null ? e += ` Received ${t}` : typeof t == "function" && t.name ? e += ` Received function ${t.name}` : typeof t == "object" && t != null && (r = t.constructor) != null && r.name && (e += ` Received an instance of ${t.constructor.name}`), e;
|
|
193
|
+
}
|
|
194
|
+
const J = (e, ...t) => U("Key must be ", e, ...t);
|
|
195
|
+
function $(e, t, ...n) {
|
|
196
|
+
return U(`Key for the ${e} algorithm must be `, t, ...n);
|
|
197
|
+
}
|
|
198
|
+
const k = (e) => D(e), S = ["CryptoKey"], X = (...e) => {
|
|
199
|
+
const t = e.filter(Boolean);
|
|
200
|
+
if (t.length === 0 || t.length === 1)
|
|
201
|
+
return !0;
|
|
202
|
+
let n;
|
|
203
|
+
for (const r of t) {
|
|
204
|
+
const a = Object.keys(r);
|
|
205
|
+
if (!n || n.size === 0) {
|
|
206
|
+
n = new Set(a);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
for (const i of a) {
|
|
210
|
+
if (n.has(i))
|
|
211
|
+
return !1;
|
|
212
|
+
n.add(i);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return !0;
|
|
216
|
+
};
|
|
217
|
+
function j(e) {
|
|
218
|
+
return typeof e == "object" && e !== null;
|
|
219
|
+
}
|
|
220
|
+
function C(e) {
|
|
221
|
+
if (!j(e) || Object.prototype.toString.call(e) !== "[object Object]")
|
|
222
|
+
return !1;
|
|
223
|
+
if (Object.getPrototypeOf(e) === null)
|
|
224
|
+
return !0;
|
|
225
|
+
let t = e;
|
|
226
|
+
for (; Object.getPrototypeOf(t) !== null; )
|
|
227
|
+
t = Object.getPrototypeOf(t);
|
|
228
|
+
return Object.getPrototypeOf(e) === t;
|
|
229
|
+
}
|
|
230
|
+
const Z = (e, t) => {
|
|
231
|
+
if (e.startsWith("RS") || e.startsWith("PS")) {
|
|
232
|
+
const { modulusLength: n } = t.algorithm;
|
|
233
|
+
if (typeof n != "number" || n < 2048)
|
|
234
|
+
throw new TypeError(`${e} requires key modulusLength to be 2048 bits or larger`);
|
|
235
|
+
}
|
|
236
|
+
}, y = (e, t, n = 0) => {
|
|
237
|
+
n === 0 && (t.unshift(t.length), t.unshift(6));
|
|
238
|
+
const r = e.indexOf(t[0], n);
|
|
239
|
+
if (r === -1)
|
|
240
|
+
return !1;
|
|
241
|
+
const a = e.subarray(r, r + t.length);
|
|
242
|
+
return a.length !== t.length ? !1 : a.every((i, o) => i === t[o]) || y(e, t, r + 1);
|
|
243
|
+
}, W = (e) => {
|
|
244
|
+
switch (!0) {
|
|
245
|
+
case y(e, [42, 134, 72, 206, 61, 3, 1, 7]):
|
|
246
|
+
return "P-256";
|
|
247
|
+
case y(e, [43, 129, 4, 0, 34]):
|
|
248
|
+
return "P-384";
|
|
249
|
+
case y(e, [43, 129, 4, 0, 35]):
|
|
250
|
+
return "P-521";
|
|
251
|
+
case y(e, [43, 101, 110]):
|
|
252
|
+
return "X25519";
|
|
253
|
+
case y(e, [43, 101, 111]):
|
|
254
|
+
return "X448";
|
|
255
|
+
case y(e, [43, 101, 112]):
|
|
256
|
+
return "Ed25519";
|
|
257
|
+
case y(e, [43, 101, 113]):
|
|
258
|
+
return "Ed448";
|
|
259
|
+
default:
|
|
260
|
+
throw new I("Invalid or unsupported EC Key Curve or OKP Key Sub Type");
|
|
261
|
+
}
|
|
262
|
+
}, ee = async (e, t, n, r, a) => {
|
|
263
|
+
let i, o;
|
|
264
|
+
const c = new Uint8Array(atob(n.replace(e, "")).split("").map((s) => s.charCodeAt(0)));
|
|
265
|
+
switch (r) {
|
|
266
|
+
case "PS256":
|
|
267
|
+
case "PS384":
|
|
268
|
+
case "PS512":
|
|
269
|
+
i = { name: "RSA-PSS", hash: `SHA-${r.slice(-3)}` }, o = ["verify"];
|
|
270
|
+
break;
|
|
271
|
+
case "RS256":
|
|
272
|
+
case "RS384":
|
|
273
|
+
case "RS512":
|
|
274
|
+
i = { name: "RSASSA-PKCS1-v1_5", hash: `SHA-${r.slice(-3)}` }, o = ["verify"];
|
|
275
|
+
break;
|
|
276
|
+
case "RSA-OAEP":
|
|
277
|
+
case "RSA-OAEP-256":
|
|
278
|
+
case "RSA-OAEP-384":
|
|
279
|
+
case "RSA-OAEP-512":
|
|
280
|
+
i = {
|
|
281
|
+
name: "RSA-OAEP",
|
|
282
|
+
hash: `SHA-${parseInt(r.slice(-3), 10) || 1}`
|
|
283
|
+
}, o = ["encrypt", "wrapKey"];
|
|
284
|
+
break;
|
|
285
|
+
case "ES256":
|
|
286
|
+
i = { name: "ECDSA", namedCurve: "P-256" }, o = ["verify"];
|
|
287
|
+
break;
|
|
288
|
+
case "ES384":
|
|
289
|
+
i = { name: "ECDSA", namedCurve: "P-384" }, o = ["verify"];
|
|
290
|
+
break;
|
|
291
|
+
case "ES512":
|
|
292
|
+
i = { name: "ECDSA", namedCurve: "P-521" }, o = ["verify"];
|
|
293
|
+
break;
|
|
294
|
+
case "ECDH-ES":
|
|
295
|
+
case "ECDH-ES+A128KW":
|
|
296
|
+
case "ECDH-ES+A192KW":
|
|
297
|
+
case "ECDH-ES+A256KW": {
|
|
298
|
+
const s = W(c);
|
|
299
|
+
i = s.startsWith("P-") ? { name: "ECDH", namedCurve: s } : { name: s }, o = [];
|
|
300
|
+
break;
|
|
301
|
+
}
|
|
302
|
+
case "EdDSA":
|
|
303
|
+
i = { name: W(c) }, o = ["verify"];
|
|
304
|
+
break;
|
|
305
|
+
default:
|
|
306
|
+
throw new I('Invalid or unsupported "alg" (Algorithm) value');
|
|
307
|
+
}
|
|
308
|
+
return P.subtle.importKey(t, c, i, !1, o);
|
|
309
|
+
}, te = (e, t, n) => ee(/(?:-----(?:BEGIN|END) PUBLIC KEY-----|\s)/g, "spki", e, t);
|
|
310
|
+
async function re(e, t, n) {
|
|
311
|
+
if (e.indexOf("-----BEGIN PUBLIC KEY-----") !== 0)
|
|
312
|
+
throw new TypeError('"spki" must be SPKI formatted string');
|
|
313
|
+
return te(e, t);
|
|
314
|
+
}
|
|
315
|
+
const ne = (e, t) => {
|
|
316
|
+
if (!(t instanceof Uint8Array)) {
|
|
317
|
+
if (!k(t))
|
|
318
|
+
throw new TypeError($(e, t, ...S, "Uint8Array"));
|
|
319
|
+
if (t.type !== "secret")
|
|
320
|
+
throw new TypeError(`${S.join(" or ")} instances for symmetric algorithms must be of type "secret"`);
|
|
321
|
+
}
|
|
322
|
+
}, ae = (e, t, n) => {
|
|
323
|
+
if (!k(t))
|
|
324
|
+
throw new TypeError($(e, t, ...S));
|
|
325
|
+
if (t.type === "secret")
|
|
326
|
+
throw new TypeError(`${S.join(" or ")} instances for asymmetric algorithms must not be of type "secret"`);
|
|
327
|
+
if (t.algorithm && n === "verify" && t.type === "private")
|
|
328
|
+
throw new TypeError(`${S.join(" or ")} instances for asymmetric algorithm verifying must be of type "public"`);
|
|
329
|
+
if (t.algorithm && n === "encrypt" && t.type === "private")
|
|
330
|
+
throw new TypeError(`${S.join(" or ")} instances for asymmetric algorithm encryption must be of type "public"`);
|
|
331
|
+
}, oe = (e, t, n) => {
|
|
332
|
+
e.startsWith("HS") || e === "dir" || e.startsWith("PBES2") || /^A\d{3}(?:GCM)?KW$/.test(e) ? ne(e, t) : ae(e, t, n);
|
|
333
|
+
};
|
|
334
|
+
function ie(e, t, n, r, a) {
|
|
335
|
+
if (a.crit !== void 0 && (r == null ? void 0 : r.crit) === void 0)
|
|
336
|
+
throw new e('"crit" (Critical) Header Parameter MUST be integrity protected');
|
|
337
|
+
if (!r || r.crit === void 0)
|
|
338
|
+
return /* @__PURE__ */ new Set();
|
|
339
|
+
if (!Array.isArray(r.crit) || r.crit.length === 0 || r.crit.some((o) => typeof o != "string" || o.length === 0))
|
|
340
|
+
throw new e('"crit" (Critical) Header Parameter MUST be an array of non-empty strings when present');
|
|
341
|
+
let i;
|
|
342
|
+
n !== void 0 ? i = new Map([...Object.entries(n), ...t.entries()]) : i = t;
|
|
343
|
+
for (const o of r.crit) {
|
|
344
|
+
if (!i.has(o))
|
|
345
|
+
throw new I(`Extension Header Parameter "${o}" is not recognized`);
|
|
346
|
+
if (a[o] === void 0)
|
|
347
|
+
throw new e(`Extension Header Parameter "${o}" is missing`);
|
|
348
|
+
if (i.get(o) && r[o] === void 0)
|
|
349
|
+
throw new e(`Extension Header Parameter "${o}" MUST be integrity protected`);
|
|
350
|
+
}
|
|
351
|
+
return new Set(r.crit);
|
|
352
|
+
}
|
|
353
|
+
const ce = (e, t) => {
|
|
354
|
+
if (t !== void 0 && (!Array.isArray(t) || t.some((n) => typeof n != "string")))
|
|
355
|
+
throw new TypeError(`"${e}" option must be an array of strings`);
|
|
356
|
+
if (t)
|
|
357
|
+
return new Set(t);
|
|
358
|
+
};
|
|
359
|
+
function se(e, t) {
|
|
360
|
+
const n = `SHA-${e.slice(-3)}`;
|
|
361
|
+
switch (e) {
|
|
362
|
+
case "HS256":
|
|
363
|
+
case "HS384":
|
|
364
|
+
case "HS512":
|
|
365
|
+
return { hash: n, name: "HMAC" };
|
|
366
|
+
case "PS256":
|
|
367
|
+
case "PS384":
|
|
368
|
+
case "PS512":
|
|
369
|
+
return { hash: n, name: "RSA-PSS", saltLength: e.slice(-3) >> 3 };
|
|
370
|
+
case "RS256":
|
|
371
|
+
case "RS384":
|
|
372
|
+
case "RS512":
|
|
373
|
+
return { hash: n, name: "RSASSA-PKCS1-v1_5" };
|
|
374
|
+
case "ES256":
|
|
375
|
+
case "ES384":
|
|
376
|
+
case "ES512":
|
|
377
|
+
return { hash: n, name: "ECDSA", namedCurve: t.namedCurve };
|
|
378
|
+
case "EdDSA":
|
|
379
|
+
return { name: t.name };
|
|
380
|
+
default:
|
|
381
|
+
throw new I(`alg ${e} is not supported either by JOSE or your javascript runtime`);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
function de(e, t, n) {
|
|
385
|
+
if (D(t))
|
|
386
|
+
return Q(t, e, n), t;
|
|
387
|
+
if (t instanceof Uint8Array) {
|
|
388
|
+
if (!e.startsWith("HS"))
|
|
389
|
+
throw new TypeError(J(t, ...S));
|
|
390
|
+
return P.subtle.importKey("raw", t, { hash: `SHA-${e.slice(-3)}`, name: "HMAC" }, !1, [n]);
|
|
391
|
+
}
|
|
392
|
+
throw new TypeError(J(t, ...S, "Uint8Array"));
|
|
393
|
+
}
|
|
394
|
+
const ue = async (e, t, n, r) => {
|
|
395
|
+
const a = await de(e, t, "verify");
|
|
396
|
+
Z(e, a);
|
|
397
|
+
const i = se(e, a.algorithm);
|
|
398
|
+
try {
|
|
399
|
+
return await P.subtle.verify(i, a, n, r);
|
|
400
|
+
} catch {
|
|
401
|
+
return !1;
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
async function fe(e, t, n) {
|
|
405
|
+
if (!C(e))
|
|
406
|
+
throw new d("Flattened JWS must be an object");
|
|
407
|
+
if (e.protected === void 0 && e.header === void 0)
|
|
408
|
+
throw new d('Flattened JWS must have either of the "protected" or "header" members');
|
|
409
|
+
if (e.protected !== void 0 && typeof e.protected != "string")
|
|
410
|
+
throw new d("JWS Protected Header incorrect type");
|
|
411
|
+
if (e.payload === void 0)
|
|
412
|
+
throw new d("JWS Payload missing");
|
|
413
|
+
if (typeof e.signature != "string")
|
|
414
|
+
throw new d("JWS Signature missing or incorrect type");
|
|
415
|
+
if (e.header !== void 0 && !C(e.header))
|
|
416
|
+
throw new d("JWS Unprotected Header incorrect type");
|
|
417
|
+
let r = {};
|
|
418
|
+
if (e.protected)
|
|
419
|
+
try {
|
|
420
|
+
const _ = R(e.protected);
|
|
421
|
+
r = JSON.parse(g.decode(_));
|
|
422
|
+
} catch {
|
|
423
|
+
throw new d("JWS Protected Header is invalid");
|
|
424
|
+
}
|
|
425
|
+
if (!X(r, e.header))
|
|
426
|
+
throw new d("JWS Protected and JWS Unprotected Header Parameter names must be disjoint");
|
|
427
|
+
const a = {
|
|
428
|
+
...r,
|
|
429
|
+
...e.header
|
|
430
|
+
}, i = ie(d, /* @__PURE__ */ new Map([["b64", !0]]), n == null ? void 0 : n.crit, r, a);
|
|
431
|
+
let o = !0;
|
|
432
|
+
if (i.has("b64") && (o = r.b64, typeof o != "boolean"))
|
|
433
|
+
throw new d('The "b64" (base64url-encode payload) Header Parameter must be a boolean');
|
|
434
|
+
const { alg: c } = a;
|
|
435
|
+
if (typeof c != "string" || !c)
|
|
436
|
+
throw new d('JWS "alg" (Algorithm) Header Parameter missing or invalid');
|
|
437
|
+
const s = n && ce("algorithms", n.algorithms);
|
|
438
|
+
if (s && !s.has(c))
|
|
439
|
+
throw new G('"alg" (Algorithm) Header Parameter value not allowed');
|
|
440
|
+
if (o) {
|
|
441
|
+
if (typeof e.payload != "string")
|
|
442
|
+
throw new d("JWS Payload must be a string");
|
|
443
|
+
} else if (typeof e.payload != "string" && !(e.payload instanceof Uint8Array))
|
|
444
|
+
throw new d("JWS Payload must be a string or an Uint8Array instance");
|
|
445
|
+
let l = !1;
|
|
446
|
+
typeof t == "function" && (t = await t(r, e), l = !0), oe(c, t, "verify");
|
|
447
|
+
const w = V(b.encode(e.protected ?? ""), b.encode("."), typeof e.payload == "string" ? b.encode(e.payload) : e.payload);
|
|
448
|
+
let f;
|
|
449
|
+
try {
|
|
450
|
+
f = R(e.signature);
|
|
451
|
+
} catch {
|
|
452
|
+
throw new d("Failed to base64url decode the signature");
|
|
453
|
+
}
|
|
454
|
+
if (!await ue(c, t, f, w))
|
|
455
|
+
throw new q();
|
|
456
|
+
let m;
|
|
457
|
+
if (o)
|
|
458
|
+
try {
|
|
459
|
+
m = R(e.payload);
|
|
460
|
+
} catch {
|
|
461
|
+
throw new d("Failed to base64url decode the payload");
|
|
462
|
+
}
|
|
463
|
+
else
|
|
464
|
+
typeof e.payload == "string" ? m = b.encode(e.payload) : m = e.payload;
|
|
465
|
+
const u = { payload: m };
|
|
466
|
+
return e.protected !== void 0 && (u.protectedHeader = r), e.header !== void 0 && (u.unprotectedHeader = e.header), l ? { ...u, key: t } : u;
|
|
467
|
+
}
|
|
468
|
+
async function he(e, t, n) {
|
|
469
|
+
if (e instanceof Uint8Array && (e = g.decode(e)), typeof e != "string")
|
|
470
|
+
throw new d("Compact JWS must be a string or Uint8Array");
|
|
471
|
+
const { 0: r, 1: a, 2: i, length: o } = e.split(".");
|
|
472
|
+
if (o !== 3)
|
|
473
|
+
throw new d("Invalid Compact JWS");
|
|
474
|
+
const c = await fe({ payload: a, protected: r, signature: i }, t, n), s = { payload: c.payload, protectedHeader: c.protectedHeader };
|
|
475
|
+
return typeof t == "function" ? { ...s, key: c.key } : s;
|
|
476
|
+
}
|
|
477
|
+
const le = (e) => Math.floor(e.getTime() / 1e3), M = 60, B = M * 60, v = B * 24, pe = v * 7, me = v * 365.25, ye = /^(\+|\-)? ?(\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) => {
|
|
478
|
+
const t = ye.exec(e);
|
|
479
|
+
if (!t || t[4] && t[1])
|
|
480
|
+
throw new TypeError("Invalid time period format");
|
|
481
|
+
const n = parseFloat(t[2]), r = t[3].toLowerCase();
|
|
482
|
+
let a;
|
|
483
|
+
switch (r) {
|
|
484
|
+
case "sec":
|
|
485
|
+
case "secs":
|
|
486
|
+
case "second":
|
|
487
|
+
case "seconds":
|
|
488
|
+
case "s":
|
|
489
|
+
a = Math.round(n);
|
|
490
|
+
break;
|
|
491
|
+
case "minute":
|
|
492
|
+
case "minutes":
|
|
493
|
+
case "min":
|
|
494
|
+
case "mins":
|
|
495
|
+
case "m":
|
|
496
|
+
a = Math.round(n * M);
|
|
497
|
+
break;
|
|
498
|
+
case "hour":
|
|
499
|
+
case "hours":
|
|
500
|
+
case "hr":
|
|
501
|
+
case "hrs":
|
|
502
|
+
case "h":
|
|
503
|
+
a = Math.round(n * B);
|
|
504
|
+
break;
|
|
505
|
+
case "day":
|
|
506
|
+
case "days":
|
|
507
|
+
case "d":
|
|
508
|
+
a = Math.round(n * v);
|
|
509
|
+
break;
|
|
510
|
+
case "week":
|
|
511
|
+
case "weeks":
|
|
512
|
+
case "w":
|
|
513
|
+
a = Math.round(n * pe);
|
|
514
|
+
break;
|
|
515
|
+
default:
|
|
516
|
+
a = Math.round(n * me);
|
|
517
|
+
break;
|
|
518
|
+
}
|
|
519
|
+
return t[1] === "-" || t[4] === "ago" ? -a : a;
|
|
520
|
+
}, H = (e) => e.toLowerCase().replace(/^application\//, ""), Se = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1, we = (e, t, n = {}) => {
|
|
521
|
+
let r;
|
|
522
|
+
try {
|
|
523
|
+
r = JSON.parse(g.decode(t));
|
|
524
|
+
} catch {
|
|
525
|
+
}
|
|
526
|
+
if (!C(r))
|
|
527
|
+
throw new L("JWT Claims Set must be a top-level JSON object");
|
|
528
|
+
const { typ: a } = n;
|
|
529
|
+
if (a && (typeof e.typ != "string" || H(e.typ) !== H(a)))
|
|
530
|
+
throw new h('unexpected "typ" JWT header value', r, "typ", "check_failed");
|
|
531
|
+
const { requiredClaims: i = [], issuer: o, subject: c, audience: s, maxTokenAge: l } = n, w = [...i];
|
|
532
|
+
l !== void 0 && w.push("iat"), s !== void 0 && w.push("aud"), c !== void 0 && w.push("sub"), o !== void 0 && w.push("iss");
|
|
533
|
+
for (const u of new Set(w.reverse()))
|
|
534
|
+
if (!(u in r))
|
|
535
|
+
throw new h(`missing required "${u}" claim`, r, u, "missing");
|
|
536
|
+
if (o && !(Array.isArray(o) ? o : [o]).includes(r.iss))
|
|
537
|
+
throw new h('unexpected "iss" claim value', r, "iss", "check_failed");
|
|
538
|
+
if (c && r.sub !== c)
|
|
539
|
+
throw new h('unexpected "sub" claim value', r, "sub", "check_failed");
|
|
540
|
+
if (s && !Se(r.aud, typeof s == "string" ? [s] : s))
|
|
541
|
+
throw new h('unexpected "aud" claim value', r, "aud", "check_failed");
|
|
542
|
+
let f;
|
|
543
|
+
switch (typeof n.clockTolerance) {
|
|
544
|
+
case "string":
|
|
545
|
+
f = K(n.clockTolerance);
|
|
546
|
+
break;
|
|
547
|
+
case "number":
|
|
548
|
+
f = n.clockTolerance;
|
|
549
|
+
break;
|
|
550
|
+
case "undefined":
|
|
551
|
+
f = 0;
|
|
552
|
+
break;
|
|
553
|
+
default:
|
|
554
|
+
throw new TypeError("Invalid clockTolerance option type");
|
|
555
|
+
}
|
|
556
|
+
const { currentDate: x } = n, m = le(x || /* @__PURE__ */ new Date());
|
|
557
|
+
if ((r.iat !== void 0 || l) && typeof r.iat != "number")
|
|
558
|
+
throw new h('"iat" claim must be a number', r, "iat", "invalid");
|
|
559
|
+
if (r.nbf !== void 0) {
|
|
560
|
+
if (typeof r.nbf != "number")
|
|
561
|
+
throw new h('"nbf" claim must be a number', r, "nbf", "invalid");
|
|
562
|
+
if (r.nbf > m + f)
|
|
563
|
+
throw new h('"nbf" claim timestamp check failed', r, "nbf", "check_failed");
|
|
564
|
+
}
|
|
565
|
+
if (r.exp !== void 0) {
|
|
566
|
+
if (typeof r.exp != "number")
|
|
567
|
+
throw new h('"exp" claim must be a number', r, "exp", "invalid");
|
|
568
|
+
if (r.exp <= m - f)
|
|
569
|
+
throw new O('"exp" claim timestamp check failed', r, "exp", "check_failed");
|
|
570
|
+
}
|
|
571
|
+
if (l) {
|
|
572
|
+
const u = m - r.iat, _ = typeof l == "number" ? l : K(l);
|
|
573
|
+
if (u - f > _)
|
|
574
|
+
throw new O('"iat" claim timestamp check failed (too far in the past)', r, "iat", "check_failed");
|
|
575
|
+
if (u < 0 - f)
|
|
576
|
+
throw new h('"iat" claim timestamp check failed (it should be in the past)', r, "iat", "check_failed");
|
|
577
|
+
}
|
|
578
|
+
return r;
|
|
579
|
+
};
|
|
580
|
+
async function Ee(e, t, n) {
|
|
581
|
+
var o;
|
|
582
|
+
const r = await he(e, t, n);
|
|
583
|
+
if ((o = r.protectedHeader.crit) != null && o.includes("b64") && r.protectedHeader.b64 === !1)
|
|
584
|
+
throw new L("JWTs MUST NOT use unencoded payload");
|
|
585
|
+
const i = { payload: we(r.protectedHeader, r.payload, n), protectedHeader: r.protectedHeader };
|
|
586
|
+
return typeof t == "function" ? { ...i, key: r.key } : i;
|
|
587
|
+
}
|
|
588
|
+
const Ae = {
|
|
15
589
|
ID_TOKEN: "id_token"
|
|
16
|
-
},
|
|
590
|
+
}, ge = {
|
|
17
591
|
CLIENT_ID: "X-Auth-ClientId"
|
|
18
|
-
},
|
|
592
|
+
}, N = {
|
|
19
593
|
ALG: "RS256",
|
|
20
594
|
USER_ID_KEY: "_id",
|
|
21
595
|
ISSUER: "gizmette.com"
|
|
596
|
+
}, be = `-----BEGIN PUBLIC KEY-----
|
|
597
|
+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsF6i3Jd9fY/3COqCw/m7
|
|
598
|
+
w5PKyTYLGAI2I6SIIdpe6i6DOCbEkmDz7LdVsBqwNtVi8gvWYIj+8ol6rU3qu1v5
|
|
599
|
+
i1Jd45GSK4kzkVdgCmQZbM5ak0KI99q5wsrAIzUd+LRJ2HRvWtr5IYdsIiXaQjle
|
|
600
|
+
aMwPFOIcJH+rKfFgNcHLcaS5syp7zU1ANwZ+trgR+DifBr8TLVkBynmNeTyhDm2+
|
|
601
|
+
l0haqjMk0UoNPPE8iYBWUHQJJE1Dqstj65d6Eh5g64Pao25y4cmYJbKjiblIGEkE
|
|
602
|
+
sjqybA9mARAqh9k/eiIopecWSiffNQTwVQVd2I9ZH3BalhEXHlqFgrjz51kFqg81
|
|
603
|
+
awIDAQAB
|
|
604
|
+
-----END PUBLIC KEY-----`, Ie = async (e, t) => {
|
|
605
|
+
try {
|
|
606
|
+
const n = N.ALG, a = await re(be, n);
|
|
607
|
+
return await Ee(e, a, {
|
|
608
|
+
issuer: N.ISSUER,
|
|
609
|
+
audience: t
|
|
610
|
+
});
|
|
611
|
+
} catch {
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
22
614
|
};
|
|
23
615
|
export {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
616
|
+
Ae as AUTH_TYPES,
|
|
617
|
+
ge as HEADERS,
|
|
618
|
+
N as JWT,
|
|
619
|
+
be as JWT_PUBLIC_KEY,
|
|
620
|
+
Ie as verifyAndExtractToken
|
|
27
621
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@versini/auth-common",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Arno Versini",
|
|
6
6
|
"publishConfig": {
|
|
@@ -29,5 +29,8 @@
|
|
|
29
29
|
"lint": "biome lint src",
|
|
30
30
|
"start": "static-server dist --port 5173"
|
|
31
31
|
},
|
|
32
|
-
"
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"jose": "5.4.1"
|
|
34
|
+
},
|
|
35
|
+
"gitHead": "8ba8747f5ea5473bf63a348fef316ccff144f272"
|
|
33
36
|
}
|