@eventuras/fides-auth 0.6.0 → 0.7.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.
@@ -1,66 +0,0 @@
1
- import { createLogger as e } from "./logger.js";
2
- import { y as t } from "./deflate-koSuX7FB.js";
3
- import { t as n } from "./decrypt-Cahlu_6Y.js";
4
- import { l as r, p as i } from "./utils-C0OavYxH.js";
5
- import { t as a } from "./decode_jwt-1J26fl4I.js";
6
- //#region ../../node_modules/.pnpm/jose@6.2.2/node_modules/jose/dist/webapi/jwt/decrypt.js
7
- async function o(e, r, a) {
8
- let o = await n(e, r, a), s = i(o.protectedHeader, o.plaintext, a), { protectedHeader: c } = o;
9
- if (c.iss !== void 0 && c.iss !== s.iss) throw new t("replicated \"iss\" claim header parameter mismatch", s, "iss", "mismatch");
10
- if (c.sub !== void 0 && c.sub !== s.sub) throw new t("replicated \"sub\" claim header parameter mismatch", s, "sub", "mismatch");
11
- if (c.aud !== void 0 && JSON.stringify(c.aud) !== JSON.stringify(s.aud)) throw new t("replicated \"aud\" claim header parameter mismatch", s, "aud", "mismatch");
12
- let l = {
13
- payload: s,
14
- protectedHeader: c
15
- };
16
- return typeof r == "function" ? {
17
- ...l,
18
- key: o.key
19
- } : l;
20
- }
21
- //#endregion
22
- //#region src/session-validation.ts
23
- var s = e({ namespace: "fides-auth:session-validation" });
24
- async function c(e, t) {
25
- s.debug("Starting session JWT validation");
26
- let n = typeof t == "string" ? r(t) : t, i;
27
- try {
28
- ({payload: i} = await o(e, n));
29
- } catch (e) {
30
- let t = e instanceof Error ? e.message : "Failed decrypting JWT";
31
- return s.warn({ reason: t }, "Session JWT decryption failed"), {
32
- status: "INVALID",
33
- reason: `Decryption error: ${t}`
34
- };
35
- }
36
- if (!u(i)) return s.warn("JWT payload does not match Session structure"), {
37
- status: "INVALID",
38
- reason: "jwt does not seem to include a session"
39
- };
40
- s.debug("Session validated successfully");
41
- let a = l(i);
42
- return a !== void 0 && a <= 0 ? (s.info({ accessTokenExpiresIn: a }, "Session access token has expired"), {
43
- session: i,
44
- status: "EXPIRED",
45
- accessTokenExpiresIn: a,
46
- reason: "Access token has expired"
47
- }) : {
48
- session: i,
49
- status: "VALID",
50
- accessTokenExpiresIn: a
51
- };
52
- }
53
- function l(e) {
54
- let t = e.tokens?.accessToken;
55
- if (t) try {
56
- let { exp: e } = a(t);
57
- return typeof e == "number" ? Math.floor(e - Date.now() / 1e3) : void 0;
58
- } catch {
59
- return;
60
- }
61
- }
62
- function u(e) {
63
- return !(typeof e != "object" || !e || "tokens" in e && typeof e.tokens != "object" || "user" in e && typeof e.user != "object");
64
- }
65
- //#endregion
66
- export { c as n, u as t };
@@ -1,382 +0,0 @@
1
- import { createLogger as e } from "./logger.js";
2
- import { C as t, D as n, E as r, T as i, _ as a, b as o, c as s, f as c, i as l, l as u, m as d, o as f, r as p, s as m, t as h, u as g, v as _, w as v, x as y, y as b } from "./deflate-koSuX7FB.js";
3
- import { t as x } from "./decode_jwt-1J26fl4I.js";
4
- //#region ../../node_modules/.pnpm/jose@6.2.2/node_modules/jose/dist/webapi/jwe/flattened/encrypt.js
5
- var S = class {
6
- #e;
7
- #t;
8
- #n;
9
- #r;
10
- #i;
11
- #a;
12
- #o;
13
- #s;
14
- constructor(e) {
15
- if (!(e instanceof Uint8Array)) throw TypeError("plaintext must be an instance of Uint8Array");
16
- this.#e = e;
17
- }
18
- setKeyManagementParameters(e) {
19
- return g(this.#s, "setKeyManagementParameters"), this.#s = e, this;
20
- }
21
- setProtectedHeader(e) {
22
- return g(this.#t, "setProtectedHeader"), this.#t = e, this;
23
- }
24
- setSharedUnprotectedHeader(e) {
25
- return g(this.#n, "setSharedUnprotectedHeader"), this.#n = e, this;
26
- }
27
- setUnprotectedHeader(e) {
28
- return g(this.#r, "setUnprotectedHeader"), this.#r = e, this;
29
- }
30
- setAdditionalAuthenticatedData(e) {
31
- return this.#i = e, this;
32
- }
33
- setContentEncryptionKey(e) {
34
- return g(this.#a, "setContentEncryptionKey"), this.#a = e, this;
35
- }
36
- setInitializationVector(e) {
37
- return g(this.#o, "setInitializationVector"), this.#o = e, this;
38
- }
39
- async encrypt(e, n) {
40
- if (!this.#t && !this.#r && !this.#n) throw new _("either setProtectedHeader, setUnprotectedHeader, or sharedUnprotectedHeader must be called before #encrypt()");
41
- if (!s(this.#t, this.#r, this.#n)) throw new _("JWE Protected, JWE Shared Unprotected and JWE Per-Recipient Header Parameter names must be disjoint");
42
- let i = {
43
- ...this.#t,
44
- ...this.#r,
45
- ...this.#n
46
- };
47
- if (l(_, /* @__PURE__ */ new Map(), n?.crit, this.#t, i), i.zip !== void 0 && i.zip !== "DEF") throw new a("Unsupported JWE \"zip\" (Compression Algorithm) Header Parameter value.");
48
- if (i.zip !== void 0 && !this.#t?.zip) throw new _("JWE \"zip\" (Compression Algorithm) Header Parameter MUST be in a protected header.");
49
- let { alg: o, enc: u } = i;
50
- if (typeof o != "string" || !o) throw new _("JWE \"alg\" (Algorithm) Header Parameter missing or invalid");
51
- if (typeof u != "string" || !u) throw new _("JWE \"enc\" (Encryption Algorithm) Header Parameter missing or invalid");
52
- let g;
53
- if (this.#a && (o === "dir" || o === "ECDH-ES")) throw TypeError(`setContentEncryptionKey cannot be called with JWE "alg" (Algorithm) Header ${o}`);
54
- p(o === "dir" ? u : o, e, "encrypt");
55
- let y;
56
- {
57
- let t, r = await m(e, o);
58
- ({cek: y, encryptedKey: g, parameters: t} = await f(o, u, r, this.#a, this.#s)), t && (n && c in n ? this.#r ? this.#r = {
59
- ...this.#r,
60
- ...t
61
- } : this.setUnprotectedHeader(t) : this.#t ? this.#t = {
62
- ...this.#t,
63
- ...t
64
- } : this.setProtectedHeader(t));
65
- }
66
- let b, x, S, C;
67
- if (this.#t ? (x = t(JSON.stringify(this.#t)), S = r(x)) : (x = "", S = new Uint8Array()), this.#i) {
68
- C = t(this.#i);
69
- let e = r(C);
70
- b = v(S, r("."), e);
71
- } else b = S;
72
- let w = this.#e;
73
- i.zip === "DEF" && (w = await h(w).catch((e) => {
74
- throw new _("Failed to compress plaintext", { cause: e });
75
- }));
76
- let { ciphertext: T, tag: E, iv: D } = await d(u, w, y, this.#o, b), O = { ciphertext: t(T) };
77
- return D && (O.iv = t(D)), E && (O.tag = t(E)), g && (O.encrypted_key = t(g)), C && (O.aad = C), this.#t && (O.protected = x), this.#n && (O.unprotected = this.#n), this.#r && (O.header = this.#r), O;
78
- }
79
- }, C = (e) => Math.floor(e.getTime() / 1e3), w = 60, T = w * 60, E = T * 24, D = E * 7, O = E * 365.25, k = /^(\+|\-)? ?(\d+|\d+\.\d+) ?(seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)(?: (ago|from now))?$/i;
80
- function A(e) {
81
- let t = k.exec(e);
82
- if (!t || t[4] && t[1]) throw TypeError("Invalid time period format");
83
- let n = parseFloat(t[2]), r = t[3].toLowerCase(), i;
84
- switch (r) {
85
- case "sec":
86
- case "secs":
87
- case "second":
88
- case "seconds":
89
- case "s":
90
- i = Math.round(n);
91
- break;
92
- case "minute":
93
- case "minutes":
94
- case "min":
95
- case "mins":
96
- case "m":
97
- i = Math.round(n * w);
98
- break;
99
- case "hour":
100
- case "hours":
101
- case "hr":
102
- case "hrs":
103
- case "h":
104
- i = Math.round(n * T);
105
- break;
106
- case "day":
107
- case "days":
108
- case "d":
109
- i = Math.round(n * E);
110
- break;
111
- case "week":
112
- case "weeks":
113
- case "w":
114
- i = Math.round(n * D);
115
- break;
116
- default:
117
- i = Math.round(n * O);
118
- break;
119
- }
120
- return t[1] === "-" || t[4] === "ago" ? -i : i;
121
- }
122
- function j(e, t) {
123
- if (!Number.isFinite(t)) throw TypeError(`Invalid ${e} input`);
124
- return t;
125
- }
126
- var M = (e) => e.includes("/") ? e.toLowerCase() : `application/${e.toLowerCase()}`, N = (e, t) => typeof e == "string" ? t.includes(e) : Array.isArray(e) ? t.some(Set.prototype.has.bind(new Set(e))) : !1;
127
- function P(e, t, n = {}) {
128
- let r;
129
- try {
130
- r = JSON.parse(i.decode(t));
131
- } catch {}
132
- if (!u(r)) throw new y("JWT Claims Set must be a top-level JSON object");
133
- let { typ: a } = n;
134
- if (a && (typeof e.typ != "string" || M(e.typ) !== M(a))) throw new b("unexpected \"typ\" JWT header value", r, "typ", "check_failed");
135
- let { requiredClaims: s = [], issuer: c, subject: l, audience: d, maxTokenAge: f } = n, p = [...s];
136
- f !== void 0 && p.push("iat"), d !== void 0 && p.push("aud"), l !== void 0 && p.push("sub"), c !== void 0 && p.push("iss");
137
- for (let e of new Set(p.reverse())) if (!(e in r)) throw new b(`missing required "${e}" claim`, r, e, "missing");
138
- if (c && !(Array.isArray(c) ? c : [c]).includes(r.iss)) throw new b("unexpected \"iss\" claim value", r, "iss", "check_failed");
139
- if (l && r.sub !== l) throw new b("unexpected \"sub\" claim value", r, "sub", "check_failed");
140
- if (d && !N(r.aud, typeof d == "string" ? [d] : d)) throw new b("unexpected \"aud\" claim value", r, "aud", "check_failed");
141
- let m;
142
- switch (typeof n.clockTolerance) {
143
- case "string":
144
- m = A(n.clockTolerance);
145
- break;
146
- case "number":
147
- m = n.clockTolerance;
148
- break;
149
- case "undefined":
150
- m = 0;
151
- break;
152
- default: throw TypeError("Invalid clockTolerance option type");
153
- }
154
- let { currentDate: h } = n, g = C(h || /* @__PURE__ */ new Date());
155
- if ((r.iat !== void 0 || f) && typeof r.iat != "number") throw new b("\"iat\" claim must be a number", r, "iat", "invalid");
156
- if (r.nbf !== void 0) {
157
- if (typeof r.nbf != "number") throw new b("\"nbf\" claim must be a number", r, "nbf", "invalid");
158
- if (r.nbf > g + m) throw new b("\"nbf\" claim timestamp check failed", r, "nbf", "check_failed");
159
- }
160
- if (r.exp !== void 0) {
161
- if (typeof r.exp != "number") throw new b("\"exp\" claim must be a number", r, "exp", "invalid");
162
- if (r.exp <= g - m) throw new o("\"exp\" claim timestamp check failed", r, "exp", "check_failed");
163
- }
164
- if (f) {
165
- let e = g - r.iat, t = typeof f == "number" ? f : A(f);
166
- if (e - m > t) throw new o("\"iat\" claim timestamp check failed (too far in the past)", r, "iat", "check_failed");
167
- if (e < 0 - m) throw new b("\"iat\" claim timestamp check failed (it should be in the past)", r, "iat", "check_failed");
168
- }
169
- return r;
170
- }
171
- var F = class {
172
- #e;
173
- constructor(e) {
174
- if (!u(e)) throw TypeError("JWT Claims Set MUST be an object");
175
- this.#e = structuredClone(e);
176
- }
177
- data() {
178
- return n.encode(JSON.stringify(this.#e));
179
- }
180
- get iss() {
181
- return this.#e.iss;
182
- }
183
- set iss(e) {
184
- this.#e.iss = e;
185
- }
186
- get sub() {
187
- return this.#e.sub;
188
- }
189
- set sub(e) {
190
- this.#e.sub = e;
191
- }
192
- get aud() {
193
- return this.#e.aud;
194
- }
195
- set aud(e) {
196
- this.#e.aud = e;
197
- }
198
- set jti(e) {
199
- this.#e.jti = e;
200
- }
201
- set nbf(e) {
202
- typeof e == "number" ? this.#e.nbf = j("setNotBefore", e) : e instanceof Date ? this.#e.nbf = j("setNotBefore", C(e)) : this.#e.nbf = C(/* @__PURE__ */ new Date()) + A(e);
203
- }
204
- set exp(e) {
205
- typeof e == "number" ? this.#e.exp = j("setExpirationTime", e) : e instanceof Date ? this.#e.exp = j("setExpirationTime", C(e)) : this.#e.exp = C(/* @__PURE__ */ new Date()) + A(e);
206
- }
207
- set iat(e) {
208
- e === void 0 ? this.#e.iat = C(/* @__PURE__ */ new Date()) : e instanceof Date ? this.#e.iat = j("setIssuedAt", C(e)) : typeof e == "string" ? this.#e.iat = j("setIssuedAt", C(/* @__PURE__ */ new Date()) + A(e)) : this.#e.iat = j("setIssuedAt", e);
209
- }
210
- }, I = class {
211
- #e;
212
- constructor(e) {
213
- this.#e = new S(e);
214
- }
215
- setContentEncryptionKey(e) {
216
- return this.#e.setContentEncryptionKey(e), this;
217
- }
218
- setInitializationVector(e) {
219
- return this.#e.setInitializationVector(e), this;
220
- }
221
- setProtectedHeader(e) {
222
- return this.#e.setProtectedHeader(e), this;
223
- }
224
- setKeyManagementParameters(e) {
225
- return this.#e.setKeyManagementParameters(e), this;
226
- }
227
- async encrypt(e, t) {
228
- let n = await this.#e.encrypt(e, t);
229
- return [
230
- n.protected,
231
- n.encrypted_key,
232
- n.iv,
233
- n.ciphertext,
234
- n.tag
235
- ].join(".");
236
- }
237
- }, L = class {
238
- #e;
239
- #t;
240
- #n;
241
- #r;
242
- #i;
243
- #a;
244
- #o;
245
- #s;
246
- constructor(e = {}) {
247
- this.#s = new F(e);
248
- }
249
- setIssuer(e) {
250
- return this.#s.iss = e, this;
251
- }
252
- setSubject(e) {
253
- return this.#s.sub = e, this;
254
- }
255
- setAudience(e) {
256
- return this.#s.aud = e, this;
257
- }
258
- setJti(e) {
259
- return this.#s.jti = e, this;
260
- }
261
- setNotBefore(e) {
262
- return this.#s.nbf = e, this;
263
- }
264
- setExpirationTime(e) {
265
- return this.#s.exp = e, this;
266
- }
267
- setIssuedAt(e) {
268
- return this.#s.iat = e, this;
269
- }
270
- setProtectedHeader(e) {
271
- return g(this.#r, "setProtectedHeader"), this.#r = e, this;
272
- }
273
- setKeyManagementParameters(e) {
274
- return g(this.#n, "setKeyManagementParameters"), this.#n = e, this;
275
- }
276
- setContentEncryptionKey(e) {
277
- return g(this.#e, "setContentEncryptionKey"), this.#e = e, this;
278
- }
279
- setInitializationVector(e) {
280
- return g(this.#t, "setInitializationVector"), this.#t = e, this;
281
- }
282
- replicateIssuerAsHeader() {
283
- return this.#i = !0, this;
284
- }
285
- replicateSubjectAsHeader() {
286
- return this.#a = !0, this;
287
- }
288
- replicateAudienceAsHeader() {
289
- return this.#o = !0, this;
290
- }
291
- async encrypt(e, t) {
292
- let n = new I(this.#s.data());
293
- return this.#r && (this.#i || this.#a || this.#o) && (this.#r = {
294
- ...this.#r,
295
- iss: this.#i ? this.#s.iss : void 0,
296
- sub: this.#a ? this.#s.sub : void 0,
297
- aud: this.#o ? this.#s.aud : void 0
298
- }), n.setProtectedHeader(this.#r), this.#t && n.setInitializationVector(this.#t), this.#e && n.setContentEncryptionKey(this.#e), this.#n && n.setKeyManagementParameters(this.#n), n.encrypt(e, t);
299
- }
300
- }, R = e({ namespace: "fides-auth:utils" });
301
- function z(e) {
302
- if (e.length % 2 != 0) throw Error("Invalid hex string: odd length");
303
- if (e.length > 0 && !/^[0-9a-fA-F]+$/.test(e)) throw Error("Invalid hex string: contains non-hex characters");
304
- let t = new Uint8Array(e.length / 2);
305
- for (let n = 0; n < t.length; n++) t[n] = Number.parseInt(e.slice(n * 2, n * 2 + 2), 16);
306
- return t;
307
- }
308
- function B(e) {
309
- return typeof e == "string" ? z(e) : e;
310
- }
311
- async function V(e, t) {
312
- return new L(e).setProtectedHeader({
313
- alg: "dir",
314
- enc: "A256GCM"
315
- }).setIssuedAt().encrypt(B(t));
316
- }
317
- async function H(e, t) {
318
- let n = B(t), r = await crypto.subtle.importKey("raw", n, { name: "AES-GCM" }, !1, ["encrypt"]), i = crypto.getRandomValues(new Uint8Array(12)), a = new TextEncoder().encode(e), o = await crypto.subtle.encrypt({
319
- name: "AES-GCM",
320
- iv: i,
321
- tagLength: 128
322
- }, r, a), s = new Uint8Array(o), c = s.slice(0, s.length - 16), l = s.slice(s.length - 16);
323
- return [
324
- Y(i),
325
- Y(l),
326
- Y(c)
327
- ].join(":");
328
- }
329
- async function U(e, t) {
330
- let [n, r, i] = e.split(":");
331
- if (!n || !r || !i) throw Error("Invalid encrypted data format");
332
- let a = z(n), o = z(r), s = z(i), c = new Uint8Array(s.length + o.length);
333
- c.set(s), c.set(o, s.length);
334
- let l = B(t), u = new Uint8Array([...l]), d = new Uint8Array([...a]), f = await crypto.subtle.importKey("raw", u, { name: "AES-GCM" }, !1, ["decrypt"]), p = await crypto.subtle.decrypt({
335
- name: "AES-GCM",
336
- iv: d,
337
- tagLength: 128
338
- }, f, c.buffer);
339
- return new TextDecoder().decode(p);
340
- }
341
- function W(e) {
342
- return typeof e == "string" ? new TextEncoder().encode(e) : e;
343
- }
344
- async function G(e) {
345
- let t = W(e), n = await crypto.subtle.digest("SHA-256", t);
346
- return Y(new Uint8Array(n));
347
- }
348
- async function K(e) {
349
- let t = W(e), n = await crypto.subtle.digest("SHA-512", t);
350
- return Y(new Uint8Array(n));
351
- }
352
- function q() {
353
- let e = process.env.SESSION_SECRET;
354
- if (!e) throw Error("SESSION_SECRET is not defined");
355
- return e;
356
- }
357
- function J() {
358
- let e = z(q());
359
- return new Uint8Array([...e]);
360
- }
361
- function Y(e) {
362
- return Array.from(e).map((e) => e.toString(16).padStart(2, "0")).join("");
363
- }
364
- function X(e = 32) {
365
- let t = new Uint8Array(e);
366
- return crypto.getRandomValues(t), Y(t);
367
- }
368
- var Z = (e, t = 10) => {
369
- try {
370
- let n = x(e);
371
- if (!n.exp) return !1;
372
- let r = Date.now() / 1e3;
373
- return n.exp - r < t;
374
- } catch (e) {
375
- return R.error({ error: e }, "Error decoding access token"), !0;
376
- }
377
- };
378
- function Q(e, t) {
379
- return e.scopes?.includes(t) ?? !1;
380
- }
381
- //#endregion
382
- export { X as a, Q as c, K as d, Y as f, H as i, z as l, V as n, q as o, P as p, U as r, J as s, Z as t, G as u };