@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,52 @@
1
+ // The auth part of a GDPR export and deletion (`@softure-ai/privacy`): the account, its roles, its
2
+ // sessions and a pending reset link. Secrets never leave: no password hash and no token hash.
3
+ import { ok } from "@softure-ai/core";
4
+ import { asc, eq } from "drizzle-orm";
5
+ import { passwordResets, sessions, userRoles, users } from "../schema.js";
6
+ const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
7
+ const NO_DATA = { account: null, roles: [], sessions: [], passwordReset: null };
8
+ /** The user's auth data. An id that is not a UUID cannot be an account, so it has none. */
9
+ export async function exportAuthUserData(context, userId) {
10
+ if (!UUID.test(userId))
11
+ return ok(NO_DATA);
12
+ // Core types `db` as unknown; privacy passes the @softure-ai/db handle or its transaction.
13
+ const db = context.db;
14
+ const [account] = await db
15
+ .select({ id: users.id, email: users.email, createdAt: users.createdAt, passwordChangedAt: users.passwordChangedAt })
16
+ .from(users)
17
+ .where(eq(users.id, userId));
18
+ const roles = await db
19
+ .select({ role: userRoles.role, grantedAt: userRoles.grantedAt })
20
+ .from(userRoles)
21
+ .where(eq(userRoles.userId, userId))
22
+ .orderBy(asc(userRoles.role));
23
+ const userSessions = await db
24
+ .select({ createdAt: sessions.createdAt, expiresAt: sessions.expiresAt })
25
+ .from(sessions)
26
+ .where(eq(sessions.userId, userId))
27
+ .orderBy(asc(sessions.createdAt));
28
+ const [passwordReset] = await db
29
+ .select({ createdAt: passwordResets.createdAt, expiresAt: passwordResets.expiresAt })
30
+ .from(passwordResets)
31
+ .where(eq(passwordResets.userId, userId));
32
+ return ok({ account: account ?? null, roles, sessions: userSessions, passwordReset: passwordReset ?? null });
33
+ }
34
+ /**
35
+ * Deletes the account and everything auth keeps for it. The child rows go first, by name, so the
36
+ * deletion does not depend on the cascades alone; every session ends with them.
37
+ */
38
+ export async function deleteAuthUserData(context, userId) {
39
+ if (!UUID.test(userId))
40
+ return ok();
41
+ const db = context.db;
42
+ await db.delete(sessions).where(eq(sessions.userId, userId));
43
+ await db.delete(passwordResets).where(eq(passwordResets.userId, userId));
44
+ await db.delete(userRoles).where(eq(userRoles.userId, userId));
45
+ await db.delete(users).where(eq(users.id, userId));
46
+ return ok();
47
+ }
48
+ export const authPrivacyContributor = {
49
+ exportUserData: exportAuthUserData,
50
+ deleteUserData: deleteAuthUserData,
51
+ };
52
+ //# sourceMappingURL=privacy.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"privacy.js","sourceRoot":"","sources":["../../src/server/privacy.ts"],"names":[],"mappings":"AAAA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,MAAM,CAAC,CAAC,CAAC;IACzE,MAAM,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;IAC/D,MAAM,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC;IACnD,OAAO,EAAE,EAAE,CAAC;AACd,CAAC;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAuB;IACxD,cAAc,EAAE,kBAAkB;IAClC,cAAc,EAAE,kBAAkB;CACnC,CAAC"}
@@ -0,0 +1,22 @@
1
+ import { type SoftureConfig } from "@softure-ai/core";
2
+ export declare const BUCKETS: {
3
+ readonly register: "register";
4
+ readonly login: "login";
5
+ readonly loginAccount: "login-account";
6
+ readonly changePassword: "change-password";
7
+ readonly passwordReset: "password-reset";
8
+ readonly passwordResetAccount: "password-reset-account";
9
+ readonly passwordResetConfirm: "password-reset-confirm";
10
+ };
11
+ /** The subject key of an account's email for `login-account` and `password-reset-account`. */
12
+ export declare function emailSubjectKey(normalizedEmail: string): string;
13
+ /** The subject key of a user for `change-password`. */
14
+ export declare function userSubjectKey(userId: string): string;
15
+ /**
16
+ * Throws, naming every missing bucket, when the app's `security({ buckets })` lacks one auth
17
+ * counts in: the reset buckets only when password reset is on (`passwordReset.send`). Checked once
18
+ * per config, before the first attempt is counted, so a setup mistake is one clear error instead of
19
+ * a generic failure on every login.
20
+ */
21
+ export declare function assertAuthBuckets(config: SoftureConfig): void;
22
+ //# sourceMappingURL=rate-limits.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limits.d.ts","sourceRoot":"","sources":["../../src/server/rate-limits.ts"],"names":[],"mappings":"AAEA,OAAO,EAAa,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAIjE,eAAO,MAAM,OAAO;;;;;;;;CAQV,CAAC;AAEX,8FAA8F;AAC9F,wBAAgB,eAAe,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAE/D;AAED,uDAAuD;AACvD,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAErD;AAOD;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,GAAG,IAAI,CAa7D"}
@@ -0,0 +1,45 @@
1
+ // The rate limit buckets auth consumes (AUTH_RATE_LIMIT_BUCKETS). Each attempt is counted before the
2
+ // work it guards, hashing included, so a flood is stopped before it costs anything.
3
+ import { getModule } from "@softure-ai/core";
4
+ import { subjectKey } from "@softure-ai/security/server";
5
+ import { getAuthOptions } from "./options.js";
6
+ export const BUCKETS = {
7
+ register: "register",
8
+ login: "login",
9
+ loginAccount: "login-account",
10
+ changePassword: "change-password",
11
+ passwordReset: "password-reset",
12
+ passwordResetAccount: "password-reset-account",
13
+ passwordResetConfirm: "password-reset-confirm",
14
+ };
15
+ /** The subject key of an account's email for `login-account` and `password-reset-account`. */
16
+ export function emailSubjectKey(normalizedEmail) {
17
+ return subjectKey(`email:${normalizedEmail}`);
18
+ }
19
+ /** The subject key of a user for `change-password`. */
20
+ export function userSubjectKey(userId) {
21
+ return subjectKey(`user:${userId}`);
22
+ }
23
+ /** The buckets only password reset counts in; not needed while it is off. */
24
+ const RESET_BUCKETS = new Set([BUCKETS.passwordReset, BUCKETS.passwordResetAccount, BUCKETS.passwordResetConfirm]);
25
+ const checkedConfigs = new WeakSet();
26
+ /**
27
+ * Throws, naming every missing bucket, when the app's `security({ buckets })` lacks one auth
28
+ * counts in: the reset buckets only when password reset is on (`passwordReset.send`). Checked once
29
+ * per config, before the first attempt is counted, so a setup mistake is one clear error instead of
30
+ * a generic failure on every login.
31
+ */
32
+ export function assertAuthBuckets(config) {
33
+ if (checkedConfigs.has(config))
34
+ return;
35
+ const options = getModule(config, "security")?.options;
36
+ const configured = options?.buckets ?? {};
37
+ const isResetOn = getAuthOptions(config).passwordReset.send !== undefined;
38
+ const required = Object.values(BUCKETS).filter((name) => isResetOn || !RESET_BUCKETS.has(name));
39
+ const missing = required.filter((name) => !Object.hasOwn(configured, name));
40
+ if (missing.length > 0) {
41
+ throw new Error(`@softure-ai/auth: security({ buckets }) lacks ${missing.map((name) => `"${name}"`).join(", ")}; spread AUTH_RATE_LIMIT_BUCKETS into it`);
42
+ }
43
+ checkedConfigs.add(config);
44
+ }
45
+ //# sourceMappingURL=rate-limits.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rate-limits.js","sourceRoot":"","sources":["../../src/server/rate-limits.ts"],"names":[],"mappings":"AAAA,qGAAqG;AACrG,oFAAoF;AACpF,OAAO,EAAE,SAAS,EAAsB,MAAM,kBAAkB,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,QAAQ,EAAE,UAAU;IACpB,KAAK,EAAE,OAAO;IACd,YAAY,EAAE,eAAe;IAC7B,cAAc,EAAE,iBAAiB;IACjC,aAAa,EAAE,gBAAgB;IAC/B,oBAAoB,EAAE,wBAAwB;IAC9C,oBAAoB,EAAE,wBAAwB;CACtC,CAAC;AAEX,8FAA8F;AAC9F,MAAM,UAAU,eAAe,CAAC,eAAuB;IACrD,OAAO,UAAU,CAAC,SAAS,eAAe,EAAE,CAAC,CAAC;AAChD,CAAC;AAED,uDAAuD;AACvD,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,OAAO,UAAU,CAAC,QAAQ,MAAM,EAAE,CAAC,CAAC;AACtC,CAAC;AAED,6EAA6E;AAC7E,MAAM,aAAa,GAAwB,IAAI,GAAG,CAAC,CAAC,OAAO,CAAC,aAAa,EAAE,OAAO,CAAC,oBAAoB,EAAE,OAAO,CAAC,oBAAoB,CAAC,CAAC,CAAC;AAExI,MAAM,cAAc,GAAG,IAAI,OAAO,EAAiB,CAAC;AAEpD;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAAqB;IACrD,IAAI,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC;QAAE,OAAO;IACvC,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,OAAsE,CAAC;IACtH,MAAM,UAAU,GAAG,OAAO,EAAE,OAAO,IAAI,EAAE,CAAC;IAC1C,MAAM,SAAS,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,IAAI,KAAK,SAAS,CAAC;IAC1E,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC;IAChG,MAAM,OAAO,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC,CAAC;IAC5E,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,KAAK,CACb,iDAAiD,OAAO,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,0CAA0C,CACzI,CAAC;IACJ,CAAC;IACD,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,7 @@
1
+ import type { AuthContext } from "./sessions.js";
2
+ /**
3
+ * Whether `password` is the current password of the account. False for an unknown account. The
4
+ * caller counts the attempt in a rate limit bucket first: this is a password check like a login.
5
+ */
6
+ export declare function isCurrentPassword(ctx: AuthContext, userId: string, password: string): Promise<boolean>;
7
+ //# sourceMappingURL=reauthenticate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reauthenticate.d.ts","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAI5G"}
@@ -0,0 +1,16 @@
1
+ // Re-authentication before an action that cannot be undone, such as deleting the account: the
2
+ // signed-in user types their password again, so a session cookie alone is not enough.
3
+ import { eq } from "drizzle-orm";
4
+ import { users } from "../schema.js";
5
+ import { verifyPassword } from "./password.js";
6
+ /**
7
+ * Whether `password` is the current password of the account. False for an unknown account. The
8
+ * caller counts the attempt in a rate limit bucket first: this is a password check like a login.
9
+ */
10
+ export async function isCurrentPassword(ctx, userId, password) {
11
+ const [stored] = await ctx.db.select({ passwordHash: users.passwordHash }).from(users).where(eq(users.id, userId)).limit(1);
12
+ if (stored === undefined)
13
+ return false;
14
+ return verifyPassword(password, stored.passwordHash);
15
+ }
16
+ //# sourceMappingURL=reauthenticate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reauthenticate.js","sourceRoot":"","sources":["../../src/server/reauthenticate.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,sFAAsF;AACtF,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAG/C;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,iBAAiB,CAAC,GAAgB,EAAE,MAAc,EAAE,QAAgB;IACxF,MAAM,CAAC,MAAM,CAAC,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,KAAK,CAAC,YAAY,EAAE,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;IAC5H,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,KAAK,CAAC;IACvC,OAAO,cAAc,CAAC,QAAQ,EAAE,MAAM,CAAC,YAAY,CAAC,CAAC;AACvD,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { type Err, type Ok } from "@softure-ai/core";
2
+ import type { RateLimitRejection } from "@softure-ai/security";
3
+ import type { SignedIn } from "../contract.js";
4
+ import { type AuthContext } from "./sessions.js";
5
+ export interface RegisterInput {
6
+ readonly email: string;
7
+ readonly password: string;
8
+ /** Whether the consent checkbox was ticked. */
9
+ readonly hasConsented: boolean;
10
+ /** The client's rate limit key (`identifyClient`). */
11
+ readonly clientKey: string;
12
+ /** Values of the app's `registrationFields`; other names are dropped. */
13
+ readonly fields?: Readonly<Record<string, string>>;
14
+ }
15
+ export type RegisterErrorCode = "auth.registration_closed" | "auth.email_invalid" | "auth.email_taken" | "auth.password_too_short" | "auth.password_too_long" | "auth.consent_required";
16
+ export type RegisterResult = Ok<SignedIn> | Err<RegisterErrorCode> | RateLimitRejection;
17
+ /**
18
+ * Creates an account and signs it in. The input is checked first (cheap, and a typo should not
19
+ * spend the client's attempts), then the `register` bucket is counted, then the password is hashed.
20
+ * Database errors and errors thrown by the hook propagate after the rollback.
21
+ */
22
+ export declare function registerUser(ctx: AuthContext, input: RegisterInput): Promise<RegisterResult>;
23
+ //# sourceMappingURL=register.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.d.ts","sourceRoot":"","sources":["../../src/server/register.ts"],"names":[],"mappings":"AAEA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAK/C,OAAO,EAAiB,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAIhE,MAAM,WAAW,aAAa;IAC5B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,+CAA+C;IAC/C,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAC;IAC/B,sDAAsD;IACtD,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,yEAAyE;IACzE,QAAQ,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC;CACpD;AAKD,MAAM,MAAM,iBAAiB,GACzB,0BAA0B,GAC1B,oBAAoB,GACpB,kBAAkB,GAClB,yBAAyB,GACzB,wBAAwB,GACxB,uBAAuB,CAAC;AAE5B,MAAM,MAAM,cAAc,GAAG,EAAE,CAAC,QAAQ,CAAC,GAAG,GAAG,CAAC,iBAAiB,CAAC,GAAG,kBAAkB,CAAC;AAExF;;;;GAIG;AACH,wBAAsB,YAAY,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,CAkClG"}
@@ -0,0 +1,64 @@
1
+ // Registration: a new account with its first session, in one transaction with the app's
2
+ // `onRegistered` hook, so a consent the hook could not store never leaves an account behind.
3
+ import { err, ok } from "@softure-ai/core";
4
+ import { consumeRateLimit } from "@softure-ai/security/server";
5
+ import { users } from "../schema.js";
6
+ import { getAuthOptions } from "./options.js";
7
+ import { hashPassword } from "./password.js";
8
+ import { assertAuthBuckets, BUCKETS } from "./rate-limits.js";
9
+ import { createSession } from "./sessions.js";
10
+ import { isRegistrationClosed } from "./switches.js";
11
+ import { checkNewPassword, parseEmail } from "./validation.js";
12
+ /** Longest registration field value handed to the hook; longer ones are cut. */
13
+ const MAX_FIELD_LENGTH = 512;
14
+ /**
15
+ * Creates an account and signs it in. The input is checked first (cheap, and a typo should not
16
+ * spend the client's attempts), then the `register` bucket is counted, then the password is hashed.
17
+ * Database errors and errors thrown by the hook propagate after the rollback.
18
+ */
19
+ export async function registerUser(ctx, input) {
20
+ const options = getAuthOptions(ctx.config);
21
+ assertAuthBuckets(ctx.config);
22
+ if (await isRegistrationClosed(ctx))
23
+ return err("auth.registration_closed");
24
+ const email = parseEmail(input.email);
25
+ if (!email.ok)
26
+ return email;
27
+ const passwordPolicy = checkNewPassword(input.password, options.password.minLength);
28
+ // A fresh error, not the check's own result: CodeQL treats that result as password data and
29
+ // would follow it through the returned union to the session token (js/insufficient-password-hash).
30
+ if (!passwordPolicy.ok)
31
+ return err(passwordPolicy.error);
32
+ if (options.requireConsent && !input.hasConsented)
33
+ return err("auth.consent_required");
34
+ const limit = await consumeRateLimit(ctx, { bucket: BUCKETS.register, key: input.clientKey });
35
+ if (!limit.ok)
36
+ return limit;
37
+ const passwordHash = await hashPassword(input.password, options.password.scrypt);
38
+ const now = ctx.clock.now();
39
+ return ctx.db.transaction(async (tx) => {
40
+ const txCtx = { ...ctx, db: tx };
41
+ const [row] = await tx
42
+ .insert(users)
43
+ .values({ email: email.value, passwordHash, createdAt: now, passwordChangedAt: now })
44
+ .onConflictDoNothing({ target: users.email })
45
+ .returning();
46
+ if (row === undefined)
47
+ return err("auth.email_taken");
48
+ const user = { id: row.id, email: row.email, createdAt: row.createdAt };
49
+ const fields = pickRegistrationFields(options.registrationFields, input.fields);
50
+ await options.onRegistered?.({ user, consent: options.requireConsent ? { acceptedAt: now } : null, fields }, txCtx);
51
+ const session = await createSession(txCtx, user.id);
52
+ return ok({ user, session });
53
+ });
54
+ }
55
+ function pickRegistrationFields(declared, sent) {
56
+ const fields = {};
57
+ for (const name of declared) {
58
+ const value = sent !== undefined && Object.hasOwn(sent, name) ? sent[name] : undefined;
59
+ if (typeof value === "string" && value !== "")
60
+ fields[name] = value.slice(0, MAX_FIELD_LENGTH);
61
+ }
62
+ return fields;
63
+ }
64
+ //# sourceMappingURL=register.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"register.js","sourceRoot":"","sources":["../../src/server/register.ts"],"names":[],"mappings":"AAAA,wFAAwF;AACxF,6FAA6F;AAC7F,OAAO,EAAE,GAAG,EAAE,EAAE,EAAqB,MAAM,kBAAkB,CAAC;AAE9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAoB,MAAM,eAAe,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAa/D,gFAAgF;AAChF,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAY7B;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,YAAY,CAAC,GAAgB,EAAE,KAAoB;IACvE,MAAM,OAAO,GAAG,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3C,iBAAiB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC9B,IAAI,MAAM,oBAAoB,CAAC,GAAG,CAAC;QAAE,OAAO,GAAG,CAAC,0BAA0B,CAAC,CAAC;IAE5E,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IACtC,IAAI,CAAC,KAAK,CAAC,EAAE;QAAE,OAAO,KAAK,CAAC;IAC5B,MAAM,cAAc,GAAG,gBAAgB,CAAC,KAAK,CAAC,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IACpF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,KAAK,EAAE,SAAS,EAAE,GAAG,CAAC,SAAS,EAAE,CAAC;QAExE,MAAM,MAAM,GAAG,sBAAsB,CAAC,OAAO,CAAC,kBAAkB,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;QAChF,MAAM,OAAO,CAAC,YAAY,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,CAAC,CAAC;QACpH,MAAM,OAAO,GAAG,MAAM,aAAa,CAAC,KAAK,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC;QACpD,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;IAC/B,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAS,sBAAsB,CAAC,QAA2B,EAAE,IAAkD;IAC7G,MAAM,MAAM,GAA2B,EAAE,CAAC;IAC1C,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;QAC5B,MAAM,KAAK,GAAG,IAAI,KAAK,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;QACvF,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,EAAE;YAAE,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,gBAAgB,CAAC,CAAC;IACjG,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1,25 @@
1
+ import { type Err, type Ok, type SoftureConfig } from "@softure-ai/core";
2
+ import type { AuthUser } from "../contract.js";
3
+ import type { AuthContext } from "./sessions.js";
4
+ export type RoleErrorCode = "auth.role_undeclared" | "auth.role_already_granted" | "auth.role_not_granted";
5
+ export interface RoleChange {
6
+ readonly userId: string;
7
+ readonly role: string;
8
+ }
9
+ /** `admin` and the roles the app declared in `auth({ roles })`. */
10
+ export declare function getDeclaredRoles(config: SoftureConfig): ReadonlySet<string>;
11
+ export declare function isDeclaredRole(config: SoftureConfig, role: string): boolean;
12
+ /**
13
+ * Throws for a role the app never declared: a check for a mistyped role is a bug, and it must
14
+ * fail loudly instead of quietly denying (or, worse, allowing) everyone.
15
+ */
16
+ export declare function assertDeclaredRole(config: SoftureConfig, role: string): void;
17
+ /** Whether `adminEmails` makes this account an admin. */
18
+ export declare function isConfiguredAdmin(config: SoftureConfig, user: Pick<AuthUser, "email">): boolean;
19
+ /** Every role the user holds: its rows, and `admin` when its email is in `adminEmails`. */
20
+ export declare function findUserRoles(ctx: AuthContext, user: Pick<AuthUser, "id" | "email">): Promise<ReadonlySet<string>>;
21
+ /** Stores a declared role for a user. An unknown user id is a foreign key error and propagates. */
22
+ export declare function grantRole(ctx: AuthContext, change: RoleChange): Promise<Ok<undefined> | Err<"auth.role_undeclared" | "auth.role_already_granted">>;
23
+ /** Deletes a stored role. It cannot take away an `admin` that comes from `adminEmails`. */
24
+ export declare function revokeRole(ctx: AuthContext, change: RoleChange): Promise<Ok<undefined> | Err<"auth.role_not_granted">>;
25
+ //# sourceMappingURL=roles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.d.ts","sourceRoot":"","sources":["../../src/server/roles.ts"],"names":[],"mappings":"AAEA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAElF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAI/C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAEjD,MAAM,MAAM,aAAa,GAAG,sBAAsB,GAAG,2BAA2B,GAAG,uBAAuB,CAAC;AAE3G,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACvB;AAED,mEAAmE;AACnE,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,aAAa,GAAG,WAAW,CAAC,MAAM,CAAC,CAE3E;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAE3E;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAK5E;AAED,yDAAyD;AACzD,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,aAAa,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,GAAG,OAAO,CAE/F;AAED,2FAA2F;AAC3F,wBAAsB,aAAa,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,EAAE,IAAI,CAAC,QAAQ,EAAE,IAAI,GAAG,OAAO,CAAC,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAKxH;AAED,mGAAmG;AACnG,wBAAsB,SAAS,CAC7B,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,UAAU,GACjB,OAAO,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,sBAAsB,GAAG,2BAA2B,CAAC,CAAC,CAQpF;AAED,2FAA2F;AAC3F,wBAAsB,UAAU,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,uBAAuB,CAAC,CAAC,CAM5H"}
@@ -0,0 +1,56 @@
1
+ // Roles: rows of auth.user_roles, plus `admin` for the accounts listed in `adminEmails`. Nothing is
2
+ // granted by default, so with no admin configured every admin-only surface stays closed.
3
+ import { err, ok } from "@softure-ai/core";
4
+ import { and, eq } from "drizzle-orm";
5
+ import { ADMIN_ROLE } from "../roles.js";
6
+ import { userRoles } from "../schema.js";
7
+ import { getAuthOptions } from "./options.js";
8
+ /** `admin` and the roles the app declared in `auth({ roles })`. */
9
+ export function getDeclaredRoles(config) {
10
+ return new Set([ADMIN_ROLE, ...getAuthOptions(config).roles]);
11
+ }
12
+ export function isDeclaredRole(config, role) {
13
+ return getDeclaredRoles(config).has(role);
14
+ }
15
+ /**
16
+ * Throws for a role the app never declared: a check for a mistyped role is a bug, and it must
17
+ * fail loudly instead of quietly denying (or, worse, allowing) everyone.
18
+ */
19
+ export function assertDeclaredRole(config, role) {
20
+ if (!isDeclaredRole(config, role)) {
21
+ const declared = [...getDeclaredRoles(config)].join(", ");
22
+ throw new Error(`@softure-ai/auth: role "${role}" is not declared (declared: ${declared}); add it to auth({ roles }) in softure.config.ts`);
23
+ }
24
+ }
25
+ /** Whether `adminEmails` makes this account an admin. */
26
+ export function isConfiguredAdmin(config, user) {
27
+ return getAuthOptions(config).adminEmails.includes(user.email);
28
+ }
29
+ /** Every role the user holds: its rows, and `admin` when its email is in `adminEmails`. */
30
+ export async function findUserRoles(ctx, user) {
31
+ const rows = await ctx.db.select({ role: userRoles.role }).from(userRoles).where(eq(userRoles.userId, user.id));
32
+ const roles = new Set(rows.map((row) => row.role));
33
+ if (isConfiguredAdmin(ctx.config, user))
34
+ roles.add(ADMIN_ROLE);
35
+ return roles;
36
+ }
37
+ /** Stores a declared role for a user. An unknown user id is a foreign key error and propagates. */
38
+ export async function grantRole(ctx, change) {
39
+ if (!isDeclaredRole(ctx.config, change.role))
40
+ return err("auth.role_undeclared");
41
+ const inserted = await ctx.db
42
+ .insert(userRoles)
43
+ .values({ userId: change.userId, role: change.role, grantedAt: ctx.clock.now() })
44
+ .onConflictDoNothing()
45
+ .returning();
46
+ return inserted.length === 0 ? err("auth.role_already_granted") : ok();
47
+ }
48
+ /** Deletes a stored role. It cannot take away an `admin` that comes from `adminEmails`. */
49
+ export async function revokeRole(ctx, change) {
50
+ const deleted = await ctx.db
51
+ .delete(userRoles)
52
+ .where(and(eq(userRoles.userId, change.userId), eq(userRoles.role, change.role)))
53
+ .returning();
54
+ return deleted.length === 0 ? err("auth.role_not_granted") : ok();
55
+ }
56
+ //# sourceMappingURL=roles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roles.js","sourceRoot":"","sources":["../../src/server/roles.ts"],"names":[],"mappings":"AAAA,oGAAoG;AACpG,yFAAyF;AACzF,OAAO,EAAE,GAAG,EAAE,EAAE,EAAyC,MAAM,kBAAkB,CAAC;AAClF,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEtC,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAU9C,mEAAmE;AACnE,MAAM,UAAU,gBAAgB,CAAC,MAAqB;IACpD,OAAO,IAAI,GAAG,CAAC,CAAC,UAAU,EAAE,GAAG,cAAc,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,MAAqB,EAAE,IAAY;IAChE,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAC5C,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAAC,MAAqB,EAAE,IAAY;IACpE,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,IAAI,CAAC,EAAE,CAAC;QAClC,MAAM,QAAQ,GAAG,CAAC,GAAG,gBAAgB,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QAC1D,MAAM,IAAI,KAAK,CAAC,2BAA2B,IAAI,gCAAgC,QAAQ,mDAAmD,CAAC,CAAC;IAC9I,CAAC;AACH,CAAC;AAED,yDAAyD;AACzD,MAAM,UAAU,iBAAiB,CAAC,MAAqB,EAAE,IAA6B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AAC,CAAC,GAAG,CAAC,2BAA2B,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AACzE,CAAC;AAED,2FAA2F;AAC3F,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,GAAgB,EAAE,MAAkB;IACnE,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,EAAE;SACzB,MAAM,CAAC,SAAS,CAAC;SACjB,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;SAChF,SAAS,EAAE,CAAC;IACf,OAAO,OAAO,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC;AACpE,CAAC"}
@@ -0,0 +1,12 @@
1
+ export declare function createSessionToken(): {
2
+ readonly token: string;
3
+ readonly tokenHash: string;
4
+ };
5
+ export declare function hashSessionToken(token: string): string;
6
+ /**
7
+ * Whether a cookie value could be a token at all; anything else is not looked up. `legacyPattern`
8
+ * (from `legacySession.tokenPattern`, anchored and without the g or y flag) admits an adopted
9
+ * system's tokens, stored as the same sha256 hex.
10
+ */
11
+ export declare function isSessionTokenShape(token: string, legacyPattern?: RegExp): boolean;
12
+ //# sourceMappingURL=session-token.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-token.d.ts","sourceRoot":"","sources":["../../src/server/session-token.ts"],"names":[],"mappings":"AAQA,wBAAgB,kBAAkB,IAAI;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAG3F;AAED,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEtD;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAElF"}
@@ -0,0 +1,22 @@
1
+ // Session tokens: 32 random bytes in the cookie, only their sha256 in the database. A stolen
2
+ // database row cannot be replayed as a cookie.
3
+ import { createHash, randomBytes } from "node:crypto";
4
+ const TOKEN_BYTES = 32;
5
+ /** base64url of 32 bytes. */
6
+ const TOKEN_SHAPE = /^[A-Za-z0-9_-]{43}$/;
7
+ export function createSessionToken() {
8
+ const token = randomBytes(TOKEN_BYTES).toString("base64url");
9
+ return { token, tokenHash: hashSessionToken(token) };
10
+ }
11
+ export function hashSessionToken(token) {
12
+ return createHash("sha256").update(token, "utf8").digest("hex");
13
+ }
14
+ /**
15
+ * Whether a cookie value could be a token at all; anything else is not looked up. `legacyPattern`
16
+ * (from `legacySession.tokenPattern`, anchored and without the g or y flag) admits an adopted
17
+ * system's tokens, stored as the same sha256 hex.
18
+ */
19
+ export function isSessionTokenShape(token, legacyPattern) {
20
+ return TOKEN_SHAPE.test(token) || (legacyPattern?.test(token) ?? false);
21
+ }
22
+ //# sourceMappingURL=session-token.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"session-token.js","sourceRoot":"","sources":["../../src/server/session-token.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,+CAA+C;AAC/C,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAEtD,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,6BAA6B;AAC7B,MAAM,WAAW,GAAG,qBAAqB,CAAC;AAE1C,MAAM,UAAU,kBAAkB;IAChC,MAAM,KAAK,GAAG,WAAW,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC7D,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,gBAAgB,CAAC,KAAK,CAAC,EAAE,CAAC;AACvD,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,KAAa;IAC5C,OAAO,UAAU,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;AAClE,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,mBAAmB,CAAC,KAAa,EAAE,aAAsB;IACvE,OAAO,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,24 @@
1
+ import type { ModuleContext } from "@softure-ai/core";
2
+ import type { Queryable } from "@softure-ai/db";
3
+ import type { AuthUser, NewSession } from "../contract.js";
4
+ export type AuthContext = ModuleContext<Queryable>;
5
+ /** Opens a session for `userId`; the returned token goes into the cookie. */
6
+ export declare function createSession(ctx: AuthContext, userId: string): Promise<NewSession>;
7
+ /** The user of a live session, or null for an unknown, malformed or expired token. */
8
+ export declare function findSessionUser(ctx: AuthContext, token: string): Promise<AuthUser | null>;
9
+ /** Ends one session. An unknown token is already logged out, so it is not an error. */
10
+ export declare function logoutSession(ctx: AuthContext, token: string): Promise<void>;
11
+ /** Deletes every expired session; for a scheduled job. Returns how many were deleted. */
12
+ export declare function pruneSessions(ctx: AuthContext): Promise<number>;
13
+ /** Deletes the expired sessions of one user (at their login, so rows do not pile up without a job). */
14
+ export declare function deleteExpiredSessions(ctx: AuthContext, userId: string): Promise<void>;
15
+ export interface RevokeUserSessionsOptions {
16
+ /** A session token to keep, e.g. the one of the admin acting on their own account. */
17
+ readonly except?: string;
18
+ }
19
+ /**
20
+ * Ends every session of `userId` (all but `except`), e.g. after an account recovery or a suspected
21
+ * leak; each device must log in again. Returns how many sessions were ended.
22
+ */
23
+ export declare function revokeUserSessions(ctx: AuthContext, userId: string, options?: RevokeUserSessionsOptions): Promise<number>;
24
+ //# sourceMappingURL=sessions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessions.d.ts","sourceRoot":"","sources":["../../src/server/sessions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAK3D,MAAM,MAAM,WAAW,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;AAInD,6EAA6E;AAC7E,wBAAsB,aAAa,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAMzF;AAED,sFAAsF;AACtF,wBAAsB,eAAe,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CAS/F;AAED,uFAAuF;AACvF,wBAAsB,aAAa,CAAC,GAAG,EAAE,WAAW,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAGlF;AAED,yFAAyF;AACzF,wBAAsB,aAAa,CAAC,GAAG,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,CAGrE;AAED,uGAAuG;AACvG,wBAAsB,qBAAqB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3F;AAED,MAAM,WAAW,yBAAyB;IACxC,sFAAsF;IACtF,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,GAAG,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,yBAA8B,GAAG,OAAO,CAAC,MAAM,CAAC,CAInI"}
@@ -0,0 +1,50 @@
1
+ import { and, eq, gt, lte, ne } from "drizzle-orm";
2
+ import { sessions, users } from "../schema.js";
3
+ import { getAuthOptions, getLegacyTokenPattern } from "./options.js";
4
+ import { createSessionToken, hashSessionToken, isSessionTokenShape } from "./session-token.js";
5
+ const DAY_MS = 24 * 60 * 60 * 1000;
6
+ /** Opens a session for `userId`; the returned token goes into the cookie. */
7
+ export async function createSession(ctx, userId) {
8
+ const now = ctx.clock.now();
9
+ const expiresAt = new Date(now.getTime() + getAuthOptions(ctx.config).session.ttlDays * DAY_MS);
10
+ const { token, tokenHash } = createSessionToken();
11
+ await ctx.db.insert(sessions).values({ tokenHash, userId, createdAt: now, expiresAt });
12
+ return { token, expiresAt };
13
+ }
14
+ /** The user of a live session, or null for an unknown, malformed or expired token. */
15
+ export async function findSessionUser(ctx, token) {
16
+ if (!isSessionTokenShape(token, getLegacyTokenPattern(ctx.config)))
17
+ return null;
18
+ const [row] = await ctx.db
19
+ .select({ id: users.id, email: users.email, createdAt: users.createdAt })
20
+ .from(sessions)
21
+ .innerJoin(users, eq(users.id, sessions.userId))
22
+ .where(and(eq(sessions.tokenHash, hashSessionToken(token)), gt(sessions.expiresAt, ctx.clock.now())))
23
+ .limit(1);
24
+ return row ?? null;
25
+ }
26
+ /** Ends one session. An unknown token is already logged out, so it is not an error. */
27
+ export async function logoutSession(ctx, token) {
28
+ if (!isSessionTokenShape(token, getLegacyTokenPattern(ctx.config)))
29
+ return;
30
+ await ctx.db.delete(sessions).where(eq(sessions.tokenHash, hashSessionToken(token)));
31
+ }
32
+ /** Deletes every expired session; for a scheduled job. Returns how many were deleted. */
33
+ export async function pruneSessions(ctx) {
34
+ const deleted = await ctx.db.delete(sessions).where(lte(sessions.expiresAt, ctx.clock.now())).returning();
35
+ return deleted.length;
36
+ }
37
+ /** Deletes the expired sessions of one user (at their login, so rows do not pile up without a job). */
38
+ export async function deleteExpiredSessions(ctx, userId) {
39
+ await ctx.db.delete(sessions).where(and(eq(sessions.userId, userId), lte(sessions.expiresAt, ctx.clock.now())));
40
+ }
41
+ /**
42
+ * Ends every session of `userId` (all but `except`), e.g. after an account recovery or a suspected
43
+ * leak; each device must log in again. Returns how many sessions were ended.
44
+ */
45
+ export async function revokeUserSessions(ctx, userId, options = {}) {
46
+ const kept = options.except === undefined ? undefined : ne(sessions.tokenHash, hashSessionToken(options.except));
47
+ const deleted = await ctx.db.delete(sessions).where(and(eq(sessions.userId, userId), kept)).returning();
48
+ return deleted.length;
49
+ }
50
+ //# sourceMappingURL=sessions.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessions.js","sourceRoot":"","sources":["../../src/server/sessions.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAEnD,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAC/C,OAAO,EAAE,cAAc,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAI/F,MAAM,MAAM,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEnC,6EAA6E;AAC7E,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAgB,EAAE,MAAc;IAClE,MAAM,GAAG,GAAG,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;IAC5B,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,GAAG,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;IAChG,MAAM,EAAE,KAAK,EAAE,SAAS,EAAE,GAAG,kBAAkB,EAAE,CAAC;IAClD,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,SAAS,EAAE,GAAG,EAAE,SAAS,EAAE,CAAC,CAAC;IACvF,OAAO,EAAE,KAAK,EAAE,SAAS,EAA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AClD,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAC1G,OAAO,OAAO,CAAC,MAAM,CAAC;AACxB,CAAC;AAED,uGAAuG;AACvG,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,GAAgB,EAAE,MAAc;IAC1E,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,GAAG,CAAC,QAAQ,CAAC,SAAS,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;AAClH,CAAC;AAOD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,GAAgB,EAAE,MAAc,EAAE,UAAqC,EAAE;IAChH,MAAM,IAAI,GAAG,OAAO,CAAC,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC;IACjH,MAAM,OAAO,GAAG,MAAM,GAAG,CAAC,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE,CAAC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,IAAI,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IACxG,OAAO,OAAO,CAAC,MAAM,CAAC;AACxB,CAAC"}
@@ -0,0 +1,9 @@
1
+ import { type ModuleContext } from "@softure-ai/core";
2
+ /**
3
+ * Whether registration is closed. The app's switch provider answers first (env override, stored
4
+ * value, default and fail mode as it defines them). Without one, the env override wins over the
5
+ * declared default; a value it cannot read closes registration (fail closed) and is logged once, by
6
+ * name, never by value.
7
+ */
8
+ export declare function isRegistrationClosed(ctx: ModuleContext, env?: Readonly<Record<string, string | undefined>>): Promise<boolean>;
9
+ //# sourceMappingURL=switches.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"switches.d.ts","sourceRoot":"","sources":["../../src/server/switches.ts"],"names":[],"mappings":"AAIA,OAAO,EAAc,KAAK,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAQlE;;;;;GAKG;AACH,wBAAsB,oBAAoB,CAAC,GAAG,EAAE,aAAa,EAAE,GAAG,GAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAe,GAAG,OAAO,CAAC,OAAO,CAAC,CAYhJ"}
@@ -0,0 +1,34 @@
1
+ // Runtime switches of the module. `auth.registration_closed` is read through the switch-reader
2
+ // contract of `@softure-ai/core`: when the app defines it in `@softure-ai/feature-switches`, the
3
+ // value stored in the switches panel applies. Otherwise auth falls back to its declared default (an
4
+ // option) with an env override, so an app without feature-switches keeps working.
5
+ import { readSwitch } from "@softure-ai/core";
6
+ import { REGISTRATION_CLOSED_ENV, REGISTRATION_CLOSED_SWITCH } from "../index.js";
7
+ import { getAuthOptions } from "./options.js";
8
+ const ON = new Set(["true", "1"]);
9
+ let hasReportedBadValue = false;
10
+ const OFF = new Set(["false", "0"]);
11
+ /**
12
+ * Whether registration is closed. The app's switch provider answers first (env override, stored
13
+ * value, default and fail mode as it defines them). Without one, the env override wins over the
14
+ * declared default; a value it cannot read closes registration (fail closed) and is logged once, by
15
+ * name, never by value.
16
+ */
17
+ export async function isRegistrationClosed(ctx, env = process.env) {
18
+ const reading = await readSwitch(ctx, REGISTRATION_CLOSED_SWITCH);
19
+ if (reading.kind === "value")
20
+ return reading.isEnabled;
21
+ const override = env[REGISTRATION_CLOSED_ENV]?.trim().toLowerCase();
22
+ if (override === undefined || override === "")
23
+ return getAuthOptions(ctx.config).registrationClosed;
24
+ if (ON.has(override))
25
+ return true;
26
+ if (OFF.has(override))
27
+ return false;
28
+ if (!hasReportedBadValue) {
29
+ hasReportedBadValue = true;
30
+ console.error(`@softure-ai/auth: ${REGISTRATION_CLOSED_ENV} must be true, false, 1 or 0; registration stays closed`);
31
+ }
32
+ return true;
33
+ }
34
+ //# sourceMappingURL=switches.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"switches.js","sourceRoot":"","sources":["../../src/server/switches.ts"],"names":[],"mappings":"AAAA,+FAA+F;AAC/F,iGAAiG;AACjG,oGAAoG;AACpG,kFAAkF;AAClF,OAAO,EAAE,UAAU,EAAsB,MAAM,kBAAkB,CAAC;AAClE,OAAO,EAAE,uBAAuB,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,EAAE,GAAG,IAAI,GAAG,CAAC,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC,CAAC;AAClC,IAAI,mBAAmB,GAAG,KAAK,CAAC;AAChC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC,CAAC;AAEpC;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,GAAkB,EAAE,MAAoD,OAAO,CAAC,GAAG;IAC5H,MAAM,OAAO,GAAG,MAAM,UAAU,CAAC,GAAG,EAAE,0BAA0B,CAAC,CAAC;IAClE,IAAI,OAAO,CAAC,IAAI,KAAK,OAAO;QAAE,OAAO,OAAO,CAAC,SAAS,CAAC;IACvD,MAAM,QAAQ,GAAG,GAAG,CAAC,uBAAuB,CAAC,EAAE,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;IACpE,IAAI,QAAQ,KAAK,SAAS,IAAI,QAAQ,KAAK,EAAE;QAAE,OAAO,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,kBAAkB,CAAC;IACpG,IAAI,EAAE,CAAC,GAAG,CAAC,QAAQ,CAAC;QAAE,OAAO,IAAI,CAAC;IAClC,IAAI,GAAG,CAAC,GAAG,CAAC,QAAQ,CAAC;QAAE,OAAO,KAAK,CAAC;IACpC,IAAI,CAAC,mBAAmB,EAAE,CAAC;QACzB,mBAAmB,GAAG,IAAI,CAAC;QAC3B,OAAO,CAAC,KAAK,CAAC,qBAAqB,uBAAuB,yDAAyD,CAAC,CAAC;IACvH,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { type Err, type Ok } from "@softure-ai/core";
2
+ /** Trims and lowercases an email, so two spellings are one account. */
3
+ export declare function normalizeEmail(email: string): string;
4
+ /** The normalized email, or `auth.email_invalid`. */
5
+ export declare function parseEmail(email: string): Ok<string> | Err<"auth.email_invalid">;
6
+ /** A password's length as the policy counts it: characters after NFC, not UTF-16 units. */
7
+ export declare function getPasswordLength(password: string): number;
8
+ /** Whether a new password fits the policy. */
9
+ export declare function checkNewPassword(password: string, minLength: number): Ok<undefined> | Err<"auth.password_too_short" | "auth.password_too_long">;
10
+ //# sourceMappingURL=validation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/server/validation.ts"],"names":[],"mappings":"AACA,OAAO,EAAW,KAAK,GAAG,EAAE,KAAK,EAAE,EAAE,MAAM,kBAAkB,CAAC;AAO9D,uEAAuE;AACvE,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED,qDAAqD;AACrD,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,EAAE,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,oBAAoB,CAAC,CAMhF;AAED,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE1D;AAED,8CAA8C;AAC9C,wBAAgB,gBAAgB,CAC9B,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,GAChB,EAAE,CAAC,SAAS,CAAC,GAAG,GAAG,CAAC,yBAAyB,GAAG,wBAAwB,CAAC,CAK3E"}
@@ -0,0 +1,32 @@
1
+ // Input rules for emails and new passwords. Expected failures are codes the form shows next to the field.
2
+ import { err, ok } from "@softure-ai/core";
3
+ import { z } from "zod";
4
+ import { MAX_PASSWORD_LENGTH } from "./password.js";
5
+ const MAX_EMAIL_LENGTH = 254;
6
+ const emailSchema = z.email();
7
+ /** Trims and lowercases an email, so two spellings are one account. */
8
+ export function normalizeEmail(email) {
9
+ return email.trim().toLowerCase();
10
+ }
11
+ /** The normalized email, or `auth.email_invalid`. */
12
+ export function parseEmail(email) {
13
+ const normalized = normalizeEmail(email);
14
+ if (normalized.length > MAX_EMAIL_LENGTH || !emailSchema.safeParse(normalized).success) {
15
+ return err("auth.email_invalid");
16
+ }
17
+ return ok(normalized);
18
+ }
19
+ /** A password's length as the policy counts it: characters after NFC, not UTF-16 units. */
20
+ export function getPasswordLength(password) {
21
+ return [...password.normalize("NFC")].length;
22
+ }
23
+ /** Whether a new password fits the policy. */
24
+ export function checkNewPassword(password, minLength) {
25
+ const length = getPasswordLength(password);
26
+ if (length < minLength)
27
+ return err("auth.password_too_short");
28
+ if (length > MAX_PASSWORD_LENGTH)
29
+ return err("auth.password_too_long");
30
+ return ok();
31
+ }
32
+ //# sourceMappingURL=validation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/server/validation.ts"],"names":[],"mappings":"AAAA,0GAA0G;AAC1G,OAAO,EAAE,GAAG,EAAE,EAAE,EAAqB,MAAM,kBAAkB,CAAC;AAC9D,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AACxB,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD,MAAM,gBAAgB,GAAG,GAAG,CAAC;AAC7B,MAAM,WAAW,GAAG,CA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