mulguard 1.1.4 → 1.1.5
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/index.mjs
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
var ne = Object.defineProperty;
|
|
2
2
|
var se = (e, r, t) => r in e ? ne(e, r, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[r] = t;
|
|
3
|
-
var
|
|
4
|
-
import { A as m, d as
|
|
5
|
-
import { a as
|
|
6
|
-
import { v as
|
|
7
|
-
import { c as
|
|
3
|
+
var b = (e, r, t) => se(e, typeof r != "symbol" ? r + "" : r, t);
|
|
4
|
+
import { A as m, d as oe, e as ie, c as ae, g as ce } from "../actions-DeCfLtHA.mjs";
|
|
5
|
+
import { a as wt, s as pt, b as mt, v as Et } from "../actions-DeCfLtHA.mjs";
|
|
6
|
+
import { v as U } from "../oauth-state-LE-qeq-K.mjs";
|
|
7
|
+
import { c as kt, p as vt, k as St, n as At, m as Rt, j as Ot, l as Tt, e as It, g as _t, b as Pt, i as Ct, a as Nt, o as bt, f as Ut, h as Ft, r as xt, d as Dt, s as Lt } from "../oauth-state-LE-qeq-K.mjs";
|
|
8
8
|
import { NextResponse as E } from "next/server";
|
|
9
9
|
const x = typeof globalThis == "object" && "crypto" in globalThis ? globalThis.crypto : void 0;
|
|
10
10
|
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
|
@@ -17,8 +17,8 @@ function ue(e = 32) {
|
|
|
17
17
|
}
|
|
18
18
|
class le {
|
|
19
19
|
constructor(r) {
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
b(this, "attempts", /* @__PURE__ */ new Map());
|
|
21
|
+
b(this, "config");
|
|
22
22
|
this.config = r;
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
@@ -56,7 +56,7 @@ class le {
|
|
|
56
56
|
this.attempts.clear();
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
function
|
|
59
|
+
function _r(e) {
|
|
60
60
|
return new le(e);
|
|
61
61
|
}
|
|
62
62
|
const fe = {
|
|
@@ -74,7 +74,7 @@ function H(e) {
|
|
|
74
74
|
...e
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
|
-
function
|
|
77
|
+
function Pr(e, r) {
|
|
78
78
|
const t = H(r);
|
|
79
79
|
for (const [n, s] of Object.entries(t))
|
|
80
80
|
s && e.set(n, s);
|
|
@@ -112,7 +112,7 @@ const ge = /* @__PURE__ */ new Set([
|
|
|
112
112
|
"guest",
|
|
113
113
|
"user"
|
|
114
114
|
]), we = /012|123|234|345|456|567|678|789|abc|bcd|cde|def|efg|fgh|ghi|hij|ijk|jkl|klm|lmn|mno|nop|opq|pqr|qrs|rst|stu|tuv|uvw|vwx|wxy|xyz/i, pe = 8, me = 128;
|
|
115
|
-
function
|
|
115
|
+
function Cr(e, r = pe) {
|
|
116
116
|
if (typeof e != "string" || !e)
|
|
117
117
|
return { valid: !1, error: "Password is required" };
|
|
118
118
|
if (e.length < r)
|
|
@@ -133,11 +133,11 @@ function Ee(e) {
|
|
|
133
133
|
let r = 0;
|
|
134
134
|
return e.length >= 12 ? r += 2 : e.length >= 8 && (r += 1), /[a-z]/.test(e) && (r += 1), /[A-Z]/.test(e) && (r += 1), /[0-9]/.test(e) && (r += 1), /[^a-zA-Z0-9]/.test(e) && (r += 1), r >= 5 ? "strong" : r >= 3 ? "medium" : "weak";
|
|
135
135
|
}
|
|
136
|
-
function
|
|
136
|
+
function Nr(e) {
|
|
137
137
|
return e.valid === !0 && e.sanitized !== void 0;
|
|
138
138
|
}
|
|
139
139
|
const ye = 100;
|
|
140
|
-
function
|
|
140
|
+
function br(e) {
|
|
141
141
|
if (typeof e != "string" || !e)
|
|
142
142
|
return { valid: !1, error: "Name is required" };
|
|
143
143
|
const r = e.trim();
|
|
@@ -148,11 +148,11 @@ function Pr(e) {
|
|
|
148
148
|
const t = r.replace(/[<>"']/g, "");
|
|
149
149
|
return t.length === 0 ? { valid: !1, error: "Name contains only invalid characters" } : { valid: !0, sanitized: t };
|
|
150
150
|
}
|
|
151
|
-
function
|
|
151
|
+
function Ur(e) {
|
|
152
152
|
return e.valid === !0 && e.sanitized !== void 0;
|
|
153
153
|
}
|
|
154
154
|
const ke = /* @__PURE__ */ new Set(["http:", "https:"]);
|
|
155
|
-
function
|
|
155
|
+
function Fr(e) {
|
|
156
156
|
if (typeof e != "string" || !e)
|
|
157
157
|
return { valid: !1, error: "URL is required" };
|
|
158
158
|
try {
|
|
@@ -162,32 +162,32 @@ function br(e) {
|
|
|
162
162
|
return { valid: !1, error: "Invalid URL format" };
|
|
163
163
|
}
|
|
164
164
|
}
|
|
165
|
-
function
|
|
165
|
+
function xr(e) {
|
|
166
166
|
return e.valid === !0 && e.sanitized !== void 0;
|
|
167
167
|
}
|
|
168
|
-
const ve = 16, Se = 512,
|
|
169
|
-
function
|
|
170
|
-
return typeof e != "string" || !e ? { valid: !1, error: "Token is required" } : e.length < r ? { valid: !1, error: "Token is too short" } : e.length > Se ? { valid: !1, error: "Token is too long" } :
|
|
168
|
+
const ve = 16, Se = 512, Ae = /^[A-Za-z0-9_-]+$/;
|
|
169
|
+
function Dr(e, r = ve) {
|
|
170
|
+
return typeof e != "string" || !e ? { valid: !1, error: "Token is required" } : e.length < r ? { valid: !1, error: "Token is too short" } : e.length > Se ? { valid: !1, error: "Token is too long" } : Ae.test(e) ? /(.)\1{10,}/.test(e) ? { valid: !1, error: "Token contains suspicious pattern" } : { valid: !0, sanitized: e } : { valid: !1, error: "Invalid token format" };
|
|
171
171
|
}
|
|
172
|
-
function
|
|
172
|
+
function Lr(e) {
|
|
173
173
|
return e.valid === !0 && e.sanitized !== void 0;
|
|
174
174
|
}
|
|
175
|
-
const
|
|
175
|
+
const Re = 1e3;
|
|
176
176
|
function X(e, r) {
|
|
177
|
-
const { maxLength: t =
|
|
177
|
+
const { maxLength: t = Re, allowHtml: n = !1, required: s = !0 } = r ?? {};
|
|
178
178
|
if (s && (typeof e != "string" || !e || e.trim().length === 0))
|
|
179
179
|
return { valid: !1, error: "Input is required" };
|
|
180
180
|
if (typeof e != "string" || !e)
|
|
181
181
|
return { valid: !0, sanitized: "" };
|
|
182
|
-
let
|
|
183
|
-
return
|
|
182
|
+
let o = e.trim();
|
|
183
|
+
return o.length > t ? { valid: !1, error: `Input must be less than ${t} characters` } : (n || (o = o.replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\//g, "/")), o = o.replace(/[\x00-\x1F\x7F]/g, ""), { valid: !0, sanitized: o });
|
|
184
184
|
}
|
|
185
|
-
function
|
|
185
|
+
function Mr(e) {
|
|
186
186
|
return e.valid === !0 && e.sanitized !== void 0;
|
|
187
187
|
}
|
|
188
188
|
class Oe {
|
|
189
189
|
constructor() {
|
|
190
|
-
|
|
190
|
+
b(this, "tokens", /* @__PURE__ */ new Map());
|
|
191
191
|
}
|
|
192
192
|
get(r) {
|
|
193
193
|
const t = this.tokens.get(r);
|
|
@@ -208,8 +208,8 @@ class Oe {
|
|
|
208
208
|
}
|
|
209
209
|
class Te {
|
|
210
210
|
constructor(r, t = 32) {
|
|
211
|
-
|
|
212
|
-
|
|
211
|
+
b(this, "store");
|
|
212
|
+
b(this, "tokenLength");
|
|
213
213
|
this.store = r || new Oe(), this.tokenLength = t;
|
|
214
214
|
}
|
|
215
215
|
/**
|
|
@@ -242,7 +242,7 @@ class Te {
|
|
|
242
242
|
this.store.delete(r);
|
|
243
243
|
}
|
|
244
244
|
}
|
|
245
|
-
function
|
|
245
|
+
function Vr(e) {
|
|
246
246
|
return new Te(e);
|
|
247
247
|
}
|
|
248
248
|
function Ie(e) {
|
|
@@ -257,13 +257,13 @@ function Ie(e) {
|
|
|
257
257
|
};
|
|
258
258
|
return e.replace(/[&<>"']/g, (t) => r[t] || t);
|
|
259
259
|
}
|
|
260
|
-
function
|
|
260
|
+
function jr(e) {
|
|
261
261
|
return typeof e != "string" ? "" : e.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, "").replace(/on\w+\s*=\s*["'][^"']*["']/gi, "").replace(/javascript:/gi, "");
|
|
262
262
|
}
|
|
263
|
-
function
|
|
263
|
+
function zr(e) {
|
|
264
264
|
return typeof e != "string" ? "" : Ie(e.trim());
|
|
265
265
|
}
|
|
266
|
-
function
|
|
266
|
+
function $r(e) {
|
|
267
267
|
return typeof e != "string" ? !1 : [
|
|
268
268
|
/<script/i,
|
|
269
269
|
/javascript:/i,
|
|
@@ -295,35 +295,35 @@ function Q(e, r) {
|
|
|
295
295
|
t |= e.charCodeAt(n) ^ r.charCodeAt(n);
|
|
296
296
|
return t === 0;
|
|
297
297
|
}
|
|
298
|
-
function
|
|
298
|
+
function Wr(e, r) {
|
|
299
299
|
return Q(e, r);
|
|
300
300
|
}
|
|
301
|
-
function
|
|
301
|
+
function qr(e) {
|
|
302
302
|
return typeof e != "string" ? "" : e.trim().replace(/[<>]/g, "");
|
|
303
303
|
}
|
|
304
304
|
const Pe = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
305
|
-
function
|
|
305
|
+
function Br(e) {
|
|
306
306
|
return typeof e == "string" && Pe.test(e);
|
|
307
307
|
}
|
|
308
308
|
function Ce(e) {
|
|
309
309
|
return !e.success && !!e.error;
|
|
310
310
|
}
|
|
311
|
-
function
|
|
311
|
+
function Hr(e) {
|
|
312
312
|
return e.requires2FA === !0 || e.errorCode === m.TWO_FA_REQUIRED;
|
|
313
313
|
}
|
|
314
|
-
function
|
|
314
|
+
function Gr(e, r) {
|
|
315
315
|
return e.error ? e.error : r || "Authentication failed";
|
|
316
316
|
}
|
|
317
|
-
function
|
|
317
|
+
function Kr(e) {
|
|
318
318
|
return e.errorCode;
|
|
319
319
|
}
|
|
320
|
-
function
|
|
320
|
+
function Xr(e) {
|
|
321
321
|
return e.success === !0 && !!e.user;
|
|
322
322
|
}
|
|
323
|
-
function
|
|
323
|
+
function Jr(e, r) {
|
|
324
324
|
return e.errorCode === r;
|
|
325
325
|
}
|
|
326
|
-
function
|
|
326
|
+
function Yr(e) {
|
|
327
327
|
if (!Ce(e)) return !1;
|
|
328
328
|
const r = [
|
|
329
329
|
m.NETWORK_ERROR,
|
|
@@ -332,7 +332,7 @@ function Kr(e) {
|
|
|
332
332
|
];
|
|
333
333
|
return e.errorCode ? r.includes(e.errorCode) : !1;
|
|
334
334
|
}
|
|
335
|
-
function
|
|
335
|
+
function Qr(e) {
|
|
336
336
|
if (e.error) return e.error;
|
|
337
337
|
switch (e.errorCode) {
|
|
338
338
|
case m.INVALID_CREDENTIALS:
|
|
@@ -360,7 +360,7 @@ function Xr(e) {
|
|
|
360
360
|
return "An unexpected error occurred. Please try again.";
|
|
361
361
|
}
|
|
362
362
|
}
|
|
363
|
-
async function
|
|
363
|
+
async function Zr(e, r, t) {
|
|
364
364
|
return e.signIn(r, t);
|
|
365
365
|
}
|
|
366
366
|
const Z = {
|
|
@@ -396,31 +396,31 @@ const Z = {
|
|
|
396
396
|
function j(e) {
|
|
397
397
|
return Z[e] ?? null;
|
|
398
398
|
}
|
|
399
|
-
function
|
|
399
|
+
function et(e) {
|
|
400
400
|
return e in Z;
|
|
401
401
|
}
|
|
402
|
-
function
|
|
402
|
+
function Ne(e, r, t, n) {
|
|
403
403
|
const s = j(e);
|
|
404
404
|
if (!s)
|
|
405
405
|
throw new Error(`Unknown OAuth provider: ${e}`);
|
|
406
406
|
if (!r.clientId)
|
|
407
407
|
throw new Error(`OAuth provider "${e}" is missing clientId`);
|
|
408
|
-
const
|
|
408
|
+
const o = r.redirectUri ?? `${t}/api/auth/callback/${e}`, i = r.scopes ?? s.defaultScopes, a = new URLSearchParams({
|
|
409
409
|
client_id: r.clientId,
|
|
410
|
-
redirect_uri:
|
|
410
|
+
redirect_uri: o,
|
|
411
411
|
response_type: "code",
|
|
412
|
-
scope: Array.isArray(
|
|
412
|
+
scope: Array.isArray(i) ? i.join(" ") : String(i),
|
|
413
413
|
state: n
|
|
414
414
|
});
|
|
415
415
|
if (s.defaultParams)
|
|
416
|
-
for (const [
|
|
417
|
-
a.append(
|
|
416
|
+
for (const [u, l] of Object.entries(s.defaultParams))
|
|
417
|
+
a.append(u, l);
|
|
418
418
|
if (r.params)
|
|
419
|
-
for (const [
|
|
420
|
-
a.set(
|
|
419
|
+
for (const [u, l] of Object.entries(r.params))
|
|
420
|
+
a.set(u, l);
|
|
421
421
|
return `${s.authorizationUrl}?${a.toString()}`;
|
|
422
422
|
}
|
|
423
|
-
async function
|
|
423
|
+
async function be(e, r, t, n) {
|
|
424
424
|
const s = j(e);
|
|
425
425
|
if (!s)
|
|
426
426
|
throw new Error(`Unknown OAuth provider: ${e}`);
|
|
@@ -428,41 +428,41 @@ async function Ue(e, r, t, n) {
|
|
|
428
428
|
throw new Error("Authorization code is required");
|
|
429
429
|
if (!r.clientId)
|
|
430
430
|
throw new Error(`OAuth provider "${e}" is missing clientId`);
|
|
431
|
-
const
|
|
431
|
+
const o = new URLSearchParams({
|
|
432
432
|
client_id: r.clientId,
|
|
433
433
|
code: t,
|
|
434
434
|
redirect_uri: n,
|
|
435
435
|
grant_type: "authorization_code"
|
|
436
436
|
});
|
|
437
|
-
r.clientSecret &&
|
|
437
|
+
r.clientSecret && o.append("client_secret", r.clientSecret);
|
|
438
438
|
try {
|
|
439
|
-
const
|
|
439
|
+
const i = await fetch(s.tokenUrl, {
|
|
440
440
|
method: "POST",
|
|
441
441
|
headers: {
|
|
442
442
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
443
443
|
Accept: "application/json"
|
|
444
444
|
},
|
|
445
|
-
body:
|
|
445
|
+
body: o.toString()
|
|
446
446
|
});
|
|
447
|
-
if (!
|
|
448
|
-
const
|
|
449
|
-
let
|
|
447
|
+
if (!i.ok) {
|
|
448
|
+
const u = await i.text();
|
|
449
|
+
let l = `Failed to exchange code for tokens: ${u}`;
|
|
450
450
|
try {
|
|
451
|
-
const
|
|
452
|
-
|
|
451
|
+
const g = JSON.parse(u);
|
|
452
|
+
l = g.error_description ?? g.error ?? l;
|
|
453
453
|
} catch {
|
|
454
454
|
}
|
|
455
|
-
throw new Error(
|
|
455
|
+
throw new Error(l);
|
|
456
456
|
}
|
|
457
|
-
const a = await
|
|
458
|
-
if (!
|
|
457
|
+
const a = await i.json();
|
|
458
|
+
if (!Ue(a))
|
|
459
459
|
throw new Error("Invalid token exchange response format");
|
|
460
460
|
return a;
|
|
461
|
-
} catch (
|
|
462
|
-
throw
|
|
461
|
+
} catch (i) {
|
|
462
|
+
throw i instanceof Error ? i : new Error(`OAuth token exchange failed: ${String(i)}`);
|
|
463
463
|
}
|
|
464
464
|
}
|
|
465
|
-
function
|
|
465
|
+
function Ue(e) {
|
|
466
466
|
return typeof e == "object" && e !== null && "access_token" in e && typeof e.access_token == "string";
|
|
467
467
|
}
|
|
468
468
|
async function Fe(e, r) {
|
|
@@ -479,14 +479,14 @@ async function Fe(e, r) {
|
|
|
479
479
|
}
|
|
480
480
|
});
|
|
481
481
|
if (!n.ok) {
|
|
482
|
-
const
|
|
483
|
-
let
|
|
482
|
+
const o = await n.text();
|
|
483
|
+
let i = `Failed to fetch user info: ${o}`;
|
|
484
484
|
try {
|
|
485
|
-
const a = JSON.parse(
|
|
486
|
-
|
|
485
|
+
const a = JSON.parse(o);
|
|
486
|
+
i = a.error_description ?? a.error ?? i;
|
|
487
487
|
} catch {
|
|
488
488
|
}
|
|
489
|
-
throw new Error(
|
|
489
|
+
throw new Error(i);
|
|
490
490
|
}
|
|
491
491
|
const s = await n.json();
|
|
492
492
|
return xe(e, s, r);
|
|
@@ -526,8 +526,8 @@ async function Le(e, r) {
|
|
|
526
526
|
headers: { Authorization: `Bearer ${r}` }
|
|
527
527
|
});
|
|
528
528
|
if (s.ok) {
|
|
529
|
-
const
|
|
530
|
-
t = (
|
|
529
|
+
const o = await s.json(), i = o.find((a) => a.primary) ?? o[0];
|
|
530
|
+
t = (i == null ? void 0 : i.email) ?? `${String(e.login ?? "user")}@users.noreply.github.com`, n = { ...e, emails: o };
|
|
531
531
|
} else
|
|
532
532
|
t = `${String(e.login ?? "user")}@users.noreply.github.com`;
|
|
533
533
|
} catch {
|
|
@@ -574,12 +574,100 @@ function je(e) {
|
|
|
574
574
|
rawProfile: e
|
|
575
575
|
};
|
|
576
576
|
}
|
|
577
|
-
function
|
|
577
|
+
function rt(e) {
|
|
578
578
|
return typeof e == "object" && e !== null && "clientId" in e && typeof e.clientId == "string";
|
|
579
579
|
}
|
|
580
|
-
|
|
580
|
+
const ze = "__mulguard_oauth_state", $e = 10 * 60 * 1e3;
|
|
581
|
+
function We(e) {
|
|
582
|
+
const r = e.cookieName || ze, t = e.ttl || $e, n = process.env.NODE_ENV === "production", s = e.secure ?? n, o = e.sameSite || "strict", i = e.cookieHandler, a = (u) => ({
|
|
583
|
+
httpOnly: !0,
|
|
584
|
+
secure: s,
|
|
585
|
+
sameSite: o,
|
|
586
|
+
maxAge: Math.floor(u / 1e3),
|
|
587
|
+
// Convert to seconds
|
|
588
|
+
path: "/"
|
|
589
|
+
});
|
|
590
|
+
return {
|
|
591
|
+
async set(u, l, g) {
|
|
592
|
+
const w = JSON.stringify({
|
|
593
|
+
state: u,
|
|
594
|
+
provider: l.provider,
|
|
595
|
+
expiresAt: l.expiresAt
|
|
596
|
+
});
|
|
597
|
+
await Promise.resolve(
|
|
598
|
+
i.setCookie(r, w, a(t))
|
|
599
|
+
);
|
|
600
|
+
},
|
|
601
|
+
async get(u) {
|
|
602
|
+
const l = await Promise.resolve(i.getCookie(r));
|
|
603
|
+
if (!l)
|
|
604
|
+
return null;
|
|
605
|
+
try {
|
|
606
|
+
const g = JSON.parse(l);
|
|
607
|
+
return g.state !== u ? null : g.expiresAt < Date.now() ? (await Promise.resolve(
|
|
608
|
+
i.deleteCookie(r, { path: "/" })
|
|
609
|
+
), null) : {
|
|
610
|
+
provider: g.provider,
|
|
611
|
+
expiresAt: g.expiresAt
|
|
612
|
+
};
|
|
613
|
+
} catch {
|
|
614
|
+
return await Promise.resolve(
|
|
615
|
+
i.deleteCookie(r, { path: "/" })
|
|
616
|
+
), null;
|
|
617
|
+
}
|
|
618
|
+
},
|
|
619
|
+
async delete(u) {
|
|
620
|
+
await this.get(u) && await Promise.resolve(
|
|
621
|
+
i.deleteCookie(r, { path: "/" })
|
|
622
|
+
);
|
|
623
|
+
},
|
|
624
|
+
async cleanup() {
|
|
625
|
+
}
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
function tt() {
|
|
629
|
+
return We({
|
|
630
|
+
cookieHandler: {
|
|
631
|
+
async getCookie(e) {
|
|
632
|
+
var r;
|
|
633
|
+
try {
|
|
634
|
+
const { cookies: t } = await import("next/headers");
|
|
635
|
+
return ((r = (await t()).get(e)) == null ? void 0 : r.value) || null;
|
|
636
|
+
} catch {
|
|
637
|
+
return null;
|
|
638
|
+
}
|
|
639
|
+
},
|
|
640
|
+
async setCookie(e, r, t) {
|
|
641
|
+
try {
|
|
642
|
+
const { cookies: n } = await import("next/headers");
|
|
643
|
+
(await n()).set(e, r, {
|
|
644
|
+
httpOnly: t.httpOnly ?? !0,
|
|
645
|
+
secure: t.secure ?? process.env.NODE_ENV === "production",
|
|
646
|
+
sameSite: t.sameSite || "strict",
|
|
647
|
+
maxAge: t.maxAge,
|
|
648
|
+
path: t.path || "/"
|
|
649
|
+
});
|
|
650
|
+
} catch (n) {
|
|
651
|
+
console.warn("[Mulguard] Failed to set OAuth state cookie:", n);
|
|
652
|
+
}
|
|
653
|
+
},
|
|
654
|
+
async deleteCookie(e, r) {
|
|
655
|
+
try {
|
|
656
|
+
const { cookies: t } = await import("next/headers");
|
|
657
|
+
(await t()).set(e, "", {
|
|
658
|
+
maxAge: 0,
|
|
659
|
+
expires: /* @__PURE__ */ new Date(0),
|
|
660
|
+
path: (r == null ? void 0 : r.path) || "/"
|
|
661
|
+
});
|
|
662
|
+
} catch {
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
class qe {
|
|
581
669
|
constructor() {
|
|
582
|
-
|
|
670
|
+
b(this, "states", /* @__PURE__ */ new Map());
|
|
583
671
|
}
|
|
584
672
|
set(r, t, n) {
|
|
585
673
|
this.states.set(r, t), this.cleanup();
|
|
@@ -597,25 +685,25 @@ class ze {
|
|
|
597
685
|
n.expiresAt < r && this.states.delete(t);
|
|
598
686
|
}
|
|
599
687
|
}
|
|
600
|
-
function
|
|
601
|
-
return new
|
|
688
|
+
function Be() {
|
|
689
|
+
return new qe();
|
|
602
690
|
}
|
|
603
|
-
function
|
|
691
|
+
function nt(e, r = "mulguard:oauth:state:") {
|
|
604
692
|
const t = (s) => `${r}${s}`, n = async (s) => {
|
|
605
|
-
const
|
|
606
|
-
await e.del(
|
|
693
|
+
const o = t(s);
|
|
694
|
+
await e.del(o);
|
|
607
695
|
};
|
|
608
696
|
return {
|
|
609
|
-
async set(s,
|
|
610
|
-
const a = t(s),
|
|
611
|
-
await e.set(a,
|
|
697
|
+
async set(s, o, i) {
|
|
698
|
+
const a = t(s), u = JSON.stringify(o);
|
|
699
|
+
await e.set(a, u, "EX", Math.floor(i / 1e3));
|
|
612
700
|
},
|
|
613
701
|
async get(s) {
|
|
614
|
-
const
|
|
615
|
-
if (!
|
|
702
|
+
const o = t(s), i = await e.get(o);
|
|
703
|
+
if (!i)
|
|
616
704
|
return null;
|
|
617
705
|
try {
|
|
618
|
-
const a = JSON.parse(
|
|
706
|
+
const a = JSON.parse(i);
|
|
619
707
|
return a.expiresAt < Date.now() ? (await n(s), null) : a;
|
|
620
708
|
} catch {
|
|
621
709
|
return await n(s), null;
|
|
@@ -626,14 +714,14 @@ function Zr(e, r = "mulguard:oauth:state:") {
|
|
|
626
714
|
},
|
|
627
715
|
async cleanup() {
|
|
628
716
|
try {
|
|
629
|
-
const s = await e.keys(`${r}*`),
|
|
630
|
-
for (const
|
|
631
|
-
const a = await e.get(
|
|
717
|
+
const s = await e.keys(`${r}*`), o = Date.now();
|
|
718
|
+
for (const i of s) {
|
|
719
|
+
const a = await e.get(i);
|
|
632
720
|
if (a)
|
|
633
721
|
try {
|
|
634
|
-
JSON.parse(a).expiresAt <
|
|
722
|
+
JSON.parse(a).expiresAt < o && await e.del(i);
|
|
635
723
|
} catch {
|
|
636
|
-
await e.del(
|
|
724
|
+
await e.del(i);
|
|
637
725
|
}
|
|
638
726
|
}
|
|
639
727
|
} catch (s) {
|
|
@@ -646,92 +734,92 @@ function D(e) {
|
|
|
646
734
|
return e.success === !0 && e.user !== void 0 && e.session !== void 0;
|
|
647
735
|
}
|
|
648
736
|
var ee = /* @__PURE__ */ ((e) => (e[e.DEBUG = 0] = "DEBUG", e[e.INFO = 1] = "INFO", e[e.WARN = 2] = "WARN", e[e.ERROR = 3] = "ERROR", e))(ee || {});
|
|
649
|
-
const
|
|
650
|
-
function
|
|
737
|
+
const He = process.env.NODE_ENV === "development" ? 0 : 1;
|
|
738
|
+
function Ge(e = {}) {
|
|
651
739
|
const {
|
|
652
740
|
enabled: r = process.env.NODE_ENV === "development",
|
|
653
|
-
level: t =
|
|
741
|
+
level: t = He,
|
|
654
742
|
context: n,
|
|
655
|
-
formatter: s =
|
|
656
|
-
} = e,
|
|
743
|
+
formatter: s = Ke
|
|
744
|
+
} = e, o = (a) => r && a >= t, i = (a, u, l, g) => ({
|
|
657
745
|
level: a,
|
|
658
|
-
message:
|
|
746
|
+
message: u,
|
|
659
747
|
timestamp: /* @__PURE__ */ new Date(),
|
|
660
748
|
context: n,
|
|
661
|
-
data:
|
|
662
|
-
error:
|
|
749
|
+
data: l ? Xe(l) : void 0,
|
|
750
|
+
error: g
|
|
663
751
|
});
|
|
664
752
|
return {
|
|
665
|
-
debug: (a,
|
|
666
|
-
if (
|
|
753
|
+
debug: (a, u) => {
|
|
754
|
+
if (o(
|
|
667
755
|
0
|
|
668
756
|
/* DEBUG */
|
|
669
757
|
)) {
|
|
670
|
-
const
|
|
671
|
-
console.debug(s(
|
|
758
|
+
const l = i(0, a, u);
|
|
759
|
+
console.debug(s(l));
|
|
672
760
|
}
|
|
673
761
|
},
|
|
674
|
-
info: (a,
|
|
675
|
-
if (
|
|
762
|
+
info: (a, u) => {
|
|
763
|
+
if (o(
|
|
676
764
|
1
|
|
677
765
|
/* INFO */
|
|
678
766
|
)) {
|
|
679
|
-
const
|
|
680
|
-
console.info(s(
|
|
767
|
+
const l = i(1, a, u);
|
|
768
|
+
console.info(s(l));
|
|
681
769
|
}
|
|
682
770
|
},
|
|
683
|
-
warn: (a,
|
|
684
|
-
if (
|
|
771
|
+
warn: (a, u) => {
|
|
772
|
+
if (o(
|
|
685
773
|
2
|
|
686
774
|
/* WARN */
|
|
687
775
|
)) {
|
|
688
|
-
const
|
|
689
|
-
console.warn(s(
|
|
776
|
+
const l = i(2, a, u);
|
|
777
|
+
console.warn(s(l));
|
|
690
778
|
}
|
|
691
779
|
},
|
|
692
|
-
error: (a,
|
|
693
|
-
if (
|
|
780
|
+
error: (a, u) => {
|
|
781
|
+
if (o(
|
|
694
782
|
3
|
|
695
783
|
/* ERROR */
|
|
696
784
|
)) {
|
|
697
|
-
const
|
|
698
|
-
console.error(s(
|
|
785
|
+
const l = u instanceof Error ? u : void 0, g = u instanceof Error ? void 0 : u, w = i(3, a, g, l);
|
|
786
|
+
console.error(s(w)), l && console.error(l);
|
|
699
787
|
}
|
|
700
788
|
}
|
|
701
789
|
};
|
|
702
790
|
}
|
|
703
|
-
function
|
|
791
|
+
function Ke(e) {
|
|
704
792
|
const r = e.timestamp.toISOString(), t = ee[e.level], n = e.context ? `[${e.context}]` : "", s = e.data ? ` ${JSON.stringify(e.data)}` : "";
|
|
705
793
|
return `${r} [${t}]${n} ${e.message}${s}`;
|
|
706
794
|
}
|
|
707
|
-
function
|
|
795
|
+
function Xe(e) {
|
|
708
796
|
const r = /* @__PURE__ */ new Set(["password", "token", "secret", "key", "accessToken", "refreshToken"]), t = {};
|
|
709
797
|
for (const [n, s] of Object.entries(e))
|
|
710
798
|
if (r.has(n.toLowerCase()))
|
|
711
799
|
t[n] = "***REDACTED***";
|
|
712
800
|
else if (typeof s == "string" && n.toLowerCase().includes("email")) {
|
|
713
|
-
const
|
|
714
|
-
if (
|
|
715
|
-
const
|
|
716
|
-
t[n] =
|
|
801
|
+
const o = s.split("@");
|
|
802
|
+
if (o.length === 2 && o[0]) {
|
|
803
|
+
const i = o[0].substring(0, 3) + "***@" + o[1];
|
|
804
|
+
t[n] = i;
|
|
717
805
|
} else
|
|
718
806
|
t[n] = s;
|
|
719
807
|
} else
|
|
720
808
|
t[n] = s;
|
|
721
809
|
return t;
|
|
722
810
|
}
|
|
723
|
-
const I =
|
|
724
|
-
function
|
|
811
|
+
const I = Ge();
|
|
812
|
+
function Je(e, r, t, n = {}) {
|
|
725
813
|
const {
|
|
726
814
|
enabled: s = !0,
|
|
727
|
-
maxRetries:
|
|
728
|
-
retryDelay:
|
|
815
|
+
maxRetries: o = 1,
|
|
816
|
+
retryDelay: i = 1e3,
|
|
729
817
|
rateLimit: a = 3,
|
|
730
|
-
autoSignOutOnFailure:
|
|
731
|
-
redirectToLogin:
|
|
732
|
-
autoRedirectOnFailure:
|
|
818
|
+
autoSignOutOnFailure: u = !0,
|
|
819
|
+
redirectToLogin: l = "/login",
|
|
820
|
+
autoRedirectOnFailure: g = !0
|
|
733
821
|
} = n;
|
|
734
|
-
let
|
|
822
|
+
let w = null, R = !1;
|
|
735
823
|
const S = [], v = [], y = 60 * 1e3;
|
|
736
824
|
let h = 0, T = !1, _ = null;
|
|
737
825
|
const L = 2, M = 60 * 1e3;
|
|
@@ -751,13 +839,13 @@ function Ge(e, r, t, n = {}) {
|
|
|
751
839
|
}
|
|
752
840
|
return v.length >= a ? !1 : (v.push(k), !0);
|
|
753
841
|
}
|
|
754
|
-
function
|
|
842
|
+
function f() {
|
|
755
843
|
h++, h >= L && (T = !0, _ = Date.now() + M, process.env.NODE_ENV === "development" && console.warn("[TokenRefreshManager] Circuit breaker opened - too many consecutive failures"));
|
|
756
844
|
}
|
|
757
845
|
function d() {
|
|
758
846
|
h = 0, T = !1, _ = null;
|
|
759
847
|
}
|
|
760
|
-
async function
|
|
848
|
+
async function A(k = 1) {
|
|
761
849
|
if (!s)
|
|
762
850
|
return null;
|
|
763
851
|
if (!c())
|
|
@@ -766,12 +854,12 @@ function Ge(e, r, t, n = {}) {
|
|
|
766
854
|
const p = await e();
|
|
767
855
|
if (p)
|
|
768
856
|
return d(), P(p), n.onTokenRefreshed && await Promise.resolve(n.onTokenRefreshed(p)), p;
|
|
769
|
-
if (
|
|
770
|
-
return await $(
|
|
857
|
+
if (f(), k < o)
|
|
858
|
+
return await $(i * k), A(k + 1);
|
|
771
859
|
throw new Error("Token refresh failed: refresh function returned null");
|
|
772
860
|
} catch (p) {
|
|
773
|
-
if (
|
|
774
|
-
return await $(
|
|
861
|
+
if (f(), k < o && C(p))
|
|
862
|
+
return await $(i * k), A(k + 1);
|
|
775
863
|
throw p;
|
|
776
864
|
}
|
|
777
865
|
}
|
|
@@ -788,25 +876,25 @@ function Ge(e, r, t, n = {}) {
|
|
|
788
876
|
function P(k) {
|
|
789
877
|
const p = [...S];
|
|
790
878
|
S.length = 0;
|
|
791
|
-
for (const { resolve:
|
|
792
|
-
|
|
879
|
+
for (const { resolve: N } of p)
|
|
880
|
+
N(k);
|
|
793
881
|
}
|
|
794
882
|
function z(k) {
|
|
795
883
|
const p = [...S];
|
|
796
884
|
S.length = 0;
|
|
797
|
-
for (const { reject:
|
|
798
|
-
|
|
885
|
+
for (const { reject: N } of p)
|
|
886
|
+
N(k);
|
|
799
887
|
}
|
|
800
888
|
function $(k) {
|
|
801
889
|
return new Promise((p) => setTimeout(p, k));
|
|
802
890
|
}
|
|
803
891
|
async function W(k) {
|
|
804
892
|
try {
|
|
805
|
-
if (n.onTokenRefreshFailed && await Promise.resolve(n.onTokenRefreshFailed(k)),
|
|
893
|
+
if (n.onTokenRefreshFailed && await Promise.resolve(n.onTokenRefreshFailed(k)), u && (await t(), await r(), g && typeof window < "u")) {
|
|
806
894
|
let p = !0;
|
|
807
895
|
if (n.onBeforeRedirect && (p = await Promise.resolve(n.onBeforeRedirect(k))), p) {
|
|
808
|
-
const
|
|
809
|
-
|
|
896
|
+
const N = new URL(l, window.location.origin);
|
|
897
|
+
N.searchParams.set("reason", "session_expired"), N.searchParams.set("redirect", window.location.pathname + window.location.search), window.location.href = N.toString();
|
|
810
898
|
}
|
|
811
899
|
}
|
|
812
900
|
} catch (p) {
|
|
@@ -818,22 +906,22 @@ function Ge(e, r, t, n = {}) {
|
|
|
818
906
|
* Refresh token with single refresh queue
|
|
819
907
|
*/
|
|
820
908
|
async refreshToken() {
|
|
821
|
-
return s ?
|
|
822
|
-
throw
|
|
909
|
+
return s ? w || (R = !0, w = A().then((k) => (R = !1, w = null, k)).catch((k) => {
|
|
910
|
+
throw R = !1, w = null, z(k), W(k).catch(() => {
|
|
823
911
|
}), k;
|
|
824
|
-
}),
|
|
912
|
+
}), w) : null;
|
|
825
913
|
},
|
|
826
914
|
/**
|
|
827
915
|
* Check if refresh is in progress
|
|
828
916
|
*/
|
|
829
917
|
isRefreshing() {
|
|
830
|
-
return
|
|
918
|
+
return R;
|
|
831
919
|
},
|
|
832
920
|
/**
|
|
833
921
|
* Wait for current refresh to complete
|
|
834
922
|
*/
|
|
835
923
|
async waitForRefresh() {
|
|
836
|
-
return
|
|
924
|
+
return w ? new Promise((k, p) => {
|
|
837
925
|
S.push({ resolve: k, reject: p });
|
|
838
926
|
}) : null;
|
|
839
927
|
},
|
|
@@ -841,7 +929,7 @@ function Ge(e, r, t, n = {}) {
|
|
|
841
929
|
* Clear state
|
|
842
930
|
*/
|
|
843
931
|
clear() {
|
|
844
|
-
|
|
932
|
+
w = null, R = !1, v.length = 0, d(), z(new Error("Token refresh manager cleared"));
|
|
845
933
|
},
|
|
846
934
|
/**
|
|
847
935
|
* Handle token refresh failure
|
|
@@ -851,7 +939,7 @@ function Ge(e, r, t, n = {}) {
|
|
|
851
939
|
}
|
|
852
940
|
};
|
|
853
941
|
}
|
|
854
|
-
function
|
|
942
|
+
function Ye() {
|
|
855
943
|
const e = process.env.NODE_ENV === "production";
|
|
856
944
|
return {
|
|
857
945
|
cookieName: "__mulguard_session",
|
|
@@ -864,7 +952,7 @@ function Ke() {
|
|
|
864
952
|
path: "/"
|
|
865
953
|
};
|
|
866
954
|
}
|
|
867
|
-
function
|
|
955
|
+
function Qe() {
|
|
868
956
|
return {
|
|
869
957
|
enabled: !0,
|
|
870
958
|
refreshThreshold: 300,
|
|
@@ -879,90 +967,90 @@ function Xe() {
|
|
|
879
967
|
autoRedirectOnFailure: !0
|
|
880
968
|
};
|
|
881
969
|
}
|
|
882
|
-
function
|
|
970
|
+
function Ze() {
|
|
883
971
|
return process.env.NEXT_PUBLIC_URL ?? (process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : "http://localhost:3000");
|
|
884
972
|
}
|
|
885
|
-
function
|
|
886
|
-
const { sessionConfig: r, cacheTtl: t, getSessionAction: n, onSessionExpired: s, onError:
|
|
973
|
+
function er(e) {
|
|
974
|
+
const { sessionConfig: r, cacheTtl: t, getSessionAction: n, onSessionExpired: s, onError: o } = e, i = r.cookieName ?? "__mulguard_session";
|
|
887
975
|
let a = null;
|
|
888
|
-
const
|
|
976
|
+
const u = async () => {
|
|
889
977
|
const y = Date.now();
|
|
890
978
|
if (a && y - a.timestamp < t)
|
|
891
979
|
return a.session;
|
|
892
980
|
if (n)
|
|
893
981
|
try {
|
|
894
982
|
const h = await n();
|
|
895
|
-
if (h &&
|
|
983
|
+
if (h && U(h))
|
|
896
984
|
return a = { session: h, timestamp: y }, h;
|
|
897
|
-
h && !
|
|
985
|
+
h && !U(h) && (await g(), a = null);
|
|
898
986
|
} catch (h) {
|
|
899
|
-
I.debug("getSession error", { error: h }),
|
|
987
|
+
I.debug("getSession error", { error: h }), o && await o(h instanceof Error ? h : new Error(String(h)), "getSession"), a = null;
|
|
900
988
|
}
|
|
901
989
|
try {
|
|
902
|
-
const h = await ce(
|
|
990
|
+
const h = await ce(i);
|
|
903
991
|
if (h)
|
|
904
992
|
try {
|
|
905
993
|
const T = JSON.parse(h);
|
|
906
|
-
if (
|
|
907
|
-
return T.expiresAt && new Date(T.expiresAt) < /* @__PURE__ */ new Date() ? (s && await s(T), await
|
|
908
|
-
await
|
|
994
|
+
if (U(T))
|
|
995
|
+
return T.expiresAt && new Date(T.expiresAt) < /* @__PURE__ */ new Date() ? (s && await s(T), await g(), a = null, null) : (a = { session: T, timestamp: y }, T);
|
|
996
|
+
await g(), a = null;
|
|
909
997
|
} catch {
|
|
910
|
-
await
|
|
998
|
+
await g(), a = null;
|
|
911
999
|
}
|
|
912
1000
|
} catch (h) {
|
|
913
1001
|
const T = h instanceof Error ? h.message : String(h);
|
|
914
|
-
!T.includes("request scope") && !T.includes("cookies") && (I.warn("getSession cookie error", { error: h }),
|
|
1002
|
+
!T.includes("request scope") && !T.includes("cookies") && (I.warn("getSession cookie error", { error: h }), o && await o(
|
|
915
1003
|
h instanceof Error ? h : new Error(String(h)),
|
|
916
1004
|
"getSession.cookie"
|
|
917
1005
|
));
|
|
918
1006
|
}
|
|
919
1007
|
return null;
|
|
920
|
-
},
|
|
921
|
-
if (!
|
|
1008
|
+
}, l = async (y) => {
|
|
1009
|
+
if (!U(y))
|
|
922
1010
|
return {
|
|
923
1011
|
success: !1,
|
|
924
1012
|
error: "Invalid session structure"
|
|
925
1013
|
};
|
|
926
1014
|
try {
|
|
927
|
-
const h = typeof y == "object" && "token" in y ? String(y.token) : JSON.stringify(y), T =
|
|
1015
|
+
const h = typeof y == "object" && "token" in y ? String(y.token) : JSON.stringify(y), T = ie(i, h, r), _ = await ae(T);
|
|
928
1016
|
return _.success && (a = { session: y, timestamp: Date.now() }), _;
|
|
929
1017
|
} catch (h) {
|
|
930
1018
|
const T = h instanceof Error ? h.message : "Failed to set session";
|
|
931
|
-
return I.error("setSession error", { error: h }),
|
|
1019
|
+
return I.error("setSession error", { error: h }), o && await o(h instanceof Error ? h : new Error(String(h)), "setSession"), {
|
|
932
1020
|
success: !1,
|
|
933
1021
|
error: T
|
|
934
1022
|
};
|
|
935
1023
|
}
|
|
936
|
-
},
|
|
1024
|
+
}, g = async () => {
|
|
937
1025
|
try {
|
|
938
|
-
await
|
|
1026
|
+
await oe(i, {
|
|
939
1027
|
path: r.path,
|
|
940
1028
|
domain: r.domain
|
|
941
1029
|
}), a = null;
|
|
942
1030
|
} catch (y) {
|
|
943
1031
|
I.warn("clearSessionCookie error", { error: y });
|
|
944
1032
|
}
|
|
945
|
-
},
|
|
946
|
-
const y = await
|
|
1033
|
+
}, w = async () => {
|
|
1034
|
+
const y = await u();
|
|
947
1035
|
return y != null && y.accessToken && typeof y.accessToken == "string" ? y.accessToken : null;
|
|
948
1036
|
};
|
|
949
1037
|
return {
|
|
950
|
-
getSession:
|
|
951
|
-
setSession:
|
|
952
|
-
clearSessionCookie:
|
|
953
|
-
getAccessToken:
|
|
1038
|
+
getSession: u,
|
|
1039
|
+
setSession: l,
|
|
1040
|
+
clearSessionCookie: g,
|
|
1041
|
+
getAccessToken: w,
|
|
954
1042
|
getRefreshToken: async () => {
|
|
955
|
-
const y = await
|
|
1043
|
+
const y = await u();
|
|
956
1044
|
return y != null && y.refreshToken && typeof y.refreshToken == "string" ? y.refreshToken : null;
|
|
957
1045
|
},
|
|
958
|
-
hasValidTokens: async () => !!await
|
|
1046
|
+
hasValidTokens: async () => !!await w(),
|
|
959
1047
|
clearCache: () => {
|
|
960
1048
|
a = null;
|
|
961
1049
|
},
|
|
962
|
-
getSessionConfig: () => ({ cookieName:
|
|
1050
|
+
getSessionConfig: () => ({ cookieName: i, config: r })
|
|
963
1051
|
};
|
|
964
1052
|
}
|
|
965
|
-
function
|
|
1053
|
+
function rr(e) {
|
|
966
1054
|
return async (r) => {
|
|
967
1055
|
try {
|
|
968
1056
|
if (!r || typeof r != "object")
|
|
@@ -1002,8 +1090,8 @@ function Qe(e) {
|
|
|
1002
1090
|
// Don't sanitize password (needed for hashing)
|
|
1003
1091
|
}, s = await e.actions.signIn.email(n);
|
|
1004
1092
|
if (D(s)) {
|
|
1005
|
-
const
|
|
1006
|
-
!
|
|
1093
|
+
const o = await e.saveSessionAfterAuth(s);
|
|
1094
|
+
!o.success && o.warning && I.warn("Session save warning", { warning: o.warning });
|
|
1007
1095
|
}
|
|
1008
1096
|
return s.success ? I.info("Sign in successful", {
|
|
1009
1097
|
email: n.email.substring(0, 3) + "***"
|
|
@@ -1024,7 +1112,7 @@ function Qe(e) {
|
|
|
1024
1112
|
}
|
|
1025
1113
|
};
|
|
1026
1114
|
}
|
|
1027
|
-
function
|
|
1115
|
+
function tr(e, r) {
|
|
1028
1116
|
return async (t) => {
|
|
1029
1117
|
if (!t || typeof t != "string")
|
|
1030
1118
|
throw new Error("Provider is required");
|
|
@@ -1040,11 +1128,11 @@ function Ze(e, r) {
|
|
|
1040
1128
|
throw new Error(
|
|
1041
1129
|
"OAuth sign in is not configured. Either provide oauth action in signIn, or configure providers.oauth in config."
|
|
1042
1130
|
);
|
|
1043
|
-
const
|
|
1044
|
-
return await r(
|
|
1131
|
+
const o = await e.actions.signIn.oauth(s);
|
|
1132
|
+
return await r(o.state, s), I.info("OAuth sign in initiated", { provider: s }), o;
|
|
1045
1133
|
};
|
|
1046
1134
|
}
|
|
1047
|
-
function
|
|
1135
|
+
function nr(e) {
|
|
1048
1136
|
return async (r, t) => {
|
|
1049
1137
|
if (!r || typeof r != "string")
|
|
1050
1138
|
return {
|
|
@@ -1074,8 +1162,8 @@ function er(e) {
|
|
|
1074
1162
|
try {
|
|
1075
1163
|
const s = await e.actions.signIn.otp(n.sanitized, t);
|
|
1076
1164
|
if (D(s)) {
|
|
1077
|
-
const
|
|
1078
|
-
!
|
|
1165
|
+
const o = await e.saveSessionAfterAuth(s);
|
|
1166
|
+
!o.success && o.warning && I.warn("Session save warning", { warning: o.warning });
|
|
1079
1167
|
}
|
|
1080
1168
|
return s.success ? I.info("OTP sign in successful", {
|
|
1081
1169
|
email: n.sanitized.substring(0, 3) + "***"
|
|
@@ -1097,7 +1185,7 @@ function er(e) {
|
|
|
1097
1185
|
}
|
|
1098
1186
|
};
|
|
1099
1187
|
}
|
|
1100
|
-
function
|
|
1188
|
+
function sr(e) {
|
|
1101
1189
|
return async (r) => {
|
|
1102
1190
|
if (!e.actions.signIn.passkey)
|
|
1103
1191
|
throw new Error("PassKey sign in is not configured. Provide passkey action in signIn.");
|
|
@@ -1119,38 +1207,38 @@ function rr(e) {
|
|
|
1119
1207
|
}
|
|
1120
1208
|
};
|
|
1121
1209
|
}
|
|
1122
|
-
function
|
|
1123
|
-
const t =
|
|
1124
|
-
return Object.assign(async (
|
|
1125
|
-
if (!
|
|
1210
|
+
function or(e, r) {
|
|
1211
|
+
const t = rr(e), n = tr(e, r), s = nr(e), o = sr(e);
|
|
1212
|
+
return Object.assign(async (u, l) => {
|
|
1213
|
+
if (!u || typeof u != "string")
|
|
1126
1214
|
throw new Error("Provider is required");
|
|
1127
|
-
const
|
|
1215
|
+
const g = X(u, {
|
|
1128
1216
|
maxLength: 50,
|
|
1129
1217
|
allowHtml: !1,
|
|
1130
1218
|
required: !0
|
|
1131
1219
|
});
|
|
1132
|
-
if (!
|
|
1220
|
+
if (!g.valid || !g.sanitized)
|
|
1133
1221
|
throw new Error("Invalid provider");
|
|
1134
|
-
const
|
|
1135
|
-
if (
|
|
1136
|
-
return n(
|
|
1137
|
-
if (
|
|
1138
|
-
return !
|
|
1222
|
+
const w = g.sanitized.toLowerCase();
|
|
1223
|
+
if (w === "google" || w === "github" || w === "apple" || w === "facebook" || typeof w == "string" && !["credentials", "otp", "passkey"].includes(w))
|
|
1224
|
+
return n(w);
|
|
1225
|
+
if (w === "credentials")
|
|
1226
|
+
return !l || !("email" in l) || !("password" in l) ? {
|
|
1139
1227
|
success: !1,
|
|
1140
1228
|
error: "Credentials are required",
|
|
1141
1229
|
errorCode: m.VALIDATION_ERROR
|
|
1142
|
-
} : t(
|
|
1143
|
-
if (
|
|
1144
|
-
if (!
|
|
1230
|
+
} : t(l);
|
|
1231
|
+
if (w === "otp") {
|
|
1232
|
+
if (!l || !("email" in l))
|
|
1145
1233
|
return {
|
|
1146
1234
|
success: !1,
|
|
1147
1235
|
error: "Email is required",
|
|
1148
1236
|
errorCode: m.VALIDATION_ERROR
|
|
1149
1237
|
};
|
|
1150
|
-
const
|
|
1151
|
-
return s(
|
|
1238
|
+
const R = l;
|
|
1239
|
+
return s(R.email, R.code);
|
|
1152
1240
|
}
|
|
1153
|
-
return
|
|
1241
|
+
return w === "passkey" ? o(l) : {
|
|
1154
1242
|
success: !1,
|
|
1155
1243
|
error: "Invalid provider",
|
|
1156
1244
|
errorCode: m.VALIDATION_ERROR
|
|
@@ -1158,11 +1246,11 @@ function tr(e, r) {
|
|
|
1158
1246
|
}, {
|
|
1159
1247
|
email: t,
|
|
1160
1248
|
oauth: e.actions.signIn.oauth ? n : void 0,
|
|
1161
|
-
passkey: e.actions.signIn.passkey ?
|
|
1249
|
+
passkey: e.actions.signIn.passkey ? o : void 0,
|
|
1162
1250
|
otp: e.actions.signIn.otp ? s : void 0
|
|
1163
1251
|
});
|
|
1164
1252
|
}
|
|
1165
|
-
function
|
|
1253
|
+
function ir(e) {
|
|
1166
1254
|
return async (r) => {
|
|
1167
1255
|
if (!e.actions.signUp)
|
|
1168
1256
|
throw new Error("Sign up is not configured. Provide signUp action in config.");
|
|
@@ -1184,22 +1272,22 @@ function nr(e) {
|
|
|
1184
1272
|
}
|
|
1185
1273
|
};
|
|
1186
1274
|
}
|
|
1187
|
-
function
|
|
1275
|
+
function ar(e, r) {
|
|
1188
1276
|
return async (t, n, s) => {
|
|
1189
|
-
const
|
|
1190
|
-
if (!
|
|
1277
|
+
const o = e.oauthProviders[t];
|
|
1278
|
+
if (!o)
|
|
1191
1279
|
return {
|
|
1192
1280
|
success: !1,
|
|
1193
1281
|
error: `OAuth provider "${t}" is not configured`,
|
|
1194
1282
|
errorCode: m.VALIDATION_ERROR
|
|
1195
1283
|
};
|
|
1196
1284
|
try {
|
|
1197
|
-
const
|
|
1198
|
-
id:
|
|
1199
|
-
email:
|
|
1200
|
-
name:
|
|
1201
|
-
avatar:
|
|
1202
|
-
emailVerified:
|
|
1285
|
+
const i = o.redirectUri ?? `${e.baseUrl}/api/auth/callback/${t}`, a = await be(t, o, n, i), u = await Fe(t, a.access_token), l = {
|
|
1286
|
+
id: u.id,
|
|
1287
|
+
email: u.email,
|
|
1288
|
+
name: u.name,
|
|
1289
|
+
avatar: u.avatar,
|
|
1290
|
+
emailVerified: u.emailVerified,
|
|
1203
1291
|
provider: t,
|
|
1204
1292
|
accessToken: a.access_token,
|
|
1205
1293
|
refreshToken: a.refresh_token,
|
|
@@ -1210,36 +1298,36 @@ function sr(e, r) {
|
|
|
1210
1298
|
token_type: a.token_type,
|
|
1211
1299
|
id_token: a.id_token
|
|
1212
1300
|
},
|
|
1213
|
-
rawProfile:
|
|
1301
|
+
rawProfile: u.rawProfile
|
|
1214
1302
|
};
|
|
1215
1303
|
if (e.callbacks.onOAuthUser) {
|
|
1216
|
-
const
|
|
1304
|
+
const g = await q(
|
|
1217
1305
|
e.callbacks.onOAuthUser,
|
|
1218
|
-
[
|
|
1306
|
+
[l, t],
|
|
1219
1307
|
e.onError
|
|
1220
1308
|
);
|
|
1221
|
-
if (!
|
|
1309
|
+
if (!g)
|
|
1222
1310
|
return {
|
|
1223
1311
|
success: !1,
|
|
1224
1312
|
error: "Failed to create or retrieve user",
|
|
1225
1313
|
errorCode: m.VALIDATION_ERROR
|
|
1226
1314
|
};
|
|
1227
|
-
const
|
|
1228
|
-
return await e.saveSession(
|
|
1315
|
+
const w = e.createSession(g, l, a);
|
|
1316
|
+
return await e.saveSession(w), e.callbacks.onSignIn && await q(
|
|
1229
1317
|
e.callbacks.onSignIn,
|
|
1230
|
-
[
|
|
1318
|
+
[w.user, w],
|
|
1231
1319
|
e.onError
|
|
1232
|
-
), { success: !0, user:
|
|
1320
|
+
), { success: !0, user: w.user, session: w };
|
|
1233
1321
|
}
|
|
1234
1322
|
return {
|
|
1235
1323
|
success: !1,
|
|
1236
1324
|
error: "OAuth user callback not implemented. Provide onOAuthUser callback or implement oauthCallback action.",
|
|
1237
1325
|
errorCode: m.VALIDATION_ERROR
|
|
1238
1326
|
};
|
|
1239
|
-
} catch (
|
|
1240
|
-
return I.error("OAuth callback failed", { provider: t, error:
|
|
1327
|
+
} catch (i) {
|
|
1328
|
+
return I.error("OAuth callback failed", { provider: t, error: i }), {
|
|
1241
1329
|
success: !1,
|
|
1242
|
-
error:
|
|
1330
|
+
error: i instanceof Error ? i.message : "OAuth callback failed",
|
|
1243
1331
|
errorCode: m.NETWORK_ERROR
|
|
1244
1332
|
};
|
|
1245
1333
|
}
|
|
@@ -1256,62 +1344,62 @@ async function q(e, r, t) {
|
|
|
1256
1344
|
), n;
|
|
1257
1345
|
}
|
|
1258
1346
|
}
|
|
1259
|
-
function
|
|
1347
|
+
function cr(e, r, t, n) {
|
|
1260
1348
|
if (Object.keys(e).length !== 0)
|
|
1261
1349
|
return async (s) => {
|
|
1262
|
-
const
|
|
1263
|
-
if (!
|
|
1350
|
+
const o = e[s];
|
|
1351
|
+
if (!o)
|
|
1264
1352
|
throw new Error(`OAuth provider "${s}" is not configured. Add it to providers.oauth in config.`);
|
|
1265
|
-
if (!
|
|
1353
|
+
if (!o.clientId)
|
|
1266
1354
|
throw new Error(`OAuth provider "${s}" is missing clientId`);
|
|
1267
|
-
const
|
|
1268
|
-
return { url: n(s,
|
|
1355
|
+
const i = t();
|
|
1356
|
+
return { url: n(s, o, r, i), state: i };
|
|
1269
1357
|
};
|
|
1270
1358
|
}
|
|
1271
|
-
function
|
|
1359
|
+
function st(e) {
|
|
1272
1360
|
var L, M;
|
|
1273
1361
|
const r = {
|
|
1274
|
-
...
|
|
1362
|
+
...Ye(),
|
|
1275
1363
|
...e.session
|
|
1276
|
-
}, t = e.actions, n = e.callbacks || {}, s = ((L = e.providers) == null ? void 0 : L.oauth) || {},
|
|
1277
|
-
...
|
|
1364
|
+
}, t = e.actions, n = e.callbacks || {}, s = ((L = e.providers) == null ? void 0 : L.oauth) || {}, o = Ze(), i = {
|
|
1365
|
+
...Qe(),
|
|
1278
1366
|
...e.tokenRefresh
|
|
1279
|
-
}, a = ((M = e.session) == null ? void 0 : M.cacheTtl) ?? e.sessionCacheTtl ?? 5e3,
|
|
1367
|
+
}, a = ((M = e.session) == null ? void 0 : M.cacheTtl) ?? e.sessionCacheTtl ?? 5e3, u = e.oauthStateStore || Be(), l = { ...t }, g = async (c, f) => {
|
|
1280
1368
|
const d = {
|
|
1281
|
-
provider:
|
|
1369
|
+
provider: f,
|
|
1282
1370
|
expiresAt: Date.now() + 6e5
|
|
1283
1371
|
// 10 minutes
|
|
1284
1372
|
};
|
|
1285
|
-
await Promise.resolve(
|
|
1286
|
-
},
|
|
1287
|
-
const d = await Promise.resolve(
|
|
1288
|
-
return d ? d.expiresAt < Date.now() ? (await Promise.resolve(
|
|
1289
|
-
},
|
|
1373
|
+
await Promise.resolve(u.set(c, d, 10 * 60 * 1e3)), u.cleanup && await Promise.resolve(u.cleanup());
|
|
1374
|
+
}, w = async (c, f) => {
|
|
1375
|
+
const d = await Promise.resolve(u.get(c));
|
|
1376
|
+
return d ? d.expiresAt < Date.now() ? (await Promise.resolve(u.delete(c)), !1) : d.provider !== f ? !1 : (await Promise.resolve(u.delete(c)), !0) : !1;
|
|
1377
|
+
}, R = cr(
|
|
1290
1378
|
s,
|
|
1291
|
-
|
|
1379
|
+
o,
|
|
1292
1380
|
_e,
|
|
1293
|
-
|
|
1381
|
+
Ne
|
|
1294
1382
|
);
|
|
1295
|
-
if (
|
|
1296
|
-
const c =
|
|
1297
|
-
|
|
1383
|
+
if (R && !l.signIn.oauth) {
|
|
1384
|
+
const c = l.signIn;
|
|
1385
|
+
l.signIn = {
|
|
1298
1386
|
...c,
|
|
1299
|
-
oauth: async (
|
|
1300
|
-
const d = await
|
|
1301
|
-
return await
|
|
1387
|
+
oauth: async (f) => {
|
|
1388
|
+
const d = await R(f);
|
|
1389
|
+
return await g(d.state, f), d;
|
|
1302
1390
|
}
|
|
1303
1391
|
};
|
|
1304
1392
|
}
|
|
1305
|
-
if (!
|
|
1393
|
+
if (!l.signIn || !l.signIn.email)
|
|
1306
1394
|
throw new Error("mulguard: signIn.email action is required");
|
|
1307
|
-
const S = async (c, ...
|
|
1395
|
+
const S = async (c, ...f) => {
|
|
1308
1396
|
if (c)
|
|
1309
1397
|
try {
|
|
1310
|
-
return await c(...
|
|
1398
|
+
return await c(...f);
|
|
1311
1399
|
} catch (d) {
|
|
1312
1400
|
throw n.onError && await n.onError(d instanceof Error ? d : new Error(String(d)), "callback"), d;
|
|
1313
1401
|
}
|
|
1314
|
-
}, v =
|
|
1402
|
+
}, v = er({
|
|
1315
1403
|
sessionConfig: r,
|
|
1316
1404
|
cacheTtl: a,
|
|
1317
1405
|
getSessionAction: t.getSession,
|
|
@@ -1320,45 +1408,45 @@ function et(e) {
|
|
|
1320
1408
|
}), y = async (c) => {
|
|
1321
1409
|
if (!D(c) || !c.session)
|
|
1322
1410
|
return { success: !0 };
|
|
1323
|
-
const
|
|
1324
|
-
return c.user && n.onSignIn && await S(n.onSignIn, c.user, c.session),
|
|
1411
|
+
const f = await v.setSession(c.session);
|
|
1412
|
+
return c.user && n.onSignIn && await S(n.onSignIn, c.user, c.session), f;
|
|
1325
1413
|
};
|
|
1326
|
-
if (Object.keys(s).length > 0 && !
|
|
1327
|
-
const c =
|
|
1414
|
+
if (Object.keys(s).length > 0 && !l.oauthCallback) {
|
|
1415
|
+
const c = ar(
|
|
1328
1416
|
{
|
|
1329
1417
|
oauthProviders: s,
|
|
1330
|
-
baseUrl:
|
|
1418
|
+
baseUrl: o,
|
|
1331
1419
|
callbacks: n,
|
|
1332
|
-
createSession: (
|
|
1420
|
+
createSession: (f, d, A) => ({
|
|
1333
1421
|
user: {
|
|
1334
|
-
...
|
|
1422
|
+
...f,
|
|
1335
1423
|
avatar: d.avatar,
|
|
1336
1424
|
emailVerified: d.emailVerified
|
|
1337
1425
|
},
|
|
1338
1426
|
expiresAt: new Date(Date.now() + (r.expiresIn || 604800) * 1e3),
|
|
1339
|
-
accessToken:
|
|
1340
|
-
refreshToken:
|
|
1427
|
+
accessToken: A.access_token,
|
|
1428
|
+
refreshToken: A.refresh_token,
|
|
1341
1429
|
tokenType: "Bearer",
|
|
1342
|
-
expiresIn:
|
|
1430
|
+
expiresIn: A.expires_in
|
|
1343
1431
|
}),
|
|
1344
|
-
saveSession: async (
|
|
1345
|
-
await v.setSession(
|
|
1432
|
+
saveSession: async (f) => {
|
|
1433
|
+
await v.setSession(f);
|
|
1346
1434
|
},
|
|
1347
1435
|
onError: n.onError
|
|
1348
1436
|
}
|
|
1349
1437
|
);
|
|
1350
|
-
|
|
1438
|
+
l.oauthCallback = c;
|
|
1351
1439
|
}
|
|
1352
|
-
const h =
|
|
1440
|
+
const h = or(
|
|
1353
1441
|
{
|
|
1354
|
-
actions:
|
|
1442
|
+
actions: l,
|
|
1355
1443
|
callbacks: n,
|
|
1356
1444
|
saveSessionAfterAuth: y,
|
|
1357
1445
|
onError: n.onError
|
|
1358
1446
|
},
|
|
1359
|
-
|
|
1360
|
-
), T =
|
|
1361
|
-
actions:
|
|
1447
|
+
g
|
|
1448
|
+
), T = ir({
|
|
1449
|
+
actions: l,
|
|
1362
1450
|
callbacks: n,
|
|
1363
1451
|
saveSessionAfterAuth: y,
|
|
1364
1452
|
onError: n.onError
|
|
@@ -1405,8 +1493,8 @@ function et(e) {
|
|
|
1405
1493
|
*/
|
|
1406
1494
|
async signOut() {
|
|
1407
1495
|
try {
|
|
1408
|
-
const c = await this.getSession(),
|
|
1409
|
-
return t.signOut && await t.signOut(), await v.clearSessionCookie(), v.clearCache(),
|
|
1496
|
+
const c = await this.getSession(), f = c == null ? void 0 : c.user;
|
|
1497
|
+
return t.signOut && await t.signOut(), await v.clearSessionCookie(), v.clearCache(), f && n.onSignOut && await S(n.onSignOut, f), { success: !0 };
|
|
1410
1498
|
} catch (c) {
|
|
1411
1499
|
return await v.clearSessionCookie(), v.clearCache(), n.onError && await S(n.onError, c instanceof Error ? c : new Error(String(c)), "signOut"), {
|
|
1412
1500
|
success: !1,
|
|
@@ -1422,10 +1510,10 @@ function et(e) {
|
|
|
1422
1510
|
throw new Error("Password reset is not configured. Provide resetPassword action in config.");
|
|
1423
1511
|
try {
|
|
1424
1512
|
return await t.resetPassword(c);
|
|
1425
|
-
} catch (
|
|
1426
|
-
return n.onError && await S(n.onError,
|
|
1513
|
+
} catch (f) {
|
|
1514
|
+
return n.onError && await S(n.onError, f instanceof Error ? f : new Error(String(f)), "resetPassword"), {
|
|
1427
1515
|
success: !1,
|
|
1428
|
-
error:
|
|
1516
|
+
error: f instanceof Error ? f.message : "Password reset failed"
|
|
1429
1517
|
};
|
|
1430
1518
|
}
|
|
1431
1519
|
},
|
|
@@ -1437,10 +1525,10 @@ function et(e) {
|
|
|
1437
1525
|
throw new Error("Email verification is not configured. Provide verifyEmail action in config.");
|
|
1438
1526
|
try {
|
|
1439
1527
|
return await t.verifyEmail(c);
|
|
1440
|
-
} catch (
|
|
1441
|
-
return n.onError && await S(n.onError,
|
|
1528
|
+
} catch (f) {
|
|
1529
|
+
return n.onError && await S(n.onError, f instanceof Error ? f : new Error(String(f)), "verifyEmail"), {
|
|
1442
1530
|
success: !1,
|
|
1443
|
-
error:
|
|
1531
|
+
error: f instanceof Error ? f.message : "Email verification failed"
|
|
1444
1532
|
};
|
|
1445
1533
|
}
|
|
1446
1534
|
},
|
|
@@ -1453,23 +1541,23 @@ function et(e) {
|
|
|
1453
1541
|
return this.getSession();
|
|
1454
1542
|
try {
|
|
1455
1543
|
const c = await t.refreshSession();
|
|
1456
|
-
if (c &&
|
|
1544
|
+
if (c && U(c)) {
|
|
1457
1545
|
if (await v.setSession(c), n.onSessionUpdate) {
|
|
1458
|
-
const
|
|
1459
|
-
if (
|
|
1460
|
-
if (await v.setSession(
|
|
1546
|
+
const f = await S(n.onSessionUpdate, c);
|
|
1547
|
+
if (f && U(f)) {
|
|
1548
|
+
if (await v.setSession(f), n.onTokenRefresh) {
|
|
1461
1549
|
const d = await this.getSession();
|
|
1462
|
-
d && await S(n.onTokenRefresh, d,
|
|
1550
|
+
d && await S(n.onTokenRefresh, d, f);
|
|
1463
1551
|
}
|
|
1464
|
-
return
|
|
1552
|
+
return f;
|
|
1465
1553
|
}
|
|
1466
1554
|
}
|
|
1467
1555
|
if (n.onTokenRefresh) {
|
|
1468
|
-
const
|
|
1469
|
-
|
|
1556
|
+
const f = await this.getSession();
|
|
1557
|
+
f && await S(n.onTokenRefresh, f, c);
|
|
1470
1558
|
}
|
|
1471
1559
|
return c;
|
|
1472
|
-
} else if (c && !
|
|
1560
|
+
} else if (c && !U(c))
|
|
1473
1561
|
return await v.clearSessionCookie(), v.clearCache(), null;
|
|
1474
1562
|
return null;
|
|
1475
1563
|
} catch (c) {
|
|
@@ -1480,22 +1568,22 @@ function et(e) {
|
|
|
1480
1568
|
* OAuth callback handler
|
|
1481
1569
|
* ✅ Auto-generated if providers.oauth is configured in config
|
|
1482
1570
|
*/
|
|
1483
|
-
async oauthCallback(c,
|
|
1484
|
-
if (!
|
|
1571
|
+
async oauthCallback(c, f, d) {
|
|
1572
|
+
if (!l.oauthCallback)
|
|
1485
1573
|
throw new Error(
|
|
1486
1574
|
"OAuth callback is not configured. Either provide oauthCallback action, or configure providers.oauth in config."
|
|
1487
1575
|
);
|
|
1488
|
-
if (!
|
|
1576
|
+
if (!f || !d)
|
|
1489
1577
|
return {
|
|
1490
1578
|
success: !1,
|
|
1491
1579
|
error: "Missing required OAuth parameters (code or state)",
|
|
1492
1580
|
errorCode: m.VALIDATION_ERROR
|
|
1493
1581
|
};
|
|
1494
|
-
let
|
|
1495
|
-
if (!
|
|
1496
|
-
const P = await Promise.resolve(
|
|
1582
|
+
let A = c;
|
|
1583
|
+
if (!A) {
|
|
1584
|
+
const P = await Promise.resolve(u.get(d));
|
|
1497
1585
|
if (P && P.provider)
|
|
1498
|
-
|
|
1586
|
+
A = P.provider;
|
|
1499
1587
|
else
|
|
1500
1588
|
return {
|
|
1501
1589
|
success: !1,
|
|
@@ -1503,14 +1591,14 @@ function et(e) {
|
|
|
1503
1591
|
errorCode: m.VALIDATION_ERROR
|
|
1504
1592
|
};
|
|
1505
1593
|
}
|
|
1506
|
-
if (!await
|
|
1594
|
+
if (!await w(d, A))
|
|
1507
1595
|
return {
|
|
1508
1596
|
success: !1,
|
|
1509
1597
|
error: "Invalid or expired state parameter",
|
|
1510
1598
|
errorCode: m.VALIDATION_ERROR
|
|
1511
1599
|
};
|
|
1512
1600
|
try {
|
|
1513
|
-
return await
|
|
1601
|
+
return await l.oauthCallback(A, f, d);
|
|
1514
1602
|
} catch (P) {
|
|
1515
1603
|
return n.onError && await S(n.onError, P instanceof Error ? P : new Error(String(P)), "oauthCallback"), {
|
|
1516
1604
|
success: !1,
|
|
@@ -1523,19 +1611,19 @@ function et(e) {
|
|
|
1523
1611
|
* Verify 2FA code after initial sign in
|
|
1524
1612
|
* Used when signIn returns requires2FA: true
|
|
1525
1613
|
*/
|
|
1526
|
-
async verify2FA(c,
|
|
1614
|
+
async verify2FA(c, f) {
|
|
1527
1615
|
if (!t.verify2FA)
|
|
1528
1616
|
throw new Error("2FA verification is not configured. Provide verify2FA action in config.");
|
|
1529
1617
|
try {
|
|
1530
1618
|
const d = await t.verify2FA(c);
|
|
1531
|
-
if (d.success && d.session && !(
|
|
1532
|
-
const
|
|
1533
|
-
|
|
1534
|
-
error:
|
|
1535
|
-
warning:
|
|
1619
|
+
if (d.success && d.session && !(f != null && f.skipCookieSave)) {
|
|
1620
|
+
const A = await y(d);
|
|
1621
|
+
A.success || (process.env.NODE_ENV === "development" && I.debug("Failed to save session cookie after verify2FA", {
|
|
1622
|
+
error: A.error,
|
|
1623
|
+
warning: A.warning
|
|
1536
1624
|
}), n.onError && await S(
|
|
1537
1625
|
n.onError,
|
|
1538
|
-
new Error(
|
|
1626
|
+
new Error(A.warning || A.error || "Failed to save session cookie"),
|
|
1539
1627
|
"verify2FA.setSession"
|
|
1540
1628
|
));
|
|
1541
1629
|
}
|
|
@@ -1566,14 +1654,24 @@ function et(e) {
|
|
|
1566
1654
|
_getCallbacks() {
|
|
1567
1655
|
return n;
|
|
1568
1656
|
},
|
|
1657
|
+
/**
|
|
1658
|
+
* Store OAuth state for validation (useful when using external backend API)
|
|
1659
|
+
* This allows storing state generated by backend APIs in mulguard's state store
|
|
1660
|
+
*
|
|
1661
|
+
* @param state - OAuth state token
|
|
1662
|
+
* @param provider - OAuth provider name
|
|
1663
|
+
*/
|
|
1664
|
+
async storeOAuthState(c, f) {
|
|
1665
|
+
await g(c, f);
|
|
1666
|
+
},
|
|
1569
1667
|
/**
|
|
1570
1668
|
* PassKey methods
|
|
1571
1669
|
*/
|
|
1572
1670
|
passkey: t.passkey ? {
|
|
1573
1671
|
register: t.passkey.register,
|
|
1574
1672
|
authenticate: async (c) => {
|
|
1575
|
-
var
|
|
1576
|
-
if (!((
|
|
1673
|
+
var f;
|
|
1674
|
+
if (!((f = t.passkey) != null && f.authenticate))
|
|
1577
1675
|
throw new Error("PassKey authenticate is not configured.");
|
|
1578
1676
|
try {
|
|
1579
1677
|
const d = await t.passkey.authenticate(c);
|
|
@@ -1586,8 +1684,8 @@ function et(e) {
|
|
|
1586
1684
|
}
|
|
1587
1685
|
},
|
|
1588
1686
|
list: t.passkey.list ? async () => {
|
|
1589
|
-
var
|
|
1590
|
-
if (!((
|
|
1687
|
+
var f;
|
|
1688
|
+
if (!((f = t.passkey) != null && f.list))
|
|
1591
1689
|
throw new Error("PassKey list is not configured.");
|
|
1592
1690
|
return [...await t.passkey.list()];
|
|
1593
1691
|
} : void 0,
|
|
@@ -1604,13 +1702,13 @@ function et(e) {
|
|
|
1604
1702
|
isEnabled: t.twoFactor.isEnabled,
|
|
1605
1703
|
verify2FA: async (c) => {
|
|
1606
1704
|
var d;
|
|
1607
|
-
const
|
|
1608
|
-
if (!
|
|
1705
|
+
const f = ((d = t.twoFactor) == null ? void 0 : d.verify2FA) || t.verify2FA;
|
|
1706
|
+
if (!f)
|
|
1609
1707
|
throw new Error("2FA verification is not configured. Provide verify2FA action in config.");
|
|
1610
1708
|
try {
|
|
1611
|
-
const
|
|
1612
|
-
if (
|
|
1613
|
-
const C = await y(
|
|
1709
|
+
const A = await f(c);
|
|
1710
|
+
if (A.success && A.session) {
|
|
1711
|
+
const C = await y(A);
|
|
1614
1712
|
C.success || (process.env.NODE_ENV === "development" && I.debug("Failed to save session cookie after twoFactor.verify2FA", {
|
|
1615
1713
|
error: C.error,
|
|
1616
1714
|
warning: C.warning
|
|
@@ -1620,11 +1718,11 @@ function et(e) {
|
|
|
1620
1718
|
"twoFactor.verify2FA.setSession"
|
|
1621
1719
|
));
|
|
1622
1720
|
}
|
|
1623
|
-
return
|
|
1624
|
-
} catch (
|
|
1625
|
-
return n.onError && await S(n.onError,
|
|
1721
|
+
return A;
|
|
1722
|
+
} catch (A) {
|
|
1723
|
+
return n.onError && await S(n.onError, A instanceof Error ? A : new Error(String(A)), "twoFactor.verify2FA"), {
|
|
1626
1724
|
success: !1,
|
|
1627
|
-
error:
|
|
1725
|
+
error: A instanceof Error ? A.message : "2FA verification failed",
|
|
1628
1726
|
errorCode: m.UNKNOWN_ERROR
|
|
1629
1727
|
};
|
|
1630
1728
|
}
|
|
@@ -1636,61 +1734,61 @@ function et(e) {
|
|
|
1636
1734
|
signInMethods: {
|
|
1637
1735
|
email: (c) => h.email(c),
|
|
1638
1736
|
oauth: (c) => {
|
|
1639
|
-
var
|
|
1640
|
-
return ((
|
|
1737
|
+
var f;
|
|
1738
|
+
return ((f = h.oauth) == null ? void 0 : f.call(h, c)) || Promise.reject(new Error("OAuth not configured"));
|
|
1641
1739
|
},
|
|
1642
1740
|
passkey: (c) => {
|
|
1643
|
-
var
|
|
1644
|
-
return ((
|
|
1741
|
+
var f;
|
|
1742
|
+
return ((f = h.passkey) == null ? void 0 : f.call(h, c)) || Promise.reject(new Error("Passkey not configured"));
|
|
1645
1743
|
},
|
|
1646
|
-
otp: (c,
|
|
1744
|
+
otp: (c, f) => {
|
|
1647
1745
|
var d;
|
|
1648
|
-
return ((d = h.otp) == null ? void 0 : d.call(h, c,
|
|
1746
|
+
return ((d = h.otp) == null ? void 0 : d.call(h, c, f)) || Promise.reject(new Error("OTP not configured"));
|
|
1649
1747
|
}
|
|
1650
1748
|
}
|
|
1651
1749
|
};
|
|
1652
1750
|
if (t.refreshSession) {
|
|
1653
|
-
const c =
|
|
1751
|
+
const c = Je(
|
|
1654
1752
|
async () => await _.refreshSession(),
|
|
1655
1753
|
async () => await _.signOut(),
|
|
1656
1754
|
async () => {
|
|
1657
1755
|
await v.clearSessionCookie(), v.clearCache();
|
|
1658
1756
|
},
|
|
1659
1757
|
{
|
|
1660
|
-
...
|
|
1661
|
-
onTokenRefreshed:
|
|
1662
|
-
onTokenRefreshFailed:
|
|
1663
|
-
onBeforeRedirect:
|
|
1758
|
+
...i,
|
|
1759
|
+
onTokenRefreshed: i.onTokenRefreshed,
|
|
1760
|
+
onTokenRefreshFailed: i.onTokenRefreshFailed,
|
|
1761
|
+
onBeforeRedirect: i.onBeforeRedirect
|
|
1664
1762
|
}
|
|
1665
1763
|
);
|
|
1666
1764
|
_._tokenRefreshManager = c, _._getTokenRefreshManager = () => c;
|
|
1667
1765
|
}
|
|
1668
1766
|
return _;
|
|
1669
1767
|
}
|
|
1670
|
-
function
|
|
1768
|
+
function ot(e) {
|
|
1671
1769
|
return {
|
|
1672
1770
|
GET: async (r) => B(r, e, "GET"),
|
|
1673
1771
|
POST: async (r) => B(r, e, "POST")
|
|
1674
1772
|
};
|
|
1675
1773
|
}
|
|
1676
1774
|
async function B(e, r, t) {
|
|
1677
|
-
const n = new URL(e.url), s =
|
|
1775
|
+
const n = new URL(e.url), s = ur(n.pathname), o = s.split("/").filter(Boolean);
|
|
1678
1776
|
try {
|
|
1679
|
-
return t === "GET" ? await
|
|
1680
|
-
} catch (
|
|
1777
|
+
return t === "GET" ? await lr(e, r, s, o, n) : t === "POST" ? await fr(e, r, s, o, n) : O("Method not allowed", 405);
|
|
1778
|
+
} catch (i) {
|
|
1681
1779
|
return O(
|
|
1682
|
-
|
|
1780
|
+
i instanceof Error ? i.message : "Request failed",
|
|
1683
1781
|
500
|
|
1684
1782
|
);
|
|
1685
1783
|
}
|
|
1686
1784
|
}
|
|
1687
|
-
function
|
|
1785
|
+
function ur(e) {
|
|
1688
1786
|
return e.replace(/^\/api\/auth/, "") || "/session";
|
|
1689
1787
|
}
|
|
1690
|
-
async function
|
|
1788
|
+
async function lr(e, r, t, n, s) {
|
|
1691
1789
|
if (t === "/session" || t === "/") {
|
|
1692
|
-
const
|
|
1693
|
-
return E.json({ session:
|
|
1790
|
+
const o = await r.getSession();
|
|
1791
|
+
return E.json({ session: o });
|
|
1694
1792
|
}
|
|
1695
1793
|
return t === "/providers" ? E.json({
|
|
1696
1794
|
providers: {
|
|
@@ -1700,11 +1798,11 @@ async function ar(e, r, t, n, s) {
|
|
|
1700
1798
|
}
|
|
1701
1799
|
}) : re(t, n) ? await te(e, r, t, n, s, "GET") : O("Not found", 404);
|
|
1702
1800
|
}
|
|
1703
|
-
async function
|
|
1704
|
-
const
|
|
1705
|
-
return t === "/sign-in" || n[0] === "sign-in" ? await
|
|
1801
|
+
async function fr(e, r, t, n, s) {
|
|
1802
|
+
const o = await dr(e);
|
|
1803
|
+
return t === "/sign-in" || n[0] === "sign-in" ? await gr(r, o) : t === "/sign-up" || n[0] === "sign-up" ? await wr(r, o) : t === "/sign-out" || n[0] === "sign-out" ? await pr(r) : t === "/reset-password" || n[0] === "reset-password" ? await mr(r, o) : t === "/verify-email" || n[0] === "verify-email" ? await Er(r, o) : t === "/refresh" || n[0] === "refresh" ? await yr(r) : re(t, n) ? await te(e, r, t, n, s, "POST", o) : t.startsWith("/passkey") ? await vr(r, t, n, o) : t === "/verify-2fa" || n[0] === "verify-2fa" ? await kr(r, o) : t.startsWith("/two-factor") ? await Sr(r, n, o) : O("Not found", 404);
|
|
1706
1804
|
}
|
|
1707
|
-
async function
|
|
1805
|
+
async function dr(e) {
|
|
1708
1806
|
try {
|
|
1709
1807
|
return await e.json();
|
|
1710
1808
|
} catch {
|
|
@@ -1714,23 +1812,23 @@ async function ur(e) {
|
|
|
1714
1812
|
function re(e, r) {
|
|
1715
1813
|
return e === "/callback" || e.startsWith("/oauth/callback") || r[0] === "oauth" && r[1] === "callback" || r[0] === "callback";
|
|
1716
1814
|
}
|
|
1717
|
-
async function te(e, r, t, n, s,
|
|
1815
|
+
async function te(e, r, t, n, s, o, i) {
|
|
1718
1816
|
if (!r.oauthCallback)
|
|
1719
|
-
return
|
|
1720
|
-
const a =
|
|
1721
|
-
if (!
|
|
1722
|
-
return
|
|
1817
|
+
return o === "GET" ? V(e.url, "oauth_not_configured") : O("OAuth callback is not configured", 400);
|
|
1818
|
+
const a = hr(n, s, i), u = (i == null ? void 0 : i.code) ?? s.searchParams.get("code"), l = (i == null ? void 0 : i.state) ?? s.searchParams.get("state");
|
|
1819
|
+
if (!u || !l)
|
|
1820
|
+
return o === "GET" ? V(e.url, "oauth_missing_params") : O("Missing required OAuth parameters. Code and state are required.", 400);
|
|
1723
1821
|
try {
|
|
1724
|
-
const
|
|
1725
|
-
return
|
|
1726
|
-
} catch (
|
|
1727
|
-
return
|
|
1822
|
+
const g = await r.oauthCallback(a ?? "", u, l);
|
|
1823
|
+
return o === "GET" ? g.success ? Ar(e.url, s.searchParams.get("callbackUrl")) : V(e.url, g.error ?? "oauth_failed") : E.json(g);
|
|
1824
|
+
} catch (g) {
|
|
1825
|
+
return o === "GET" ? V(e.url, g instanceof Error ? g.message : "oauth_error") : O(g instanceof Error ? g.message : "OAuth callback failed", 500);
|
|
1728
1826
|
}
|
|
1729
1827
|
}
|
|
1730
|
-
function
|
|
1828
|
+
function hr(e, r, t) {
|
|
1731
1829
|
return t != null && t.provider ? t.provider : e[0] === "callback" && e[1] ? e[1] : e[0] === "oauth" && e[1] === "callback" && e[2] ? e[2] : r.searchParams.get("provider");
|
|
1732
1830
|
}
|
|
1733
|
-
async function
|
|
1831
|
+
async function gr(e, r) {
|
|
1734
1832
|
if (r.provider === "email" && r.email && r.password) {
|
|
1735
1833
|
const t = {
|
|
1736
1834
|
email: r.email,
|
|
@@ -1752,17 +1850,17 @@ async function fr(e, r) {
|
|
|
1752
1850
|
}
|
|
1753
1851
|
return O("Invalid sign in request", 400);
|
|
1754
1852
|
}
|
|
1755
|
-
async function
|
|
1853
|
+
async function wr(e, r) {
|
|
1756
1854
|
if (!e.signUp)
|
|
1757
1855
|
return O("Sign up is not configured", 400);
|
|
1758
1856
|
const t = await e.signUp(r);
|
|
1759
1857
|
return E.json(t);
|
|
1760
1858
|
}
|
|
1761
|
-
async function
|
|
1859
|
+
async function pr(e) {
|
|
1762
1860
|
const r = await e.signOut();
|
|
1763
1861
|
return E.json(r);
|
|
1764
1862
|
}
|
|
1765
|
-
async function
|
|
1863
|
+
async function mr(e, r) {
|
|
1766
1864
|
if (!e.resetPassword)
|
|
1767
1865
|
return O("Password reset is not configured", 400);
|
|
1768
1866
|
if (!r.email || typeof r.email != "string")
|
|
@@ -1770,7 +1868,7 @@ async function gr(e, r) {
|
|
|
1770
1868
|
const t = await e.resetPassword(r.email);
|
|
1771
1869
|
return E.json(t);
|
|
1772
1870
|
}
|
|
1773
|
-
async function
|
|
1871
|
+
async function Er(e, r) {
|
|
1774
1872
|
if (!e.verifyEmail)
|
|
1775
1873
|
return O("Email verification is not configured", 400);
|
|
1776
1874
|
if (!r.token || typeof r.token != "string")
|
|
@@ -1778,7 +1876,7 @@ async function wr(e, r) {
|
|
|
1778
1876
|
const t = await e.verifyEmail(r.token);
|
|
1779
1877
|
return E.json(t);
|
|
1780
1878
|
}
|
|
1781
|
-
async function
|
|
1879
|
+
async function yr(e) {
|
|
1782
1880
|
if (!e.refreshSession) {
|
|
1783
1881
|
const t = await e.getSession();
|
|
1784
1882
|
return E.json({ session: t });
|
|
@@ -1786,7 +1884,7 @@ async function pr(e) {
|
|
|
1786
1884
|
const r = await e.refreshSession();
|
|
1787
1885
|
return E.json({ session: r });
|
|
1788
1886
|
}
|
|
1789
|
-
async function
|
|
1887
|
+
async function kr(e, r) {
|
|
1790
1888
|
if (!e.verify2FA)
|
|
1791
1889
|
return O("2FA verification is not configured", 400);
|
|
1792
1890
|
if (!r.email || !r.userId || !r.code)
|
|
@@ -1798,27 +1896,27 @@ async function mr(e, r) {
|
|
|
1798
1896
|
}, n = await e.verify2FA(t);
|
|
1799
1897
|
return E.json(n);
|
|
1800
1898
|
}
|
|
1801
|
-
async function
|
|
1899
|
+
async function vr(e, r, t, n) {
|
|
1802
1900
|
if (!e.passkey)
|
|
1803
1901
|
return O("PassKey is not configured", 400);
|
|
1804
1902
|
const s = t[1];
|
|
1805
1903
|
if (s === "register" && e.passkey.register) {
|
|
1806
|
-
const
|
|
1807
|
-
return E.json(
|
|
1904
|
+
const o = await e.passkey.register(n.options);
|
|
1905
|
+
return E.json(o);
|
|
1808
1906
|
}
|
|
1809
1907
|
if (s === "list" && e.passkey.list) {
|
|
1810
|
-
const
|
|
1811
|
-
return E.json(
|
|
1908
|
+
const o = await e.passkey.list();
|
|
1909
|
+
return E.json(o);
|
|
1812
1910
|
}
|
|
1813
1911
|
if (s === "remove" && e.passkey.remove) {
|
|
1814
1912
|
if (!n.passkeyId || typeof n.passkeyId != "string")
|
|
1815
1913
|
return O("Passkey ID is required", 400);
|
|
1816
|
-
const
|
|
1817
|
-
return E.json(
|
|
1914
|
+
const o = await e.passkey.remove(n.passkeyId);
|
|
1915
|
+
return E.json(o);
|
|
1818
1916
|
}
|
|
1819
1917
|
return O("Invalid Passkey request", 400);
|
|
1820
1918
|
}
|
|
1821
|
-
async function
|
|
1919
|
+
async function Sr(e, r, t) {
|
|
1822
1920
|
if (!e.twoFactor)
|
|
1823
1921
|
return O("Two-Factor Authentication is not configured", 400);
|
|
1824
1922
|
const n = r[1];
|
|
@@ -1858,52 +1956,52 @@ function O(e, r) {
|
|
|
1858
1956
|
function V(e, r) {
|
|
1859
1957
|
return E.redirect(new URL(`/login?error=${encodeURIComponent(r)}`, e));
|
|
1860
1958
|
}
|
|
1861
|
-
function
|
|
1959
|
+
function Ar(e, r) {
|
|
1862
1960
|
const t = r ?? "/";
|
|
1863
1961
|
return E.redirect(new URL(t, e));
|
|
1864
1962
|
}
|
|
1865
|
-
function
|
|
1963
|
+
function it(e) {
|
|
1866
1964
|
return async (r) => {
|
|
1867
|
-
const { method: t, nextUrl: n } = r,
|
|
1965
|
+
const { method: t, nextUrl: n } = r, o = n.pathname.replace(/^\/api\/auth/, "") || "/";
|
|
1868
1966
|
try {
|
|
1869
|
-
let
|
|
1967
|
+
let i;
|
|
1870
1968
|
if (t !== "GET" && t !== "HEAD")
|
|
1871
1969
|
try {
|
|
1872
|
-
|
|
1970
|
+
i = await r.json();
|
|
1873
1971
|
} catch {
|
|
1874
1972
|
}
|
|
1875
|
-
const a = Object.fromEntries(n.searchParams.entries()),
|
|
1876
|
-
`${process.env.NEXT_PUBLIC_API_URL || ""}/api/auth${
|
|
1973
|
+
const a = Object.fromEntries(n.searchParams.entries()), u = await fetch(
|
|
1974
|
+
`${process.env.NEXT_PUBLIC_API_URL || ""}/api/auth${o}${Object.keys(a).length > 0 ? `?${new URLSearchParams(a).toString()}` : ""}`,
|
|
1877
1975
|
{
|
|
1878
1976
|
method: t,
|
|
1879
1977
|
headers: {
|
|
1880
1978
|
"Content-Type": "application/json",
|
|
1881
1979
|
...Object.fromEntries(r.headers.entries())
|
|
1882
1980
|
},
|
|
1883
|
-
body:
|
|
1981
|
+
body: i ? JSON.stringify(i) : void 0
|
|
1884
1982
|
}
|
|
1885
|
-
),
|
|
1886
|
-
return E.json(
|
|
1887
|
-
status:
|
|
1983
|
+
), l = await u.json();
|
|
1984
|
+
return E.json(l, {
|
|
1985
|
+
status: u.status,
|
|
1888
1986
|
headers: {
|
|
1889
|
-
...Object.fromEntries(
|
|
1987
|
+
...Object.fromEntries(u.headers.entries())
|
|
1890
1988
|
}
|
|
1891
1989
|
});
|
|
1892
|
-
} catch (
|
|
1893
|
-
return console.error("API handler error:",
|
|
1990
|
+
} catch (i) {
|
|
1991
|
+
return console.error("API handler error:", i), E.json(
|
|
1894
1992
|
{
|
|
1895
1993
|
success: !1,
|
|
1896
|
-
error:
|
|
1994
|
+
error: i instanceof Error ? i.message : "Internal server error"
|
|
1897
1995
|
},
|
|
1898
1996
|
{ status: 500 }
|
|
1899
1997
|
);
|
|
1900
1998
|
}
|
|
1901
1999
|
};
|
|
1902
2000
|
}
|
|
1903
|
-
function
|
|
2001
|
+
function at(e) {
|
|
1904
2002
|
return async (r) => {
|
|
1905
|
-
const { searchParams: t } = r.nextUrl, n = t.get("provider"), s = t.get("code"),
|
|
1906
|
-
if (!n || !s || !
|
|
2003
|
+
const { searchParams: t } = r.nextUrl, n = t.get("provider"), s = t.get("code"), o = t.get("state");
|
|
2004
|
+
if (!n || !s || !o)
|
|
1907
2005
|
return E.redirect(
|
|
1908
2006
|
new URL("/login?error=oauth_missing_params", r.url)
|
|
1909
2007
|
);
|
|
@@ -1912,20 +2010,20 @@ function nt(e) {
|
|
|
1912
2010
|
return E.redirect(
|
|
1913
2011
|
new URL("/login?error=oauth_not_configured", r.url)
|
|
1914
2012
|
);
|
|
1915
|
-
const
|
|
1916
|
-
if (
|
|
2013
|
+
const i = await e.oauthCallback(n, s, o);
|
|
2014
|
+
if (i.success) {
|
|
1917
2015
|
const a = t.get("callbackUrl") || "/";
|
|
1918
2016
|
return E.redirect(new URL(a, r.url));
|
|
1919
2017
|
} else {
|
|
1920
|
-
const a =
|
|
2018
|
+
const a = i.errorCode ? `${encodeURIComponent(i.error || "oauth_failed")}&code=${i.errorCode}` : encodeURIComponent(i.error || "oauth_failed");
|
|
1921
2019
|
return E.redirect(
|
|
1922
2020
|
new URL(`/login?error=${a}`, r.url)
|
|
1923
2021
|
);
|
|
1924
2022
|
}
|
|
1925
|
-
} catch (
|
|
1926
|
-
return process.env.NODE_ENV === "development" && console.error("[Mulguard] OAuth callback error:",
|
|
2023
|
+
} catch (i) {
|
|
2024
|
+
return process.env.NODE_ENV === "development" && console.error("[Mulguard] OAuth callback error:", i), E.redirect(
|
|
1927
2025
|
new URL(
|
|
1928
|
-
`/login?error=${encodeURIComponent(
|
|
2026
|
+
`/login?error=${encodeURIComponent(i instanceof Error ? i.message : "oauth_error")}`,
|
|
1929
2027
|
r.url
|
|
1930
2028
|
)
|
|
1931
2029
|
);
|
|
@@ -1942,42 +2040,42 @@ function F(e, r) {
|
|
|
1942
2040
|
s && typeof s == "string" && r.headers.set(n, s);
|
|
1943
2041
|
return r;
|
|
1944
2042
|
}
|
|
1945
|
-
function
|
|
2043
|
+
function ct() {
|
|
1946
2044
|
return async (e) => {
|
|
1947
2045
|
const r = E.next();
|
|
1948
2046
|
return F(e, r);
|
|
1949
2047
|
};
|
|
1950
2048
|
}
|
|
1951
|
-
function
|
|
2049
|
+
function ut(e, r = {}) {
|
|
1952
2050
|
const {
|
|
1953
2051
|
protectedRoutes: t = [],
|
|
1954
2052
|
publicRoutes: n = [],
|
|
1955
2053
|
redirectTo: s = "/login",
|
|
1956
|
-
redirectIfAuthenticated:
|
|
2054
|
+
redirectIfAuthenticated: o
|
|
1957
2055
|
} = r;
|
|
1958
|
-
return async (
|
|
1959
|
-
const { pathname: a } =
|
|
1960
|
-
let
|
|
2056
|
+
return async (i) => {
|
|
2057
|
+
const { pathname: a } = i.nextUrl, u = t.some((w) => a.startsWith(w));
|
|
2058
|
+
let l = null;
|
|
1961
2059
|
try {
|
|
1962
|
-
|
|
1963
|
-
} catch (
|
|
1964
|
-
console.error("Middleware: Failed to get session:",
|
|
2060
|
+
l = await e.getSession();
|
|
2061
|
+
} catch (w) {
|
|
2062
|
+
console.error("Middleware: Failed to get session:", w);
|
|
1965
2063
|
}
|
|
1966
|
-
if (
|
|
1967
|
-
const
|
|
1968
|
-
return
|
|
2064
|
+
if (u && !l) {
|
|
2065
|
+
const w = i.nextUrl.clone();
|
|
2066
|
+
return w.pathname = s, w.searchParams.set("callbackUrl", a), E.redirect(w);
|
|
1969
2067
|
}
|
|
1970
|
-
if (
|
|
1971
|
-
const
|
|
1972
|
-
|
|
1973
|
-
const S = E.redirect(
|
|
1974
|
-
return F(
|
|
2068
|
+
if (o && l && (a.startsWith("/login") || a.startsWith("/register"))) {
|
|
2069
|
+
const R = i.nextUrl.clone();
|
|
2070
|
+
R.pathname = o;
|
|
2071
|
+
const S = E.redirect(R);
|
|
2072
|
+
return F(i, S);
|
|
1975
2073
|
}
|
|
1976
|
-
const
|
|
1977
|
-
return F(
|
|
2074
|
+
const g = E.next();
|
|
2075
|
+
return F(i, g);
|
|
1978
2076
|
};
|
|
1979
2077
|
}
|
|
1980
|
-
async function
|
|
2078
|
+
async function lt(e, r) {
|
|
1981
2079
|
var t;
|
|
1982
2080
|
try {
|
|
1983
2081
|
const n = await e.getSession();
|
|
@@ -1986,46 +2084,46 @@ async function ot(e, r) {
|
|
|
1986
2084
|
return !1;
|
|
1987
2085
|
}
|
|
1988
2086
|
}
|
|
1989
|
-
function
|
|
2087
|
+
function ft(e) {
|
|
1990
2088
|
const {
|
|
1991
2089
|
auth: r,
|
|
1992
2090
|
protectedRoutes: t = [],
|
|
1993
2091
|
publicRoutes: n = [],
|
|
1994
2092
|
redirectTo: s = "/login",
|
|
1995
|
-
redirectIfAuthenticated:
|
|
1996
|
-
apiPrefix:
|
|
2093
|
+
redirectIfAuthenticated: o,
|
|
2094
|
+
apiPrefix: i = "/api/auth"
|
|
1997
2095
|
} = e;
|
|
1998
2096
|
return async (a) => {
|
|
1999
|
-
const { pathname:
|
|
2000
|
-
if (
|
|
2001
|
-
const
|
|
2002
|
-
return F(a,
|
|
2097
|
+
const { pathname: u } = a.nextUrl;
|
|
2098
|
+
if (u.startsWith(i)) {
|
|
2099
|
+
const R = E.next();
|
|
2100
|
+
return F(a, R);
|
|
2003
2101
|
}
|
|
2004
|
-
const
|
|
2005
|
-
let
|
|
2006
|
-
if (
|
|
2102
|
+
const l = t.some((R) => u.startsWith(R));
|
|
2103
|
+
let g = null;
|
|
2104
|
+
if (l || o)
|
|
2007
2105
|
try {
|
|
2008
|
-
|
|
2009
|
-
} catch (
|
|
2010
|
-
console.error("Middleware: Failed to get session:",
|
|
2106
|
+
g = await r.getSession();
|
|
2107
|
+
} catch (R) {
|
|
2108
|
+
console.error("Middleware: Failed to get session:", R);
|
|
2011
2109
|
}
|
|
2012
|
-
if (
|
|
2013
|
-
const
|
|
2014
|
-
|
|
2015
|
-
const S = E.redirect(
|
|
2110
|
+
if (l && !g) {
|
|
2111
|
+
const R = a.nextUrl.clone();
|
|
2112
|
+
R.pathname = s, R.searchParams.set("callbackUrl", u);
|
|
2113
|
+
const S = E.redirect(R);
|
|
2016
2114
|
return F(a, S);
|
|
2017
2115
|
}
|
|
2018
|
-
if (
|
|
2116
|
+
if (o && g && (u.startsWith("/login") || u.startsWith("/register"))) {
|
|
2019
2117
|
const S = a.nextUrl.clone();
|
|
2020
|
-
S.pathname =
|
|
2118
|
+
S.pathname = o;
|
|
2021
2119
|
const v = E.redirect(S);
|
|
2022
2120
|
return F(a, v);
|
|
2023
2121
|
}
|
|
2024
|
-
const
|
|
2025
|
-
return F(a,
|
|
2122
|
+
const w = E.next();
|
|
2123
|
+
return F(a, w);
|
|
2026
2124
|
};
|
|
2027
2125
|
}
|
|
2028
|
-
async function
|
|
2126
|
+
async function dt(e, r) {
|
|
2029
2127
|
var t;
|
|
2030
2128
|
try {
|
|
2031
2129
|
const n = await e.getSession();
|
|
@@ -2038,85 +2136,87 @@ export {
|
|
|
2038
2136
|
Te as CSRFProtection,
|
|
2039
2137
|
fe as DEFAULT_SECURITY_HEADERS,
|
|
2040
2138
|
Oe as MemoryCSRFStore,
|
|
2041
|
-
|
|
2139
|
+
qe as MemoryOAuthStateStore,
|
|
2042
2140
|
le as RateLimiter,
|
|
2043
|
-
|
|
2044
|
-
|
|
2045
|
-
|
|
2046
|
-
|
|
2047
|
-
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2141
|
+
Pr as applySecurityHeaders,
|
|
2142
|
+
ie as buildCookieOptions,
|
|
2143
|
+
Ne as buildOAuthAuthorizationUrl,
|
|
2144
|
+
lt as checkRole,
|
|
2145
|
+
dt as checkRoleProxy,
|
|
2146
|
+
$r as containsXSSPattern,
|
|
2147
|
+
it as createApiHandler,
|
|
2148
|
+
ut as createAuthMiddleware,
|
|
2149
|
+
Vr as createCSRFProtection,
|
|
2150
|
+
We as createCookieOAuthStateStore,
|
|
2151
|
+
Be as createMemoryOAuthStateStore,
|
|
2152
|
+
tt as createNextJsCookieOAuthStateStore,
|
|
2153
|
+
at as createOAuthCallbackHandler,
|
|
2154
|
+
ft as createProxyMiddleware,
|
|
2155
|
+
_r as createRateLimiter,
|
|
2156
|
+
nt as createRedisOAuthStateStore,
|
|
2157
|
+
ct as createSecurityMiddleware,
|
|
2158
|
+
kt as createServerAuthMiddleware,
|
|
2159
|
+
vt as createServerHelpers,
|
|
2160
|
+
St as createServerUtils,
|
|
2161
|
+
At as createSessionManager,
|
|
2162
|
+
oe as deleteCookie,
|
|
2163
|
+
Rt as deleteOAuthStateCookie,
|
|
2064
2164
|
Ie as escapeHTML,
|
|
2065
|
-
|
|
2165
|
+
be as exchangeOAuthCode,
|
|
2066
2166
|
_e as generateCSRFToken,
|
|
2067
2167
|
Y as generateToken,
|
|
2068
2168
|
ce as getCookie,
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2169
|
+
Ot as getCurrentUser,
|
|
2170
|
+
Kr as getErrorCode,
|
|
2171
|
+
Gr as getErrorMessage,
|
|
2172
|
+
Tt as getOAuthStateCookie,
|
|
2073
2173
|
Fe as getOAuthUserInfo,
|
|
2074
2174
|
j as getProviderMetadata,
|
|
2075
2175
|
H as getSecurityHeaders,
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2176
|
+
It as getServerSession,
|
|
2177
|
+
_t as getSessionTimeUntilExpiry,
|
|
2178
|
+
Qr as getUserFriendlyError,
|
|
2179
|
+
Jr as hasErrorCode,
|
|
2080
2180
|
Ce as isAuthError,
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2181
|
+
Xr as isAuthSuccess,
|
|
2182
|
+
rt as isOAuthProviderConfig,
|
|
2183
|
+
Yr as isRetryableError,
|
|
2184
|
+
Pt as isSessionExpiredNullable,
|
|
2185
|
+
Ct as isSessionExpiringSoon,
|
|
2186
|
+
Nt as isSessionValid,
|
|
2187
|
+
et as isSupportedProvider,
|
|
2188
|
+
Hr as isTwoFactorRequired,
|
|
2189
|
+
Wr as isValidCSRFToken,
|
|
2190
|
+
Br as isValidEmail,
|
|
2191
|
+
Mr as isValidInput,
|
|
2192
|
+
Ur as isValidName,
|
|
2193
|
+
Nr as isValidPassword,
|
|
2194
|
+
Lr as isValidToken,
|
|
2195
|
+
xr as isValidURL,
|
|
2196
|
+
st as mulguard,
|
|
2197
|
+
bt as refreshSession,
|
|
2198
|
+
Ut as requireAuth,
|
|
2199
|
+
Ft as requireRole,
|
|
2200
|
+
xt as requireServerAuthMiddleware,
|
|
2201
|
+
Dt as requireServerRoleMiddleware,
|
|
2202
|
+
jr as sanitizeHTML,
|
|
2203
|
+
qr as sanitizeInput,
|
|
2204
|
+
zr as sanitizeUserInput,
|
|
2105
2205
|
ae as setCookie,
|
|
2106
|
-
|
|
2107
|
-
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2111
|
-
|
|
2206
|
+
Zr as signIn,
|
|
2207
|
+
wt as signInEmailAction,
|
|
2208
|
+
pt as signOutAction,
|
|
2209
|
+
mt as signUpAction,
|
|
2210
|
+
Lt as storeOAuthStateCookie,
|
|
2211
|
+
ot as toNextJsHandler,
|
|
2112
2212
|
G as validateAndSanitizeEmail,
|
|
2113
2213
|
X as validateAndSanitizeInput,
|
|
2114
|
-
|
|
2115
|
-
|
|
2214
|
+
br as validateAndSanitizeName,
|
|
2215
|
+
Cr as validateAndSanitizePassword,
|
|
2116
2216
|
Q as validateCSRFToken,
|
|
2117
|
-
|
|
2118
|
-
|
|
2119
|
-
|
|
2120
|
-
|
|
2217
|
+
U as validateSessionStructure,
|
|
2218
|
+
Dr as validateToken,
|
|
2219
|
+
Fr as validateURL,
|
|
2220
|
+
Et as verify2FAAction,
|
|
2121
2221
|
F as withSecurityHeaders
|
|
2122
2222
|
};
|