@lunora/auth 1.0.0-alpha.14 → 1.0.0-alpha.141

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 (76) hide show
  1. package/LICENSE.md +38 -0
  2. package/README.md +99 -10
  3. package/dist/adapter.d.mts +4 -43
  4. package/dist/adapter.d.ts +4 -43
  5. package/dist/adapter.mjs +1 -48
  6. package/dist/audit.d.mts +131 -0
  7. package/dist/audit.d.ts +131 -0
  8. package/dist/audit.mjs +12 -0
  9. package/dist/email-guard.d.mts +127 -0
  10. package/dist/email-guard.d.ts +127 -0
  11. package/dist/email-guard.mjs +1 -0
  12. package/dist/index.d.mts +801 -157
  13. package/dist/index.d.ts +801 -157
  14. package/dist/index.mjs +1 -12
  15. package/dist/middleware.d.mts +167 -156
  16. package/dist/middleware.d.ts +167 -156
  17. package/dist/middleware.mjs +1 -53
  18. package/dist/packem_shared/AUTH_DO_AUDIT_PATH-th2-Hb30.mjs +1 -0
  19. package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DneiLGpv.mjs +1 -0
  20. package/dist/packem_shared/LunoraAuthAdminError-4r29Kj_y.mjs +1 -0
  21. package/dist/packem_shared/adapter-CG0Yv85f.mjs +2 -0
  22. package/dist/packem_shared/adapter.d-BW_ovHul.d.mts +89 -0
  23. package/dist/packem_shared/adapter.d-BWw-tC_U.d.ts +89 -0
  24. package/dist/packem_shared/authAuditHook-C9Gt2iYp.mjs +1 -0
  25. package/dist/packem_shared/authDoColumnAdditions-UwF3Rl41.mjs +1 -0
  26. package/dist/packem_shared/compileMigrationsSql-Ds2snsWg.mjs +1 -0
  27. package/dist/packem_shared/create-auth.d-D1CKOFWc.d.mts +142 -0
  28. package/dist/packem_shared/create-auth.d-D1CKOFWc.d.ts +142 -0
  29. package/dist/packem_shared/createAuth-8aUw8v8D.mjs +1 -0
  30. package/dist/packem_shared/createDoAuthWiring-C8BRUjf6.mjs +1 -0
  31. package/dist/packem_shared/createLunoraAuthClient-DhjhCwmM.mjs +1 -0
  32. package/dist/packem_shared/createSignUpInvitation-ClvskwuB.mjs +1 -0
  33. package/dist/packem_shared/emailGateDatabaseHooks-CAygR3iq.mjs +1 -0
  34. package/dist/packem_shared/invite-only.d-CsPJWAwM.d.ts +126 -0
  35. package/dist/packem_shared/invite-only.d-DKyHHkkQ.d.mts +126 -0
  36. package/dist/packem_shared/legacyIssuerCleanupStatements-D9Kf8OD1.mjs +1 -0
  37. package/dist/packem_shared/on-cloudflare-edge-Dx6gFjBO.mjs +1 -0
  38. package/dist/packem_shared/quote-identifier-CGiYFBvY.mjs +1 -0
  39. package/dist/packem_shared/sessionPresets-C867Mlo4.mjs +1 -0
  40. package/dist/packem_shared/sql-store-CYa4HJsO.mjs +1 -0
  41. package/dist/packem_shared/uiConfig-B8pN6pks.mjs +1 -0
  42. package/dist/plugins-client.d.mts +165 -1
  43. package/dist/plugins-client.d.ts +165 -1
  44. package/dist/plugins-client.mjs +1 -2
  45. package/dist/plugins-enterprise-client.d.mts +15 -0
  46. package/dist/plugins-enterprise-client.d.ts +15 -0
  47. package/dist/plugins-enterprise-client.mjs +1 -0
  48. package/dist/plugins-enterprise.d.mts +1670 -0
  49. package/dist/plugins-enterprise.d.ts +1670 -0
  50. package/dist/plugins-enterprise.mjs +1 -0
  51. package/dist/plugins.d.mts +125 -2
  52. package/dist/plugins.d.ts +125 -2
  53. package/dist/plugins.mjs +1 -22
  54. package/dist/schema.d.mts +39 -39
  55. package/dist/schema.d.ts +39 -39
  56. package/dist/schema.mjs +1 -62
  57. package/dist/sql-store.d.mts +28 -28
  58. package/dist/sql-store.d.ts +28 -28
  59. package/dist/sql-store.mjs +1 -184
  60. package/dist/store.d.mts +65 -47
  61. package/dist/store.d.ts +65 -47
  62. package/dist/store.mjs +1 -183
  63. package/dist/turnstile-middleware.d.mts +73 -55
  64. package/dist/turnstile-middleware.d.ts +73 -55
  65. package/dist/turnstile-middleware.mjs +1 -45
  66. package/dist/turnstile.d.mts +42 -59
  67. package/dist/turnstile.d.ts +42 -59
  68. package/dist/turnstile.mjs +1 -61
  69. package/package.json +39 -5
  70. package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DjcUWEQl.mjs +0 -11
  71. package/dist/packem_shared/LunoraAuthAdminError-BxrfEeA_.mjs +0 -249
  72. package/dist/packem_shared/compileMigrationsSql-cyE5U18R.mjs +0 -29
  73. package/dist/packem_shared/create-auth.d-Mwhb4gSc.d.mts +0 -128
  74. package/dist/packem_shared/create-auth.d-Mwhb4gSc.d.ts +0 -128
  75. package/dist/packem_shared/createAuth-hRGxUJ9w.mjs +0 -85
  76. package/dist/packem_shared/sessionPresets-Dwwd74_J.mjs +0 -38
@@ -0,0 +1,127 @@
1
+ import { Middleware } from '@lunora/server';
2
+ /**
3
+ * The resolved trust class of an email address' domain: `disposable` (a
4
+ * throwaway/temporary provider, or a caller `denyDomains` hit), `free` (a free
5
+ * consumer provider like Gmail — deliverable but not B2B), or `business`
6
+ * (anything else — a custom/company domain, or an `allowDomains` hit).
7
+ */
8
+ type EmailClass = "business" | "disposable" | "free";
9
+ /** Result of {@link classifyEmail} — the resolved class plus the normalized domain. */
10
+ interface EmailClassification {
11
+ /** The normalized (lowercased, trimmed) domain, or `undefined` for a structurally invalid address. */
12
+ domain: string | undefined;
13
+ /** The resolved trust class. A structurally invalid address resolves to `business` with `domain: undefined`. */
14
+ emailClass: EmailClass;
15
+ }
16
+ /** Configuration for {@link classifyEmail} / {@link assertEmailAllowed} / {@link emailGateMiddleware}. */
17
+ interface EmailGateConfig {
18
+ /**
19
+ * Domains that are always allowed and always classified `business`, even if
20
+ * they appear on the disposable or free lists. Wildcard/subdomain aware
21
+ * (`example.com` also allows `mail.example.com`). Your own hosted domains go
22
+ * here.
23
+ */
24
+ allowDomains?: ReadonlyArray<string>;
25
+ /**
26
+ * Reject disposable/throwaway signups. Defaults to `true` — the whole point
27
+ * of the gate. Set `false` to classify-only (surface `emailClass` without
28
+ * blocking).
29
+ */
30
+ blockDisposable?: boolean;
31
+ /**
32
+ * Extra domains to treat as disposable (blocked when `blockDisposable`), on
33
+ * top of the built-in list. Wildcard/subdomain aware.
34
+ */
35
+ denyDomains?: ReadonlyArray<string>;
36
+ /**
37
+ * Opt-in MX deliverability verification. Off by default because it needs DNS
38
+ * (`@visulima/email-verifier/checks/mx` → `node:dns`), which is not available
39
+ * on the default workerd path. When `true`, {@link assertEmailAllowed} rejects
40
+ * an address whose domain publishes no MX (or fallback A/AAAA) records with
41
+ * `EMAIL_UNDELIVERABLE`. The check is loaded via a dynamic import, so leaving
42
+ * this off keeps the DNS module out of the bundle entirely.
43
+ */
44
+ mx?: boolean;
45
+ /**
46
+ * Reject structurally invalid addresses up front (via
47
+ * `@visulima/email-verifier/checks/syntax`, pure-data/edge-safe). Defaults to
48
+ * `true`. When `false`, an unparseable address classifies as `business` with
49
+ * `domain: undefined` and is not rejected on syntax alone.
50
+ */
51
+ requireValidSyntax?: boolean;
52
+ }
53
+ /** Options for {@link emailGateMiddleware}: the base gate config plus how to read the email from `ctx`. */
54
+ interface EmailGateMiddlewareOptions<Context> extends EmailGateConfig {
55
+ /**
56
+ * Selector that pulls the signup email off `ctx`. The procedure context
57
+ * carries only the resolved identity, not the raw request body, so the email
58
+ * travels in the function `args` — which the builder surfaces to middleware
59
+ * as `ctx.args` (validated, frozen). Mirrors `verifyTurnstileMiddleware`'s
60
+ * `token` selector:
61
+ *
62
+ * ```ts
63
+ * export const signUp = mutation
64
+ * .input({ email: v.string() })
65
+ * .use(emailGateMiddleware({ email: (ctx) => ctx.args.email }))
66
+ * .mutation(async ({ args, ctx }) => { … });
67
+ * ```
68
+ */
69
+ email: (context: Context) => string | undefined;
70
+ /**
71
+ * Called with the resolved classification once the gate passes, so app policy
72
+ * can branch on `free` vs `business` (e.g. gate a plan behind a business
73
+ * email). Never fires when the gate rejects.
74
+ */
75
+ onClassify?: (classification: EmailClassification, context: Context) => void;
76
+ }
77
+ /**
78
+ * Inject the built-in disposable + free domain lists into the lookup packages,
79
+ * once. Call this at worker init on workerd (where the packages' `node:fs`
80
+ * loader is unavailable) so {@link classifyEmail} has data to match against;
81
+ * {@link assertEmailAllowed} / {@link emailGateMiddleware} await it for you, so
82
+ * the gating path is always edge-safe. Idempotent — repeat calls share one load.
83
+ */
84
+ declare const loadEmailDomainLists: () => Promise<void>;
85
+ /**
86
+ * Classify an email address' domain as `disposable` / `free` / `business`,
87
+ * pure-data and edge-safe (no DNS, no filesystem). A structurally invalid
88
+ * address resolves to `{ domain: undefined, emailClass: "business" }` — use
89
+ * {@link assertEmailAllowed} (which can reject on syntax) for the gating path.
90
+ * `allowDomains` wins over both lists; `denyDomains` adds to the disposable list.
91
+ *
92
+ * The lookup relies on the built-in domain lists being loaded. On Node they
93
+ * auto-load from disk; on workerd, `await loadEmailDomainLists()` first (or use
94
+ * {@link assertEmailAllowed} / {@link emailGateMiddleware}, which await it).
95
+ *
96
+ * The returned/compared domain is IDNA-normalized (see {@link toAsciiDomain}) so
97
+ * a Unicode-form internationalized domain matches its `xn--` blocklist entry.
98
+ */
99
+ declare const classifyEmail: (email: string, config?: EmailGateConfig) => EmailClassification;
100
+ /**
101
+ * Classify `email` and enforce the gate, throwing a coded {@link LunoraError}
102
+ * when it fails: `VALIDATION_ERROR` (structurally invalid, only when
103
+ * `requireValidSyntax`, the default), `EMAIL_DOMAIN_BLOCKED` (disposable or
104
+ * deny-listed, only when `blockDisposable`, the default), or `EMAIL_UNDELIVERABLE`
105
+ * (no MX records, only when `mx: true`).
106
+ *
107
+ * Returns the {@link EmailClassification} on success so callers can branch on
108
+ * `free` vs `business`. Async only because of the opt-in MX step; with `mx` off
109
+ * it resolves without any network I/O.
110
+ */
111
+ declare const assertEmailAllowed: (email: string, config?: EmailGateConfig) => Promise<EmailClassification>;
112
+ /**
113
+ * Lunora procedure middleware that gates a non-auth, signup-shaped
114
+ * `mutation`/`action` on the email-domain policy. Attach it with `.use()`; it
115
+ * reads the email from `ctx` via the `email` selector (declare it with
116
+ * `.input(...)` and read it back as `ctx.args.email`)
117
+ * and runs {@link assertEmailAllowed}, which throws a coded {@link LunoraError}
118
+ * (`EMAIL_DOMAIN_BLOCKED` / `EMAIL_UNDELIVERABLE` / `VALIDATION_ERROR`) the
119
+ * runtime maps to the matching status.
120
+ *
121
+ * To gate better-auth's native `/sign-up/email` endpoint instead, use
122
+ * `emailGateDatabaseHooks` / `withEmailGate` from `@lunora/auth` — those hook
123
+ * better-auth's own user-create path. The `Middleware` import is type-only, so
124
+ * this stays free of any runtime `@lunora/server` dependency.
125
+ */
126
+ declare const emailGateMiddleware: <Context>(options: EmailGateMiddlewareOptions<Context>) => Middleware<Context, Context>;
127
+ export { type EmailClass, type EmailClassification, type EmailGateConfig, type EmailGateMiddlewareOptions, assertEmailAllowed, classifyEmail, emailGateMiddleware, loadEmailDomainLists };
@@ -0,0 +1,127 @@
1
+ import { Middleware } from '@lunora/server';
2
+ /**
3
+ * The resolved trust class of an email address' domain: `disposable` (a
4
+ * throwaway/temporary provider, or a caller `denyDomains` hit), `free` (a free
5
+ * consumer provider like Gmail — deliverable but not B2B), or `business`
6
+ * (anything else — a custom/company domain, or an `allowDomains` hit).
7
+ */
8
+ type EmailClass = "business" | "disposable" | "free";
9
+ /** Result of {@link classifyEmail} — the resolved class plus the normalized domain. */
10
+ interface EmailClassification {
11
+ /** The normalized (lowercased, trimmed) domain, or `undefined` for a structurally invalid address. */
12
+ domain: string | undefined;
13
+ /** The resolved trust class. A structurally invalid address resolves to `business` with `domain: undefined`. */
14
+ emailClass: EmailClass;
15
+ }
16
+ /** Configuration for {@link classifyEmail} / {@link assertEmailAllowed} / {@link emailGateMiddleware}. */
17
+ interface EmailGateConfig {
18
+ /**
19
+ * Domains that are always allowed and always classified `business`, even if
20
+ * they appear on the disposable or free lists. Wildcard/subdomain aware
21
+ * (`example.com` also allows `mail.example.com`). Your own hosted domains go
22
+ * here.
23
+ */
24
+ allowDomains?: ReadonlyArray<string>;
25
+ /**
26
+ * Reject disposable/throwaway signups. Defaults to `true` — the whole point
27
+ * of the gate. Set `false` to classify-only (surface `emailClass` without
28
+ * blocking).
29
+ */
30
+ blockDisposable?: boolean;
31
+ /**
32
+ * Extra domains to treat as disposable (blocked when `blockDisposable`), on
33
+ * top of the built-in list. Wildcard/subdomain aware.
34
+ */
35
+ denyDomains?: ReadonlyArray<string>;
36
+ /**
37
+ * Opt-in MX deliverability verification. Off by default because it needs DNS
38
+ * (`@visulima/email-verifier/checks/mx` → `node:dns`), which is not available
39
+ * on the default workerd path. When `true`, {@link assertEmailAllowed} rejects
40
+ * an address whose domain publishes no MX (or fallback A/AAAA) records with
41
+ * `EMAIL_UNDELIVERABLE`. The check is loaded via a dynamic import, so leaving
42
+ * this off keeps the DNS module out of the bundle entirely.
43
+ */
44
+ mx?: boolean;
45
+ /**
46
+ * Reject structurally invalid addresses up front (via
47
+ * `@visulima/email-verifier/checks/syntax`, pure-data/edge-safe). Defaults to
48
+ * `true`. When `false`, an unparseable address classifies as `business` with
49
+ * `domain: undefined` and is not rejected on syntax alone.
50
+ */
51
+ requireValidSyntax?: boolean;
52
+ }
53
+ /** Options for {@link emailGateMiddleware}: the base gate config plus how to read the email from `ctx`. */
54
+ interface EmailGateMiddlewareOptions<Context> extends EmailGateConfig {
55
+ /**
56
+ * Selector that pulls the signup email off `ctx`. The procedure context
57
+ * carries only the resolved identity, not the raw request body, so the email
58
+ * travels in the function `args` — which the builder surfaces to middleware
59
+ * as `ctx.args` (validated, frozen). Mirrors `verifyTurnstileMiddleware`'s
60
+ * `token` selector:
61
+ *
62
+ * ```ts
63
+ * export const signUp = mutation
64
+ * .input({ email: v.string() })
65
+ * .use(emailGateMiddleware({ email: (ctx) => ctx.args.email }))
66
+ * .mutation(async ({ args, ctx }) => { … });
67
+ * ```
68
+ */
69
+ email: (context: Context) => string | undefined;
70
+ /**
71
+ * Called with the resolved classification once the gate passes, so app policy
72
+ * can branch on `free` vs `business` (e.g. gate a plan behind a business
73
+ * email). Never fires when the gate rejects.
74
+ */
75
+ onClassify?: (classification: EmailClassification, context: Context) => void;
76
+ }
77
+ /**
78
+ * Inject the built-in disposable + free domain lists into the lookup packages,
79
+ * once. Call this at worker init on workerd (where the packages' `node:fs`
80
+ * loader is unavailable) so {@link classifyEmail} has data to match against;
81
+ * {@link assertEmailAllowed} / {@link emailGateMiddleware} await it for you, so
82
+ * the gating path is always edge-safe. Idempotent — repeat calls share one load.
83
+ */
84
+ declare const loadEmailDomainLists: () => Promise<void>;
85
+ /**
86
+ * Classify an email address' domain as `disposable` / `free` / `business`,
87
+ * pure-data and edge-safe (no DNS, no filesystem). A structurally invalid
88
+ * address resolves to `{ domain: undefined, emailClass: "business" }` — use
89
+ * {@link assertEmailAllowed} (which can reject on syntax) for the gating path.
90
+ * `allowDomains` wins over both lists; `denyDomains` adds to the disposable list.
91
+ *
92
+ * The lookup relies on the built-in domain lists being loaded. On Node they
93
+ * auto-load from disk; on workerd, `await loadEmailDomainLists()` first (or use
94
+ * {@link assertEmailAllowed} / {@link emailGateMiddleware}, which await it).
95
+ *
96
+ * The returned/compared domain is IDNA-normalized (see {@link toAsciiDomain}) so
97
+ * a Unicode-form internationalized domain matches its `xn--` blocklist entry.
98
+ */
99
+ declare const classifyEmail: (email: string, config?: EmailGateConfig) => EmailClassification;
100
+ /**
101
+ * Classify `email` and enforce the gate, throwing a coded {@link LunoraError}
102
+ * when it fails: `VALIDATION_ERROR` (structurally invalid, only when
103
+ * `requireValidSyntax`, the default), `EMAIL_DOMAIN_BLOCKED` (disposable or
104
+ * deny-listed, only when `blockDisposable`, the default), or `EMAIL_UNDELIVERABLE`
105
+ * (no MX records, only when `mx: true`).
106
+ *
107
+ * Returns the {@link EmailClassification} on success so callers can branch on
108
+ * `free` vs `business`. Async only because of the opt-in MX step; with `mx` off
109
+ * it resolves without any network I/O.
110
+ */
111
+ declare const assertEmailAllowed: (email: string, config?: EmailGateConfig) => Promise<EmailClassification>;
112
+ /**
113
+ * Lunora procedure middleware that gates a non-auth, signup-shaped
114
+ * `mutation`/`action` on the email-domain policy. Attach it with `.use()`; it
115
+ * reads the email from `ctx` via the `email` selector (declare it with
116
+ * `.input(...)` and read it back as `ctx.args.email`)
117
+ * and runs {@link assertEmailAllowed}, which throws a coded {@link LunoraError}
118
+ * (`EMAIL_DOMAIN_BLOCKED` / `EMAIL_UNDELIVERABLE` / `VALIDATION_ERROR`) the
119
+ * runtime maps to the matching status.
120
+ *
121
+ * To gate better-auth's native `/sign-up/email` endpoint instead, use
122
+ * `emailGateDatabaseHooks` / `withEmailGate` from `@lunora/auth` — those hook
123
+ * better-auth's own user-create path. The `Middleware` import is type-only, so
124
+ * this stays free of any runtime `@lunora/server` dependency.
125
+ */
126
+ declare const emailGateMiddleware: <Context>(options: EmailGateMiddlewareOptions<Context>) => Middleware<Context, Context>;
127
+ export { type EmailClass, type EmailClassification, type EmailGateConfig, type EmailGateMiddlewareOptions, assertEmailAllowed, classifyEmail, emailGateMiddleware, loadEmailDomainLists };
@@ -0,0 +1 @@
1
+ import{LunoraError as r}from"@lunora/errors";import{isDisposableDomain as c,setDomains as d}from"@visulima/disposable-email-domains";import{extractDomain as u,isFreeDomain as w,setDomains as h}from"@visulima/free-email-domains";const n=a=>{const s=a.default??a;return Array.isArray(s)?s:[]};let o;const D=async()=>{if(o)return o;const a=(async()=>{const[s,t]=await Promise.all([import("@visulima/disposable-email-domains/domains"),import("@visulima/free-email-domains/domains",{with:{type:"json"}})]);d(n(s)),h(n(t))})();return o=a,a.catch(()=>{o=void 0}),a},m=a=>{try{return new URL(`https://${a}`).hostname}catch{return a.toLowerCase()}},l=a=>a&&a.length>0?new Set(a.map(s=>m(s))):void 0,f=(a,s={})=>{const t=u(a);if(t===void 0)return{domain:void 0,emailClass:"business"};const e=m(t),i=l(s.allowDomains);return c(e,{allowDomains:i,customDomains:l(s.denyDomains)})?{domain:e,emailClass:"disposable"}:w(e,{allowDomains:i})?{domain:e,emailClass:"free"}:{domain:e,emailClass:"business"}},p=async a=>{const{checkMxRecords:s}=await import("@visulima/email-verifier/checks/mx");return(await s(a)).valid},y=async(a,s={})=>{if(s.requireValidSyntax!==!1){const{validateSyntax:e}=await import("@visulima/email-verifier/checks/syntax");if(!e(a))throw new r("VALIDATION_ERROR",`@lunora/auth: "${a}" is not a valid email address.`)}await D();const t=f(a,s);if(t.emailClass==="disposable"&&s.blockDisposable!==!1)throw new r("EMAIL_DOMAIN_BLOCKED",`@lunora/auth: signups from the disposable/throwaway domain "${t.domain??a}" are not allowed.`);if(s.mx===!0&&t.domain!==void 0&&!await p(t.domain))throw new r("EMAIL_UNDELIVERABLE",`@lunora/auth: the domain "${t.domain}" publishes no MX records, so mail to it cannot be delivered.`);return t},b=a=>async({ctx:s,next:t})=>{const e=a.email(s);if(e===void 0||e==="")throw new r("VALIDATION_ERROR","@lunora/auth: emailGateMiddleware received no email to check.");const i=await y(e,a);return a.onClassify?.(i,s),t()};export{y as assertEmailAllowed,f as classifyEmail,b as emailGateMiddleware,D as loadEmailDomainLists};