@softure-ai/auth 0.0.0-stage → 0.1.7

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.
Files changed (255) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/LICENSE +21 -0
  3. package/README.md +490 -2
  4. package/dist/contract.d.ts +46 -0
  5. package/dist/contract.d.ts.map +1 -0
  6. package/dist/contract.js +2 -0
  7. package/dist/contract.js.map +1 -0
  8. package/dist/index.d.ts +194 -0
  9. package/dist/index.d.ts.map +1 -0
  10. package/dist/index.js +84 -0
  11. package/dist/index.js.map +1 -0
  12. package/dist/mailing/index.d.ts +2 -0
  13. package/dist/mailing/index.d.ts.map +1 -0
  14. package/dist/mailing/index.js +4 -0
  15. package/dist/mailing/index.js.map +1 -0
  16. package/dist/mailing/reset-mail.d.ts +27 -0
  17. package/dist/mailing/reset-mail.d.ts.map +1 -0
  18. package/dist/mailing/reset-mail.js +55 -0
  19. package/dist/mailing/reset-mail.js.map +1 -0
  20. package/dist/messages/en.d.ts +98 -0
  21. package/dist/messages/en.d.ts.map +1 -0
  22. package/dist/messages/en.js +93 -0
  23. package/dist/messages/en.js.map +1 -0
  24. package/dist/messages/index.d.ts +201 -0
  25. package/dist/messages/index.d.ts.map +1 -0
  26. package/dist/messages/index.js +11 -0
  27. package/dist/messages/index.js.map +1 -0
  28. package/dist/messages/pl.d.ts +3 -0
  29. package/dist/messages/pl.d.ts.map +1 -0
  30. package/dist/messages/pl.js +92 -0
  31. package/dist/messages/pl.js.map +1 -0
  32. package/dist/next/actions.d.ts +19 -0
  33. package/dist/next/actions.d.ts.map +1 -0
  34. package/dist/next/actions.js +231 -0
  35. package/dist/next/actions.js.map +1 -0
  36. package/dist/next/context.d.ts +4 -0
  37. package/dist/next/context.d.ts.map +1 -0
  38. package/dist/next/context.js +13 -0
  39. package/dist/next/context.js.map +1 -0
  40. package/dist/next/current-user.d.ts +34 -0
  41. package/dist/next/current-user.d.ts.map +1 -0
  42. package/dist/next/current-user.js +77 -0
  43. package/dist/next/current-user.js.map +1 -0
  44. package/dist/next/index.d.ts +8 -0
  45. package/dist/next/index.d.ts.map +1 -0
  46. package/dist/next/index.js +9 -0
  47. package/dist/next/index.js.map +1 -0
  48. package/dist/next/logout-button.d.ts +7 -0
  49. package/dist/next/logout-button.d.ts.map +1 -0
  50. package/dist/next/logout-button.js +12 -0
  51. package/dist/next/logout-button.js.map +1 -0
  52. package/dist/next/messages.d.ts +5 -0
  53. package/dist/next/messages.d.ts.map +1 -0
  54. package/dist/next/messages.js +7 -0
  55. package/dist/next/messages.js.map +1 -0
  56. package/dist/next/pages.d.ts +31 -0
  57. package/dist/next/pages.d.ts.map +1 -0
  58. package/dist/next/pages.js +118 -0
  59. package/dist/next/pages.js.map +1 -0
  60. package/dist/next/params.d.ts +3 -0
  61. package/dist/next/params.d.ts.map +1 -0
  62. package/dist/next/params.js +4 -0
  63. package/dist/next/params.js.map +1 -0
  64. package/dist/next/route.d.ts +3 -0
  65. package/dist/next/route.d.ts.map +1 -0
  66. package/dist/next/route.js +17 -0
  67. package/dist/next/route.js.map +1 -0
  68. package/dist/next/search-params.d.ts +5 -0
  69. package/dist/next/search-params.d.ts.map +1 -0
  70. package/dist/next/search-params.js +13 -0
  71. package/dist/next/search-params.js.map +1 -0
  72. package/dist/next/session-cookie.d.ts +11 -0
  73. package/dist/next/session-cookie.d.ts.map +1 -0
  74. package/dist/next/session-cookie.js +71 -0
  75. package/dist/next/session-cookie.js.map +1 -0
  76. package/dist/options.d.ts +72 -0
  77. package/dist/options.d.ts.map +1 -0
  78. package/dist/options.js +115 -0
  79. package/dist/options.js.map +1 -0
  80. package/dist/password-reset-sender.d.ts +21 -0
  81. package/dist/password-reset-sender.d.ts.map +1 -0
  82. package/dist/password-reset-sender.js +12 -0
  83. package/dist/password-reset-sender.js.map +1 -0
  84. package/dist/proxy/index.d.ts +14 -0
  85. package/dist/proxy/index.d.ts.map +1 -0
  86. package/dist/proxy/index.js +59 -0
  87. package/dist/proxy/index.js.map +1 -0
  88. package/dist/redirect-target.d.ts +7 -0
  89. package/dist/redirect-target.d.ts.map +1 -0
  90. package/dist/redirect-target.js +25 -0
  91. package/dist/redirect-target.js.map +1 -0
  92. package/dist/roles.d.ts +5 -0
  93. package/dist/roles.d.ts.map +1 -0
  94. package/dist/roles.js +6 -0
  95. package/dist/roles.js.map +1 -0
  96. package/dist/safe-next-path.d.ts +5 -0
  97. package/dist/safe-next-path.d.ts.map +1 -0
  98. package/dist/safe-next-path.js +25 -0
  99. package/dist/safe-next-path.js.map +1 -0
  100. package/dist/schema.d.ts +302 -0
  101. package/dist/schema.d.ts.map +1 -0
  102. package/dist/schema.js +37 -0
  103. package/dist/schema.js.map +1 -0
  104. package/dist/scripts/index.d.ts +3 -0
  105. package/dist/scripts/index.d.ts.map +1 -0
  106. package/dist/scripts/index.js +5 -0
  107. package/dist/scripts/index.js.map +1 -0
  108. package/dist/scripts/role-scripts.d.ts +15 -0
  109. package/dist/scripts/role-scripts.d.ts.map +1 -0
  110. package/dist/scripts/role-scripts.js +76 -0
  111. package/dist/scripts/role-scripts.js.map +1 -0
  112. package/dist/scripts/temporary-password.d.ts +12 -0
  113. package/dist/scripts/temporary-password.d.ts.map +1 -0
  114. package/dist/scripts/temporary-password.js +47 -0
  115. package/dist/scripts/temporary-password.js.map +1 -0
  116. package/dist/server/change-password.d.ts +14 -0
  117. package/dist/server/change-password.d.ts.map +1 -0
  118. package/dist/server/change-password.js +50 -0
  119. package/dist/server/change-password.js.map +1 -0
  120. package/dist/server/health.d.ts +3 -0
  121. package/dist/server/health.d.ts.map +1 -0
  122. package/dist/server/health.js +15 -0
  123. package/dist/server/health.js.map +1 -0
  124. package/dist/server/index.d.ts +12 -0
  125. package/dist/server/index.d.ts.map +1 -0
  126. package/dist/server/index.js +14 -0
  127. package/dist/server/index.js.map +1 -0
  128. package/dist/server/login.d.ts +14 -0
  129. package/dist/server/login.d.ts.map +1 -0
  130. package/dist/server/login.js +50 -0
  131. package/dist/server/login.js.map +1 -0
  132. package/dist/server/options.d.ts +19 -0
  133. package/dist/server/options.d.ts.map +1 -0
  134. package/dist/server/options.js +50 -0
  135. package/dist/server/options.js.map +1 -0
  136. package/dist/server/password-reset.d.ts +54 -0
  137. package/dist/server/password-reset.d.ts.map +1 -0
  138. package/dist/server/password-reset.js +134 -0
  139. package/dist/server/password-reset.js.map +1 -0
  140. package/dist/server/password.d.ts +25 -0
  141. package/dist/server/password.d.ts.map +1 -0
  142. package/dist/server/password.js +93 -0
  143. package/dist/server/password.js.map +1 -0
  144. package/dist/server/privacy.d.ts +33 -0
  145. package/dist/server/privacy.d.ts.map +1 -0
  146. package/dist/server/privacy.js +52 -0
  147. package/dist/server/privacy.js.map +1 -0
  148. package/dist/server/rate-limits.d.ts +22 -0
  149. package/dist/server/rate-limits.d.ts.map +1 -0
  150. package/dist/server/rate-limits.js +45 -0
  151. package/dist/server/rate-limits.js.map +1 -0
  152. package/dist/server/reauthenticate.d.ts +7 -0
  153. package/dist/server/reauthenticate.d.ts.map +1 -0
  154. package/dist/server/reauthenticate.js +16 -0
  155. package/dist/server/reauthenticate.js.map +1 -0
  156. package/dist/server/register.d.ts +23 -0
  157. package/dist/server/register.d.ts.map +1 -0
  158. package/dist/server/register.js +64 -0
  159. package/dist/server/register.js.map +1 -0
  160. package/dist/server/roles.d.ts +25 -0
  161. package/dist/server/roles.d.ts.map +1 -0
  162. package/dist/server/roles.js +56 -0
  163. package/dist/server/roles.js.map +1 -0
  164. package/dist/server/session-token.d.ts +12 -0
  165. package/dist/server/session-token.d.ts.map +1 -0
  166. package/dist/server/session-token.js +22 -0
  167. package/dist/server/session-token.js.map +1 -0
  168. package/dist/server/sessions.d.ts +24 -0
  169. package/dist/server/sessions.d.ts.map +1 -0
  170. package/dist/server/sessions.js +50 -0
  171. package/dist/server/sessions.js.map +1 -0
  172. package/dist/server/switches.d.ts +9 -0
  173. package/dist/server/switches.d.ts.map +1 -0
  174. package/dist/server/switches.js +34 -0
  175. package/dist/server/switches.js.map +1 -0
  176. package/dist/server/validation.d.ts +10 -0
  177. package/dist/server/validation.d.ts.map +1 -0
  178. package/dist/server/validation.js +32 -0
  179. package/dist/server/validation.js.map +1 -0
  180. package/dist/session-cookie.d.ts +14 -0
  181. package/dist/session-cookie.d.ts.map +1 -0
  182. package/dist/session-cookie.js +31 -0
  183. package/dist/session-cookie.js.map +1 -0
  184. package/dist/testing/account.d.ts +17 -0
  185. package/dist/testing/account.d.ts.map +1 -0
  186. package/dist/testing/account.js +31 -0
  187. package/dist/testing/account.js.map +1 -0
  188. package/dist/testing/index.d.ts +2 -0
  189. package/dist/testing/index.d.ts.map +1 -0
  190. package/dist/testing/index.js +4 -0
  191. package/dist/testing/index.js.map +1 -0
  192. package/dist/ui/auth-forms.d.ts +64 -0
  193. package/dist/ui/auth-forms.d.ts.map +1 -0
  194. package/dist/ui/auth-forms.js +67 -0
  195. package/dist/ui/auth-forms.js.map +1 -0
  196. package/dist/ui/index.d.ts +2 -0
  197. package/dist/ui/index.d.ts.map +1 -0
  198. package/dist/ui/index.js +4 -0
  199. package/dist/ui/index.js.map +1 -0
  200. package/migrations/0001_create_users_and_sessions.sql +25 -0
  201. package/migrations/0002_create_user_roles.sql +10 -0
  202. package/migrations/0003_create_password_resets.sql +15 -0
  203. package/migrations/0004_index_users_created_at.sql +5 -0
  204. package/module.json +34 -0
  205. package/package.json +97 -4
  206. package/src/contract.ts +67 -0
  207. package/src/index.ts +100 -0
  208. package/src/mailing/index.ts +3 -0
  209. package/src/mailing/reset-mail.ts +73 -0
  210. package/src/messages/en.ts +92 -0
  211. package/src/messages/index.ts +15 -0
  212. package/src/messages/pl.ts +93 -0
  213. package/src/next/actions.ts +240 -0
  214. package/src/next/context.ts +14 -0
  215. package/src/next/current-user.ts +90 -0
  216. package/src/next/index.ts +33 -0
  217. package/src/next/logout-button.tsx +22 -0
  218. package/src/next/messages.ts +9 -0
  219. package/src/next/next-modules.d.ts +16 -0
  220. package/src/next/pages.tsx +223 -0
  221. package/src/next/params.ts +4 -0
  222. package/src/next/route.ts +17 -0
  223. package/src/next/search-params.ts +14 -0
  224. package/src/next/session-cookie.ts +79 -0
  225. package/src/options.ts +154 -0
  226. package/src/password-reset-sender.ts +33 -0
  227. package/src/proxy/index.ts +85 -0
  228. package/src/redirect-target.ts +24 -0
  229. package/src/roles.ts +7 -0
  230. package/src/safe-next-path.ts +21 -0
  231. package/src/schema.ts +45 -0
  232. package/src/scripts/index.ts +8 -0
  233. package/src/scripts/role-scripts.ts +98 -0
  234. package/src/scripts/temporary-password.ts +62 -0
  235. package/src/server/change-password.ts +64 -0
  236. package/src/server/health.ts +16 -0
  237. package/src/server/index.ts +46 -0
  238. package/src/server/login.ts +61 -0
  239. package/src/server/options.ts +64 -0
  240. package/src/server/password-reset.ts +164 -0
  241. package/src/server/password.ts +108 -0
  242. package/src/server/privacy.ts +70 -0
  243. package/src/server/rate-limits.ts +51 -0
  244. package/src/server/reauthenticate.ts +16 -0
  245. package/src/server/register.ts +87 -0
  246. package/src/server/roles.ts +72 -0
  247. package/src/server/session-token.ts +25 -0
  248. package/src/server/sessions.ts +65 -0
  249. package/src/server/switches.ts +31 -0
  250. package/src/server/validation.ts +37 -0
  251. package/src/session-cookie.ts +47 -0
  252. package/src/testing/account.ts +43 -0
  253. package/src/testing/index.ts +3 -0
  254. package/src/ui/auth-forms.tsx +383 -0
  255. package/src/ui/index.ts +16 -0
@@ -0,0 +1,72 @@
1
+ import type { ModuleContext, SoftureConfig } from "@softure-ai/core";
2
+ import type { Queryable } from "@softure-ai/db";
3
+ import { z } from "zod";
4
+ import type { RegisteredEvent } from "./contract.js";
5
+ import type { PasswordResetSender } from "./password-reset-sender.js";
6
+ /**
7
+ * OWASP's scrypt cost for passwords: N = 2^17, r = 8, p = 1. One hash takes 128 * N * r bytes
8
+ * (128 MiB) and about 0.4 s of a core; Node runs up to four at once on its libuv pool, so a burst of
9
+ * logins can take 512 MiB. A small server lowers `password.scrypt.cost` (README §3).
10
+ */
11
+ export declare const DEFAULT_SCRYPT_COST: number;
12
+ /** Runs inside the registration transaction; a thrown error rolls the registration back. */
13
+ export type OnRegisteredHook = (event: RegisteredEvent, ctx: ModuleContext<Queryable>) => Promise<void>;
14
+ /** What a redirect rewrite gets besides the path. */
15
+ export interface RewriteRedirectContext {
16
+ readonly config: SoftureConfig;
17
+ /**
18
+ * The page's own search params, when a page redirects while it renders (the login and register
19
+ * pages send a signed-in visitor on); absent for an action's redirect. A render's `Referer` is the
20
+ * page before, not this one.
21
+ */
22
+ readonly searchParams?: URLSearchParams;
23
+ }
24
+ /**
25
+ * Rewrites the path auth redirects to (after login, sign-up, a password reset and logout, and the
26
+ * login and register pages' redirect of a signed-in visitor), e.g. analytics' `tagRedirect`, which
27
+ * keeps the channel tag. A result that is not a path on this app, or a failure, leaves auth's own path.
28
+ */
29
+ export type RewriteRedirect = (path: string, ctx: RewriteRedirectContext) => Promise<string> | string;
30
+ declare const scryptSchema: z.ZodObject<{
31
+ cost: z.ZodDefault<z.ZodNumber>;
32
+ blockSize: z.ZodDefault<z.ZodNumber>;
33
+ parallelization: z.ZodDefault<z.ZodNumber>;
34
+ }, z.core.$strict>;
35
+ export declare const authOptionsSchema: z.ZodObject<{
36
+ password: z.ZodPrefault<z.ZodObject<{
37
+ minLength: z.ZodDefault<z.ZodNumber>;
38
+ scrypt: z.ZodPrefault<z.ZodObject<{
39
+ cost: z.ZodDefault<z.ZodNumber>;
40
+ blockSize: z.ZodDefault<z.ZodNumber>;
41
+ parallelization: z.ZodDefault<z.ZodNumber>;
42
+ }, z.core.$strict>>;
43
+ }, z.core.$strict>>;
44
+ session: z.ZodPrefault<z.ZodObject<{
45
+ ttlDays: z.ZodDefault<z.ZodNumber>;
46
+ }, z.core.$strict>>;
47
+ cookie: z.ZodPrefault<z.ZodObject<{
48
+ name: z.ZodDefault<z.ZodString>;
49
+ domain: z.ZodOptional<z.ZodString>;
50
+ secure: z.ZodOptional<z.ZodBoolean>;
51
+ }, z.core.$strict>>;
52
+ legacySession: z.ZodOptional<z.ZodObject<{
53
+ cookieName: z.ZodString;
54
+ tokenPattern: z.ZodCustom<RegExp, RegExp>;
55
+ }, z.core.$strict>>;
56
+ registrationFields: z.ZodDefault<z.ZodArray<z.ZodString>>;
57
+ requireConsent: z.ZodDefault<z.ZodBoolean>;
58
+ registrationClosed: z.ZodDefault<z.ZodBoolean>;
59
+ roles: z.ZodDefault<z.ZodArray<z.ZodString>>;
60
+ adminEmails: z.ZodDefault<z.ZodArray<z.ZodPipe<z.ZodString, z.ZodEmail>>>;
61
+ passwordReset: z.ZodPrefault<z.ZodObject<{
62
+ send: z.ZodOptional<z.ZodCustom<PasswordResetSender, PasswordResetSender>>;
63
+ ttlMinutes: z.ZodDefault<z.ZodNumber>;
64
+ }, z.core.$strict>>;
65
+ onRegistered: z.ZodOptional<z.ZodCustom<OnRegisteredHook, OnRegisteredHook>>;
66
+ rewriteRedirect: z.ZodOptional<z.ZodCustom<RewriteRedirect, RewriteRedirect>>;
67
+ }, z.core.$strict>;
68
+ export type AuthOptionsInput = z.input<typeof authOptionsSchema>;
69
+ export type AuthOptions = z.output<typeof authOptionsSchema>;
70
+ export type ScryptParams = z.output<typeof scryptSchema>;
71
+ export {};
72
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAGtE;;;;GAIG;AACH,eAAO,MAAM,mBAAmB,QAAU,CAAC;AAY3C,4FAA4F;AAC5F,MAAM,MAAM,gBAAgB,GAAG,CAAC,KAAK,EAAE,eAAe,EAAE,GAAG,EAAE,aAAa,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAExG,qDAAqD;AACrD,MAAM,WAAW,sBAAsB;IACrC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;IAC/B;;;;OAIG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,eAAe,CAAC;CACzC;AAED;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,sBAAsB,KAAK,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;AAMtG,QAAA,MAAM,YAAY;;;;kBAahB,CAAC;AAEH,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAoF5B,CAAC;AAEH,MAAM,MAAM,gBAAgB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAC;AACjE,MAAM,MAAM,WAAW,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAC7D,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,YAAY,CAAC,CAAC"}
@@ -0,0 +1,115 @@
1
+ import { z } from "zod";
2
+ import { ROLE_NAME_PATTERN } from "./roles.js";
3
+ /**
4
+ * OWASP's scrypt cost for passwords: N = 2^17, r = 8, p = 1. One hash takes 128 * N * r bytes
5
+ * (128 MiB) and about 0.4 s of a core; Node runs up to four at once on its libuv pool, so a burst of
6
+ * logins can take 512 MiB. A small server lowers `password.scrypt.cost` (README §3).
7
+ */
8
+ export const DEFAULT_SCRYPT_COST = 2 ** 17;
9
+ const MAX_EMAIL_LENGTH = 254;
10
+ const COOKIE_NAME = /^[A-Za-z0-9_-]{1,64}$/;
11
+ /** A legacy cookie may carry its own prefix (`__Host-session`) or a dot. */
12
+ const LEGACY_COOKIE_NAME = /^[A-Za-z0-9_.-]{1,128}$/;
13
+ const DEFAULT_COOKIE_NAME = "softure_session";
14
+ const REGISTRATION_FIELD_NAME = /^[a-z][a-zA-Z0-9_]{0,31}$/;
15
+ /** The register form's own fields, which an app field must not shadow. */
16
+ const REGISTER_FORM_FIELDS = new Set(["email", "password", "next", "consent"]);
17
+ const DOMAIN = /^(?=.{1,253}$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)+$/;
18
+ function isPowerOfTwo(value) {
19
+ return Number.isInteger(Math.log2(value));
20
+ }
21
+ const scryptSchema = z.strictObject({
22
+ /** N: CPU and memory cost, a power of two. */
23
+ cost: z
24
+ .number()
25
+ .int()
26
+ .min(2 ** 10)
27
+ .max(2 ** 20)
28
+ .refine(isPowerOfTwo, "must be a power of two")
29
+ .default(DEFAULT_SCRYPT_COST),
30
+ /** r: block size. */
31
+ blockSize: z.number().int().min(1).max(32).default(8),
32
+ /** p: parallelization. */
33
+ parallelization: z.number().int().min(1).max(16).default(1),
34
+ });
35
+ export const authOptionsSchema = z.strictObject({
36
+ password: z
37
+ .strictObject({
38
+ /** Shortest password a user may set (characters). */
39
+ minLength: z.number().int().min(8).max(128).default(10),
40
+ /** Hash cost; memory per hash is 128 * cost * blockSize bytes. Raising it rehashes each password at its owner's next login. */
41
+ scrypt: scryptSchema.prefault({}),
42
+ })
43
+ .prefault({}),
44
+ session: z
45
+ .strictObject({
46
+ /** How long a session lasts from login; it is not extended by use. */
47
+ ttlDays: z.number().int().min(1).max(365).default(30),
48
+ })
49
+ .prefault({}),
50
+ cookie: z
51
+ .strictObject({
52
+ /** Base name; `__Host-` or `__Secure-` is added when the cookie is secure. */
53
+ name: z.string().regex(COOKIE_NAME, "must be 1-64 letters, digits, _ or -").default(DEFAULT_COOKIE_NAME),
54
+ /** Share the session with subdomains, e.g. `example.com` for the apex and `app.example.com`. */
55
+ domain: z.string().regex(DOMAIN, "must be a lowercase host name such as example.com").optional(),
56
+ /** Defaults to whether `appOrigin` is https. */
57
+ secure: z.boolean().optional(),
58
+ })
59
+ .prefault({}),
60
+ /**
61
+ * Sessions of the system the app took over, so adopting the module does not log everyone out:
62
+ * the cookie it set and the shape of its tokens, stored as the sha256 hex of the token like here.
63
+ * Read when the current cookie is absent; ended and cleared at the next login, register or logout.
64
+ */
65
+ legacySession: z
66
+ .strictObject({
67
+ /** The old cookie's full name, prefix included. */
68
+ cookieName: z.string().regex(LEGACY_COOKIE_NAME, "must be 1-128 letters, digits, _ . or -"),
69
+ /** The whole token's shape, e.g. /[0-9a-f]{64}/; anchored by the module. */
70
+ tokenPattern: z.custom((value) => value instanceof RegExp && !value.global && !value.sticky, "must be a RegExp without the g or y flag"),
71
+ })
72
+ .optional(),
73
+ /**
74
+ * Extra register form fields handed to `onRegistered` as `event.fields` (strings, at most 512
75
+ * characters; empty ones are left out). `RegisterPage` carries a declared field from its URL as a
76
+ * hidden input (e.g. `?z=` for a channel tag). Values come from the client: validate them in the hook.
77
+ */
78
+ registrationFields: z
79
+ .array(z
80
+ .string()
81
+ .regex(REGISTRATION_FIELD_NAME, "must be a field name such as channel (a-z first, then letters, digits or _, at most 32)")
82
+ .refine((name) => !REGISTER_FORM_FIELDS.has(name), "is a field of the register form itself"))
83
+ .refine((names) => new Set(names).size === names.length, "must not repeat a name")
84
+ .default([]),
85
+ /** Registration needs a ticked consent checkbox. */
86
+ requireConsent: z.boolean().default(true),
87
+ /** Declared default of the `auth.registration_closed` switch. */
88
+ registrationClosed: z.boolean().default(false),
89
+ /** Role names the app checks besides `admin`, which is always declared. */
90
+ roles: z.array(z.string().regex(ROLE_NAME_PATTERN, "must be a role name such as editor (a-z, 0-9, _ or -, at most 32)")).default([]),
91
+ /**
92
+ * Initial admin list: while an email is listed, the account with that email holds `admin`.
93
+ * Auth does not verify emails, so create these accounts before deploying the list.
94
+ */
95
+ adminEmails: z.array(z.string().trim().toLowerCase().max(MAX_EMAIL_LENGTH).pipe(z.email("must be an email address"))).default([]),
96
+ passwordReset: z
97
+ .strictObject({
98
+ /** Delivers reset links. Without it password reset is off: no link, and its pages are not found. */
99
+ send: z.custom((value) => typeof value === "function", "must be a function").optional(),
100
+ /** How long a reset link works. */
101
+ ttlMinutes: z.number().int().min(5).max(1440).default(60),
102
+ })
103
+ .prefault({}),
104
+ /** Called after an account is created, in the same transaction (e.g. to store the consent). */
105
+ onRegistered: z.custom((value) => typeof value === "function", "must be a function").optional(),
106
+ /** Rewrites the path of every auth redirect (e.g. `tagRedirect` from `@softure-ai/analytics/next`). */
107
+ rewriteRedirect: z.custom((value) => typeof value === "function", "must be a function").optional(),
108
+ }).superRefine((options, context) => {
109
+ const legacy = options.legacySession?.cookieName;
110
+ const base = options.cookie.name;
111
+ if (legacy !== undefined && [base, `__Host-${base}`, `__Secure-${base}`].includes(legacy)) {
112
+ context.addIssue({ code: "custom", path: ["legacySession", "cookieName"], message: "must differ from the current session cookie's name" });
113
+ }
114
+ });
115
+ //# sourceMappingURL=options.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.js","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAE/C;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,CAAC;AAE3C,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,WAAW,GAAG,uBAAuB,CAAC;AAC5C,4EAA4E;AAC5E,MAAM,kBAAkB,GAAG,yBAAyB,CAAC;AACrD,MAAM,mBAAmB,GAAG,iBAAiB,CAAC;AAC9C,MAAM,uBAAuB,GAAG,2BAA2B,CAAC;AAC5D,0EAA0E;AAC1E,MAAM,oBAAoB,GAAwB,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,UAAU,EAAE,MAAM,EAAE,SAAS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B,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,UAAU,EAAE,oBAAoB,CAAC,CAAC,QAAQ,EAAE;IACjH,uGAAuG;IACvG,eAAe,EAAE,CAAC,CAAC,MAAM,CAAkB,CAAC,KAAK,EAAE,EAAE,CAAC,OAAO,KAAK,KAAK,UAAU,EAAE,oBAAoB,CAAC,CAAC,QAAQ,EAAE;CACpH,CAAC,CAAC,WAAW,CAAC,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE;IAClC,MAAM,MAAM,GAAG,OAAO,CAAC,aAAa,EAAE,UAAU,CAAC;IACjD,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC;IACjC,IAAI,MAAM,KAAK,SAAS,IAAI,CAAC,IAAI,EAAE,UAAU,IAAI,EAAE,EAAE,YAAY,IAAI,EAAE,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1F,OAAO,CAAC,QAAQ,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,eAAe,EAAE,YAAY,CAAC,EAAE,OAAO,EAAE,oDAAoD,EAAE,CAAC,CAAC;IAC7I,CAAC;AACH,CAAC,CAAC,CAAC"}
@@ -0,0 +1,21 @@
1
+ import type { Locale } from "@softure-ai/core";
2
+ import type { AuthUser } from "./contract.js";
3
+ export interface PasswordResetDetails {
4
+ /** When the link stops working. */
5
+ readonly expiresAt: Date;
6
+ /** The app's locale, for the copy of the message. */
7
+ readonly locale: Locale;
8
+ }
9
+ /**
10
+ * Delivers a reset link to `user`. The link is built on `appOrigin` (never on the request's Host);
11
+ * a multi-host app keeps its path and query and swaps the origin. It runs after the response is
12
+ * sent, so a slow or failing sender never shows on the request page. A thrown error is logged
13
+ * without the link.
14
+ */
15
+ export type PasswordResetSender = (link: string, user: AuthUser, details: PasswordResetDetails) => Promise<void>;
16
+ /**
17
+ * A development sender that prints the link to the server console. It refuses to run in
18
+ * production, where a reset link in the logs would be a live credential in the logs.
19
+ */
20
+ export declare const consolePasswordResetSender: PasswordResetSender;
21
+ //# sourceMappingURL=password-reset-sender.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password-reset-sender.d.ts","sourceRoot":"","sources":["../src/password-reset-sender.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAE9C,MAAM,WAAW,oBAAoB;IACnC,mCAAmC;IACnC,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAC;IACzB,qDAAqD;IACrD,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;CACzB;AAED;;;;;GAKG;AACH,MAAM,MAAM,mBAAmB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,oBAAoB,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEjH;;;GAGG;AACH,eAAO,MAAM,0BAA0B,EAAE,mBAQxC,CAAC"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * A development sender that prints the link to the server console. It refuses to run in
3
+ * production, where a reset link in the logs would be a live credential in the logs.
4
+ */
5
+ export const consolePasswordResetSender = (link, user, details) => {
6
+ if (process.env.NODE_ENV === "production") {
7
+ return Promise.reject(new Error("@softure-ai/auth: consolePasswordResetSender is for development only; pass a real sender to auth({ passwordReset: { send } })"));
8
+ }
9
+ console.info(`@softure-ai/auth: password reset link for ${user.email}, valid until ${details.expiresAt.toISOString()}: ${link}`);
10
+ return Promise.resolve();
11
+ };
12
+ //# sourceMappingURL=password-reset-sender.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"password-reset-sender.js","sourceRoot":"","sources":["../src/password-reset-sender.ts"],"names":[],"mappings":"AAoBA;;;GAGG;AACH,MAAM,CAAC,MAAM,0BAA0B,GAAwB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,EAAE;IACrF,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,EAAE,CAAC;QAC1C,OAAO,OAAO,CAAC,MAAM,CACnB,IAAI,KAAK,CAAC,+HAA+H,CAAC,CAC3I,CAAC;IACJ,CAAC;IACD,OAAO,CAAC,IAAI,CAAC,6CAA6C,IAAI,CAAC,KAAK,iBAAiB,OAAO,CAAC,SAAS,CAAC,WAAW,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC;IACjI,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;AAC3B,CAAC,CAAC"}
@@ -0,0 +1,14 @@
1
+ import type { SoftureConfig } from "@softure-ai/core";
2
+ export interface AuthGuardOptions {
3
+ /** Path prefixes that need a session; each matches whole segments (`/account` → `/account/…`). */
4
+ readonly protect: readonly string[];
5
+ /**
6
+ * Path prefixes that never need one, checked before `protect` (e.g. `protect: ["/"]` with the
7
+ * public pages here). `"/"` here is the home page only. The change-password route stays guarded.
8
+ */
9
+ readonly exclude?: readonly string[];
10
+ }
11
+ export type AuthGuard = (request: Request) => Response | null;
12
+ /** A guard that redirects to the login page (with `?next=`) for protected paths without a session cookie. */
13
+ export declare function createAuthGuard(config: SoftureConfig, options: AuthGuardOptions): AuthGuard;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAItD,MAAM,WAAW,gBAAgB;IAC/B,kGAAkG;IAClG,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC;;;OAGG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CACtC;AAKD,MAAM,MAAM,SAAS,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,GAAG,IAAI,CAAC;AAE9D,6GAA6G;AAC7G,wBAAgB,eAAe,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,EAAE,gBAAgB,GAAG,SAAS,CA8B3F"}
@@ -0,0 +1,59 @@
1
+ import { getAuthOptions, getAuthRoutes } from "../server/options.js";
2
+ import { getSessionCookie, readSessionToken } from "../session-cookie.js";
3
+ /** Mounted at a fixed path by the module (manifest `mount`); it answers `{ user: null }` without a session. */
4
+ const SESSION_ROUTE = "/api/auth/session";
5
+ /** A guard that redirects to the login page (with `?next=`) for protected paths without a session cookie. */
6
+ export function createAuthGuard(config, options) {
7
+ const routes = getAuthRoutes(config);
8
+ const cookieNames = [getSessionCookie(config).name, getAuthOptions(config).legacySession?.cookieName].filter((name) => name !== undefined);
9
+ const prefixes = options.protect.map((prefix) => normalizePrefix(prefix, "protected"));
10
+ const changePassword = normalizePrefix(routes.changePassword, "protected");
11
+ // Auth's own public pages: guarding them would send a visitor from the login page to itself.
12
+ const publicPaths = [routes.login, routes.register, routes.forgotPassword, routes.resetPassword, SESSION_ROUTE].map((path) => normalizePrefix(path, "excluded"));
13
+ const excluded = (options.exclude ?? []).map((prefix) => normalizePrefix(prefix, "excluded"));
14
+ const isGuarded = (path) => {
15
+ if (isUnder(path, changePassword))
16
+ return true;
17
+ if ([...publicPaths, ...excluded].some((prefix) => isExcludedBy(path, prefix)))
18
+ return false;
19
+ return prefixes.some((prefix) => isUnder(path, prefix));
20
+ };
21
+ return (request) => {
22
+ const url = new URL(request.url);
23
+ // Compared decoded and lowercased, so `/%61ccount` or `/ACCOUNT` (which a case-insensitive
24
+ // front proxy may route to /account) is guarded too. Undecodable paths are guarded.
25
+ const path = decodePath(url.pathname);
26
+ if (path !== null && !isGuarded(path))
27
+ return null;
28
+ const cookieHeader = request.headers.get("cookie");
29
+ if (cookieNames.some((name) => readSessionToken(cookieHeader, name) !== null))
30
+ return null;
31
+ // Built on appOrigin: behind a proxy the request URL may carry an internal host.
32
+ const login = new URL(routes.login, config.appOrigin);
33
+ login.searchParams.set("next", `${url.pathname}${url.search}`);
34
+ return Response.redirect(login, 307);
35
+ };
36
+ }
37
+ function normalizePrefix(prefix, kind) {
38
+ if (!prefix.startsWith("/")) {
39
+ throw new Error(`createAuthGuard: ${kind} path "${prefix}" must start with /`);
40
+ }
41
+ const trimmed = prefix.length > 1 && prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
42
+ return trimmed.toLowerCase();
43
+ }
44
+ function decodePath(pathname) {
45
+ try {
46
+ return decodeURIComponent(pathname).toLowerCase();
47
+ }
48
+ catch {
49
+ return null;
50
+ }
51
+ }
52
+ /** An exclusion of "/" is the home page: excluding every path would switch the guard off. */
53
+ function isExcludedBy(pathname, prefix) {
54
+ return prefix === "/" ? pathname === "/" : isUnder(pathname, prefix);
55
+ }
56
+ function isUnder(pathname, prefix) {
57
+ return prefix === "/" || pathname === prefix || pathname.startsWith(`${prefix}/`);
58
+ }
59
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/proxy/index.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC;AAY1E,+GAA+G;AAC/G,MAAM,aAAa,GAAG,mBAAmB,CAAC;AAI1C,6GAA6G;AAC7G,MAAM,UAAU,eAAe,CAAC,MAAqB,EAAE,OAAyB;IAC9E,MAAM,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;IACrC,MAAM,WAAW,GAAG,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,cAAc,CAAC,MAAM,CAAC,CAAC,aAAa,EAAE,UAAU,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,KAAK,SAAS,CAAC,CAAC;IAC3I,MAAM,QAAQ,GAAG,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,C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I,CAAC;IACd,CAAC;AACH,CAAC;AAED,6FAA6F;AAC7F,SAAS,YAAY,CAAC,QAAgB,EAAE,MAAc;IACpD,OAAO,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;AACvE,CAAC;AAED,SAAS,OAAO,CAAC,QAAgB,EAAE,MAAc;IAC/C,OAAO,MAAM,KAAK,GAAG,IAAI,QAAQ,KAAK,MAAM,IAAI,QAAQ,CAAC,UAAU,CAAC,GAAG,MAAM,GAAG,CAAC,CAAC;AACpF,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { type SoftureConfig } from "@softure-ai/core";
2
+ /**
3
+ * `path` as the app's `rewriteRedirect` returns it, or `path` itself without one or on failure.
4
+ * A page passes its own `searchParams`; an action passes none.
5
+ */
6
+ export declare function resolveRedirectTarget(config: SoftureConfig, path: string, searchParams?: URLSearchParams): Promise<string>;
7
+ //# sourceMappingURL=redirect-target.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirect-target.d.ts","sourceRoot":"","sources":["../src/redirect-target.ts"],"names":[],"mappings":"AAIA,OAAO,EAAiB,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAKrE;;;GAGG;AACH,wBAAsB,qBAAqB,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC,CAUhI"}
@@ -0,0 +1,25 @@
1
+ // Where auth redirects, from an action or from a page's render. The app's `rewriteRedirect` option
2
+ // may change auth's path (e.g. analytics' `tagRedirect` keeps the channel tag), but its result must
3
+ // still be a path on this app, and a rewrite that fails never blocks the redirect: auth's own path
4
+ // is used instead.
5
+ import { errorLogLabel } from "@softure-ai/core";
6
+ import { toSafeNextPath } from "./safe-next-path.js";
7
+ import { getAuthOptions } from "./server/options.js";
8
+ /**
9
+ * `path` as the app's `rewriteRedirect` returns it, or `path` itself without one or on failure.
10
+ * A page passes its own `searchParams`; an action passes none.
11
+ */
12
+ export async function resolveRedirectTarget(config, path, searchParams) {
13
+ const { rewriteRedirect } = getAuthOptions(config);
14
+ if (rewriteRedirect === undefined)
15
+ return path;
16
+ const ctx = searchParams === undefined ? { config } : { config, searchParams };
17
+ try {
18
+ return toSafeNextPath(await rewriteRedirect(path, ctx), path);
19
+ }
20
+ catch (error) {
21
+ console.error(`@softure-ai/auth: rewriting the redirect to ${path} failed: ${errorLogLabel(error)}`);
22
+ return path;
23
+ }
24
+ }
25
+ //# sourceMappingURL=redirect-target.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"redirect-target.js","sourceRoot":"","sources":["../src/redirect-target.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,oGAAoG;AACpG,mGAAmG;AACnG,mBAAmB;AACnB,OAAO,EAAE,aAAa,EAAsB,MAAM,kBAAkB,CAAC;AAErE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AACrD,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAErD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,MAAqB,EAAE,IAAY,EAAE,YAA8B;IAC7G,MAAM,EAAE,eAAe,EAAE,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC;IACnD,IAAI,eAAe,KAAK,SAAS;QAAE,OAAO,IAAI,CAAC;IAC/C,MAAM,GAAG,GAA2B,YAAY,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,EAAE,YAAY,EAAE,CAAC;IACvG,IAAI,CAAC;QACH,OAAO,cAAc,CAAC,MAAM,eAAe,CAAC,IAAI,EAAE,GAAG,CAAC,EAAE,IAAI,CAAC,CAAC;IAChE,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,+CAA+C,IAAI,YAAY,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACrG,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** The role every app has: admin-only surfaces ask for it. */
2
+ export declare const ADMIN_ROLE = "admin";
3
+ /** What a role name looks like; `migrations/0002_create_user_roles.sql` enforces the same shape. */
4
+ export declare const ROLE_NAME_PATTERN: RegExp;
5
+ //# sourceMappingURL=roles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.d.ts","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":"AAEA,8DAA8D;AAC9D,eAAO,MAAM,UAAU,UAAU,CAAC;AAElC,oGAAoG;AACpG,eAAO,MAAM,iBAAiB,QAA4B,CAAC"}
package/dist/roles.js ADDED
@@ -0,0 +1,6 @@
1
+ // Role names shared by the options, the server functions and the migration's CHECK constraint.
2
+ /** The role every app has: admin-only surfaces ask for it. */
3
+ export const ADMIN_ROLE = "admin";
4
+ /** What a role name looks like; `migrations/0002_create_user_roles.sql` enforces the same shape. */
5
+ export const ROLE_NAME_PATTERN = /^[a-z][a-z0-9_-]{0,31}$/;
6
+ //# sourceMappingURL=roles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.js","sourceRoot":"","sources":["../src/roles.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAE/F,8DAA8D;AAC9D,MAAM,CAAC,MAAM,UAAU,GAAG,OAAO,CAAC;AAElC,oGAAoG;AACpG,MAAM,CAAC,MAAM,iBAAiB,GAAG,yBAAyB,CAAC"}
@@ -0,0 +1,5 @@
1
+ /** Room for an OAuth authorize URL (state, PKCE challenge, resource, scope) under common URL limits. */
2
+ export declare const MAX_NEXT_PATH_LENGTH = 8192;
3
+ /** `candidate` when it is a same-origin path, else `fallback`. A path over the cap is logged by length. */
4
+ export declare function toSafeNextPath(candidate: unknown, fallback: string): string;
5
+ //# sourceMappingURL=safe-next-path.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-next-path.d.ts","sourceRoot":"","sources":["../src/safe-next-path.ts"],"names":[],"mappings":"AAGA,wGAAwG;AACxG,eAAO,MAAM,oBAAoB,OAAO,CAAC;AAEzC,2GAA2G;AAC3G,wBAAgB,cAAc,CAAC,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAa3E"}
@@ -0,0 +1,25 @@
1
+ // Where to go after login. `next` comes from the query string, so anyone can craft it: only a path
2
+ // on this app is accepted, never another origin (`//evil.example`, `/\evil.example`, `https://…`).
3
+ const BASE = "http://softure.invalid";
4
+ /** Room for an OAuth authorize URL (state, PKCE challenge, resource, scope) under common URL limits. */
5
+ export const MAX_NEXT_PATH_LENGTH = 8192;
6
+ /** `candidate` when it is a same-origin path, else `fallback`. A path over the cap is logged by length. */
7
+ export function toSafeNextPath(candidate, fallback) {
8
+ if (typeof candidate !== "string")
9
+ return fallback;
10
+ if (candidate.length > MAX_NEXT_PATH_LENGTH) {
11
+ // The length only: the path may carry tokens.
12
+ console.warn(`@softure-ai/auth: a next path of ${String(candidate.length)} characters is over the ${String(MAX_NEXT_PATH_LENGTH)} limit; using the fallback`);
13
+ return fallback;
14
+ }
15
+ if (!candidate.startsWith("/") || candidate.startsWith("//") || candidate.startsWith("/\\"))
16
+ return fallback;
17
+ // eslint-disable-next-line no-control-regex -- control characters are exactly what is refused
18
+ if (/[\u0000-\u001f\u007f\\]/.test(candidate))
19
+ return fallback;
20
+ const url = new URL(candidate, BASE);
21
+ if (url.origin !== BASE)
22
+ return fallback;
23
+ return `${url.pathname}${url.search}${url.hash}`;
24
+ }
25
+ //# sourceMappingURL=safe-next-path.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe-next-path.js","sourceRoot":"","sources":["../src/safe-next-path.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,mGAAmG;AACnG,MAAM,IAAI,GAAG,wBAAwB,CAAC;AACtC,wGAAwG;AACxG,MAAM,CAAC,MAAM,oBAAoB,GAAG,IAAI,CAAC;AAEzC,2GAA2G;AAC3G,MAAM,UAAU,cAAc,CAAC,SAAkB,EAAE,QAAgB;IACjE,IAAI,OAAO,SAAS,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC;IACnD,IAAI,SAAS,CAAC,MAAM,GAAG,oBAAoB,EAAE,CAAC;QAC5C,8CAA8C;QAC9C,OAAO,CAAC,IAAI,CAAC,oCAAoC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC,2BAA2B,MAAM,CAAC,oBAAoB,CAAC,4BAA4B,CAAC,CAAC;QAC9J,OAAO,QAAQ,CAAC;IAClB,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC;QAAE,OAAO,QAAQ,CAAC;IAC7G,8FAA8F;IAC9F,IAAI,yBAAyB,CAAC,IAAI,CAAC,SAAS,CAAC;QAAE,OAAO,QAAQ,CAAC;IAC/D,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACrC,IAAI,GAAG,CAAC,MAAM,KAAK,IAAI;QAAE,OAAO,QAAQ,CAAC;IACzC,OAAO,GAAG,GAAG,CAAC,QAAQ,GAAG,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;AACnD,CAAC"}