faire-auth 1.0.3 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{adapter-factory-lKIOf2OR.mjs → adapter-factory-qPVW8x9-.mjs} +34 -36
- package/dist/{adapter-factory-D1I7joki.cjs → adapter-factory-u8r3oTez.cjs} +66 -68
- package/dist/adapters/drizzle-adapter/index.cjs +34 -40
- package/dist/adapters/drizzle-adapter/index.d.cts +4 -9
- package/dist/adapters/drizzle-adapter/index.d.mts +4 -9
- package/dist/adapters/drizzle-adapter/index.mjs +20 -26
- package/dist/adapters/index.cjs +11 -16
- package/dist/adapters/index.d.cts +11 -37
- package/dist/adapters/index.d.mts +11 -37
- package/dist/adapters/index.mjs +4 -13
- package/dist/adapters/kysely-adapter/index.cjs +270 -11
- package/dist/adapters/kysely-adapter/index.d.cts +9 -19
- package/dist/adapters/kysely-adapter/index.d.mts +9 -19
- package/dist/adapters/kysely-adapter/index.mjs +267 -9
- package/dist/adapters/memory-adapter/index.cjs +146 -9
- package/dist/adapters/memory-adapter/index.d.cts +4 -10
- package/dist/adapters/memory-adapter/index.d.mts +4 -10
- package/dist/adapters/memory-adapter/index.mjs +145 -9
- package/dist/adapters/mongodb-adapter/index.cjs +13 -21
- package/dist/adapters/mongodb-adapter/index.d.cts +3 -8
- package/dist/adapters/mongodb-adapter/index.d.mts +4 -9
- package/dist/adapters/mongodb-adapter/index.mjs +12 -20
- package/dist/adapters/prisma-adapter/index.cjs +12 -20
- package/dist/adapters/prisma-adapter/index.d.cts +4 -8
- package/dist/adapters/prisma-adapter/index.d.mts +4 -8
- package/dist/adapters/prisma-adapter/index.mjs +5 -13
- package/dist/adapters/test.cjs +86 -69
- package/dist/adapters/test.d.cts +7 -11
- package/dist/adapters/test.d.mts +7 -11
- package/dist/adapters/test.mjs +83 -66
- package/dist/api/index.cjs +3 -20
- package/dist/api/index.d.cts +2 -3
- package/dist/api/index.d.mts +2 -3
- package/dist/api/index.mjs +1 -20
- package/dist/api/middleware/index.cjs +4 -11
- package/dist/api/middleware/index.d.cts +3 -9
- package/dist/api/middleware/index.d.mts +3 -9
- package/dist/api/middleware/index.mjs +3 -11
- package/dist/{api-B4RDwBty.mjs → api-0f-F-Fi2.mjs} +292 -189
- package/dist/{api-CaRC1Gio.cjs → api-LjfH6hQi.cjs} +557 -452
- package/dist/{test-utils/bun-sqlite-dialect-BZBvHchL.cjs → bun-sqlite-dialect-BHkarnxN.cjs} +5 -9
- package/dist/{test-utils/bun-sqlite-dialect-BSWhhKNs.mjs → bun-sqlite-dialect-BiZBuEF6.mjs} +6 -9
- package/dist/client/index.cjs +8 -15
- package/dist/client/index.d.cts +9 -14
- package/dist/client/index.d.mts +9 -14
- package/dist/client/index.mjs +7 -15
- package/dist/client/lynx/index.cjs +10 -18
- package/dist/client/lynx/index.d.cts +402 -71
- package/dist/client/lynx/index.d.mts +402 -71
- package/dist/client/lynx/index.mjs +5 -13
- package/dist/client/plugins/index.cjs +3 -5
- package/dist/client/plugins/index.d.cts +5 -10
- package/dist/client/plugins/index.d.mts +5 -10
- package/dist/client/plugins/index.mjs +2 -4
- package/dist/client/react/index.cjs +6 -14
- package/dist/client/react/index.d.cts +402 -71
- package/dist/client/react/index.d.mts +402 -71
- package/dist/client/react/index.mjs +5 -13
- package/dist/client/solid/index.cjs +6 -14
- package/dist/client/solid/index.d.cts +393 -57
- package/dist/client/solid/index.d.mts +393 -57
- package/dist/client/solid/index.mjs +5 -13
- package/dist/client/svelte/index.cjs +5 -10
- package/dist/client/svelte/index.d.cts +400 -67
- package/dist/client/svelte/index.d.mts +400 -67
- package/dist/client/svelte/index.mjs +4 -10
- package/dist/client/vue/index.cjs +6 -14
- package/dist/client/vue/index.d.cts +401 -68
- package/dist/client/vue/index.d.mts +401 -68
- package/dist/client/vue/index.mjs +5 -13
- package/dist/{cookies-BTs1o992.mjs → cookies-D9aY_gJh.mjs} +25 -21
- package/dist/{cookies-BTl72THv.cjs → cookies-DQ52rcOy.cjs} +119 -115
- package/dist/crypto/index.cjs +10 -8
- package/dist/crypto/index.d.cts +5 -13
- package/dist/crypto/index.d.mts +5 -13
- package/dist/crypto/index.mjs +2 -5
- package/dist/{crypto-DH2Aa7jN.mjs → crypto-BH-hD0lJ.mjs} +7 -10
- package/dist/{crypto-DpSCnrTp.cjs → crypto-BITfWVwD.cjs} +24 -27
- package/dist/db/index.cjs +1135 -29
- package/dist/db/index.d.cts +1 -2
- package/dist/db/index.d.mts +1 -2
- package/dist/db/index.mjs +1115 -16
- package/dist/defu-D2h4S3X-.mjs +87 -0
- package/dist/defu-DWS5RPWt.cjs +134 -0
- package/dist/{email-verification-DI_0H4JC.cjs → email-verification-DOPnSgbI.cjs} +65 -71
- package/dist/{email-verification-C_lBu_JY.mjs → email-verification-YSR5n5lb.mjs} +23 -29
- package/dist/{hono-CO-zsqI-.mjs → hono-BAV2wQkK.mjs} +16 -33
- package/dist/hono-SVQuc5Ru.mjs +4 -0
- package/dist/hono-pgK4H_U1.cjs +9 -0
- package/dist/{hono-DnW5crl1.cjs → hono-tyFFJFdS.cjs} +42 -60
- package/dist/index-B86Cy0dE.d.cts +2 -0
- package/dist/index-B86Cy0dE.d.mts +2 -0
- package/dist/index.cjs +115 -150
- package/dist/index.d.cts +4 -5
- package/dist/index.d.mts +4 -5
- package/dist/index.mjs +46 -81
- package/dist/integrations/next-js.cjs +5 -9
- package/dist/integrations/next-js.d.cts +6 -14
- package/dist/integrations/next-js.d.mts +6 -14
- package/dist/integrations/next-js.mjs +4 -9
- package/dist/integrations/node.cjs +16 -14
- package/dist/integrations/node.d.cts +6 -14
- package/dist/integrations/node.d.mts +6 -14
- package/dist/integrations/node.mjs +15 -13
- package/dist/integrations/react-start.cjs +7 -11
- package/dist/integrations/react-start.d.cts +5 -13
- package/dist/integrations/react-start.d.mts +5 -13
- package/dist/integrations/react-start.mjs +6 -11
- package/dist/integrations/solid-start.cjs +3 -5
- package/dist/integrations/solid-start.d.cts +2 -5
- package/dist/integrations/solid-start.d.mts +2 -5
- package/dist/integrations/solid-start.mjs +2 -4
- package/dist/integrations/svelte-kit.cjs +6 -10
- package/dist/integrations/svelte-kit.d.cts +11 -23
- package/dist/integrations/svelte-kit.d.mts +11 -23
- package/dist/integrations/svelte-kit.mjs +5 -10
- package/dist/{ip-BjNYspCH.mjs → ip-6Lv2o42m.mjs} +4 -9
- package/dist/{ip-C17Qdq-G.cjs → ip-DPydExzV.cjs} +21 -27
- package/dist/{json--tl7Grvs.cjs → json-D3aq-nH2.cjs} +9 -13
- package/dist/{json-B1v2qRTQ.mjs → json-DooGMtnJ.mjs} +2 -5
- package/dist/{logged-CyGfp8_H.mjs → logged-B83SzMnJ.mjs} +6 -10
- package/dist/{logged-Dv3st5qc.cjs → logged-BIpCpnVY.cjs} +13 -18
- package/dist/{middleware-CE6OhDkq.mjs → middleware-Cakg4e1D.mjs} +2 -4
- package/dist/{middleware-CWvRH4EW.cjs → middleware-Cd-VkNxB.cjs} +17 -20
- package/dist/{node-sqlite-dialect-w4DAg0PH.mjs → node-sqlite-dialect-BMVOjEMh.mjs} +6 -9
- package/dist/{test-utils/node-sqlite-dialect-BgJqjBOu.cjs → node-sqlite-dialect-D3QPFROH.cjs} +5 -9
- package/dist/oauth2/index.cjs +166 -22
- package/dist/oauth2/index.d.cts +5 -17
- package/dist/oauth2/index.d.mts +5 -17
- package/dist/oauth2/index.mjs +156 -16
- package/dist/{origin-check-DjLTuZQ8.cjs → origin-check-BflGhEEN.cjs} +34 -38
- package/dist/{origin-check-8Gajklvh.mjs → origin-check-CRgs1M7k.mjs} +13 -16
- package/dist/{parser-B25ylT5u.mjs → parser-CqwvpF6l.cjs} +12 -10
- package/dist/{parser-B_wxvaAr.cjs → parser-DcKDjjHO.mjs} +7 -22
- package/dist/plugins/access/index.cjs +38 -4
- package/dist/plugins/access/index.d.cts +36 -2
- package/dist/plugins/access/index.d.mts +36 -2
- package/dist/plugins/access/index.mjs +36 -3
- package/dist/plugins/bearer/index.cjs +52 -7
- package/dist/plugins/bearer/index.d.cts +27 -4
- package/dist/plugins/bearer/index.d.mts +27 -4
- package/dist/plugins/bearer/index.mjs +51 -7
- package/dist/plugins/index.cjs +1385 -842
- package/dist/plugins/index.d.cts +489 -7566
- package/dist/plugins/index.d.mts +489 -7566
- package/dist/plugins/index.mjs +1118 -576
- package/dist/plugins/passkey/index.cjs +551 -15
- package/dist/plugins/passkey/index.d.cts +123 -4
- package/dist/plugins/passkey/index.d.mts +123 -4
- package/dist/plugins/passkey/index.mjs +547 -15
- package/dist/{chunk-BFnQceUY.mjs → rolldown-runtime-BKpuH2hG.mjs} +6 -7
- package/dist/{chunk-fPmz2rbM.cjs → rolldown-runtime-C_Pgydhf.cjs} +22 -24
- package/dist/{schema-DTIwLLmf.mjs → schema-CFCpv0tK.mjs} +3 -6
- package/dist/{schema-CirKflVs.cjs → schema-b-lKhMkG.cjs} +51 -55
- package/dist/{session-CM3rmOYF.mjs → session-BGjJgihS.mjs} +51 -74
- package/dist/{session-Cwrj4GfV.cjs → session-CzZSMmQC.cjs} +197 -219
- package/dist/shared.d.cts +1581 -0
- package/dist/shared.d.mts +1581 -0
- package/dist/social-providers/index.cjs +9 -9
- package/dist/social-providers/index.d.cts +1 -1
- package/dist/social-providers/index.d.mts +1 -1
- package/dist/social-providers/index.mjs +3 -5
- package/dist/social-providers-CjPww-qf.mjs +6 -0
- package/dist/social-providers-D7w_7Ym8.cjs +10 -0
- package/dist/{state-CvSihlVT.cjs → state-2OrNjOO-.cjs} +24 -24
- package/dist/{state-CQghgH9I.mjs → state-Dooeisfq.mjs} +17 -17
- package/dist/{bun-sqlite-dialect-DPgTzc0w.cjs → test-utils/bun-sqlite-dialect-BHkarnxN.cjs} +5 -9
- package/dist/{bun-sqlite-dialect-6NqeV3px.mjs → test-utils/bun-sqlite-dialect-BiZBuEF6.mjs} +6 -9
- package/dist/test-utils/index.cjs +8831 -7941
- package/dist/test-utils/index.d.cts +310 -12440
- package/dist/test-utils/index.d.mts +313 -12440
- package/dist/test-utils/index.mjs +8591 -7701
- package/dist/test-utils/{node-sqlite-dialect-DAQXiobH.mjs → node-sqlite-dialect-BMVOjEMh.mjs} +6 -9
- package/dist/{node-sqlite-dialect-DP11CB0y.cjs → test-utils/node-sqlite-dialect-D3QPFROH.cjs} +5 -9
- package/dist/types/index.d.cts +2 -3
- package/dist/types/index.d.mts +2 -3
- package/dist/types/index.mjs +1 -1
- package/dist/{url-BoUuoIwZ.mjs → url-CJtdrrUu.mjs} +12 -13
- package/dist/{url-Ce7CVaQD.cjs → url-DjCmCUHS.cjs} +42 -44
- package/dist/utils/cookies.cjs +29 -30
- package/dist/utils/cookies.d.cts +21 -25
- package/dist/utils/cookies.d.mts +20 -24
- package/dist/utils/cookies.mjs +2 -7
- package/dist/utils-C7OHyfHa.mjs +4 -0
- package/dist/utils-iX0Vy_UL.cjs +3 -0
- package/package.json +40 -30
- package/dist/access-BZ7-AbTM.cjs +0 -51
- package/dist/access-D0DRqUKC.mjs +0 -39
- package/dist/bearer-DDzrp5M7.cjs +0 -60
- package/dist/bearer-WZcs7gxL.mjs +0 -54
- package/dist/db-4tUsQJXd.mjs +0 -1119
- package/dist/db-ChMbb_U-.cjs +0 -1180
- package/dist/defu-BmOYq4PU.cjs +0 -34
- package/dist/defu-D9ZTXqlo.mjs +0 -28
- package/dist/hono-C14rpOEa.cjs +0 -13
- package/dist/hono-CoKYWnXA.mjs +0 -7
- package/dist/hono-D0VqxVy1.d.mts +0 -18
- package/dist/hono-DOM31WQz.d.cts +0 -18
- package/dist/index-BCTFWP_o.d.mts +0 -992
- package/dist/index-BDhhaE6Y.d.cts +0 -3
- package/dist/index-BVRfQy6L.d.mts +0 -992
- package/dist/index-Baarsq5J.d.mts +0 -33
- package/dist/index-By364WbF.d.cts +0 -992
- package/dist/index-BzWwMgxr.d.cts +0 -992
- package/dist/index-CF9iq55a.d.mts +0 -3
- package/dist/index-CJD2yB-W.d.cts +0 -33
- package/dist/index-CRt_fppq.d.mts +0 -41
- package/dist/index-CdLyn7qP.d.cts +0 -41
- package/dist/index-CsWF3f7U.d.cts +0 -13350
- package/dist/index-CzidsAyA.d.mts +0 -13350
- package/dist/index-DvW-GCcI.d.cts +0 -33
- package/dist/index-f-5OJ9yV.d.cts +0 -13350
- package/dist/index-rFv8Hxld.d.mts +0 -13350
- package/dist/index-ztVZPQdV.d.mts +0 -33
- package/dist/kysely-adapter-CtIdoHa8.mjs +0 -270
- package/dist/kysely-adapter-QZTx077T.cjs +0 -288
- package/dist/memory-adapter-17RZd5df.cjs +0 -154
- package/dist/memory-adapter-Dp4XdCZK.mjs +0 -148
- package/dist/oauth2-5RL4cVNS.cjs +0 -177
- package/dist/oauth2-C56wz-n9.mjs +0 -159
- package/dist/passkey-BTT-v5c-.cjs +0 -512
- package/dist/passkey-CzY_W2us.mjs +0 -505
- package/dist/request-1NL_1lyJ.mjs +0 -58
- package/dist/request-CPNlp-DK.cjs +0 -100
- package/dist/social-providers-B8RK5yqI.mjs +0 -9
- package/dist/social-providers-BmFOIIPk.cjs +0 -13
- package/dist/utils-44aDDP8f.cjs +0 -2
- package/dist/utils-D4vZd1-9.mjs +0 -4
|
@@ -1,34 +1,44 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const require_cookies = require(
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const require_schema = require(
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
let
|
|
17
|
-
let
|
|
18
|
-
let
|
|
1
|
+
const require_rolldown_runtime = require("./rolldown-runtime-C_Pgydhf.cjs");
|
|
2
|
+
require("./hono-pgK4H_U1.cjs");
|
|
3
|
+
const require_oauth2_index = require("./oauth2/index.cjs");
|
|
4
|
+
const require_state = require("./state-2OrNjOO-.cjs");
|
|
5
|
+
const require_cookies = require("./cookies-DQ52rcOy.cjs");
|
|
6
|
+
const require_session = require("./session-CzZSMmQC.cjs");
|
|
7
|
+
const require_social_providers = require("./social-providers-D7w_7Ym8.cjs");
|
|
8
|
+
const require_email_verification = require("./email-verification-DOPnSgbI.cjs");
|
|
9
|
+
const require_json = require("./json-D3aq-nH2.cjs");
|
|
10
|
+
require("./utils-iX0Vy_UL.cjs");
|
|
11
|
+
const require_origin_check = require("./origin-check-BflGhEEN.cjs");
|
|
12
|
+
const require_crypto = require("./crypto-BITfWVwD.cjs");
|
|
13
|
+
const require_schema = require("./schema-b-lKhMkG.cjs");
|
|
14
|
+
const require_ip = require("./ip-DPydExzV.cjs");
|
|
15
|
+
const require_middleware = require("./middleware-Cd-VkNxB.cjs");
|
|
16
|
+
let _faire_auth_core_env = require("@faire-auth/core/env");
|
|
17
|
+
let _faire_auth_core_error = require("@faire-auth/core/error");
|
|
18
|
+
let _faire_auth_core_factory = require("@faire-auth/core/factory");
|
|
19
|
+
let _faire_auth_core_static = require("@faire-auth/core/static");
|
|
20
|
+
let _faire_auth_core_utils = require("@faire-auth/core/utils");
|
|
19
21
|
let hono = require("hono");
|
|
20
22
|
let hono_router_reg_exp_router = require("hono/router/reg-exp-router");
|
|
21
23
|
let zod = require("zod");
|
|
22
|
-
zod =
|
|
23
|
-
let
|
|
24
|
-
let
|
|
24
|
+
zod = require_rolldown_runtime.__toESM(zod, 1);
|
|
25
|
+
let _faire_auth_core_db = require("@faire-auth/core/db");
|
|
26
|
+
let _faire_auth_core_crypto = require("@faire-auth/core/crypto");
|
|
25
27
|
let hono_context_storage = require("hono/context-storage");
|
|
26
|
-
|
|
27
|
-
//#region src/api/schema/account.ts
|
|
28
|
+
// src/api/schema/account.ts
|
|
28
29
|
const socialProviderListEnum = zod.enum(require_social_providers.social_providers_exports.socialProviderList).or(zod.string());
|
|
29
30
|
const linkAccountSchema = zod.object({
|
|
30
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Callback URL to redirect to after the user has signed in.
|
|
33
|
+
*/
|
|
34
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The URL to redirect to after the user has signed in" }),
|
|
35
|
+
/**
|
|
36
|
+
* OAuth2 provider to use
|
|
37
|
+
*/
|
|
31
38
|
provider: socialProviderListEnum,
|
|
39
|
+
/**
|
|
40
|
+
* ID Token for direct authentication without redirect
|
|
41
|
+
*/
|
|
32
42
|
idToken: zod.object({
|
|
33
43
|
token: zod.string(),
|
|
34
44
|
nonce: zod.string().optional(),
|
|
@@ -36,8 +46,16 @@ const linkAccountSchema = zod.object({
|
|
|
36
46
|
refreshToken: zod.string().optional(),
|
|
37
47
|
scopes: zod.array(zod.string().min(1)).optional()
|
|
38
48
|
}).optional(),
|
|
49
|
+
/**
|
|
50
|
+
* Additional scopes to request when linking the account.
|
|
51
|
+
* This is useful for requesting additional permissions when
|
|
52
|
+
* linking a social account compared to the initial authentication.
|
|
53
|
+
*/
|
|
39
54
|
scopes: zod.array(zod.string().min(1)).optional().openapi({ description: "Additional scopes to request when linking the account" }),
|
|
40
|
-
|
|
55
|
+
/**
|
|
56
|
+
* The URL to redirect to if there is an error during the link process.
|
|
57
|
+
*/
|
|
58
|
+
errorCallbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The URL to redirect to if there is an error during the link process" })
|
|
41
59
|
});
|
|
42
60
|
const unlinkAccountSchema = zod.object({
|
|
43
61
|
providerId: zod.string(),
|
|
@@ -54,42 +72,112 @@ const refreshAccessTokenSchema = zod.object({
|
|
|
54
72
|
userId: zod.string().optional().openapi({ description: "The user ID associated with the account" })
|
|
55
73
|
});
|
|
56
74
|
const getAccountInfoSchema = zod.object({ accountId: zod.string().openapi({ description: "The provider given account id for which to get the account info" }) });
|
|
57
|
-
|
|
58
|
-
//#endregion
|
|
59
|
-
//#region src/api/schema/sign-in.ts
|
|
75
|
+
// src/api/schema/sign-in.ts
|
|
60
76
|
const signInSocialSchema = zod.object({
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
77
|
+
/**
|
|
78
|
+
* Callback URL to redirect to after the user
|
|
79
|
+
* has signed in.
|
|
80
|
+
*/
|
|
81
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "Callback URL to redirect to after the user has signed in" }),
|
|
82
|
+
/**
|
|
83
|
+
* callback url to redirect if the user is newly registered.
|
|
84
|
+
*
|
|
85
|
+
* useful if you have different routes for existing users and new users
|
|
86
|
+
*/
|
|
87
|
+
newUserCallbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true),
|
|
88
|
+
/**
|
|
89
|
+
* Callback url to redirect to if an error happens
|
|
90
|
+
*
|
|
91
|
+
* If it's initiated from the client sdk this defaults to
|
|
92
|
+
* the current url.
|
|
93
|
+
*/
|
|
94
|
+
errorCallbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "Callback URL to redirect to if an error happens" }),
|
|
95
|
+
/**
|
|
96
|
+
* OAuth2 provider to use
|
|
97
|
+
*/
|
|
64
98
|
provider: socialProviderListEnum,
|
|
99
|
+
/**
|
|
100
|
+
* Disable automatic redirection to the provider
|
|
101
|
+
*
|
|
102
|
+
* This is useful if you want to handle the redirection
|
|
103
|
+
* yourself like in a popup or a different tab.
|
|
104
|
+
*/
|
|
65
105
|
disableRedirect: zod.boolean().optional().openapi({ description: "Disable automatic redirection to the provider. Useful for handling the redirection yourself" }),
|
|
106
|
+
/**
|
|
107
|
+
* ID token from the provider
|
|
108
|
+
*
|
|
109
|
+
* This is used to sign in the user
|
|
110
|
+
* if the user is already signed in with the
|
|
111
|
+
* provider in the frontend.
|
|
112
|
+
*
|
|
113
|
+
* Only applicable if the provider supports
|
|
114
|
+
* it. Currently only `apple` and `google` is
|
|
115
|
+
* supported out of the box.
|
|
116
|
+
*/
|
|
66
117
|
idToken: zod.optional(zod.object({
|
|
118
|
+
/**
|
|
119
|
+
* ID token from the provider
|
|
120
|
+
*/
|
|
67
121
|
token: zod.string().openapi({ description: "ID token from the provider" }),
|
|
122
|
+
/**
|
|
123
|
+
* The nonce used to generate the token
|
|
124
|
+
*/
|
|
68
125
|
nonce: zod.string().optional().openapi({ description: "Nonce used to generate the token" }),
|
|
126
|
+
/**
|
|
127
|
+
* Access token from the provider
|
|
128
|
+
*/
|
|
69
129
|
accessToken: zod.string().optional().openapi({ description: "Access token from the provider" }),
|
|
130
|
+
/**
|
|
131
|
+
* Refresh token from the provider
|
|
132
|
+
*/
|
|
70
133
|
refreshToken: zod.string().optional().openapi({ description: "Refresh token from the provider" }),
|
|
134
|
+
/**
|
|
135
|
+
* Expiry date of the token
|
|
136
|
+
*/
|
|
71
137
|
expiresAt: zod.number().optional().openapi({ description: "Expiry date of the token" })
|
|
72
138
|
})).openapi({ description: "ID token from the provider to sign in the user with id token" }),
|
|
73
139
|
scopes: zod.array(zod.string().min(1)).optional().openapi({ description: "Array of scopes to request from the provider. This will override the default scopes passed." }),
|
|
140
|
+
/**
|
|
141
|
+
* Explicitly request sign-up
|
|
142
|
+
*
|
|
143
|
+
* Should be used to allow sign up when
|
|
144
|
+
* disableImplicitSignUp for this provider is
|
|
145
|
+
* true
|
|
146
|
+
*/
|
|
74
147
|
requestSignUp: zod.boolean().optional().openapi({ description: "Explicitly request sign-up. Useful when disableImplicitSignUp is true for this provider" }),
|
|
148
|
+
/**
|
|
149
|
+
* The login hint to use for the authorization code request
|
|
150
|
+
*/
|
|
75
151
|
loginHint: zod.string().optional().openapi({ description: "The login hint to use for the authorization code request" })
|
|
76
152
|
});
|
|
77
153
|
const signInEmailSchema = zod.object({
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
154
|
+
/**
|
|
155
|
+
* Email of the user
|
|
156
|
+
*/
|
|
157
|
+
email: _faire_auth_core_factory.emailSchema.openapi({ description: "Email of the user" }),
|
|
158
|
+
/**
|
|
159
|
+
* Password of the user
|
|
160
|
+
*/
|
|
161
|
+
password: _faire_auth_core_factory.passwordSchema.openapi({ description: "Password of the user" }),
|
|
162
|
+
/**
|
|
163
|
+
* Callback URL to use as a redirect for email
|
|
164
|
+
* verification and for possible redirects
|
|
165
|
+
*/
|
|
166
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "Callback URL to use as a redirect for email verification" }),
|
|
167
|
+
/**
|
|
168
|
+
* If this is false, the session will not be remembered
|
|
169
|
+
* @default true
|
|
170
|
+
*/
|
|
81
171
|
rememberMe: zod.boolean().default(true).openapi({ description: "If this is false, the session will not be remembered. Default is `true`." })
|
|
82
172
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
const signInSocialRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
87
|
-
operationId: __faire_auth_core_static.Routes.SIGN_IN_SOCIAL,
|
|
173
|
+
// src/api/routes/sign-in.ts
|
|
174
|
+
const signInSocialRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
175
|
+
operationId: _faire_auth_core_static.Routes.SIGN_IN_SOCIAL,
|
|
88
176
|
method: "post",
|
|
89
177
|
path: "/sign-in/social",
|
|
90
178
|
description: "Sign in with a social provider",
|
|
91
|
-
request: (0,
|
|
92
|
-
responses: (0,
|
|
179
|
+
request: (0, _faire_auth_core_factory.req)().bdy(signInSocialSchema).bld(),
|
|
180
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SIGN_IN_UP].default, "Returns either session details or redirect URL").rdr().err(401, "Invalid token or user not found").err(404, "Provider or id token not supported").zod().bld()
|
|
93
181
|
});
|
|
94
182
|
const signInSocial = require_session.createEndpoint(signInSocialRoute, (options) => async (ctx) => {
|
|
95
183
|
const { provider: requestedProvider, idToken, requestSignUp, callbackURL, scopes, loginHint, disableRedirect } = ctx.req.valid("json");
|
|
@@ -98,24 +186,24 @@ const signInSocial = require_session.createEndpoint(signInSocialRoute, (options)
|
|
|
98
186
|
if (!provider) {
|
|
99
187
|
context.logger.error("Provider not found. Make sure to add the provider in your auth config", { provider: requestedProvider });
|
|
100
188
|
return ctx.render({
|
|
101
|
-
success:
|
|
102
|
-
message:
|
|
189
|
+
success: _faire_auth_core_static.False,
|
|
190
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PROVIDER_NOT_FOUND
|
|
103
191
|
}, 404);
|
|
104
192
|
}
|
|
105
193
|
if (idToken) {
|
|
106
194
|
if (!provider.verifyIdToken) {
|
|
107
195
|
context.logger.error("Provider does not support id token verification", { provider: requestedProvider });
|
|
108
196
|
return ctx.render({
|
|
109
|
-
success:
|
|
110
|
-
message:
|
|
197
|
+
success: _faire_auth_core_static.False,
|
|
198
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.ID_TOKEN_NOT_SUPPORTED
|
|
111
199
|
}, 404);
|
|
112
200
|
}
|
|
113
201
|
const { token, nonce } = idToken;
|
|
114
202
|
if (!await provider.verifyIdToken(token, nonce)) {
|
|
115
203
|
context.logger.error("Invalid id token", { provider: requestedProvider });
|
|
116
204
|
return ctx.render({
|
|
117
|
-
success:
|
|
118
|
-
message:
|
|
205
|
+
success: _faire_auth_core_static.False,
|
|
206
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
119
207
|
}, 401);
|
|
120
208
|
}
|
|
121
209
|
const userInfo = await provider.getUserInfo({
|
|
@@ -126,18 +214,18 @@ const signInSocial = require_session.createEndpoint(signInSocialRoute, (options)
|
|
|
126
214
|
if (!userInfo?.user) {
|
|
127
215
|
context.logger.error("Failed to get user info", { provider: requestedProvider });
|
|
128
216
|
return ctx.render({
|
|
129
|
-
success:
|
|
130
|
-
message:
|
|
217
|
+
success: _faire_auth_core_static.False,
|
|
218
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_GET_USER_INFO
|
|
131
219
|
}, 401);
|
|
132
220
|
}
|
|
133
221
|
if (userInfo.user.email == null) {
|
|
134
222
|
context.logger.error("User email not found", { provider: requestedProvider });
|
|
135
223
|
return ctx.render({
|
|
136
|
-
success:
|
|
137
|
-
message:
|
|
224
|
+
success: _faire_auth_core_static.False,
|
|
225
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.USER_EMAIL_NOT_FOUND
|
|
138
226
|
}, 401);
|
|
139
227
|
}
|
|
140
|
-
const data = await
|
|
228
|
+
const data = await require_oauth2_index.handleOAuthUserInfo(ctx, {
|
|
141
229
|
userInfo: {
|
|
142
230
|
...userInfo.user,
|
|
143
231
|
email: userInfo.user.email,
|
|
@@ -156,13 +244,13 @@ const signInSocial = require_session.createEndpoint(signInSocialRoute, (options)
|
|
|
156
244
|
}, options);
|
|
157
245
|
if (data.type === "redirect") return data.response;
|
|
158
246
|
if (data.type === "error") return ctx.render({
|
|
159
|
-
success:
|
|
247
|
+
success: _faire_auth_core_static.False,
|
|
160
248
|
message: data.reason.replaceAll("_", " ")
|
|
161
249
|
}, 401);
|
|
162
250
|
await require_cookies.setSessionCookie(ctx, options, data.data);
|
|
163
251
|
return ctx.render({
|
|
164
|
-
success:
|
|
165
|
-
redirect:
|
|
252
|
+
success: _faire_auth_core_static.True,
|
|
253
|
+
redirect: _faire_auth_core_static.False,
|
|
166
254
|
token: data.data.session.token,
|
|
167
255
|
user: data.data.user
|
|
168
256
|
}, 200);
|
|
@@ -176,29 +264,29 @@ const signInSocial = require_session.createEndpoint(signInSocialRoute, (options)
|
|
|
176
264
|
...loginHint != null && loginHint !== "" && { loginHint }
|
|
177
265
|
});
|
|
178
266
|
if (disableRedirect === true) return ctx.render({
|
|
179
|
-
success:
|
|
180
|
-
redirect:
|
|
267
|
+
success: _faire_auth_core_static.False,
|
|
268
|
+
redirect: _faire_auth_core_static.False
|
|
181
269
|
}, 200);
|
|
182
270
|
return ctx.render({
|
|
183
|
-
success:
|
|
184
|
-
redirect:
|
|
271
|
+
success: _faire_auth_core_static.False,
|
|
272
|
+
redirect: _faire_auth_core_static.True,
|
|
185
273
|
url: url.toString()
|
|
186
274
|
}, 200);
|
|
187
275
|
});
|
|
188
|
-
const signInEmailRoute = (0,
|
|
189
|
-
operationId:
|
|
276
|
+
const signInEmailRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
277
|
+
operationId: _faire_auth_core_static.Routes.SIGN_IN_EMAIL,
|
|
190
278
|
method: "post",
|
|
191
279
|
path: "/sign-in/email",
|
|
192
280
|
description: "Sign in with email and password",
|
|
193
|
-
request: (0,
|
|
194
|
-
responses: (0,
|
|
281
|
+
request: (0, _faire_auth_core_factory.req)().bdy(signInEmailSchema).bld(),
|
|
282
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SIGN_IN_UP].default, "Returns either session details or redirect URL").err(400, "Email and password login not enabled in options").err(401, "Invalid email or password").err(403, "Email not verified").zod().err(500, "Failed to create session").bld()
|
|
195
283
|
});
|
|
196
284
|
const signInEmail = require_session.createEndpoint(signInEmailRoute, (options) => async (ctx) => {
|
|
197
285
|
const context = ctx.get("context");
|
|
198
286
|
if (!(options.emailAndPassword?.enabled ?? false)) {
|
|
199
287
|
context.logger.error("Email and password is not enabled. Make sure to enable it in the options on you `auth.ts` file. Check `https://faire-auth.com/docs/authentication/email-password` for more!");
|
|
200
288
|
return ctx.render({
|
|
201
|
-
success:
|
|
289
|
+
success: _faire_auth_core_static.False,
|
|
202
290
|
message: "Email and password is not enabled"
|
|
203
291
|
}, 400);
|
|
204
292
|
}
|
|
@@ -208,24 +296,24 @@ const signInEmail = require_session.createEndpoint(signInEmailRoute, (options) =
|
|
|
208
296
|
await context.password.hash(password);
|
|
209
297
|
context.logger.error("User not found", { email });
|
|
210
298
|
return ctx.render({
|
|
211
|
-
success:
|
|
212
|
-
message:
|
|
299
|
+
success: _faire_auth_core_static.False,
|
|
300
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_EMAIL_OR_PASSWORD
|
|
213
301
|
}, 401);
|
|
214
302
|
}
|
|
215
303
|
const credentialAccount = user.accounts.find((a) => a.providerId === "credential");
|
|
216
304
|
if (!credentialAccount) {
|
|
217
305
|
context.logger.error("Credential account not found", { email });
|
|
218
306
|
return ctx.render({
|
|
219
|
-
success:
|
|
220
|
-
message:
|
|
307
|
+
success: _faire_auth_core_static.False,
|
|
308
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_EMAIL_OR_PASSWORD
|
|
221
309
|
}, 401);
|
|
222
310
|
}
|
|
223
311
|
const currentPassword = credentialAccount.password;
|
|
224
312
|
if (currentPassword == null) {
|
|
225
313
|
context.logger.error("Password not found", { email });
|
|
226
314
|
return ctx.render({
|
|
227
|
-
success:
|
|
228
|
-
message:
|
|
315
|
+
success: _faire_auth_core_static.False,
|
|
316
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_EMAIL_OR_PASSWORD
|
|
229
317
|
}, 401);
|
|
230
318
|
}
|
|
231
319
|
if (!await context.password.verify({
|
|
@@ -234,14 +322,14 @@ const signInEmail = require_session.createEndpoint(signInEmailRoute, (options) =
|
|
|
234
322
|
})) {
|
|
235
323
|
context.logger.error("Invalid password");
|
|
236
324
|
return ctx.render({
|
|
237
|
-
success:
|
|
238
|
-
message:
|
|
325
|
+
success: _faire_auth_core_static.False,
|
|
326
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_EMAIL_OR_PASSWORD
|
|
239
327
|
}, 401);
|
|
240
328
|
}
|
|
241
329
|
if (options.emailAndPassword?.requireEmailVerification === true && !user.user.emailVerified) {
|
|
242
330
|
if (!options.emailVerification?.sendVerificationEmail) return ctx.render({
|
|
243
|
-
success:
|
|
244
|
-
message:
|
|
331
|
+
success: _faire_auth_core_static.False,
|
|
332
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.EMAIL_NOT_VERIFIED
|
|
245
333
|
}, 403);
|
|
246
334
|
const token = await require_email_verification.createEmailVerificationToken(context.secret, user.user.email, void 0, options.emailVerification.expiresIn);
|
|
247
335
|
const url = `${context.baseURL}/verify-email?token=${token}&callbackURL=${callbackURL ?? "/"}`;
|
|
@@ -251,16 +339,16 @@ const signInEmail = require_session.createEndpoint(signInEmailRoute, (options) =
|
|
|
251
339
|
token
|
|
252
340
|
}, ctx);
|
|
253
341
|
return ctx.render({
|
|
254
|
-
success:
|
|
255
|
-
message:
|
|
342
|
+
success: _faire_auth_core_static.False,
|
|
343
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.EMAIL_NOT_VERIFIED
|
|
256
344
|
}, 403);
|
|
257
345
|
}
|
|
258
346
|
const session = await context.internalAdapter.createSession(user.user.id, !rememberMe);
|
|
259
347
|
if (session == null) {
|
|
260
348
|
context.logger.error("Failed to create session");
|
|
261
349
|
return ctx.render({
|
|
262
|
-
success:
|
|
263
|
-
message:
|
|
350
|
+
success: _faire_auth_core_static.False,
|
|
351
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_CREATE_SESSION
|
|
264
352
|
}, 500);
|
|
265
353
|
}
|
|
266
354
|
await require_cookies.setSessionCookie(ctx, options, {
|
|
@@ -268,22 +356,20 @@ const signInEmail = require_session.createEndpoint(signInEmailRoute, (options) =
|
|
|
268
356
|
user: user.user
|
|
269
357
|
}, !rememberMe);
|
|
270
358
|
if (callbackURL != null) return ctx.render({
|
|
271
|
-
success:
|
|
272
|
-
redirect:
|
|
359
|
+
success: _faire_auth_core_static.True,
|
|
360
|
+
redirect: _faire_auth_core_static.True,
|
|
273
361
|
url: callbackURL,
|
|
274
362
|
token: session.token,
|
|
275
363
|
user: user.user
|
|
276
364
|
}, 200);
|
|
277
365
|
return ctx.render({
|
|
278
|
-
success:
|
|
279
|
-
redirect:
|
|
366
|
+
success: _faire_auth_core_static.True,
|
|
367
|
+
redirect: _faire_auth_core_static.False,
|
|
280
368
|
token: session.token,
|
|
281
369
|
user: user.user
|
|
282
370
|
}, 200);
|
|
283
371
|
});
|
|
284
|
-
|
|
285
|
-
//#endregion
|
|
286
|
-
//#region src/api/schema/callback.ts
|
|
372
|
+
// src/api/schema/callback.ts
|
|
287
373
|
const callbackSchema = zod.object({
|
|
288
374
|
code: zod.string(),
|
|
289
375
|
error: zod.string(),
|
|
@@ -300,24 +386,22 @@ const oauthCallbackParamsSchema = zod.object({ id: zod.string().openapi({
|
|
|
300
386
|
description: "OAuth provider ID"
|
|
301
387
|
}) });
|
|
302
388
|
const oauthCallbackQuerySchema = callbackSchema;
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
//#region src/api/routes/callback.ts
|
|
306
|
-
const handleCallbackOAuth = async (ctx, paramsId, { code, error: error$1, device_id, error_description, state, user } = {}, options) => {
|
|
389
|
+
// src/api/routes/callback.ts
|
|
390
|
+
const handleCallbackOAuth = async (ctx, paramsId, { code, error, device_id, error_description, state, user } = {}, options) => {
|
|
307
391
|
const context = ctx.get("context");
|
|
308
392
|
const defaultErrorURL = options.onAPIError?.errorURL ?? `${context.baseURL}/error`;
|
|
309
|
-
if (error
|
|
393
|
+
if (error != null) return ctx.redirect(`${defaultErrorURL}?error=${error}&error_description=${error_description}`, 302);
|
|
310
394
|
if (state == null) {
|
|
311
|
-
context.logger.error("State not found", error
|
|
395
|
+
context.logger.error("State not found", error);
|
|
312
396
|
return ctx.redirect(`${defaultErrorURL}?error=state_not_found`, 302);
|
|
313
397
|
}
|
|
314
|
-
const res
|
|
315
|
-
if (res
|
|
316
|
-
const { codeVerifier, callbackURL, link, errorURL, newUserURL, requestSignUp } = res
|
|
317
|
-
const redirectOnError = (error
|
|
398
|
+
const res = await require_state.parseState(ctx, options);
|
|
399
|
+
if (res.type === "redirect") return res.response;
|
|
400
|
+
const { codeVerifier, callbackURL, link, errorURL, newUserURL, requestSignUp } = res.data;
|
|
401
|
+
const redirectOnError = (error) => {
|
|
318
402
|
let url = errorURL ?? defaultErrorURL;
|
|
319
|
-
if (url.includes("?")) url = `${url}&error=${error
|
|
320
|
-
else url = `${url}?error=${error
|
|
403
|
+
if (url.includes("?")) url = `${url}&error=${error}`;
|
|
404
|
+
else url = `${url}?error=${error}`;
|
|
321
405
|
return ctx.redirect(url, 302);
|
|
322
406
|
};
|
|
323
407
|
if (code == null) {
|
|
@@ -344,7 +428,7 @@ const handleCallbackOAuth = async (ctx, paramsId, { code, error: error$1, device
|
|
|
344
428
|
const userInfo = await provider.getUserInfo({
|
|
345
429
|
...tokens,
|
|
346
430
|
...user && { user: require_json.safeJSONParse(user) }
|
|
347
|
-
}).then((res
|
|
431
|
+
}).then((res) => res?.user);
|
|
348
432
|
if (!userInfo) {
|
|
349
433
|
context.logger.error("Unable to get user info");
|
|
350
434
|
return redirectOnError("unable_to_get_user_info");
|
|
@@ -363,8 +447,8 @@ const handleCallbackOAuth = async (ctx, paramsId, { code, error: error$1, device
|
|
|
363
447
|
if (existingAccount.userId.toString() !== link.userId.toString()) return redirectOnError("account_already_linked_to_different_user");
|
|
364
448
|
const updateData = Object.fromEntries(Object.entries({
|
|
365
449
|
idToken: tokens.idToken,
|
|
366
|
-
accessToken: await
|
|
367
|
-
refreshToken: await
|
|
450
|
+
accessToken: await require_oauth2_index.setTokenUtil(tokens.accessToken, ctx, options),
|
|
451
|
+
refreshToken: await require_oauth2_index.setTokenUtil(tokens.refreshToken, ctx, options),
|
|
368
452
|
accessTokenExpiresAt: tokens.accessTokenExpiresAt,
|
|
369
453
|
refreshTokenExpiresAt: tokens.refreshTokenExpiresAt,
|
|
370
454
|
...tokens.scopes && tokens.scopes.length > 0 && { scope: tokens.scopes.join(",") }
|
|
@@ -375,23 +459,23 @@ const handleCallbackOAuth = async (ctx, paramsId, { code, error: error$1, device
|
|
|
375
459
|
providerId: provider.id,
|
|
376
460
|
accountId: userInfo.id,
|
|
377
461
|
...tokens,
|
|
378
|
-
accessToken: await
|
|
379
|
-
refreshToken: await
|
|
462
|
+
accessToken: await require_oauth2_index.setTokenUtil(tokens.accessToken, ctx, options),
|
|
463
|
+
refreshToken: await require_oauth2_index.setTokenUtil(tokens.refreshToken, ctx, options),
|
|
380
464
|
...tokens.scopes && tokens.scopes.length > 0 && { scope: tokens.scopes.join(",") }
|
|
381
465
|
}) == null) return redirectOnError("unable_to_link_account");
|
|
382
|
-
let toRedirectTo
|
|
466
|
+
let toRedirectTo;
|
|
383
467
|
try {
|
|
384
|
-
toRedirectTo
|
|
468
|
+
toRedirectTo = callbackURL.toString();
|
|
385
469
|
} catch {
|
|
386
|
-
toRedirectTo
|
|
470
|
+
toRedirectTo = callbackURL;
|
|
387
471
|
}
|
|
388
|
-
return ctx.redirect(toRedirectTo
|
|
472
|
+
return ctx.redirect(toRedirectTo, 302);
|
|
389
473
|
}
|
|
390
474
|
if (userInfo.email == null) {
|
|
391
475
|
context.logger.error("Provider did not return email. This could be due to misconfiguration in the provider settings.");
|
|
392
476
|
return redirectOnError("email_not_found");
|
|
393
477
|
}
|
|
394
|
-
const result = await
|
|
478
|
+
const result = await require_oauth2_index.handleOAuthUserInfo(ctx, {
|
|
395
479
|
userInfo: {
|
|
396
480
|
...userInfo,
|
|
397
481
|
id: userInfo.id.toString(),
|
|
@@ -426,43 +510,41 @@ const handleCallbackOAuth = async (ctx, paramsId, { code, error: error$1, device
|
|
|
426
510
|
}
|
|
427
511
|
return ctx.redirect(toRedirectTo, 302);
|
|
428
512
|
};
|
|
429
|
-
const getOAuthCallbackRoute = (0,
|
|
430
|
-
operationId:
|
|
513
|
+
const getOAuthCallbackRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
514
|
+
operationId: _faire_auth_core_static.Routes.OAUTH_CALLBACK_GET,
|
|
431
515
|
isAction: false,
|
|
432
516
|
method: "get",
|
|
433
517
|
path: "/callback/:id",
|
|
434
518
|
description: "Handle OAuth callback",
|
|
435
|
-
request: (0,
|
|
436
|
-
responses: (0,
|
|
519
|
+
request: (0, _faire_auth_core_factory.req)().qry(oauthCallbackQuerySchema).prm(oauthCallbackParamsSchema).bld(),
|
|
520
|
+
responses: (0, _faire_auth_core_factory.res)().rdr("Redirect to success or error URL").err(400).bld()
|
|
437
521
|
});
|
|
438
522
|
const getOAuthCallback = require_session.createEndpoint(getOAuthCallbackRoute, (options) => async (ctx) => {
|
|
439
523
|
const query = ctx.req.valid("query");
|
|
440
524
|
const { id: paramsId } = ctx.req.valid("param");
|
|
441
525
|
return handleCallbackOAuth(ctx, paramsId, query, options);
|
|
442
526
|
});
|
|
443
|
-
const postOAuthCallbackRoute = (0,
|
|
444
|
-
operationId:
|
|
527
|
+
const postOAuthCallbackRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
528
|
+
operationId: _faire_auth_core_static.Routes.OAUTH_CALLBACK_POST,
|
|
445
529
|
isAction: false,
|
|
446
530
|
method: "post",
|
|
447
531
|
path: "/callback/:id",
|
|
448
532
|
description: "Handle OAuth callback (POST)",
|
|
449
|
-
request: (0,
|
|
450
|
-
responses: (0,
|
|
533
|
+
request: (0, _faire_auth_core_factory.req)().bdy(callbackSchema.optional()).prm(oauthCallbackParamsSchema).bld(),
|
|
534
|
+
responses: (0, _faire_auth_core_factory.res)().rdr("Redirect to success or error URL").err(400).zod().bld()
|
|
451
535
|
});
|
|
452
536
|
const postOAuthCallback = require_session.createEndpoint(postOAuthCallbackRoute, (options) => async (ctx) => {
|
|
453
537
|
const body = ctx.req.valid("json");
|
|
454
538
|
const { id: paramsId } = ctx.req.valid("param");
|
|
455
539
|
return handleCallbackOAuth(ctx, paramsId, body, options);
|
|
456
540
|
});
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
const signOutRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
461
|
-
operationId: __faire_auth_core_static.Routes.SIGN_OUT,
|
|
541
|
+
// src/api/routes/sign-out.ts
|
|
542
|
+
const signOutRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
543
|
+
operationId: _faire_auth_core_static.Routes.SIGN_OUT,
|
|
462
544
|
method: "post",
|
|
463
545
|
path: "/sign-out",
|
|
464
546
|
description: "Sign out the current user",
|
|
465
|
-
responses: (0,
|
|
547
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).err(400).bld()
|
|
466
548
|
});
|
|
467
549
|
const signOut = require_session.createEndpoint(signOutRoute, (_options) => async (ctx) => {
|
|
468
550
|
const context = ctx.get("context");
|
|
@@ -470,26 +552,33 @@ const signOut = require_session.createEndpoint(signOutRoute, (_options) => async
|
|
|
470
552
|
if (sessionCookieToken == null || sessionCookieToken === false) {
|
|
471
553
|
require_cookies.deleteSessionCookie(ctx);
|
|
472
554
|
return ctx.render({
|
|
473
|
-
success:
|
|
474
|
-
message:
|
|
555
|
+
success: _faire_auth_core_static.False,
|
|
556
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_GET_SESSION
|
|
475
557
|
}, 400);
|
|
476
558
|
}
|
|
477
559
|
await context.internalAdapter.deleteSession(sessionCookieToken);
|
|
478
560
|
require_cookies.deleteSessionCookie(ctx);
|
|
479
|
-
return ctx.render({ success:
|
|
561
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
480
562
|
});
|
|
481
|
-
|
|
482
|
-
//#endregion
|
|
483
|
-
//#region src/api/schema/reset-password.ts
|
|
563
|
+
// src/api/schema/reset-password.ts
|
|
484
564
|
const requestPasswordResetSchema = zod.object({
|
|
485
|
-
|
|
486
|
-
|
|
565
|
+
/**
|
|
566
|
+
* The email address of the user to send the password reset email to.
|
|
567
|
+
*/
|
|
568
|
+
email: _faire_auth_core_factory.emailSchema.openapi({ description: "The email address of the user to send the password reset email to" }),
|
|
569
|
+
/**
|
|
570
|
+
* The URL to redirect the user to reset their password.
|
|
571
|
+
* If the token isn't valid or expired, it'll be redirected with a query parameter `?
|
|
572
|
+
* error=INVALID_TOKEN`. If the token is valid, it'll be redirected with a query parameter `?
|
|
573
|
+
* token=VALID_TOKEN
|
|
574
|
+
*/
|
|
575
|
+
redirectTo: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The URL to redirect the user to reset their password. If the token isn't valid or expired, it'll be redirected with a query parameter `?error=INVALID_TOKEN`. If the token is valid, it'll be redirected with a query parameter `?token=VALID_TOKEN`" })
|
|
487
576
|
});
|
|
488
577
|
const requestPasswordResetCallbackParamsSchema = zod.object({ token: zod.string().openapi({ param: {
|
|
489
578
|
in: "path",
|
|
490
579
|
name: "token"
|
|
491
580
|
} }) });
|
|
492
|
-
const requestPasswordResetCallbackQuerySchema = zod.object({ callbackURL: (0,
|
|
581
|
+
const requestPasswordResetCallbackQuerySchema = zod.object({ callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)().openapi({
|
|
493
582
|
param: {
|
|
494
583
|
in: "query",
|
|
495
584
|
name: "callbackURL"
|
|
@@ -497,16 +586,14 @@ const requestPasswordResetCallbackQuerySchema = zod.object({ callbackURL: (0, __
|
|
|
497
586
|
description: "The URL to redirect the user to reset their password"
|
|
498
587
|
}) });
|
|
499
588
|
const resetPasswordSchema = zod.object({
|
|
500
|
-
newPassword:
|
|
589
|
+
newPassword: _faire_auth_core_factory.passwordSchema.openapi({ description: "The new password to set" }),
|
|
501
590
|
token: zod.string().optional().openapi({ description: "The token to reset the password" })
|
|
502
591
|
});
|
|
503
592
|
const resetPasswordQuerySchema = zod.object({ token: zod.string().optional().openapi({ param: {
|
|
504
593
|
in: "query",
|
|
505
594
|
name: "token"
|
|
506
595
|
} }) });
|
|
507
|
-
|
|
508
|
-
//#endregion
|
|
509
|
-
//#region src/api/routes/reset-password.ts
|
|
596
|
+
// src/api/routes/reset-password.ts
|
|
510
597
|
const redirectError = (ctx, callbackURL, query) => {
|
|
511
598
|
const url = callbackURL != null ? new URL(callbackURL, ctx.get("context").baseURL) : new URL(`${ctx.get("context").baseURL}/error`);
|
|
512
599
|
if (query) Object.entries(query).forEach(([k, v]) => url.searchParams.set(k, v));
|
|
@@ -517,20 +604,20 @@ const redirectCallback = (ctx, callbackURL, query) => {
|
|
|
517
604
|
if (query) Object.entries(query).forEach(([k, v]) => url.searchParams.set(k, v));
|
|
518
605
|
return url.href;
|
|
519
606
|
};
|
|
520
|
-
const requestPasswordResetRoute = (0,
|
|
521
|
-
operationId:
|
|
607
|
+
const requestPasswordResetRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
608
|
+
operationId: _faire_auth_core_static.Routes.REQUEST_PASSWORD_RESET,
|
|
522
609
|
method: "post",
|
|
523
610
|
path: "/request-password-reset",
|
|
524
611
|
description: "Send a password reset email to the user",
|
|
525
|
-
request: (0,
|
|
526
|
-
responses: (0,
|
|
612
|
+
request: (0, _faire_auth_core_factory.req)().bdy(requestPasswordResetSchema).bld(),
|
|
613
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).err(400, "Reset password not enabled in options").zod().bld()
|
|
527
614
|
});
|
|
528
615
|
const requestPasswordReset = require_session.createEndpoint(requestPasswordResetRoute, (options) => async (ctx) => {
|
|
529
616
|
const context = ctx.get("context");
|
|
530
617
|
if (!options.emailAndPassword?.sendResetPassword) {
|
|
531
618
|
context.logger.error("Reset password isn't enabled. Please add an emailAndPassword and sendResetPassword function to your auth config!");
|
|
532
619
|
return ctx.render({
|
|
533
|
-
success:
|
|
620
|
+
success: _faire_auth_core_static.False,
|
|
534
621
|
message: "Reset password isn't enabled"
|
|
535
622
|
}, 400);
|
|
536
623
|
}
|
|
@@ -539,12 +626,12 @@ const requestPasswordReset = require_session.createEndpoint(requestPasswordReset
|
|
|
539
626
|
if (!user) {
|
|
540
627
|
context.logger.error("Reset Password: User not found", { email });
|
|
541
628
|
return ctx.render({
|
|
542
|
-
success:
|
|
629
|
+
success: _faire_auth_core_static.True,
|
|
543
630
|
message: "If this email exists in our system, check your email for the reset link"
|
|
544
631
|
}, 200);
|
|
545
632
|
}
|
|
546
|
-
const expiresAt = (0,
|
|
547
|
-
const verificationToken = (0,
|
|
633
|
+
const expiresAt = (0, _faire_auth_core_utils.getDate)(options.emailAndPassword.resetPasswordTokenExpiresIn ?? 3600, "sec");
|
|
634
|
+
const verificationToken = (0, _faire_auth_core_crypto.generateId)(24);
|
|
548
635
|
await context.internalAdapter.createVerificationValue({
|
|
549
636
|
value: user.user.id,
|
|
550
637
|
identifier: `reset-password:${verificationToken}`,
|
|
@@ -557,16 +644,16 @@ const requestPasswordReset = require_session.createEndpoint(requestPasswordReset
|
|
|
557
644
|
url,
|
|
558
645
|
token: verificationToken
|
|
559
646
|
}, ctx);
|
|
560
|
-
return ctx.render({ success:
|
|
647
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
561
648
|
});
|
|
562
|
-
const requestPasswordResetCallbackRoute = (0,
|
|
563
|
-
operationId:
|
|
649
|
+
const requestPasswordResetCallbackRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
650
|
+
operationId: _faire_auth_core_static.Routes.REQUEST_PASSWORD_RESET_CALLBACK,
|
|
564
651
|
method: "get",
|
|
565
652
|
path: "/reset-password/:token",
|
|
566
653
|
description: "Redirects the user to the callback URL with the token",
|
|
567
654
|
middleware: [require_origin_check.originCheck((ctx) => ctx.req.query("callbackURL"))],
|
|
568
|
-
request: (0,
|
|
569
|
-
responses: (0,
|
|
655
|
+
request: (0, _faire_auth_core_factory.req)().prm(requestPasswordResetCallbackParamsSchema).qry(requestPasswordResetCallbackQuerySchema).bld(),
|
|
656
|
+
responses: (0, _faire_auth_core_factory.res)().rdr().bld()
|
|
570
657
|
});
|
|
571
658
|
const requestPasswordResetCallback = require_session.createEndpoint(requestPasswordResetCallbackRoute, (_options) => async (ctx) => {
|
|
572
659
|
const { token } = ctx.req.valid("param");
|
|
@@ -576,37 +663,37 @@ const requestPasswordResetCallback = require_session.createEndpoint(requestPassw
|
|
|
576
663
|
if (!verification || verification.expiresAt < /* @__PURE__ */ new Date()) return ctx.redirect(redirectError(ctx, callbackURL, { error: "INVALID_TOKEN" }), 302);
|
|
577
664
|
return ctx.redirect(redirectCallback(ctx, callbackURL, { token }), 302);
|
|
578
665
|
});
|
|
579
|
-
const resetPasswordRoute = (0,
|
|
580
|
-
operationId:
|
|
666
|
+
const resetPasswordRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
667
|
+
operationId: _faire_auth_core_static.Routes.RESET_PASSWORD,
|
|
581
668
|
method: "post",
|
|
582
669
|
path: "/reset-password",
|
|
583
670
|
description: "Reset the password for a user",
|
|
584
|
-
request: (0,
|
|
585
|
-
responses: (0,
|
|
671
|
+
request: (0, _faire_auth_core_factory.req)().qry(resetPasswordQuerySchema).bdy(resetPasswordSchema).bld(),
|
|
672
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).err(400, "Invalid token or password").zod().bld()
|
|
586
673
|
});
|
|
587
674
|
const resetPassword = require_session.createEndpoint(resetPasswordRoute, (options) => async (ctx) => {
|
|
588
675
|
const token = ctx.req.valid("json").token || ctx.req.valid("query").token;
|
|
589
676
|
if (token == null || token === "") return ctx.render({
|
|
590
|
-
success:
|
|
591
|
-
message:
|
|
677
|
+
success: _faire_auth_core_static.False,
|
|
678
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
592
679
|
}, 400);
|
|
593
680
|
const { newPassword } = ctx.req.valid("json");
|
|
594
681
|
const context = ctx.get("context");
|
|
595
682
|
const minLength = context.password.config.minPasswordLength;
|
|
596
683
|
const maxLength = context.password.config.maxPasswordLength;
|
|
597
684
|
if (newPassword.length < minLength) return ctx.render({
|
|
598
|
-
success:
|
|
599
|
-
message:
|
|
685
|
+
success: _faire_auth_core_static.False,
|
|
686
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_SHORT
|
|
600
687
|
}, 400);
|
|
601
688
|
if (newPassword.length > maxLength) return ctx.render({
|
|
602
|
-
success:
|
|
603
|
-
message:
|
|
689
|
+
success: _faire_auth_core_static.False,
|
|
690
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_LONG
|
|
604
691
|
}, 400);
|
|
605
692
|
const id = `reset-password:${token}`;
|
|
606
693
|
const verification = await context.internalAdapter.findVerificationValue(id);
|
|
607
694
|
if (!verification || verification.expiresAt < /* @__PURE__ */ new Date()) return ctx.render({
|
|
608
|
-
success:
|
|
609
|
-
message:
|
|
695
|
+
success: _faire_auth_core_static.False,
|
|
696
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
610
697
|
}, 400);
|
|
611
698
|
const userId = verification.value;
|
|
612
699
|
const hashedPassword = await context.password.hash(newPassword);
|
|
@@ -623,12 +710,10 @@ const resetPassword = require_session.createEndpoint(resetPasswordRoute, (option
|
|
|
623
710
|
if (user) await options.emailAndPassword.onPasswordReset({ user }, ctx.req);
|
|
624
711
|
}
|
|
625
712
|
if (options.emailAndPassword?.revokeSessionsOnPasswordReset) await context.internalAdapter.deleteSessions(userId);
|
|
626
|
-
return ctx.render({ success:
|
|
713
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
627
714
|
});
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
//#region src/api/schema/update-user.ts
|
|
631
|
-
const updateUserSchema = __faire_auth_core_db.looseUserSchema.omit({
|
|
715
|
+
// src/api/schema/update-user.ts
|
|
716
|
+
const updateUserSchema = _faire_auth_core_db.looseUserSchema.omit({
|
|
632
717
|
id: true,
|
|
633
718
|
createdAt: true,
|
|
634
719
|
updatedAt: true,
|
|
@@ -636,14 +721,35 @@ const updateUserSchema = __faire_auth_core_db.looseUserSchema.omit({
|
|
|
636
721
|
email: true
|
|
637
722
|
});
|
|
638
723
|
const changePasswordSchema = zod.object({
|
|
639
|
-
|
|
640
|
-
|
|
724
|
+
/**
|
|
725
|
+
* The new password to set
|
|
726
|
+
*/
|
|
727
|
+
newPassword: _faire_auth_core_factory.passwordSchema.openapi({ description: "The new password to set" }),
|
|
728
|
+
/**
|
|
729
|
+
* The current password of the user
|
|
730
|
+
*/
|
|
731
|
+
currentPassword: _faire_auth_core_factory.passwordSchema.openapi({ description: "The current password" }),
|
|
732
|
+
/**
|
|
733
|
+
* revoke all sessions that are not the
|
|
734
|
+
* current one logged in by the user
|
|
735
|
+
*/
|
|
641
736
|
revokeOtherSessions: zod.boolean().optional().openapi({ description: "Revoke all other sessions" })
|
|
642
737
|
});
|
|
643
|
-
const setPasswordSchema = zod.object({ newPassword:
|
|
738
|
+
const setPasswordSchema = zod.object({ newPassword: _faire_auth_core_factory.passwordSchema });
|
|
644
739
|
const deleteUserSchema = zod.object({
|
|
645
|
-
|
|
646
|
-
|
|
740
|
+
/**
|
|
741
|
+
* The callback URL to redirect to after the user is deleted
|
|
742
|
+
* this is only used on delete user callback
|
|
743
|
+
*/
|
|
744
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The callback URL to redirect to after the user is deleted" }),
|
|
745
|
+
/**
|
|
746
|
+
* The password of the user. If the password isn't provided, session freshness
|
|
747
|
+
* will be checked.
|
|
748
|
+
*/
|
|
749
|
+
password: _faire_auth_core_factory.passwordSchema.optional().openapi({ description: "The password of the user. If the password isn't provided, session freshness will be checked." }),
|
|
750
|
+
/**
|
|
751
|
+
* The token to delete the user. If the token is provided, the user will be deleted
|
|
752
|
+
*/
|
|
647
753
|
token: zod.string().optional().openapi({ description: "The token to delete the user. If the token is provided, the user will be deleted" })
|
|
648
754
|
});
|
|
649
755
|
const deleteUserCallbackQuerySchema = zod.object({
|
|
@@ -651,30 +757,28 @@ const deleteUserCallbackQuerySchema = zod.object({
|
|
|
651
757
|
in: "query",
|
|
652
758
|
name: "token"
|
|
653
759
|
} }),
|
|
654
|
-
callbackURL: (0,
|
|
760
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ param: {
|
|
655
761
|
in: "query",
|
|
656
762
|
name: "callbackURL"
|
|
657
763
|
} })
|
|
658
764
|
});
|
|
659
765
|
const changeEmailSchema = zod.object({
|
|
660
|
-
newEmail:
|
|
661
|
-
callbackURL: (0,
|
|
766
|
+
newEmail: _faire_auth_core_factory.emailSchema.openapi({ description: "The new email to set" }),
|
|
767
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The URL to redirect to after email verification" })
|
|
662
768
|
});
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
const updateUserRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
667
|
-
operationId: __faire_auth_core_static.Routes.UPDATE_USER,
|
|
769
|
+
// src/api/routes/update-user.ts
|
|
770
|
+
const updateUserRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
771
|
+
operationId: _faire_auth_core_static.Routes.UPDATE_USER,
|
|
668
772
|
method: "post",
|
|
669
773
|
path: "/update-user",
|
|
670
774
|
description: "Update the current user",
|
|
671
775
|
middleware: [require_session.sessionMiddleware()],
|
|
672
|
-
request: (0,
|
|
673
|
-
responses: (0,
|
|
776
|
+
request: (0, _faire_auth_core_factory.req)().bdy(updateUserSchema).bld(),
|
|
777
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).zod().err(500, "Failed to update user").bld()
|
|
674
778
|
});
|
|
675
779
|
const updateUser = require_session.createEndpoint(updateUserRoute, (options) => async (ctx) => {
|
|
676
|
-
const { name, image
|
|
677
|
-
if (image === void 0 && name === void 0 && Object.keys(rest).length === 0) return ctx.render({ success:
|
|
780
|
+
const { name, image, ...rest } = ctx.req.valid("json");
|
|
781
|
+
if (image === void 0 && name === void 0 && Object.keys(rest).length === 0) return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
678
782
|
const context = ctx.get("context");
|
|
679
783
|
const session = ctx.get("session");
|
|
680
784
|
const additionalFields = await require_schema.parseUserInput(options, rest, "update");
|
|
@@ -685,8 +789,8 @@ const updateUser = require_session.createEndpoint(updateUserRoute, (options) =>
|
|
|
685
789
|
...additionalFields
|
|
686
790
|
});
|
|
687
791
|
if (user == null) return ctx.render({
|
|
688
|
-
success:
|
|
689
|
-
message:
|
|
792
|
+
success: _faire_auth_core_static.False,
|
|
793
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_UPDATE_USER
|
|
690
794
|
}, 500);
|
|
691
795
|
/**
|
|
692
796
|
* Update the session cookie with the new user data
|
|
@@ -695,58 +799,58 @@ const updateUser = require_session.createEndpoint(updateUserRoute, (options) =>
|
|
|
695
799
|
session: session.session,
|
|
696
800
|
user
|
|
697
801
|
});
|
|
698
|
-
return ctx.render({ success:
|
|
802
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
699
803
|
});
|
|
700
|
-
const changePasswordRoute = (0,
|
|
701
|
-
operationId:
|
|
804
|
+
const changePasswordRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
805
|
+
operationId: _faire_auth_core_static.Routes.CHANGE_PASSWORD,
|
|
702
806
|
method: "post",
|
|
703
807
|
path: "/change-password",
|
|
704
808
|
description: "Change the password of the user",
|
|
705
809
|
middleware: [require_session.sensitiveSessionMiddleware],
|
|
706
|
-
request: (0,
|
|
707
|
-
responses: (0,
|
|
810
|
+
request: (0, _faire_auth_core_factory.req)().bdy(changePasswordSchema).bld(),
|
|
811
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SIGN_IN_UP].default).err(400, "Invalid password").err(404, "Account not found").zod().err(500, "Failed to get session").bld()
|
|
708
812
|
});
|
|
709
813
|
const changePassword = require_session.createEndpoint(changePasswordRoute, (options) => async (ctx) => {
|
|
710
814
|
const context = ctx.get("context");
|
|
711
815
|
const session = ctx.get("session");
|
|
712
|
-
const { newPassword, currentPassword, revokeOtherSessions
|
|
816
|
+
const { newPassword, currentPassword, revokeOtherSessions } = ctx.req.valid("json");
|
|
713
817
|
const { minPasswordLength } = context.password.config;
|
|
714
818
|
if (newPassword.length < minPasswordLength) {
|
|
715
819
|
context.logger.error("Password is too short");
|
|
716
820
|
return ctx.render({
|
|
717
|
-
success:
|
|
718
|
-
message:
|
|
821
|
+
success: _faire_auth_core_static.False,
|
|
822
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_SHORT
|
|
719
823
|
}, 400);
|
|
720
824
|
}
|
|
721
825
|
const { maxPasswordLength } = context.password.config;
|
|
722
826
|
if (newPassword.length > maxPasswordLength) {
|
|
723
827
|
context.logger.error("Password is too long");
|
|
724
828
|
return ctx.render({
|
|
725
|
-
success:
|
|
726
|
-
message:
|
|
829
|
+
success: _faire_auth_core_static.False,
|
|
830
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_LONG
|
|
727
831
|
}, 400);
|
|
728
832
|
}
|
|
729
|
-
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account
|
|
833
|
+
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account) => account.providerId === "credential" && account.password != null);
|
|
730
834
|
if (account?.password == null) return ctx.render({
|
|
731
|
-
success:
|
|
732
|
-
message:
|
|
835
|
+
success: _faire_auth_core_static.False,
|
|
836
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.CREDENTIAL_ACCOUNT_NOT_FOUND
|
|
733
837
|
}, 404);
|
|
734
838
|
const passwordHash = await context.password.hash(newPassword);
|
|
735
839
|
if (!await context.password.verify({
|
|
736
840
|
hash: account.password,
|
|
737
841
|
password: currentPassword
|
|
738
842
|
})) return ctx.render({
|
|
739
|
-
success:
|
|
740
|
-
message:
|
|
843
|
+
success: _faire_auth_core_static.False,
|
|
844
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_PASSWORD
|
|
741
845
|
}, 400);
|
|
742
846
|
await context.internalAdapter.updateAccount(account.id, { password: passwordHash });
|
|
743
847
|
let token = session.session.token;
|
|
744
|
-
if (revokeOtherSessions
|
|
848
|
+
if (revokeOtherSessions === true) {
|
|
745
849
|
await context.internalAdapter.deleteSessions(session.user.id);
|
|
746
850
|
const newSession = await context.internalAdapter.createSession(session.user.id);
|
|
747
851
|
if (newSession == null) return ctx.render({
|
|
748
|
-
success:
|
|
749
|
-
message:
|
|
852
|
+
success: _faire_auth_core_static.False,
|
|
853
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_GET_SESSION
|
|
750
854
|
}, 500);
|
|
751
855
|
await require_cookies.setSessionCookie(ctx, options, {
|
|
752
856
|
session: newSession,
|
|
@@ -755,21 +859,21 @@ const changePassword = require_session.createEndpoint(changePasswordRoute, (opti
|
|
|
755
859
|
token = newSession.token;
|
|
756
860
|
}
|
|
757
861
|
return ctx.render({
|
|
758
|
-
redirect:
|
|
759
|
-
success:
|
|
862
|
+
redirect: _faire_auth_core_static.False,
|
|
863
|
+
success: _faire_auth_core_static.True,
|
|
760
864
|
token,
|
|
761
865
|
user: session.user
|
|
762
866
|
}, 200);
|
|
763
867
|
});
|
|
764
|
-
const setPasswordRoute = (0,
|
|
765
|
-
operationId:
|
|
868
|
+
const setPasswordRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
869
|
+
operationId: _faire_auth_core_static.Routes.SET_PASSWORD,
|
|
766
870
|
hide: true,
|
|
767
871
|
SERVER_ONLY: true,
|
|
768
872
|
method: "post",
|
|
769
873
|
path: "/set-password",
|
|
770
874
|
middleware: [require_session.sensitiveSessionMiddleware],
|
|
771
|
-
request: (0,
|
|
772
|
-
responses: (0,
|
|
875
|
+
request: (0, _faire_auth_core_factory.req)().bdy(setPasswordSchema).bld(),
|
|
876
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).err(400, "Invalid password or user already has password").zod().bld()
|
|
773
877
|
});
|
|
774
878
|
const setPassword = require_session.createEndpoint(setPasswordRoute, (_options) => async (ctx) => {
|
|
775
879
|
const context = ctx.get("context");
|
|
@@ -779,19 +883,19 @@ const setPassword = require_session.createEndpoint(setPasswordRoute, (_options)
|
|
|
779
883
|
if (newPassword.length < minPasswordLength) {
|
|
780
884
|
context.logger.error("Password is too short");
|
|
781
885
|
return ctx.render({
|
|
782
|
-
success:
|
|
783
|
-
message:
|
|
886
|
+
success: _faire_auth_core_static.False,
|
|
887
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_SHORT
|
|
784
888
|
}, 400);
|
|
785
889
|
}
|
|
786
890
|
const { maxPasswordLength } = context.password.config;
|
|
787
891
|
if (newPassword.length > maxPasswordLength) {
|
|
788
892
|
context.logger.error("Password is too long");
|
|
789
893
|
return ctx.render({
|
|
790
|
-
success:
|
|
791
|
-
message:
|
|
894
|
+
success: _faire_auth_core_static.False,
|
|
895
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_LONG
|
|
792
896
|
}, 400);
|
|
793
897
|
}
|
|
794
|
-
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account
|
|
898
|
+
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account) => account.providerId === "credential" && account.password != null);
|
|
795
899
|
const passwordHash = await context.password.hash(newPassword);
|
|
796
900
|
if (!account) {
|
|
797
901
|
await context.internalAdapter.linkAccount({
|
|
@@ -800,28 +904,28 @@ const setPassword = require_session.createEndpoint(setPasswordRoute, (_options)
|
|
|
800
904
|
accountId: session.user.id,
|
|
801
905
|
password: passwordHash
|
|
802
906
|
});
|
|
803
|
-
return ctx.render({ success:
|
|
907
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
804
908
|
}
|
|
805
909
|
return ctx.render({
|
|
806
|
-
success:
|
|
910
|
+
success: _faire_auth_core_static.False,
|
|
807
911
|
message: "User already has a password"
|
|
808
912
|
}, 400);
|
|
809
913
|
});
|
|
810
|
-
const deleteUserRoute = (0,
|
|
811
|
-
operationId:
|
|
914
|
+
const deleteUserRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
915
|
+
operationId: _faire_auth_core_static.Routes.DELETE_USER,
|
|
812
916
|
method: "post",
|
|
813
917
|
path: "/delete-user",
|
|
814
918
|
description: "Delete the user",
|
|
815
919
|
middleware: [require_session.sensitiveSessionMiddleware],
|
|
816
|
-
request: (0,
|
|
817
|
-
responses: (0,
|
|
920
|
+
request: (0, _faire_auth_core_factory.req)().bdy(deleteUserSchema).bld(),
|
|
921
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default, "User deletion processed successfully").rdr("Redirect to callback URL").err(400, "Account not found or invalid password").err(404, "Delete user is disabled").zod().bld()
|
|
818
922
|
});
|
|
819
923
|
const deleteUser = require_session.createEndpoint(deleteUserRoute, (options) => async (ctx) => {
|
|
820
924
|
const context = ctx.get("context");
|
|
821
925
|
if (options.user?.deleteUser?.enabled !== true) {
|
|
822
926
|
context.logger.error("Delete user is disabled. Enable it in the options");
|
|
823
927
|
return ctx.render({
|
|
824
|
-
success:
|
|
928
|
+
success: _faire_auth_core_static.False,
|
|
825
929
|
message: "Delete user is disabled"
|
|
826
930
|
}, 404);
|
|
827
931
|
}
|
|
@@ -829,41 +933,41 @@ const deleteUser = require_session.createEndpoint(deleteUserRoute, (options) =>
|
|
|
829
933
|
const api = ctx.get("api");
|
|
830
934
|
const { callbackURL, password, token } = ctx.req.valid("json");
|
|
831
935
|
let canDelete = false;
|
|
832
|
-
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account
|
|
936
|
+
const account = (await context.internalAdapter.findAccounts(session.user.id)).find((account) => account.providerId === "credential" && account.password != null);
|
|
833
937
|
if (password != null) {
|
|
834
938
|
if (account?.password == null) return ctx.render({
|
|
835
|
-
success:
|
|
836
|
-
message:
|
|
939
|
+
success: _faire_auth_core_static.False,
|
|
940
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.CREDENTIAL_ACCOUNT_NOT_FOUND
|
|
837
941
|
}, 400);
|
|
838
942
|
if (!await context.password.verify({
|
|
839
943
|
hash: account.password,
|
|
840
944
|
password
|
|
841
945
|
})) return ctx.render({
|
|
842
|
-
success:
|
|
843
|
-
message:
|
|
946
|
+
success: _faire_auth_core_static.False,
|
|
947
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_PASSWORD
|
|
844
948
|
}, 400);
|
|
845
949
|
canDelete = true;
|
|
846
950
|
}
|
|
847
|
-
if (token != null) return await api.deleteUserCallback({ query: { token } }, ctx).then((res
|
|
951
|
+
if (token != null) return await api.deleteUserCallback({ query: { token } }, ctx).then((res) => res instanceof Response ? res : ctx.render(res, res.success === true ? 200 : "errors" in res ? 422 : 404));
|
|
848
952
|
if (options.user?.deleteUser?.sendDeleteAccountVerification != null) {
|
|
849
953
|
if (account?.password != null && !canDelete) return ctx.render({
|
|
850
|
-
success:
|
|
851
|
-
message:
|
|
954
|
+
success: _faire_auth_core_static.False,
|
|
955
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.USER_ALREADY_HAS_PASSWORD
|
|
852
956
|
}, 400);
|
|
853
|
-
const token
|
|
957
|
+
const token = (0, require_crypto.crypto_exports.generateRandomString)(32, "0-9", "a-z");
|
|
854
958
|
await context.internalAdapter.createVerificationValue({
|
|
855
959
|
value: session.user.id,
|
|
856
|
-
identifier: `delete-account-${token
|
|
857
|
-
expiresAt: new Date(Date.now() + (options.user.deleteUser.deleteTokenExpiresIn ??
|
|
960
|
+
identifier: `delete-account-${token}`,
|
|
961
|
+
expiresAt: new Date(Date.now() + (options.user.deleteUser.deleteTokenExpiresIn ?? 86400) * 1e3)
|
|
858
962
|
});
|
|
859
|
-
const url = `${context.baseURL}/delete-user/callback?token=${token
|
|
963
|
+
const url = `${context.baseURL}/delete-user/callback?token=${token}${callbackURL ? `&callbackURL=${callbackURL}` : ""}`;
|
|
860
964
|
await options.user.deleteUser.sendDeleteAccountVerification({
|
|
861
965
|
user: session.user,
|
|
862
966
|
url,
|
|
863
|
-
token
|
|
967
|
+
token
|
|
864
968
|
}, ctx);
|
|
865
969
|
return ctx.render({
|
|
866
|
-
success:
|
|
970
|
+
success: _faire_auth_core_static.True,
|
|
867
971
|
message: "Verification email sent"
|
|
868
972
|
}, 200);
|
|
869
973
|
}
|
|
@@ -871,13 +975,13 @@ const deleteUser = require_session.createEndpoint(deleteUserRoute, (options) =>
|
|
|
871
975
|
const currentAge = session.session.createdAt.getTime();
|
|
872
976
|
const freshAge = context.sessionConfig.freshAge * 1e3;
|
|
873
977
|
if (Date.now() - currentAge > freshAge * 1e3) return ctx.render({
|
|
874
|
-
success:
|
|
875
|
-
message:
|
|
978
|
+
success: _faire_auth_core_static.False,
|
|
979
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.SESSION_EXPIRED
|
|
876
980
|
}, 400);
|
|
877
981
|
canDelete = true;
|
|
878
982
|
}
|
|
879
983
|
if (!canDelete) return ctx.render({
|
|
880
|
-
success:
|
|
984
|
+
success: _faire_auth_core_static.False,
|
|
881
985
|
message: "User cannot be deleted. please provide a password or token"
|
|
882
986
|
}, 400);
|
|
883
987
|
if (options.user?.deleteUser?.beforeDelete) await options.user.deleteUser.beforeDelete(session.user, ctx.req);
|
|
@@ -887,25 +991,25 @@ const deleteUser = require_session.createEndpoint(deleteUserRoute, (options) =>
|
|
|
887
991
|
require_cookies.deleteSessionCookie(ctx);
|
|
888
992
|
if (options.user?.deleteUser?.afterDelete) await options.user.deleteUser.afterDelete(session.user, ctx.req);
|
|
889
993
|
return ctx.render({
|
|
890
|
-
success:
|
|
994
|
+
success: _faire_auth_core_static.True,
|
|
891
995
|
message: "User deleted"
|
|
892
996
|
}, 200);
|
|
893
997
|
});
|
|
894
|
-
const deleteUserCallbackRoute = (0,
|
|
895
|
-
operationId:
|
|
998
|
+
const deleteUserCallbackRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
999
|
+
operationId: _faire_auth_core_static.Routes.DELETE_USER_CALLBACK,
|
|
896
1000
|
method: "get",
|
|
897
1001
|
path: "/delete-user/callback",
|
|
898
1002
|
description: "Callback to complete user deletion with verification token",
|
|
899
1003
|
middleware: [require_origin_check.originCheck((ctx) => ctx.req.query("callbackURL")), require_session.sessionMiddleware()],
|
|
900
|
-
request: (0,
|
|
901
|
-
responses: (0,
|
|
1004
|
+
request: (0, _faire_auth_core_factory.req)().qry(deleteUserCallbackQuerySchema).bld(),
|
|
1005
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default, "User successfully deleted").rdr("Redirect to callback URL").err(404, "Invalid token or delete user is disabled").zod().bld()
|
|
902
1006
|
});
|
|
903
1007
|
const deleteUserCallback = require_session.createEndpoint(deleteUserCallbackRoute, (options) => async (ctx) => {
|
|
904
1008
|
const context = ctx.get("context");
|
|
905
1009
|
if (!(options.user?.deleteUser?.enabled ?? false)) {
|
|
906
1010
|
context.logger.error("Delete user is disabled. Enable it in the options");
|
|
907
1011
|
return ctx.render({
|
|
908
|
-
success:
|
|
1012
|
+
success: _faire_auth_core_static.False,
|
|
909
1013
|
message: "Delete user is disabled"
|
|
910
1014
|
}, 404);
|
|
911
1015
|
}
|
|
@@ -915,13 +1019,13 @@ const deleteUserCallback = require_session.createEndpoint(deleteUserCallbackRout
|
|
|
915
1019
|
if (!token || token.expiresAt < /* @__PURE__ */ new Date()) {
|
|
916
1020
|
context.logger.info(requestedToken);
|
|
917
1021
|
return ctx.render({
|
|
918
|
-
success:
|
|
919
|
-
message:
|
|
1022
|
+
success: _faire_auth_core_static.False,
|
|
1023
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
920
1024
|
}, 404);
|
|
921
1025
|
}
|
|
922
1026
|
if (token.value !== session.user.id) return ctx.render({
|
|
923
|
-
success:
|
|
924
|
-
message:
|
|
1027
|
+
success: _faire_auth_core_static.False,
|
|
1028
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
925
1029
|
}, 404);
|
|
926
1030
|
const { beforeDelete } = options.user?.deleteUser ?? {};
|
|
927
1031
|
if (beforeDelete) await beforeDelete(session.user, ctx.req);
|
|
@@ -934,24 +1038,24 @@ const deleteUserCallback = require_session.createEndpoint(deleteUserCallbackRout
|
|
|
934
1038
|
if (afterDelete) await afterDelete(session.user, ctx.req);
|
|
935
1039
|
if (callbackURL != null) return ctx.redirect(callbackURL);
|
|
936
1040
|
return ctx.render({
|
|
937
|
-
success:
|
|
1041
|
+
success: _faire_auth_core_static.True,
|
|
938
1042
|
message: "User deleted"
|
|
939
1043
|
}, 200);
|
|
940
1044
|
});
|
|
941
|
-
const changeEmailRoute = (0,
|
|
942
|
-
operationId:
|
|
1045
|
+
const changeEmailRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1046
|
+
operationId: _faire_auth_core_static.Routes.CHANGE_EMAIL,
|
|
943
1047
|
method: "post",
|
|
944
1048
|
path: "/change-email",
|
|
945
1049
|
middleware: [require_session.sensitiveSessionMiddleware],
|
|
946
|
-
request: (0,
|
|
947
|
-
responses: (0,
|
|
1050
|
+
request: (0, _faire_auth_core_factory.req)().bdy(changeEmailSchema).bld(),
|
|
1051
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default, "Email change request processed successfully").err(400, "Change email is disabled or user already exists").zod().bld()
|
|
948
1052
|
});
|
|
949
1053
|
const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) => async (ctx) => {
|
|
950
1054
|
const context = ctx.get("context");
|
|
951
1055
|
if (options.user?.changeEmail?.enabled !== true) {
|
|
952
1056
|
context.logger.error("Change email is disabled.");
|
|
953
1057
|
return ctx.render({
|
|
954
|
-
success:
|
|
1058
|
+
success: _faire_auth_core_static.False,
|
|
955
1059
|
message: "Change email is disabled"
|
|
956
1060
|
}, 400);
|
|
957
1061
|
}
|
|
@@ -961,14 +1065,14 @@ const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) =
|
|
|
961
1065
|
if (newEmail === session.user.email) {
|
|
962
1066
|
context.logger.error("Email is the same");
|
|
963
1067
|
return ctx.render({
|
|
964
|
-
success:
|
|
1068
|
+
success: _faire_auth_core_static.False,
|
|
965
1069
|
message: "Email is the same"
|
|
966
1070
|
}, 400);
|
|
967
1071
|
}
|
|
968
1072
|
if (await context.internalAdapter.findUserByEmail(newEmail)) {
|
|
969
1073
|
context.logger.error("Email already exists");
|
|
970
1074
|
return ctx.render({
|
|
971
|
-
success:
|
|
1075
|
+
success: _faire_auth_core_static.False,
|
|
972
1076
|
message: "Couldn't update your email"
|
|
973
1077
|
}, 400);
|
|
974
1078
|
}
|
|
@@ -977,8 +1081,8 @@ const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) =
|
|
|
977
1081
|
*/
|
|
978
1082
|
if (!session.user.emailVerified) {
|
|
979
1083
|
if (await context.internalAdapter.findUserByEmail(newEmail)) return ctx.render({
|
|
980
|
-
success:
|
|
981
|
-
message:
|
|
1084
|
+
success: _faire_auth_core_static.False,
|
|
1085
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.USER_ALREADY_EXISTS
|
|
982
1086
|
}, 400);
|
|
983
1087
|
await context.internalAdapter.updateUserByEmail(session.user.email, {
|
|
984
1088
|
email: newEmail,
|
|
@@ -992,15 +1096,15 @@ const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) =
|
|
|
992
1096
|
}
|
|
993
1097
|
});
|
|
994
1098
|
if (options.emailVerification?.sendVerificationEmail) {
|
|
995
|
-
const token
|
|
996
|
-
const url
|
|
1099
|
+
const token = await require_email_verification.createEmailVerificationToken(context.secret, newEmail, void 0, options.emailVerification.expiresIn);
|
|
1100
|
+
const url = `${context.baseURL}/verify-email?token=${token}&callbackURL=${callbackURL ?? "/"}`;
|
|
997
1101
|
await options.emailVerification.sendVerificationEmail({
|
|
998
1102
|
user: session.user,
|
|
999
|
-
url
|
|
1000
|
-
token
|
|
1103
|
+
url,
|
|
1104
|
+
token
|
|
1001
1105
|
}, ctx);
|
|
1002
1106
|
}
|
|
1003
|
-
return ctx.render({ success:
|
|
1107
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
1004
1108
|
}
|
|
1005
1109
|
/**
|
|
1006
1110
|
* If the email is verified, we need to send a verification email
|
|
@@ -1008,7 +1112,7 @@ const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) =
|
|
|
1008
1112
|
if (options.user?.changeEmail?.sendChangeEmailVerification == null) {
|
|
1009
1113
|
context.logger.error("Verification email isn't enabled.");
|
|
1010
1114
|
return ctx.render({
|
|
1011
|
-
success:
|
|
1115
|
+
success: _faire_auth_core_static.False,
|
|
1012
1116
|
message: "Verification email isn't enabled"
|
|
1013
1117
|
}, 400);
|
|
1014
1118
|
}
|
|
@@ -1020,11 +1124,9 @@ const changeEmail = require_session.createEndpoint(changeEmailRoute, (options) =
|
|
|
1020
1124
|
url,
|
|
1021
1125
|
token
|
|
1022
1126
|
}, ctx);
|
|
1023
|
-
return ctx.render({ success:
|
|
1127
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
1024
1128
|
});
|
|
1025
|
-
|
|
1026
|
-
//#endregion
|
|
1027
|
-
//#region src/api/routes/error.ts
|
|
1129
|
+
// src/api/routes/error.ts
|
|
1028
1130
|
const sanitize = (input) => input.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">").replaceAll("\"", """).replaceAll("'", "'");
|
|
1029
1131
|
const html = (errorCode = "Unknown", errorDescription) => `<!DOCTYPE html>
|
|
1030
1132
|
<html lang="en">
|
|
@@ -1117,83 +1219,97 @@ const errorQuerySchema = zod.object({
|
|
|
1117
1219
|
error: zod.string().optional(),
|
|
1118
1220
|
error_description: zod.string().optional()
|
|
1119
1221
|
});
|
|
1120
|
-
const errorRoute = (0,
|
|
1121
|
-
operationId:
|
|
1222
|
+
const errorRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1223
|
+
operationId: _faire_auth_core_static.Routes.ERROR,
|
|
1122
1224
|
hide: true,
|
|
1123
1225
|
isAction: false,
|
|
1124
1226
|
method: "get",
|
|
1125
1227
|
path: "/error",
|
|
1126
1228
|
description: "Displays an error page",
|
|
1127
|
-
request: (0,
|
|
1128
|
-
responses: (0,
|
|
1229
|
+
request: (0, _faire_auth_core_factory.req)().qry(errorQuerySchema).bld(),
|
|
1230
|
+
responses: (0, _faire_auth_core_factory.res)(zod.string().openapi({ description: "The HTML content of the error page" }), "A HTML error page", "text/html").zod().bld()
|
|
1129
1231
|
});
|
|
1130
1232
|
const error = require_session.createEndpoint(errorRoute, (_options) => async (ctx) => {
|
|
1131
|
-
const { error
|
|
1132
|
-
return ctx.html(html(error
|
|
1233
|
+
const { error, error_description: _error_description } = ctx.req.valid("query");
|
|
1234
|
+
return ctx.html(html(error), 200);
|
|
1133
1235
|
});
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
const okRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
1138
|
-
operationId: __faire_auth_core_static.Routes.OK,
|
|
1236
|
+
// src/api/routes/ok.ts
|
|
1237
|
+
const okRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1238
|
+
operationId: _faire_auth_core_static.Routes.OK,
|
|
1139
1239
|
hide: true,
|
|
1140
1240
|
isAction: false,
|
|
1141
1241
|
method: "get",
|
|
1142
1242
|
path: "/ok",
|
|
1143
1243
|
description: "Check if the API is working",
|
|
1144
|
-
responses: (0,
|
|
1244
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default, "API is working").bld()
|
|
1145
1245
|
});
|
|
1146
|
-
const ok = require_session.createEndpoint(okRoute, (_options) => async (ctx) => ctx.render({ success:
|
|
1147
|
-
|
|
1148
|
-
//#endregion
|
|
1149
|
-
//#region src/api/schema/sign-up.ts
|
|
1246
|
+
const ok = require_session.createEndpoint(okRoute, (_options) => async (ctx) => ctx.render({ success: _faire_auth_core_static.True }, 200));
|
|
1247
|
+
// src/api/schema/sign-up.ts
|
|
1150
1248
|
const signUpEmailSchema = zod.looseObject({
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1249
|
+
/**
|
|
1250
|
+
* Name of the user
|
|
1251
|
+
*/
|
|
1252
|
+
name: _faire_auth_core_factory.nameSchema,
|
|
1253
|
+
/**
|
|
1254
|
+
* Email of the user
|
|
1255
|
+
*/
|
|
1256
|
+
email: _faire_auth_core_factory.emailSchema.openapi({ description: "The email of the user" }),
|
|
1257
|
+
/**
|
|
1258
|
+
* Password of the user
|
|
1259
|
+
*/
|
|
1260
|
+
password: _faire_auth_core_factory.passwordSchema.openapi({ description: "The password of the user" }),
|
|
1261
|
+
/**
|
|
1262
|
+
* Image url for the user
|
|
1263
|
+
*/
|
|
1154
1264
|
image: zod.url().nullish().openapi({ description: "The profile image URL of the user" }),
|
|
1155
|
-
|
|
1265
|
+
/**
|
|
1266
|
+
* Callback URL to use as a redirect for email
|
|
1267
|
+
* verification and for possible redirects
|
|
1268
|
+
*/
|
|
1269
|
+
callbackURL: (0, _faire_auth_core_factory.callbackURLSchema)(true).openapi({ description: "The URL to use for email verification callback" }),
|
|
1270
|
+
/**
|
|
1271
|
+
* If this is false, the session will not be remembered
|
|
1272
|
+
* @default true
|
|
1273
|
+
*/
|
|
1156
1274
|
rememberMe: zod.boolean().default(true).openapi({ description: "If this is false, the session will not be remembered. Default is `true`." })
|
|
1157
1275
|
});
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
const signUpEmailRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
1162
|
-
operationId: __faire_auth_core_static.Routes.SIGN_UP_EMAIL,
|
|
1276
|
+
// src/api/routes/sign-up.ts
|
|
1277
|
+
const signUpEmailRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1278
|
+
operationId: _faire_auth_core_static.Routes.SIGN_UP_EMAIL,
|
|
1163
1279
|
method: "post",
|
|
1164
1280
|
path: "/sign-up/email",
|
|
1165
1281
|
description: "Sign up a user using email and password",
|
|
1166
|
-
request: (0,
|
|
1167
|
-
responses: (0,
|
|
1282
|
+
request: (0, _faire_auth_core_factory.req)().bdy(signUpEmailSchema).bld(),
|
|
1283
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SIGN_IN_UP].default, "Successfully created user").err(400, "Email and password sign up is not enabled, invalid password, or user already exists").zod().bld()
|
|
1168
1284
|
});
|
|
1169
1285
|
const signUpEmail = require_session.createEndpoint(signUpEmailRoute, (options) => async (ctx) => {
|
|
1170
1286
|
if (!(options.emailAndPassword?.enabled ?? false) || options.emailAndPassword?.disableSignUp === true) return ctx.render({
|
|
1171
|
-
success:
|
|
1287
|
+
success: _faire_auth_core_static.False,
|
|
1172
1288
|
message: "Email and password sign up is not enabled"
|
|
1173
1289
|
}, 400);
|
|
1174
|
-
const { name, email, password, image, callbackURL, rememberMe
|
|
1290
|
+
const { name, email, password, image, callbackURL, rememberMe, ...additionalFields } = ctx.req.valid("json");
|
|
1175
1291
|
const context = ctx.get("context");
|
|
1176
1292
|
const { minPasswordLength, maxPasswordLength } = context.password.config;
|
|
1177
1293
|
if (password.length < minPasswordLength) {
|
|
1178
1294
|
context.logger.error("Password is too short");
|
|
1179
1295
|
return ctx.render({
|
|
1180
|
-
success:
|
|
1181
|
-
message:
|
|
1296
|
+
success: _faire_auth_core_static.False,
|
|
1297
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_SHORT
|
|
1182
1298
|
}, 400);
|
|
1183
1299
|
}
|
|
1184
1300
|
if (password.length > maxPasswordLength) {
|
|
1185
1301
|
context.logger.error("Password is too long");
|
|
1186
1302
|
return ctx.render({
|
|
1187
|
-
success:
|
|
1188
|
-
message:
|
|
1303
|
+
success: _faire_auth_core_static.False,
|
|
1304
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PASSWORD_TOO_LONG
|
|
1189
1305
|
}, 400);
|
|
1190
1306
|
}
|
|
1191
1307
|
return require_ip.runWithTransaction(context.adapter, async () => {
|
|
1192
1308
|
if ((await context.internalAdapter.findUserByEmail(email))?.user) {
|
|
1193
1309
|
context.logger.info(`Sign-up attempt for existing email: ${email}`);
|
|
1194
1310
|
return ctx.render({
|
|
1195
|
-
success:
|
|
1196
|
-
message:
|
|
1311
|
+
success: _faire_auth_core_static.False,
|
|
1312
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.USER_ALREADY_EXISTS
|
|
1197
1313
|
}, 400);
|
|
1198
1314
|
}
|
|
1199
1315
|
const additionalData = await require_schema.parseUserInput(options, additionalFields, "create");
|
|
@@ -1217,15 +1333,15 @@ const signUpEmail = require_session.createEndpoint(signUpEmailRoute, (options) =
|
|
|
1217
1333
|
emailVerified: false
|
|
1218
1334
|
});
|
|
1219
1335
|
} catch (e) {
|
|
1220
|
-
if ((0,
|
|
1336
|
+
if ((0, _faire_auth_core_env.isDevelopment)()) context.logger.error("Failed to create user", e);
|
|
1221
1337
|
return ctx.render({
|
|
1222
|
-
success:
|
|
1223
|
-
message:
|
|
1338
|
+
success: _faire_auth_core_static.False,
|
|
1339
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_CREATE_USER
|
|
1224
1340
|
}, 400);
|
|
1225
1341
|
}
|
|
1226
1342
|
if (createdUser == null) return ctx.render({
|
|
1227
|
-
success:
|
|
1228
|
-
message:
|
|
1343
|
+
success: _faire_auth_core_static.False,
|
|
1344
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_CREATE_USER
|
|
1229
1345
|
}, 400);
|
|
1230
1346
|
await context.internalAdapter.linkAccount({
|
|
1231
1347
|
userId: createdUser.id,
|
|
@@ -1243,37 +1359,35 @@ const signUpEmail = require_session.createEndpoint(signUpEmailRoute, (options) =
|
|
|
1243
1359
|
}, ctx);
|
|
1244
1360
|
}
|
|
1245
1361
|
if (options.emailAndPassword?.autoSignIn === false || options.emailAndPassword?.requireEmailVerification === true) return ctx.render({
|
|
1246
|
-
success:
|
|
1247
|
-
redirect:
|
|
1362
|
+
success: _faire_auth_core_static.True,
|
|
1363
|
+
redirect: _faire_auth_core_static.False,
|
|
1248
1364
|
user: createdUser
|
|
1249
1365
|
}, 200);
|
|
1250
1366
|
const session = await context.internalAdapter.createSession(createdUser.id, rememberMe === false);
|
|
1251
1367
|
if (session == null) return ctx.render({
|
|
1252
|
-
success:
|
|
1253
|
-
message:
|
|
1368
|
+
success: _faire_auth_core_static.False,
|
|
1369
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_CREATE_SESSION
|
|
1254
1370
|
}, 400);
|
|
1255
1371
|
await require_cookies.setSessionCookie(ctx, options, {
|
|
1256
1372
|
session,
|
|
1257
1373
|
user: createdUser
|
|
1258
1374
|
}, rememberMe === false);
|
|
1259
1375
|
return ctx.render({
|
|
1260
|
-
success:
|
|
1261
|
-
redirect:
|
|
1376
|
+
success: _faire_auth_core_static.True,
|
|
1377
|
+
redirect: _faire_auth_core_static.False,
|
|
1262
1378
|
token: session.token,
|
|
1263
1379
|
user: createdUser
|
|
1264
1380
|
}, 200);
|
|
1265
1381
|
});
|
|
1266
1382
|
});
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
const listAccountsRoute = (0, __faire_auth_core_factory.createRoute)({
|
|
1271
|
-
operationId: __faire_auth_core_static.Routes.LIST_ACCOUNTS,
|
|
1383
|
+
// src/api/routes/account.ts
|
|
1384
|
+
const listAccountsRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1385
|
+
operationId: _faire_auth_core_static.Routes.LIST_ACCOUNTS,
|
|
1272
1386
|
method: "get",
|
|
1273
1387
|
path: "/list-accounts",
|
|
1274
1388
|
description: "List all accounts linked to the user",
|
|
1275
1389
|
middleware: [require_session.sessionMiddleware()],
|
|
1276
|
-
responses: (0,
|
|
1390
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.ACCOUNTS_LIST].default).bld()
|
|
1277
1391
|
});
|
|
1278
1392
|
const listAccounts = require_session.createEndpoint(listAccountsRoute, (_options) => async (ctx) => {
|
|
1279
1393
|
const context = ctx.get("context");
|
|
@@ -1288,14 +1402,14 @@ const listAccounts = require_session.createEndpoint(listAccountsRoute, (_options
|
|
|
1288
1402
|
scopes: a.scope ? a.scope.split(",") : []
|
|
1289
1403
|
})), 200);
|
|
1290
1404
|
});
|
|
1291
|
-
const linkAccountRoute = (0,
|
|
1292
|
-
operationId:
|
|
1405
|
+
const linkAccountRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1406
|
+
operationId: _faire_auth_core_static.Routes.LINK_ACCOUNT,
|
|
1293
1407
|
method: "post",
|
|
1294
1408
|
path: "/link-social",
|
|
1295
1409
|
description: "Link a social account to the user",
|
|
1296
1410
|
middleware: [require_session.sessionMiddleware()],
|
|
1297
|
-
request: (0,
|
|
1298
|
-
responses: (0,
|
|
1411
|
+
request: (0, _faire_auth_core_factory.req)().bdy(linkAccountSchema).bld(),
|
|
1412
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.REDIRECT_URL].default).err(401, "Invalid token, failed to get user info, email not found, linking not allowed, or different emails not allowed").err(404, "Provider not found").err(417, "Unable to create account").zod().bld()
|
|
1299
1413
|
});
|
|
1300
1414
|
const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) => async (ctx) => {
|
|
1301
1415
|
const { scopes, provider: requestedProvider, idToken } = ctx.req.valid("json");
|
|
@@ -1305,24 +1419,24 @@ const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) =
|
|
|
1305
1419
|
if (!provider) {
|
|
1306
1420
|
context.logger.error("Provider not found. Make sure to add the provider in your auth config", { provider: requestedProvider });
|
|
1307
1421
|
return ctx.render({
|
|
1308
|
-
success:
|
|
1309
|
-
message:
|
|
1422
|
+
success: _faire_auth_core_static.False,
|
|
1423
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.PROVIDER_NOT_FOUND
|
|
1310
1424
|
}, 404);
|
|
1311
1425
|
}
|
|
1312
1426
|
if (idToken) {
|
|
1313
1427
|
if (!provider.verifyIdToken) {
|
|
1314
1428
|
context.logger.error("Provider does not support id token verification", { provider: requestedProvider });
|
|
1315
1429
|
return ctx.render({
|
|
1316
|
-
success:
|
|
1317
|
-
message:
|
|
1430
|
+
success: _faire_auth_core_static.False,
|
|
1431
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.ID_TOKEN_NOT_SUPPORTED
|
|
1318
1432
|
}, 404);
|
|
1319
1433
|
}
|
|
1320
1434
|
const { token, nonce } = idToken;
|
|
1321
1435
|
if (!await provider.verifyIdToken(token, nonce)) {
|
|
1322
1436
|
context.logger.error("Invalid id token", { provider: requestedProvider });
|
|
1323
1437
|
return ctx.render({
|
|
1324
|
-
success:
|
|
1325
|
-
message:
|
|
1438
|
+
success: _faire_auth_core_static.False,
|
|
1439
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.INVALID_TOKEN
|
|
1326
1440
|
}, 401);
|
|
1327
1441
|
}
|
|
1328
1442
|
const linkingUserInfo = await provider.getUserInfo({
|
|
@@ -1333,25 +1447,25 @@ const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) =
|
|
|
1333
1447
|
if (!linkingUserInfo || !linkingUserInfo?.user) {
|
|
1334
1448
|
context.logger.error("Failed to get user info", { provider: requestedProvider });
|
|
1335
1449
|
return ctx.render({
|
|
1336
|
-
success:
|
|
1337
|
-
message:
|
|
1450
|
+
success: _faire_auth_core_static.False,
|
|
1451
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_GET_USER_INFO
|
|
1338
1452
|
}, 401);
|
|
1339
1453
|
}
|
|
1340
1454
|
const linkingUserId = String(linkingUserInfo.user.id);
|
|
1341
1455
|
if (!linkingUserInfo.user.email) {
|
|
1342
1456
|
context.logger.error("User email not found", { provider: requestedProvider });
|
|
1343
1457
|
return ctx.render({
|
|
1344
|
-
success:
|
|
1345
|
-
message:
|
|
1458
|
+
success: _faire_auth_core_static.False,
|
|
1459
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.USER_EMAIL_NOT_FOUND
|
|
1346
1460
|
}, 401);
|
|
1347
1461
|
}
|
|
1348
|
-
if ((await context.internalAdapter.findAccounts(session.user.id)).find((a) => a.providerId === provider.id && a.accountId === linkingUserId)) return ctx.render({ redirect:
|
|
1462
|
+
if ((await context.internalAdapter.findAccounts(session.user.id)).find((a) => a.providerId === provider.id && a.accountId === linkingUserId)) return ctx.render({ redirect: _faire_auth_core_static.False }, 200);
|
|
1349
1463
|
if (!(options.account?.accountLinking?.trustedProviders)?.includes(provider.id) && !linkingUserInfo.user.emailVerified || options.account?.accountLinking?.enabled === false) return ctx.render({
|
|
1350
|
-
success:
|
|
1464
|
+
success: _faire_auth_core_static.False,
|
|
1351
1465
|
message: "Account not linked - linking not allowed"
|
|
1352
1466
|
}, 401);
|
|
1353
1467
|
if (linkingUserInfo.user.email !== session.user.email && options.account?.accountLinking?.allowDifferentEmails !== true) return ctx.render({
|
|
1354
|
-
success:
|
|
1468
|
+
success: _faire_auth_core_static.False,
|
|
1355
1469
|
message: "Account not linked - different emails not allowed"
|
|
1356
1470
|
}, 401);
|
|
1357
1471
|
try {
|
|
@@ -1366,7 +1480,7 @@ const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) =
|
|
|
1366
1480
|
});
|
|
1367
1481
|
} catch (e) {
|
|
1368
1482
|
return ctx.render({
|
|
1369
|
-
success:
|
|
1483
|
+
success: _faire_auth_core_static.False,
|
|
1370
1484
|
message: "Account not linked - unable to create account"
|
|
1371
1485
|
}, 417);
|
|
1372
1486
|
}
|
|
@@ -1378,7 +1492,7 @@ const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) =
|
|
|
1378
1492
|
} catch (e) {
|
|
1379
1493
|
console.warn("Could not update user - " + e.toString());
|
|
1380
1494
|
}
|
|
1381
|
-
return ctx.render({ redirect:
|
|
1495
|
+
return ctx.render({ redirect: _faire_auth_core_static.False }, 200);
|
|
1382
1496
|
}
|
|
1383
1497
|
const state = await require_state.generateState(ctx, {
|
|
1384
1498
|
userId: session.user.id,
|
|
@@ -1391,18 +1505,18 @@ const linkAccount = require_session.createEndpoint(linkAccountRoute, (options) =
|
|
|
1391
1505
|
...scopes && { scopes }
|
|
1392
1506
|
});
|
|
1393
1507
|
return ctx.render({
|
|
1394
|
-
redirect:
|
|
1508
|
+
redirect: _faire_auth_core_static.True,
|
|
1395
1509
|
url: url.toString()
|
|
1396
1510
|
}, 200);
|
|
1397
1511
|
});
|
|
1398
|
-
const unlinkAccountRoute = (0,
|
|
1399
|
-
operationId:
|
|
1512
|
+
const unlinkAccountRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1513
|
+
operationId: _faire_auth_core_static.Routes.UNLINK_ACCOUNT,
|
|
1400
1514
|
method: "post",
|
|
1401
1515
|
path: "/unlink-account",
|
|
1402
1516
|
description: "Unlink an account",
|
|
1403
1517
|
middleware: [require_session.freshSessionMiddleware],
|
|
1404
|
-
request: (0,
|
|
1405
|
-
responses: (0,
|
|
1518
|
+
request: (0, _faire_auth_core_factory.req)().bdy(unlinkAccountSchema).bld(),
|
|
1519
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.SUCCESS].default).err(400, "Account not found or failed to unlink").zod().bld()
|
|
1406
1520
|
});
|
|
1407
1521
|
const unlinkAccount = require_session.createEndpoint(unlinkAccountRoute, (options) => async (ctx) => {
|
|
1408
1522
|
const { providerId, accountId } = ctx.req.valid("json");
|
|
@@ -1410,47 +1524,47 @@ const unlinkAccount = require_session.createEndpoint(unlinkAccountRoute, (option
|
|
|
1410
1524
|
const session = ctx.get("session");
|
|
1411
1525
|
const accounts = await context.internalAdapter.findAccounts(session.user.id);
|
|
1412
1526
|
if (accounts.length === 1 && options.account?.accountLinking?.allowUnlinkingAll !== true) return ctx.render({
|
|
1413
|
-
success:
|
|
1414
|
-
message:
|
|
1527
|
+
success: _faire_auth_core_static.False,
|
|
1528
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.FAILED_TO_UNLINK_LAST_ACCOUNT
|
|
1415
1529
|
}, 400);
|
|
1416
1530
|
const accountExist = accounts.find((account) => accountId != null ? account.accountId === accountId && account.providerId === providerId : account.providerId === providerId);
|
|
1417
1531
|
if (!accountExist) return ctx.render({
|
|
1418
|
-
success:
|
|
1419
|
-
message:
|
|
1532
|
+
success: _faire_auth_core_static.False,
|
|
1533
|
+
message: _faire_auth_core_error.BASE_ERROR_CODES.ACCOUNT_NOT_FOUND
|
|
1420
1534
|
}, 400);
|
|
1421
1535
|
await context.internalAdapter.deleteAccount(accountExist.id);
|
|
1422
|
-
return ctx.render({ success:
|
|
1536
|
+
return ctx.render({ success: _faire_auth_core_static.True }, 200);
|
|
1423
1537
|
});
|
|
1424
|
-
const getAccessTokenRoute = (0,
|
|
1425
|
-
operationId:
|
|
1538
|
+
const getAccessTokenRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1539
|
+
operationId: _faire_auth_core_static.Routes.GET_ACCESS_TOKEN,
|
|
1426
1540
|
method: "post",
|
|
1427
1541
|
path: "/get-access-token",
|
|
1428
1542
|
description: "Get a valid access token, doing a refresh if needed",
|
|
1429
1543
|
middleware: [require_session.requestOnlySessionMiddleware],
|
|
1430
|
-
request: (0,
|
|
1431
|
-
responses: (0,
|
|
1544
|
+
request: (0, _faire_auth_core_factory.req)().bdy(getAccessTokenSchema).bld(),
|
|
1545
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.ACCESS_TOKEN].default, "A valid access token").err(400, "Invalid refresh token or provider configuration").zod().bld()
|
|
1432
1546
|
});
|
|
1433
1547
|
const getAccessToken = require_session.createEndpoint(getAccessTokenRoute, (options) => async (ctx) => {
|
|
1434
1548
|
const { providerId, accountId, userId } = ctx.req.valid("json");
|
|
1435
|
-
if (userId && ctx.get("isServer") !== true) return ctx.render({ success:
|
|
1549
|
+
if (userId && ctx.get("isServer") !== true) return ctx.render({ success: _faire_auth_core_static.False }, 401);
|
|
1436
1550
|
const resolvedUserId = ctx.get("session")?.user.id ?? userId;
|
|
1437
1551
|
if (resolvedUserId == null) return ctx.render({
|
|
1438
|
-
success:
|
|
1552
|
+
success: _faire_auth_core_static.False,
|
|
1439
1553
|
message: `Either userId or session is required`
|
|
1440
1554
|
}, 400);
|
|
1441
1555
|
const context = ctx.get("context");
|
|
1442
1556
|
if (!context.socialProviders.find((p) => p.id === providerId)) return ctx.render({
|
|
1443
|
-
success:
|
|
1557
|
+
success: _faire_auth_core_static.False,
|
|
1444
1558
|
message: `Provider ${providerId} is not supported.`
|
|
1445
1559
|
}, 400);
|
|
1446
1560
|
const account = (await context.internalAdapter.findAccounts(resolvedUserId)).find((acc) => accountId != null ? acc.id === accountId && acc.providerId === providerId : acc.providerId === providerId);
|
|
1447
1561
|
if (!account) return ctx.render({
|
|
1448
|
-
success:
|
|
1562
|
+
success: _faire_auth_core_static.False,
|
|
1449
1563
|
message: "Account not found"
|
|
1450
1564
|
}, 400);
|
|
1451
1565
|
const provider = context.socialProviders.find((p) => p.id === providerId);
|
|
1452
1566
|
if (!provider) return ctx.render({
|
|
1453
|
-
success:
|
|
1567
|
+
success: _faire_auth_core_static.False,
|
|
1454
1568
|
message: `Provider ${providerId} not found.`
|
|
1455
1569
|
}, 400);
|
|
1456
1570
|
try {
|
|
@@ -1460,17 +1574,17 @@ const getAccessToken = require_session.createEndpoint(getAccessTokenRoute, (opti
|
|
|
1460
1574
|
newTokens = await provider.refreshAccessToken(account.refreshToken);
|
|
1461
1575
|
await context.internalAdapter.updateAccount(account.id, {
|
|
1462
1576
|
...newTokens.accessToken && {
|
|
1463
|
-
accessToken: await
|
|
1577
|
+
accessToken: await require_oauth2_index.setTokenUtil(newTokens.accessToken, ctx, options),
|
|
1464
1578
|
...newTokens.accessTokenExpiresAt && { accessTokenExpiresAt: newTokens.accessTokenExpiresAt }
|
|
1465
1579
|
},
|
|
1466
1580
|
...newTokens.refreshToken && {
|
|
1467
|
-
refreshToken: await
|
|
1581
|
+
refreshToken: await require_oauth2_index.setTokenUtil(newTokens.refreshToken, ctx, options),
|
|
1468
1582
|
...newTokens.refreshTokenExpiresAt && { refreshTokenExpiresAt: newTokens.refreshTokenExpiresAt }
|
|
1469
1583
|
}
|
|
1470
1584
|
});
|
|
1471
1585
|
}
|
|
1472
1586
|
const tokens = {
|
|
1473
|
-
accessToken: await
|
|
1587
|
+
accessToken: await require_oauth2_index.decryptOAuthToken(newTokens?.accessToken ?? account.accessToken ?? "", ctx, options),
|
|
1474
1588
|
...newTokens?.accessTokenExpiresAt != null || account.accessTokenExpiresAt != null ? { accessTokenExpiresAt: newTokens?.accessTokenExpiresAt ?? account.accessTokenExpiresAt } : {},
|
|
1475
1589
|
scopes: account.scope ? account.scope.split(",") : [],
|
|
1476
1590
|
...(newTokens?.idToken != null && newTokens.idToken !== "" || account.idToken != null && account.idToken !== "") && { idToken: newTokens?.idToken || account.idToken }
|
|
@@ -1478,19 +1592,19 @@ const getAccessToken = require_session.createEndpoint(getAccessTokenRoute, (opti
|
|
|
1478
1592
|
return ctx.render(tokens, 200);
|
|
1479
1593
|
} catch {
|
|
1480
1594
|
return ctx.render({
|
|
1481
|
-
success:
|
|
1595
|
+
success: _faire_auth_core_static.False,
|
|
1482
1596
|
message: "Failed to get a valid access token"
|
|
1483
1597
|
}, 400);
|
|
1484
1598
|
}
|
|
1485
1599
|
});
|
|
1486
|
-
const refreshAccessTokenRoute = (0,
|
|
1487
|
-
operationId:
|
|
1600
|
+
const refreshAccessTokenRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1601
|
+
operationId: _faire_auth_core_static.Routes.REFRESH_ACCESS_TOKEN,
|
|
1488
1602
|
method: "post",
|
|
1489
1603
|
path: "/refresh-token",
|
|
1490
1604
|
description: "Refresh the access token using a refresh token",
|
|
1491
1605
|
middleware: [require_session.sessionMiddleware()],
|
|
1492
|
-
request: (0,
|
|
1493
|
-
responses: (0,
|
|
1606
|
+
request: (0, _faire_auth_core_factory.req)().bdy(refreshAccessTokenSchema).bld(),
|
|
1607
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.ACCESS_TOKEN].default, "Access token refreshed successfully").err(400, "Invalid refresh token or provider configuration").zod().bld()
|
|
1494
1608
|
});
|
|
1495
1609
|
const refreshAccessToken = require_session.createEndpoint(refreshAccessTokenRoute, (options) => async (ctx) => {
|
|
1496
1610
|
const context = ctx.get("context");
|
|
@@ -1498,51 +1612,51 @@ const refreshAccessToken = require_session.createEndpoint(refreshAccessTokenRout
|
|
|
1498
1612
|
const { providerId, accountId, userId } = ctx.req.valid("json");
|
|
1499
1613
|
const resolvedUserId = userId ?? session.user.id;
|
|
1500
1614
|
if (!context.socialProviders.find((p) => p.id === providerId)) return ctx.render({
|
|
1501
|
-
success:
|
|
1615
|
+
success: _faire_auth_core_static.False,
|
|
1502
1616
|
message: `Provider ${providerId} is not supported.`
|
|
1503
1617
|
}, 400);
|
|
1504
1618
|
const account = (await context.internalAdapter.findAccounts(resolvedUserId)).find((acc) => accountId != null ? acc.id === accountId && acc.providerId === providerId : acc.providerId === providerId);
|
|
1505
1619
|
if (account?.refreshToken == null) return ctx.render({
|
|
1506
|
-
success:
|
|
1620
|
+
success: _faire_auth_core_static.False,
|
|
1507
1621
|
message: "Account not found"
|
|
1508
1622
|
}, 400);
|
|
1509
1623
|
const provider = context.socialProviders.find((p) => p.id === providerId);
|
|
1510
1624
|
if (!provider) return ctx.render({
|
|
1511
|
-
success:
|
|
1625
|
+
success: _faire_auth_core_static.False,
|
|
1512
1626
|
message: `Provider ${providerId} not found.`
|
|
1513
1627
|
}, 400);
|
|
1514
1628
|
if (!provider.refreshAccessToken) return ctx.render({
|
|
1515
|
-
success:
|
|
1629
|
+
success: _faire_auth_core_static.False,
|
|
1516
1630
|
message: `Provider ${providerId} does not support token refreshing.`
|
|
1517
1631
|
}, 400);
|
|
1518
1632
|
try {
|
|
1519
1633
|
const tokens = await provider.refreshAccessToken(account.refreshToken);
|
|
1520
1634
|
await context.internalAdapter.updateAccount(account.id, {
|
|
1521
1635
|
...tokens.accessToken && {
|
|
1522
|
-
accessToken: await
|
|
1636
|
+
accessToken: await require_oauth2_index.setTokenUtil(tokens.accessToken, ctx, options),
|
|
1523
1637
|
...tokens.accessTokenExpiresAt && { accessTokenExpiresAt: tokens.accessTokenExpiresAt }
|
|
1524
1638
|
},
|
|
1525
1639
|
...tokens.refreshToken && {
|
|
1526
|
-
refreshToken: await
|
|
1640
|
+
refreshToken: await require_oauth2_index.setTokenUtil(tokens.refreshToken, ctx, options),
|
|
1527
1641
|
...tokens.refreshTokenExpiresAt && { refreshTokenExpiresAt: tokens.refreshTokenExpiresAt }
|
|
1528
1642
|
}
|
|
1529
1643
|
});
|
|
1530
1644
|
return ctx.render(tokens, 200);
|
|
1531
1645
|
} catch {
|
|
1532
1646
|
return ctx.render({
|
|
1533
|
-
success:
|
|
1647
|
+
success: _faire_auth_core_static.False,
|
|
1534
1648
|
message: "Failed to refresh access token"
|
|
1535
1649
|
}, 400);
|
|
1536
1650
|
}
|
|
1537
1651
|
});
|
|
1538
|
-
const getAccountInfoRoute = (0,
|
|
1539
|
-
operationId:
|
|
1652
|
+
const getAccountInfoRoute = (0, _faire_auth_core_factory.createRoute)({
|
|
1653
|
+
operationId: _faire_auth_core_static.Routes.GET_ACCOUNT_INFO,
|
|
1540
1654
|
method: "post",
|
|
1541
1655
|
path: "/account-info",
|
|
1542
1656
|
description: "Get the account info provided by the provider",
|
|
1543
1657
|
middleware: [require_session.sessionMiddleware()],
|
|
1544
|
-
request: (0,
|
|
1545
|
-
responses: (0,
|
|
1658
|
+
request: (0, _faire_auth_core_factory.req)().bdy(getAccountInfoSchema).bld(),
|
|
1659
|
+
responses: (0, _faire_auth_core_factory.res)(_faire_auth_core_static.SCHEMAS[_faire_auth_core_static.Definitions.ACCOUNT_INFO].default).err(400, "Account not found").zod().err(500, "Provider not configured or failed to get user info").bld()
|
|
1546
1660
|
});
|
|
1547
1661
|
const getAccountInfo = require_session.createEndpoint(getAccountInfoRoute, (_options) => async (ctx) => {
|
|
1548
1662
|
const { accountId } = ctx.req.valid("json");
|
|
@@ -1551,12 +1665,12 @@ const getAccountInfo = require_session.createEndpoint(getAccountInfoRoute, (_opt
|
|
|
1551
1665
|
const api = ctx.get("api");
|
|
1552
1666
|
const account = await context.internalAdapter.findAccount(accountId);
|
|
1553
1667
|
if (!account || account.userId !== session.user.id) return ctx.render({
|
|
1554
|
-
success:
|
|
1668
|
+
success: _faire_auth_core_static.False,
|
|
1555
1669
|
message: "Account not found"
|
|
1556
1670
|
}, 400);
|
|
1557
1671
|
const provider = context.socialProviders.find((p) => p.id === account.providerId);
|
|
1558
1672
|
if (!provider) return ctx.render({
|
|
1559
|
-
success:
|
|
1673
|
+
success: _faire_auth_core_static.False,
|
|
1560
1674
|
message: `Provider account provider is ${account.providerId} but it is not configured`
|
|
1561
1675
|
}, 500);
|
|
1562
1676
|
const tokensResponse = await api.getAccessToken({ json: {
|
|
@@ -1571,45 +1685,43 @@ const getAccountInfo = require_session.createEndpoint(getAccountInfoRoute, (_opt
|
|
|
1571
1685
|
...tokensResponse.data.refreshTokenExpiresAt != null && { refreshTokenExpiresAt: new Date(tokensResponse.data.refreshTokenExpiresAt) }
|
|
1572
1686
|
});
|
|
1573
1687
|
if (info == null) return ctx.render({
|
|
1574
|
-
success:
|
|
1688
|
+
success: _faire_auth_core_static.False,
|
|
1575
1689
|
message: "Failed to get user info"
|
|
1576
1690
|
}, 500);
|
|
1577
1691
|
return ctx.render(info, 200);
|
|
1578
1692
|
});
|
|
1579
|
-
|
|
1580
|
-
//#endregion
|
|
1581
|
-
//#region src/api/endpoints.ts
|
|
1693
|
+
// src/api/endpoints.ts
|
|
1582
1694
|
const staticRouteMap = {
|
|
1583
|
-
[
|
|
1584
|
-
[
|
|
1585
|
-
[
|
|
1586
|
-
[
|
|
1587
|
-
[
|
|
1588
|
-
[
|
|
1589
|
-
[
|
|
1590
|
-
[
|
|
1591
|
-
[
|
|
1592
|
-
[
|
|
1593
|
-
[
|
|
1594
|
-
[
|
|
1595
|
-
[
|
|
1596
|
-
[
|
|
1597
|
-
[
|
|
1598
|
-
[
|
|
1599
|
-
[
|
|
1600
|
-
[
|
|
1601
|
-
[
|
|
1602
|
-
[
|
|
1603
|
-
[
|
|
1604
|
-
[
|
|
1605
|
-
[
|
|
1606
|
-
[
|
|
1607
|
-
[
|
|
1608
|
-
[
|
|
1609
|
-
[
|
|
1610
|
-
[
|
|
1611
|
-
[
|
|
1612
|
-
[
|
|
1695
|
+
[_faire_auth_core_static.Routes.SET_PASSWORD]: setPassword,
|
|
1696
|
+
[_faire_auth_core_static.Routes.CHANGE_EMAIL]: changeEmail,
|
|
1697
|
+
[_faire_auth_core_static.Routes.CHANGE_PASSWORD]: changePassword,
|
|
1698
|
+
[_faire_auth_core_static.Routes.DELETE_USER]: deleteUser,
|
|
1699
|
+
[_faire_auth_core_static.Routes.DELETE_USER_CALLBACK]: deleteUserCallback,
|
|
1700
|
+
[_faire_auth_core_static.Routes.ERROR]: error,
|
|
1701
|
+
[_faire_auth_core_static.Routes.GET_ACCESS_TOKEN]: getAccessToken,
|
|
1702
|
+
[_faire_auth_core_static.Routes.GET_ACCOUNT_INFO]: getAccountInfo,
|
|
1703
|
+
[_faire_auth_core_static.Routes.GET_SESSION]: require_session.getSession,
|
|
1704
|
+
[_faire_auth_core_static.Routes.LINK_ACCOUNT]: linkAccount,
|
|
1705
|
+
[_faire_auth_core_static.Routes.LIST_ACCOUNTS]: listAccounts,
|
|
1706
|
+
[_faire_auth_core_static.Routes.LIST_SESSIONS]: require_session.listSessions,
|
|
1707
|
+
[_faire_auth_core_static.Routes.OAUTH_CALLBACK_GET]: getOAuthCallback,
|
|
1708
|
+
[_faire_auth_core_static.Routes.OAUTH_CALLBACK_POST]: postOAuthCallback,
|
|
1709
|
+
[_faire_auth_core_static.Routes.OK]: ok,
|
|
1710
|
+
[_faire_auth_core_static.Routes.REFRESH_ACCESS_TOKEN]: refreshAccessToken,
|
|
1711
|
+
[_faire_auth_core_static.Routes.REQUEST_PASSWORD_RESET]: requestPasswordReset,
|
|
1712
|
+
[_faire_auth_core_static.Routes.REQUEST_PASSWORD_RESET_CALLBACK]: requestPasswordResetCallback,
|
|
1713
|
+
[_faire_auth_core_static.Routes.RESET_PASSWORD]: resetPassword,
|
|
1714
|
+
[_faire_auth_core_static.Routes.REVOKE_OTHER_SESSIONS]: require_session.revokeOtherSessions,
|
|
1715
|
+
[_faire_auth_core_static.Routes.REVOKE_SESSION]: require_session.revokeSession,
|
|
1716
|
+
[_faire_auth_core_static.Routes.REVOKE_SESSIONS]: require_session.revokeSessions,
|
|
1717
|
+
[_faire_auth_core_static.Routes.SEND_VERIFICATION_EMAIL]: require_email_verification.sendVerificationEmail,
|
|
1718
|
+
[_faire_auth_core_static.Routes.SIGN_IN_EMAIL]: signInEmail,
|
|
1719
|
+
[_faire_auth_core_static.Routes.SIGN_IN_SOCIAL]: signInSocial,
|
|
1720
|
+
[_faire_auth_core_static.Routes.SIGN_OUT]: signOut,
|
|
1721
|
+
[_faire_auth_core_static.Routes.SIGN_UP_EMAIL]: signUpEmail,
|
|
1722
|
+
[_faire_auth_core_static.Routes.UNLINK_ACCOUNT]: unlinkAccount,
|
|
1723
|
+
[_faire_auth_core_static.Routes.UPDATE_USER]: updateUser,
|
|
1724
|
+
[_faire_auth_core_static.Routes.VERIFY_EMAIL]: require_email_verification.verifyEmail
|
|
1613
1725
|
};
|
|
1614
1726
|
const resolveHooks = (options) => {
|
|
1615
1727
|
const routeHooks = { ...options.routeHooks };
|
|
@@ -1644,16 +1756,14 @@ const createArgs = (endpoints, builtSchemas, context, routeHooks) => {
|
|
|
1644
1756
|
}
|
|
1645
1757
|
return groups;
|
|
1646
1758
|
};
|
|
1647
|
-
|
|
1648
|
-
//#endregion
|
|
1649
|
-
//#region src/api/middleware/intercept.ts
|
|
1759
|
+
// src/api/middleware/intercept.ts
|
|
1650
1760
|
const runIntercept = (ctx, hooks) => {
|
|
1651
1761
|
let response = void 0;
|
|
1652
1762
|
for (const hook of hooks) try {
|
|
1653
1763
|
const result = hook(ctx);
|
|
1654
1764
|
if (result instanceof Response) response = result;
|
|
1655
|
-
} catch (error
|
|
1656
|
-
ctx.error = error
|
|
1765
|
+
} catch (error) {
|
|
1766
|
+
ctx.error = error;
|
|
1657
1767
|
}
|
|
1658
1768
|
return response;
|
|
1659
1769
|
};
|
|
@@ -1685,9 +1795,7 @@ const initInterceptMiddleware = (options) => {
|
|
|
1685
1795
|
if (after instanceof Response) ctx.res = after;
|
|
1686
1796
|
});
|
|
1687
1797
|
};
|
|
1688
|
-
|
|
1689
|
-
//#endregion
|
|
1690
|
-
//#region src/api/middleware/on-disabled.ts
|
|
1798
|
+
// src/api/middleware/on-disabled.ts
|
|
1691
1799
|
/**
|
|
1692
1800
|
* Initializes the middleware to handle disabled paths.
|
|
1693
1801
|
* @param options - The FaireAuth options.
|
|
@@ -1701,9 +1809,7 @@ const initHandleDisabledMiddleware = (options) => {
|
|
|
1701
1809
|
return await next();
|
|
1702
1810
|
});
|
|
1703
1811
|
};
|
|
1704
|
-
|
|
1705
|
-
//#endregion
|
|
1706
|
-
//#region src/api/middleware/rate-limit.ts
|
|
1812
|
+
// src/api/middleware/rate-limit.ts
|
|
1707
1813
|
const shouldRateLimit = (max, window, rateLimitData) => {
|
|
1708
1814
|
const now = Date.now();
|
|
1709
1815
|
const windowInMs = window * 1e3;
|
|
@@ -1712,7 +1818,7 @@ const shouldRateLimit = (max, window, rateLimitData) => {
|
|
|
1712
1818
|
const rateLimitResponse = (ctx, retryAfter) => {
|
|
1713
1819
|
ctx.header("X-Retry-After", retryAfter.toString());
|
|
1714
1820
|
return ctx.json({
|
|
1715
|
-
status:
|
|
1821
|
+
status: _faire_auth_core_static.False,
|
|
1716
1822
|
message: "Too many requests. Please try again later."
|
|
1717
1823
|
}, 429);
|
|
1718
1824
|
};
|
|
@@ -1726,21 +1832,21 @@ const createDBStorage = (ctx) => {
|
|
|
1726
1832
|
const db = ctx.adapter;
|
|
1727
1833
|
return {
|
|
1728
1834
|
get: async (key) => {
|
|
1729
|
-
const res
|
|
1835
|
+
const res = db.findMany({
|
|
1730
1836
|
model,
|
|
1731
1837
|
where: [{
|
|
1732
1838
|
field: "key",
|
|
1733
1839
|
value: key
|
|
1734
1840
|
}]
|
|
1735
1841
|
});
|
|
1736
|
-
const data = (0,
|
|
1842
|
+
const data = (0, _faire_auth_core_static.isPromise)(res) ? (await res)[0] : res[0];
|
|
1737
1843
|
if (typeof data?.lastRequest === "bigint") data.lastRequest = Number(data.lastRequest);
|
|
1738
1844
|
return data;
|
|
1739
1845
|
},
|
|
1740
1846
|
set: async (key, value, _update) => {
|
|
1741
1847
|
try {
|
|
1742
1848
|
if (_update === true) {
|
|
1743
|
-
const res
|
|
1849
|
+
const res = db.updateMany({
|
|
1744
1850
|
model: "rateLimit",
|
|
1745
1851
|
where: [{
|
|
1746
1852
|
field: "key",
|
|
@@ -1751,9 +1857,9 @@ const createDBStorage = (ctx) => {
|
|
|
1751
1857
|
lastRequest: value.lastRequest
|
|
1752
1858
|
}
|
|
1753
1859
|
});
|
|
1754
|
-
if ((0,
|
|
1860
|
+
if ((0, _faire_auth_core_static.isPromise)(res)) await res;
|
|
1755
1861
|
} else {
|
|
1756
|
-
const res
|
|
1862
|
+
const res = await db.create({
|
|
1757
1863
|
model: "rateLimit",
|
|
1758
1864
|
data: {
|
|
1759
1865
|
key,
|
|
@@ -1761,7 +1867,7 @@ const createDBStorage = (ctx) => {
|
|
|
1761
1867
|
lastRequest: value.lastRequest
|
|
1762
1868
|
}
|
|
1763
1869
|
});
|
|
1764
|
-
if ((0,
|
|
1870
|
+
if ((0, _faire_auth_core_static.isPromise)(res)) await res;
|
|
1765
1871
|
}
|
|
1766
1872
|
} catch (e) {
|
|
1767
1873
|
ctx.logger.error("Error setting rate limit", e);
|
|
@@ -1780,8 +1886,8 @@ const getRateLimitStorage = (config, context, options) => {
|
|
|
1780
1886
|
if (options.rateLimit?.customStorage) return options.rateLimit.customStorage;
|
|
1781
1887
|
if (config.storage === "secondary-storage") return {
|
|
1782
1888
|
get: (key) => {
|
|
1783
|
-
const res
|
|
1784
|
-
return (0,
|
|
1889
|
+
const res = options.secondaryStorage?.get(key);
|
|
1890
|
+
return (0, _faire_auth_core_static.isPromise)(res) ? res.then((r) => r ? require_json.safeJSONParse(r) : void 0) : res ? require_json.safeJSONParse(res) : void 0;
|
|
1785
1891
|
},
|
|
1786
1892
|
set: (key, value) => options.secondaryStorage?.set(key, JSON.stringify(value))
|
|
1787
1893
|
};
|
|
@@ -1829,8 +1935,8 @@ const initRateLimitMiddleware = (options, rateLimit) => {
|
|
|
1829
1935
|
const _path = Object.keys(rateLimit.customRules).find((p) => p.includes("*") ? require_origin_check.wildcardMatch(p)(path) : p === path);
|
|
1830
1936
|
if (_path) {
|
|
1831
1937
|
const customRule = rateLimit.customRules[_path];
|
|
1832
|
-
const res
|
|
1833
|
-
const resolved = (0,
|
|
1938
|
+
const res = typeof customRule === "function" ? customRule(ctx.req) : customRule;
|
|
1939
|
+
const resolved = (0, _faire_auth_core_static.isPromise)(res) ? await res : res;
|
|
1834
1940
|
if (resolved) {
|
|
1835
1941
|
window = resolved.window;
|
|
1836
1942
|
max = resolved.max;
|
|
@@ -1839,47 +1945,48 @@ const initRateLimitMiddleware = (options, rateLimit) => {
|
|
|
1839
1945
|
}
|
|
1840
1946
|
}
|
|
1841
1947
|
const storage = getRateLimitStorage(rateLimit, context, options);
|
|
1842
|
-
const res
|
|
1948
|
+
const res = storage.get(key);
|
|
1843
1949
|
const now = Date.now();
|
|
1844
|
-
const data = (0,
|
|
1950
|
+
const data = (0, _faire_auth_core_static.isPromise)(res) ? await res : res;
|
|
1845
1951
|
if (!data) {
|
|
1846
1952
|
const res2 = storage.set(key, {
|
|
1847
1953
|
key,
|
|
1848
1954
|
count: 1,
|
|
1849
1955
|
lastRequest: now
|
|
1850
1956
|
});
|
|
1851
|
-
if ((0,
|
|
1957
|
+
if ((0, _faire_auth_core_static.isPromise)(res2)) await res2;
|
|
1852
1958
|
} else {
|
|
1853
1959
|
const timeSinceLastRequest = now - data.lastRequest;
|
|
1854
|
-
if (shouldRateLimit(max, window, data))
|
|
1960
|
+
if (shouldRateLimit(max, window, data)) {
|
|
1961
|
+
const retryAfter = getRetryAfter(data.lastRequest, window);
|
|
1962
|
+
return rateLimitResponse(ctx, retryAfter);
|
|
1963
|
+
}
|
|
1855
1964
|
if (timeSinceLastRequest > window * 1e3) {
|
|
1856
1965
|
const res3 = storage.set(key, {
|
|
1857
1966
|
...data,
|
|
1858
1967
|
count: 1,
|
|
1859
1968
|
lastRequest: now
|
|
1860
1969
|
}, true);
|
|
1861
|
-
if ((0,
|
|
1970
|
+
if ((0, _faire_auth_core_static.isPromise)(res3)) await res3;
|
|
1862
1971
|
} else {
|
|
1863
1972
|
const res3 = storage.set(key, {
|
|
1864
1973
|
...data,
|
|
1865
1974
|
count: data.count + 1,
|
|
1866
1975
|
lastRequest: now
|
|
1867
1976
|
}, true);
|
|
1868
|
-
if ((0,
|
|
1977
|
+
if ((0, _faire_auth_core_static.isPromise)(res3)) await res3;
|
|
1869
1978
|
}
|
|
1870
1979
|
}
|
|
1871
1980
|
return await next();
|
|
1872
1981
|
});
|
|
1873
1982
|
};
|
|
1874
|
-
|
|
1875
|
-
//#endregion
|
|
1876
|
-
//#region src/api/index.ts
|
|
1983
|
+
// src/api/index.ts
|
|
1877
1984
|
const router = (context, options) => {
|
|
1878
|
-
const schemas = (0,
|
|
1985
|
+
const schemas = (0, _faire_auth_core_utils.buildSchemas)(_faire_auth_core_static.SCHEMAS, options);
|
|
1879
1986
|
const { endpoints, hooks } = createEndpoints(options);
|
|
1880
1987
|
const { pub } = createArgs(endpoints, schemas, context, hooks);
|
|
1881
1988
|
const api = require_session.createAPI(endpoints);
|
|
1882
|
-
let app = new
|
|
1989
|
+
let app = new _faire_auth_core_factory.OpenAPIHono(options).openapi(...pub[0]).openapi(...pub[1]).openapi(...pub[2]).openapi(...pub[3]).openapi(...pub[4]).openapi(...pub[5]).openapi(...pub[6]).openapi(...pub[7]).openapi(...pub[8]).openapi(...pub[9]).openapi(...pub[10]).openapi(...pub[11]).openapi(...pub[12]).openapi(...pub[13]).openapi(...pub[14]).openapi(...pub[15]).openapi(...pub[16]).openapi(...pub[17]).openapi(...pub[18]).openapi(...pub[19]).openapi(...pub[20]).openapi(...pub[21]).openapi(...pub[22]).openapi(...pub[23]).openapi(...pub[24]).openapi(...pub[25]).openapi(...pub[26]).openapi(...pub[27]).openapi(...pub[28]);
|
|
1883
1990
|
pub.slice(29).forEach((route) => app = app.openapi(...route));
|
|
1884
1991
|
return {
|
|
1885
1992
|
api,
|
|
@@ -1889,11 +1996,9 @@ const router = (context, options) => {
|
|
|
1889
1996
|
}).basePath(options.basePath).use(require_session.setDefaultExecutionCtx, require_session.setRenderer, require_session.initContextMiddleware(options, context, endpoints), (0, hono_context_storage.contextStorage)(), initHandleDisabledMiddleware(options), initInterceptMiddleware(options), initRateLimitMiddleware(options, context.rateLimit), require_origin_check.initOriginCheckMiddleware(options, context), require_session.initHooksMiddleware(options)).onError(require_session.initErrorHandler(options)).route("/", app)
|
|
1890
1997
|
};
|
|
1891
1998
|
};
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
}
|
|
1899
|
-
});
|
|
1999
|
+
Object.defineProperty(exports, "router", {
|
|
2000
|
+
enumerable: true,
|
|
2001
|
+
get: function() {
|
|
2002
|
+
return router;
|
|
2003
|
+
}
|
|
2004
|
+
});
|