@lunora/auth 1.0.0-alpha.2 → 1.0.0-alpha.200
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +38 -0
- package/README.md +99 -10
- package/__assets__/package-og.svg +1 -1
- package/dist/adapter.d.mts +4 -43
- package/dist/adapter.d.ts +4 -43
- package/dist/adapter.mjs +1 -47
- package/dist/audit.d.mts +142 -0
- package/dist/audit.d.ts +142 -0
- package/dist/audit.mjs +12 -0
- package/dist/cimd-node.d.mts +1 -0
- package/dist/cimd-node.d.ts +1 -0
- package/dist/cimd-node.mjs +1 -0
- package/dist/cimd-workers.d.mts +32 -0
- package/dist/cimd-workers.d.ts +32 -0
- package/dist/cimd-workers.mjs +1 -0
- package/dist/email-guard.d.mts +127 -0
- package/dist/email-guard.d.ts +127 -0
- package/dist/email-guard.mjs +1 -0
- package/dist/index.d.mts +935 -147
- package/dist/index.d.ts +935 -147
- package/dist/index.mjs +1 -12
- package/dist/middleware.d.mts +167 -156
- package/dist/middleware.d.ts +167 -156
- package/dist/middleware.mjs +1 -53
- package/dist/packem_shared/AUTH_DO_AUDIT_PATH-CptaQ6ZX.mjs +1 -0
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DneiLGpv.mjs +1 -0
- package/dist/packem_shared/LunoraAuthAdminError-uhKG1MgQ.mjs +1 -0
- package/dist/packem_shared/adapter-CG0Yv85f.mjs +2 -0
- package/dist/packem_shared/adapter.d-CElAPJ1U.d.ts +88 -0
- package/dist/packem_shared/adapter.d-CaPwF2v3.d.mts +88 -0
- package/dist/packem_shared/auth-do-B2X2lboC.mjs +1 -0
- package/dist/packem_shared/authAuditHook-DPTKdjBe.mjs +1 -0
- package/dist/packem_shared/authDiscoveryPaths-B14lM14u.mjs +1 -0
- package/dist/packem_shared/authDoColumnAdditions-UwF3Rl41.mjs +1 -0
- package/dist/packem_shared/compileMigrationsSql-BiYsLz91.mjs +1 -0
- package/dist/packem_shared/create-auth.d-D1CKOFWc.d.mts +142 -0
- package/dist/packem_shared/create-auth.d-D1CKOFWc.d.ts +142 -0
- package/dist/packem_shared/createAuth-BdVltKyf.mjs +1 -0
- package/dist/packem_shared/createDoAuthWiring-cqHnqjyh.mjs +1 -0
- package/dist/packem_shared/createLunoraAuthClient-DRcFNwp9.mjs +1 -0
- package/dist/packem_shared/createSignUpInvitation-DHZL9fNl.mjs +1 -0
- package/dist/packem_shared/emailGateDatabaseHooks-CAygR3iq.mjs +1 -0
- package/dist/packem_shared/invite-only.d-CsPJWAwM.d.ts +126 -0
- package/dist/packem_shared/invite-only.d-DKyHHkkQ.d.mts +126 -0
- package/dist/packem_shared/legacyIssuerCleanupStatements-D9Kf8OD1.mjs +1 -0
- package/dist/packem_shared/mcp-CgX1hTOv.mjs +1 -0
- package/dist/packem_shared/on-cloudflare-edge-Dx6gFjBO.mjs +1 -0
- package/dist/packem_shared/quote-identifier-CGiYFBvY.mjs +1 -0
- package/dist/packem_shared/sessionPresets-C867Mlo4.mjs +1 -0
- package/dist/packem_shared/sql-store-CYa4HJsO.mjs +1 -0
- package/dist/packem_shared/uiConfig-B8pN6pks.mjs +1 -0
- package/dist/plugins-client.d.mts +250 -1
- package/dist/plugins-client.d.ts +250 -1
- package/dist/plugins-client.mjs +1 -2
- package/dist/plugins-enterprise-client.d.mts +13 -0
- package/dist/plugins-enterprise-client.d.ts +13 -0
- package/dist/plugins-enterprise-client.mjs +1 -0
- package/dist/plugins-enterprise.d.mts +1668 -0
- package/dist/plugins-enterprise.d.ts +1668 -0
- package/dist/plugins-enterprise.mjs +1 -0
- package/dist/plugins.d.mts +167 -2
- package/dist/plugins.d.ts +167 -2
- package/dist/plugins.mjs +1 -22
- package/dist/schema.d.mts +39 -39
- package/dist/schema.d.ts +39 -39
- package/dist/schema.mjs +1 -62
- package/dist/sql-store.d.mts +28 -28
- package/dist/sql-store.d.ts +28 -28
- package/dist/sql-store.mjs +1 -162
- package/dist/store.d.mts +67 -31
- package/dist/store.d.ts +67 -31
- package/dist/store.mjs +1 -170
- package/dist/turnstile-middleware.d.mts +73 -55
- package/dist/turnstile-middleware.d.ts +73 -55
- package/dist/turnstile-middleware.mjs +1 -45
- package/dist/turnstile.d.mts +42 -59
- package/dist/turnstile.d.ts +42 -59
- package/dist/turnstile.mjs +1 -61
- package/package.json +49 -6
- package/dist/packem_shared/DEFAULT_AUTH_BASE_PATH-DjcUWEQl.mjs +0 -11
- package/dist/packem_shared/LunoraAuthAdminError-BxrfEeA_.mjs +0 -249
- package/dist/packem_shared/compileMigrationsSql-wZH3oXDu.mjs +0 -28
- package/dist/packem_shared/create-auth.d-M36jwG_Y.d.mts +0 -58
- package/dist/packem_shared/create-auth.d-M36jwG_Y.d.ts +0 -58
- package/dist/packem_shared/createAuth-B-tvsvQU.mjs +0 -56
- package/dist/packem_shared/sessionPresets-B95rXrd8.mjs +0 -35
package/LICENSE.md
CHANGED
|
@@ -103,3 +103,41 @@ Unless required by applicable law or agreed to in writing, software distributed
|
|
|
103
103
|
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
104
104
|
CONDITIONS OF ANY KIND, either express or implied. See the License for the
|
|
105
105
|
specific language governing permissions and limitations under the License.
|
|
106
|
+
|
|
107
|
+
<!-- DEPENDENCIES -->
|
|
108
|
+
<!-- /DEPENDENCIES -->
|
|
109
|
+
|
|
110
|
+
<!-- TYPE_DEPENDENCIES -->
|
|
111
|
+
|
|
112
|
+
# Licenses of bundled types
|
|
113
|
+
The published @lunora/auth artifact additionally contains code with the following licenses:
|
|
114
|
+
MIT
|
|
115
|
+
|
|
116
|
+
# Bundled types:
|
|
117
|
+
## @better-auth/sso
|
|
118
|
+
License: MIT
|
|
119
|
+
By: Bereket Engida
|
|
120
|
+
Repository: git+https://github.com/better-auth/better-auth.git
|
|
121
|
+
|
|
122
|
+
> The MIT License (MIT)
|
|
123
|
+
> Copyright (c) 2024 - present, Bereket Engida
|
|
124
|
+
>
|
|
125
|
+
> Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
126
|
+
> this software and associated documentation files (the “Software”), to deal in
|
|
127
|
+
> the Software without restriction, including without limitation the rights to
|
|
128
|
+
> use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
129
|
+
> the Software, and to permit persons to whom the Software is furnished to do so,
|
|
130
|
+
> subject to the following conditions:
|
|
131
|
+
>
|
|
132
|
+
> The above copyright notice and this permission notice shall be included in all
|
|
133
|
+
> copies or substantial portions of the Software.
|
|
134
|
+
>
|
|
135
|
+
> THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
136
|
+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
137
|
+
> FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
138
|
+
> IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
139
|
+
> DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
140
|
+
> ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
141
|
+
> DEALINGS IN THE SOFTWARE.
|
|
142
|
+
|
|
143
|
+
<!-- /TYPE_DEPENDENCIES -->
|
package/README.md
CHANGED
|
@@ -57,20 +57,25 @@ pnpm add @lunora/auth
|
|
|
57
57
|
```ts
|
|
58
58
|
import { createAuth, ensureMigrated, handleAuthRequest, lunoraD1Adapter } from "@lunora/auth";
|
|
59
59
|
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
60
|
+
const options = { secret: env.AUTH_SECRET, emailAndPassword: { enabled: true } };
|
|
61
|
+
|
|
62
|
+
// Prefer lunoraD1Adapter over passing raw env.DB — the raw binding makes
|
|
63
|
+
// better-auth resolve its Kysely adapter via a dynamic import that hangs
|
|
64
|
+
// under @cloudflare/vite-plugin's dev runner.
|
|
65
|
+
const auth = createAuth({ ...options, database: lunoraD1Adapter(env.DB) });
|
|
66
|
+
|
|
67
|
+
// A SECOND instance, over the RAW binding, only for migrating: better-auth
|
|
68
|
+
// migrates through its Kysely adapter alone and throws on lunoraD1Adapter. Build
|
|
69
|
+
// it once and keep it — `ensureMigrated` single-flights on the options object it
|
|
70
|
+
// is handed, so a `createAuth({...})` built inside `fetch` is a fresh key every
|
|
71
|
+
// request and the migration re-runs on each one.
|
|
72
|
+
const migrationAuth = createAuth({ ...options, database: env.DB });
|
|
68
73
|
|
|
69
74
|
// In your Worker's fetch handler, route /api/auth/* to better-auth and fall
|
|
70
75
|
// through to the Lunora worker for everything else:
|
|
71
76
|
export default {
|
|
72
77
|
async fetch(request, env, ctx) {
|
|
73
|
-
await ensureMigrated(
|
|
78
|
+
await ensureMigrated(migrationAuth); // idempotent schema sync; dev/small deploys
|
|
74
79
|
|
|
75
80
|
const authResponse = await handleAuthRequest(auth, request);
|
|
76
81
|
if (authResponse) return authResponse;
|
|
@@ -84,10 +89,94 @@ The runtime resolves the inbound session and stamps `ctx.auth` on every `query`
|
|
|
84
89
|
|
|
85
90
|
### Plugins & CAPTCHA
|
|
86
91
|
|
|
87
|
-
better-auth's plugin factories are re-exported from `@lunora/auth/plugins` (so you don't need better-auth's deep import paths): `admin`, `anonymous`, `bearer`, `captcha`, `createAccessControl`, `customSession`, `deviceAuthorization`, `emailOTP`, `genericOAuth`, `haveIBeenPwned`, `jwt`, `magicLink`, `mcp`, `multiSession`, `oAuthProxy`, `
|
|
92
|
+
better-auth's plugin factories are re-exported from `@lunora/auth/plugins` (so you don't need better-auth's deep import paths): `admin`, `anonymous`, `bearer`, `captcha`, `createAccessControl`, `createMcpProtectedRequestHandler`, `customSession`, `deviceAuthorization`, `emailOTP`, `genericOAuth`, `haveIBeenPwned`, `jwt`, `lastLoginMethod`, `magicLink`, `mcp`, `multiSession`, `oAuthProxy`, `oauthDeviceAuthorization`, `oauthProvider`, `oneTap`, `oneTimeToken`, `organization`, `passkey`, `phoneNumber`, `requireMcpAuth`, `scim`, `siwe`, `twoFactor`, and `username`. Enterprise SSO ships separately as `sso` from `@lunora/auth/plugins/enterprise` (an optional peer — see the docs).
|
|
88
93
|
|
|
89
94
|
For Cloudflare Turnstile on the **auth flow**, use the `captcha` plugin (`captcha({ provider: "cloudflare-turnstile", secretKey: env.TURNSTILE_SECRET_KEY })`); it reads the token from the `x-captcha-response` header. For **non-auth** procedures, the package root also exports standalone helpers — `verifyTurnstile` (pure `siteverify`) and `verifyTurnstileMiddleware` (a `.use()` middleware that takes the token from the function args).
|
|
90
95
|
|
|
96
|
+
### Disposable / free-email gating
|
|
97
|
+
|
|
98
|
+
Reject throwaway/disposable signups (and branch on free-vs-business email) by reusing the visulima email lists — pure-data and **edge-safe on the default path** (no DNS). Wire it into better-auth's native signup with `withEmailGate` (or `emailGateDatabaseHooks`):
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import { createAuth, withEmailGate, lunoraD1Adapter } from "@lunora/auth";
|
|
102
|
+
|
|
103
|
+
const auth = createAuth(
|
|
104
|
+
withEmailGate(
|
|
105
|
+
{ secret: env.AUTH_SECRET, database: lunoraD1Adapter(env.DB), emailAndPassword: { enabled: true } },
|
|
106
|
+
{
|
|
107
|
+
blockDisposable: true, // default — reject disposable domains with `EMAIL_DOMAIN_BLOCKED`
|
|
108
|
+
allowDomains: ["your-company.com"], // never blocked; always classified `business`
|
|
109
|
+
denyDomains: [], // extra domains to treat as disposable
|
|
110
|
+
// mx: true, // OPT-IN deliverability check — needs DNS (node:dns), so keep it OFF on the edge path
|
|
111
|
+
onClassify: (c, user) => console.log(`signup ${user.email as string}: ${c.emailClass}`),
|
|
112
|
+
},
|
|
113
|
+
),
|
|
114
|
+
);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
A blocked signup fails with the coded error `EMAIL_DOMAIN_BLOCKED` (HTTP 400); a business/free email passes and its `emailClass` (`disposable | free | business`) is surfaced via `onClassify`. Everything is config-gated and defaults sensibly.
|
|
118
|
+
|
|
119
|
+
- **Programmatic / non-auth use:** `classifyEmail(email, config)` (sync, pure-data) and `assertEmailAllowed(email, config)` (async; throws the coded error) come from `@lunora/auth/email-guard`, plus `emailGateMiddleware({ email: (ctx) => ctx.args.email })` for a `.use()` gate on your own signup mutations.
|
|
120
|
+
- **Edge-safety:** on workerd, `await loadEmailDomainLists()` once at worker init (the gate helpers do this for you). The optional `mx: true` deliverability check is loaded via a dynamic import so `node:dns` never enters the default bundle — enable it only with `nodejs_compat` (or a DNS-over-HTTPS shim).
|
|
121
|
+
|
|
122
|
+
### Invite-only sign-up
|
|
123
|
+
|
|
124
|
+
Close self-serve registration: `inviteOnly()` creates an account only for an address an administrator invited. It hooks `user.create.before`, so it gates every path that mints a user — password sign-up, an OAuth callback creating a new account, magic link, `admin.createUser`, and Lunora's own `AuthAdmin.createUser` — and declares the `signUpInvitation` table it reads, so migrations pick it up on their own.
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
import { createAuth, createSignUpInvitation } from "@lunora/auth";
|
|
128
|
+
import { inviteOnly } from "@lunora/auth/plugins";
|
|
129
|
+
|
|
130
|
+
const auth = createAuth({
|
|
131
|
+
secret: env.AUTH_SECRET,
|
|
132
|
+
database: lunoraD1Adapter(env.DB),
|
|
133
|
+
emailAndPassword: { enabled: true, requireEmailVerification: true },
|
|
134
|
+
plugins: [inviteOnly()],
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
// …from your own admin-authorized code:
|
|
138
|
+
const invite = await createSignUpInvitation(auth, { email: "ada@example.com" });
|
|
139
|
+
const link = `https://app.example/sign-up?email=${encodeURIComponent(invite.email)}&invite=${invite.token}`;
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
An uninvited signup fails with the coded error `SIGN_UP_INVITE_REQUIRED` (HTTP **400** — a 403 from a create hook is swallowed by better-auth's sign-up route and answered with a fabricated success). Leave `emailAndPassword.disableSignUp` **off** — the invitee still uses the ordinary sign-up form, which `@lunora/auth-ui` prefills from `?email=`. `listSignUpInvitations` and `revokeSignUpInvitation` complete the set; all three are trusted server-side calls with no authorization of their own, so gate them like any other admin action.
|
|
143
|
+
|
|
144
|
+
- **An invitation carries a secret token** — 256 CSPRNG bits, returned in the clear exactly once and stored only as a SHA-256. It is checked on `/sign-up/email` and nowhere else, because every other account-minting path (OAuth callback, magic link, email OTP) has already proved the person controls the address. So there are two layers: the database gate requires an unspent invitation whatever created the row, and the route hook additionally requires the token. Missing token, wrong token, expired invitation and never-invited address all answer with one message, so the form is not a directory oracle.
|
|
145
|
+
- **The link is a bearer credential.** Whoever holds it takes the seat — send it to the invitee, not a shared inbox, and reissue rather than resend if unsure. `requireEmailVerification` still matters: the user row is written before the verification mail goes out, so verification is what keeps a spent invitation from becoming a usable session. The plugin warns on startup when password sign-up runs without it.
|
|
146
|
+
- **Plugins that synthesize an address are refused, not admitted.** `anonymous` (`temp-<id>@…`), `siwe` (`<wallet>@<domain>`), and `phoneNumber`'s sign-up-on-verification all create users with a generated email that matches no invitation, so those flows are rejected — but only once a user exists. Under `allowFirstUser: true` the bootstrap runs before the address is compared, so the first anonymous session or wallet sign-in is what claims it. Don't combine them with this.
|
|
147
|
+
- **Nobody signs up before the first invitation exists, including you.** Seed it with `createSignUpInvitation` at worker init or from a one-off internal mutation. `inviteOnly({ allowFirstUser: true })` instead admits the first account uninvited — convenient, but the "is the user table empty" check is racy and open to whoever finds the URL first.
|
|
148
|
+
- **The studio's Users page grows a Sign-up invitations section** when the plugin is installed — invite, see pending/accepted/expired, revoke. The same three ops are on `AuthAdmin` and the client (`listAuthSignUpInvitations`, `createAuthSignUpInvitation`, `revokeAuthSignUpInvitation`), with `useSignUpInvitations()` in `@lunora/react`. Nothing prunes the table; `pruneSignUpInvitations(auth)` deletes the invitations that expired unused and returns the count.
|
|
149
|
+
- **Revocation is not retroactive, and not atomic against a sign-up in flight.** better-auth does not wrap the `before` hook and the user insert in one transaction, and the adapter contract offers no conditional consume, so a revoke landing between the two lets that one account through. `AuthAdmin.removeUser` is how you undo one that already happened.
|
|
150
|
+
|
|
151
|
+
### Security / audit trail
|
|
152
|
+
|
|
153
|
+
Record authentication & security events (sign-in, sign-up, password change, MFA enable/disable, token refresh, session revoke, …) to a durable, queryable audit trail. Install the better-auth `hooks.after` recorder with `authAuditHook` (or compose via `withAuthAudit`), backed by the same D1 database as the auth tables:
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
import { authAuditHook, createAuth, d1Executor, lunoraD1Adapter, readAuthAuditLog } from "@lunora/auth";
|
|
157
|
+
|
|
158
|
+
const executor = d1Executor(env.DB);
|
|
159
|
+
|
|
160
|
+
const auth = createAuth({
|
|
161
|
+
secret: env.AUTH_SECRET,
|
|
162
|
+
database: lunoraD1Adapter(env.DB),
|
|
163
|
+
hooks: {
|
|
164
|
+
after: authAuditHook({
|
|
165
|
+
executor,
|
|
166
|
+
// retention is CONFIGURABLE and NOT capped — omit it for an unbounded, compliance-grade trail
|
|
167
|
+
retention: 100_000,
|
|
168
|
+
// optional export tap for SIEM forwarding (receives each redacted entry)
|
|
169
|
+
onRecord: (entry) => forwardToSiem(entry),
|
|
170
|
+
}),
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// Query the trail (RLS/admin-gate this in your own read):
|
|
175
|
+
const recent = await readAuthAuditLog(executor, { event: "sign-in", limit: 100 });
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
The built-in hook records only the request path in `detail`; it is free-form for entries you append yourself. Either way the payload is scrubbed with `@visulima/redact` before it is persisted, so a token/password that leaks into one never reaches the durable table. Retention defaults to unbounded (set `retention` to bound it). The store lives in the reserved `__lunora_auth_audit__` table (auto-hidden from the data browser).
|
|
179
|
+
|
|
91
180
|
> This README covers the basics. For the full API, options, and guides, see the **[documentation](https://lunora.sh/docs/packages/auth)**.
|
|
92
181
|
|
|
93
182
|
## Related
|