@santi020k/auth-cloudflare 0.2.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +127 -87
- package/dist/client.d.ts +9 -5
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +8 -4
- package/dist/client.js.map +1 -1
- package/dist/hono.d.ts +11 -1
- package/dist/hono.d.ts.map +1 -1
- package/dist/hono.js +16 -4
- package/dist/hono.js.map +1 -1
- package/dist/index.d.ts +154 -23
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +444 -96
- package/dist/index.js.map +1 -1
- package/dist/passkey-verification.d.ts +16 -0
- package/dist/passkey-verification.d.ts.map +1 -0
- package/dist/passkey-verification.js +9 -0
- package/dist/passkey-verification.js.map +1 -0
- package/dist/schema.d.ts +3 -0
- package/dist/schema.d.ts.map +1 -1
- package/dist/schema.js +3 -0
- package/dist/schema.js.map +1 -1
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -1,127 +1,167 @@
|
|
|
1
1
|
# `@santi020k/auth-cloudflare`
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
policy.
|
|
3
|
+
Email-code and passkey authentication policy for Hono applications running on Cloudflare Workers and D1. Choose a
|
|
4
|
+
single configured owner or let the consuming application approve multiple identities. Better Auth owns the
|
|
5
|
+
authentication protocol and persistence implementation; this package owns the shared Santiago policy.
|
|
6
6
|
|
|
7
7
|
The package intentionally does not share sessions, cookies, passkeys, databases, secrets, or relying-party IDs between
|
|
8
|
-
applications. Every consumer supplies a unique
|
|
9
|
-
email delivery callback.
|
|
10
|
-
|
|
11
|
-
## Status
|
|
8
|
+
applications. Every consumer supplies a unique origin, cookie prefix, secret, D1 binding, and email delivery callback.
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
10
|
+
When the authentication Worker is hosted separately from the browser application, `baseURL` is the public Worker URL
|
|
11
|
+
and `browserOrigin` is the single exact browser origin allowed to make unsafe requests and register passkeys. Omitting
|
|
12
|
+
`browserOrigin` keeps the same-origin default.
|
|
16
13
|
|
|
17
|
-
##
|
|
14
|
+
## Current status
|
|
18
15
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
16
|
+
This package is already public. Version 0.4 preserves its v0.3 policy options and compatibility subpaths and prepares
|
|
17
|
+
the seven split packages for public release. Publication does not replace consumer rollout validation: an application
|
|
18
|
+
must still prove its own D1 migration, compatibility route, session cutover, and browser passkey flow. See the
|
|
19
|
+
repository's [release process](https://github.com/santi020k/auth/blob/main/docs/releasing.md).
|
|
22
20
|
|
|
23
|
-
|
|
21
|
+
## v0.3 compatibility
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
23
|
+
Existing consumers may continue using `applicationOrigin` and `authServerURL`; they are compatibility aliases for
|
|
24
|
+
`browserOrigin` and `baseURL`. Versioned `secrets` with an optional `legacySecret`, configurable `emailOtpRateLimit`,
|
|
25
|
+
and the original two-field `AuthSessionIdentity` assignment contract also remain supported. Newly resolved sessions
|
|
26
|
+
include optional typed lifecycle fields (`authenticatedAt`, `expiresAt`, and `sessionId`). New code should use
|
|
27
|
+
`browserOrigin` and `baseURL`; providing a legacy and replacement origin option with different values fails closed.
|
|
29
28
|
|
|
30
|
-
|
|
29
|
+
Version 0.4 intentionally expands three pre-1.0 TypeScript contracts. Code that manually constructs an `AuthPolicy`
|
|
30
|
+
must use `resolveOwnerAuthPolicy` or `resolveMultiUserAuthPolicy` so the new `baseURL`, `origin`, and `tableNames` fields
|
|
31
|
+
are present. Test doubles typed as `OwnerAuthInstance` or `MultiUserAuthInstance` must add the session inventory,
|
|
32
|
+
revocation, emergency-lockout, and rate-limit pruning methods. Their `resolveSession` implementation must return the lifecycle fields in
|
|
33
|
+
`ResolvedAuthSessionIdentity`. Runtime consumers that call the factory functions require no adapter.
|
|
31
34
|
|
|
32
35
|
## Policy
|
|
33
36
|
|
|
34
37
|
- Email OTPs are six digits, expire after ten minutes, allow five verification attempts, and are stored hashed.
|
|
35
|
-
-
|
|
36
|
-
-
|
|
38
|
+
- Rate limits are enabled in every environment and use D1 rather than per-isolate memory.
|
|
39
|
+
- A D1-backed pre-authentication limit also bounds rejected POST requests per Cloudflare client IP and auth path
|
|
40
|
+
before email admission; Better Auth's stricter endpoint limits still apply to requests that continue into its
|
|
41
|
+
handler.
|
|
42
|
+
- Only the configured owner or identities approved by the consumer may create or update an identity.
|
|
37
43
|
- Unauthorized email-code requests receive the same success-shaped response without sending mail.
|
|
38
|
-
-
|
|
39
|
-
CORS responses never use a wildcard origin.
|
|
44
|
+
- Unsafe requests require the exact configured browser `Origin` header.
|
|
40
45
|
- Passkeys require discoverable credentials and user verification.
|
|
41
|
-
-
|
|
42
|
-
|
|
43
|
-
-
|
|
46
|
+
- Optional social providers use the same consumer email-admission policy; provider credentials remain consumer-owned.
|
|
47
|
+
- Optional Cloudflare Turnstile protection defaults to the email-code request endpoint.
|
|
48
|
+
- The WebAuthn relying-party ID is always the exact application hostname.
|
|
49
|
+
- Production cookies are secure and remain scoped to the authentication API hostname.
|
|
44
50
|
|
|
45
|
-
##
|
|
51
|
+
## Example
|
|
46
52
|
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
```ts
|
|
54
|
+
const ownerAuth = createOwnerAuth({
|
|
55
|
+
appName: "Example owner workspace",
|
|
56
|
+
baseURL: "https://planner.example.com",
|
|
57
|
+
cookiePrefix: "example-owner",
|
|
58
|
+
database: env.DB,
|
|
59
|
+
ownerEmail: env.OWNER_EMAIL,
|
|
60
|
+
secret: env.AUTH_SECRET,
|
|
61
|
+
sendVerificationOTP: ({ email, otp }) => sendLoginCode(env, email, otp),
|
|
62
|
+
waitUntil: (task) => context.waitUntil(task),
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
app.all("/api/auth/*", (context) => ownerAuth.handler(context.req.raw));
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
For multiple accounts, keep membership and roles in the consuming application and provide a current access decision:
|
|
49
69
|
|
|
50
70
|
```ts
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
ownerEmail: context.env.OWNER_EMAIL,
|
|
61
|
-
secret: context.env.AUTH_SECRET,
|
|
62
|
-
sendVerificationOTP: ({ email, otp }) => sendLoginCode(context.env, email, otp),
|
|
63
|
-
waitUntil: (task) => {
|
|
64
|
-
context.executionCtx.waitUntil(task);
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
return ownerAuth.handler(context.req.raw);
|
|
71
|
+
const auth = createMultiUserAuth({
|
|
72
|
+
appName: "Example team workspace",
|
|
73
|
+
authorizeEmail: async (email) => Boolean(await findActiveMember(env.DB, email)),
|
|
74
|
+
baseURL: "https://workspace.example.com",
|
|
75
|
+
cookiePrefix: "example-members",
|
|
76
|
+
database: env.DB,
|
|
77
|
+
secret: env.AUTH_SECRET,
|
|
78
|
+
sendVerificationOTP: ({ email, otp }) => sendLoginCode(env, email, otp),
|
|
79
|
+
waitUntil: (task) => context.executionCtx.waitUntil(task),
|
|
68
80
|
});
|
|
69
81
|
```
|
|
70
82
|
|
|
71
|
-
For
|
|
83
|
+
For a split web/API deployment, keep the two security boundaries explicit:
|
|
72
84
|
|
|
73
85
|
```ts
|
|
74
86
|
const ownerAuth = createOwnerAuth({
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
87
|
+
appName: "Example control room",
|
|
88
|
+
baseURL: "https://api.example.com",
|
|
89
|
+
browserOrigin: "https://control.example.com",
|
|
90
|
+
cookiePrefix: "example-control-owner",
|
|
91
|
+
database: env.DB,
|
|
92
|
+
ownerEmail: env.OWNER_EMAIL,
|
|
93
|
+
secret: env.AUTH_SECRET,
|
|
94
|
+
sendVerificationOTP: ({ email, otp }) => sendLoginCode(env, email, otp),
|
|
95
|
+
waitUntil: (task) => context.executionCtx.waitUntil(task),
|
|
81
96
|
});
|
|
82
97
|
```
|
|
83
98
|
|
|
84
|
-
|
|
85
|
-
|
|
99
|
+
The API must still return credentialed CORS headers for that exact browser origin. `browserOrigin` does not accept an
|
|
100
|
+
origin list and does not create a shared cookie domain.
|
|
86
101
|
|
|
87
|
-
|
|
102
|
+
`waitUntil` is required because code delivery runs outside the response path to keep approved and rejected addresses
|
|
103
|
+
indistinguishable even when the provider fails. On Workers, pass `context.executionCtx.waitUntil`; do not substitute an
|
|
104
|
+
untracked promise that the runtime may terminate after sending the response.
|
|
88
105
|
|
|
89
|
-
|
|
90
|
-
|
|
106
|
+
Set `tablePrefix` when the consumer database already contains generic tables such as `user` or `session`. Generate its
|
|
107
|
+
app-owned migration with the same prefix through `@santi020k/auth-migrations`; a prefix is part of the persistent storage
|
|
108
|
+
contract and must not be changed after deployment.
|
|
91
109
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
110
|
+
`authorizeEmail` receives a normalized email address and is checked during code requests, identity writes, code
|
|
111
|
+
delivery, and session resolution. A member rejected after signing in no longer resolves as authenticated. The package
|
|
112
|
+
does not own roles, invitations, organizations, or cross-application identity state.
|
|
113
|
+
|
|
114
|
+
Consumers may pass Better Auth `socialProviders` for approved OAuth providers. The package checks the fresh provider
|
|
115
|
+
email before provisioning, linking, or OAuth sign-in, so OAuth cannot bypass `ownerEmail` or `authorizeEmail`.
|
|
116
|
+
Account-linking UX, scopes, callback routes, and provider credentials remain consumer-owned.
|
|
117
|
+
|
|
118
|
+
Public code-request surfaces can enable Turnstile through the same server policy:
|
|
96
119
|
|
|
97
120
|
```ts
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
protectedRoutes.use(
|
|
107
|
-
"*",
|
|
108
|
-
createOwnerAuthMiddleware<AppEnv>((context) => createOwnerAuthForContext(context)),
|
|
109
|
-
);
|
|
110
|
-
protectedRoutes.get("/account", (context) => context.json(context.var.ownerAuthSession));
|
|
121
|
+
const auth = createMultiUserAuth({
|
|
122
|
+
// ...application-owned configuration
|
|
123
|
+
turnstile: {
|
|
124
|
+
allowedHostnames: ["workspace.example.com"],
|
|
125
|
+
expectedAction: "request-login-code",
|
|
126
|
+
secretKey: env.TURNSTILE_SECRET_KEY,
|
|
127
|
+
},
|
|
128
|
+
});
|
|
111
129
|
```
|
|
112
130
|
|
|
113
|
-
`
|
|
114
|
-
current request context and bindings, as in the server example above.
|
|
115
|
-
|
|
116
|
-
## D1 ownership
|
|
131
|
+
The browser supplies Better Auth's `x-captcha-response` header. Site and secret keys must not be shared across products.
|
|
117
132
|
|
|
118
133
|
Cloudflare Workers must enable `nodejs_compat` (or the narrower `nodejs_als` flag when no other Node compatibility is
|
|
119
|
-
needed).
|
|
120
|
-
|
|
134
|
+
needed). Each application must generate and review its own Better Auth core and passkey migration; the package does not
|
|
135
|
+
silently create or mutate production tables.
|
|
136
|
+
|
|
137
|
+
D1 rate-limit counters are persistent. Call `auth.pruneRateLimits()` from the consuming application's scheduled
|
|
138
|
+
maintenance to remove counters older than the default 24-hour retention window. A consumer may provide a positive
|
|
139
|
+
`retentionSeconds` override and deterministic `now` value for testing. Cleanup is explicit and never changes schemas or
|
|
140
|
+
runs implicitly inside an authentication request.
|
|
141
|
+
|
|
142
|
+
## Session operations and security events
|
|
143
|
+
|
|
144
|
+
The configured instance provides server-side `listSessions(userId)`, `revokeSession(userId, sessionId)`,
|
|
145
|
+
`revokeAllSessions(userId)`, and `emergencyLockout(userId)` operations. Inventory entries contain ISO timestamps and
|
|
146
|
+
device metadata but never session tokens, and expired sessions are omitted. The explicit `userId` scope prevents a
|
|
147
|
+
session ID from revoking another account's session. Consumers must authenticate and authorize their own management
|
|
148
|
+
routes before calling these methods; this package deliberately does not define roles, recovery, or administrative
|
|
149
|
+
policy.
|
|
150
|
+
|
|
151
|
+
Supply `onSecurityEvent` to forward authentication events into the consumer's established audit or alerting system:
|
|
152
|
+
|
|
153
|
+
```ts
|
|
154
|
+
const auth = createMultiUserAuth({
|
|
155
|
+
// ...application-owned configuration
|
|
156
|
+
onSecurityEvent: (event) => auditSecurityEvent(event),
|
|
157
|
+
});
|
|
158
|
+
```
|
|
121
159
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
160
|
+
Events cover blocked origins or credentials, pre-authentication rate limiting, suppressed and failed email-code
|
|
161
|
+
delivery, code requests, session creation, scoped revocation, revoke-all, and explicit emergency lockout. Listener
|
|
162
|
+
failures are isolated from the auth flow. Event payloads intentionally omit codes, cookies, session tokens, secrets,
|
|
163
|
+
client IPs, and request bodies.
|
|
125
164
|
|
|
126
|
-
|
|
127
|
-
and
|
|
165
|
+
Resolved identities include `authenticatedAt` and `expiresAt` ISO timestamps. Use `isRecentAuthentication` or the Hono
|
|
166
|
+
recent-authentication middleware for sensitive actions. The application still chooses the step-up method and owns the
|
|
167
|
+
authorization decision.
|
package/dist/client.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { passkeyClient } from "@better-auth/passkey/client";
|
|
2
2
|
import type { AuthClient } from "better-auth/client";
|
|
3
3
|
import { emailOTPClient } from "better-auth/client/plugins";
|
|
4
|
-
export interface
|
|
5
|
-
/** Origin of the Worker that mounts the
|
|
4
|
+
export interface AuthClientOptions {
|
|
5
|
+
/** Origin of the Worker that mounts the auth handler. */
|
|
6
6
|
authServerURL: string;
|
|
7
|
-
/** Path where the
|
|
7
|
+
/** Path where the auth handler is mounted. */
|
|
8
8
|
basePath?: `/${string}`;
|
|
9
9
|
}
|
|
10
10
|
interface OwnerAuthClientConfiguration {
|
|
@@ -15,11 +15,15 @@ interface OwnerAuthClientConfiguration {
|
|
|
15
15
|
};
|
|
16
16
|
plugins: [ReturnType<typeof emailOTPClient>, ReturnType<typeof passkeyClient>];
|
|
17
17
|
}
|
|
18
|
-
export type
|
|
18
|
+
export type ApplicationAuthClient = AuthClient<OwnerAuthClientConfiguration>;
|
|
19
|
+
export type OwnerAuthClientOptions = AuthClientOptions;
|
|
20
|
+
export type OwnerAuthClient = ApplicationAuthClient;
|
|
19
21
|
/**
|
|
20
|
-
* Creates the browser client for the
|
|
22
|
+
* Creates the browser client for the email OTP and passkey policy.
|
|
21
23
|
* Credentials are always included so a UI and its auth Worker may use separate origins.
|
|
22
24
|
*/
|
|
25
|
+
export declare function createApplicationAuthClient(options: AuthClientOptions): ApplicationAuthClient;
|
|
26
|
+
/** Backward-compatible single-owner browser client name. */
|
|
23
27
|
export declare function createOwnerAuthClient(options: OwnerAuthClientOptions): OwnerAuthClient;
|
|
24
28
|
export {};
|
|
25
29
|
//# sourceMappingURL=client.d.ts.map
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAI5D,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAC5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAI5D,MAAM,WAAW,iBAAiB;IAChC,yDAAyD;IACzD,aAAa,EAAE,MAAM,CAAC;IACtB,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,IAAI,MAAM,EAAE,CAAC;CACzB;AAED,UAAU,4BAA4B;IACpC,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE;QAAE,WAAW,EAAE,SAAS,CAAA;KAAE,CAAC;IACzC,OAAO,EAAE,CAAC,UAAU,CAAC,OAAO,cAAc,CAAC,EAAE,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC,CAAC;CAChF;AAED,MAAM,MAAM,qBAAqB,GAAG,UAAU,CAAC,4BAA4B,CAAC,CAAC;AAC7E,MAAM,MAAM,sBAAsB,GAAG,iBAAiB,CAAC;AACvD,MAAM,MAAM,eAAe,GAAG,qBAAqB,CAAC;AA2BpD;;;GAGG;AACH,wBAAgB,2BAA2B,CAAC,OAAO,EAAE,iBAAiB,GAAG,qBAAqB,CAO7F;AAED,4DAA4D;AAC5D,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,sBAAsB,GAAG,eAAe,CAEtF"}
|
package/dist/client.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { passkeyClient } from "@better-auth/passkey/client";
|
|
2
|
-
import { createAuthClient } from "better-auth/client";
|
|
2
|
+
import { createAuthClient as createBetterAuthClient } from "better-auth/client";
|
|
3
3
|
import { emailOTPClient } from "better-auth/client/plugins";
|
|
4
4
|
const DEFAULT_BASE_PATH = "/api/auth";
|
|
5
5
|
function normalizeAuthServerURL(value) {
|
|
@@ -25,15 +25,19 @@ function normalizeBasePath(value) {
|
|
|
25
25
|
return basePath;
|
|
26
26
|
}
|
|
27
27
|
/**
|
|
28
|
-
* Creates the browser client for the
|
|
28
|
+
* Creates the browser client for the email OTP and passkey policy.
|
|
29
29
|
* Credentials are always included so a UI and its auth Worker may use separate origins.
|
|
30
30
|
*/
|
|
31
|
-
export function
|
|
32
|
-
return
|
|
31
|
+
export function createApplicationAuthClient(options) {
|
|
32
|
+
return createBetterAuthClient({
|
|
33
33
|
basePath: normalizeBasePath(options.basePath),
|
|
34
34
|
baseURL: normalizeAuthServerURL(options.authServerURL),
|
|
35
35
|
fetchOptions: { credentials: "include" },
|
|
36
36
|
plugins: [emailOTPClient(), passkeyClient()],
|
|
37
37
|
});
|
|
38
38
|
}
|
|
39
|
+
/** Backward-compatible single-owner browser client name. */
|
|
40
|
+
export function createOwnerAuthClient(options) {
|
|
41
|
+
return createApplicationAuthClient(options);
|
|
42
|
+
}
|
|
39
43
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,6BAA6B,CAAC;AAE5D,OAAO,EAAE,gBAAgB,IAAI,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAChF,OAAO,EAAE,cAAc,EAAE,MAAM,4BAA4B,CAAC;AAE5D,MAAM,iBAAiB,GAAG,WAAW,CAAC;AAoBtC,SAAS,sBAAsB,CAAC,KAAa;IAC3C,IAAI,GAAQ,CAAC;IACb,IAAI,CAAC;QACH,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;IACvB,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,SAAS,GAAG,GAAG,CAAC,QAAQ,KAAK,WAAW,IAAI,GAAG,CAAC,QAAQ,KAAK,WAAW,CAAC;IAC/E,MAAM,eAAe,GAAG,GAAG,CAAC,QAAQ,KAAK,QAAQ,IAAI,CAAC,SAAS,IAAI,GAAG,CAAC,QAAQ,KAAK,OAAO,CAAC,CAAC;IAC7F,IAAI,CAAC,eAAe,IAAI,GAAG,CAAC,QAAQ,KAAK,GAAG,IAAI,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,IAAI,EAAE,CAAC;QACvE,MAAM,IAAI,KAAK,CAAC,+BAA+B,CAAC,CAAC;IACnD,CAAC;IAED,OAAO,GAAG,CAAC,MAAM,CAAC;AACpB,CAAC;AAED,SAAS,iBAAiB,CAAC,KAA+B;IACxD,MAAM,QAAQ,GAAG,KAAK,IAAI,iBAAiB,CAAC;IAC5C,IAAI,CAAC,mCAAmC,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC;QACnF,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IAClD,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,2BAA2B,CAAC,OAA0B;IACpE,OAAO,sBAAsB,CAA+B;QAC1D,QAAQ,EAAE,iBAAiB,CAAC,OAAO,CAAC,QAAQ,CAAC;QAC7C,OAAO,EAAE,sBAAsB,CAAC,OAAO,CAAC,aAAa,CAAC;QACtD,YAAY,EAAE,EAAE,WAAW,EAAE,SAAS,EAAE;QACxC,OAAO,EAAE,CAAC,cAAc,EAAE,EAAE,aAAa,EAAE,CAAC;KAC7C,CAAC,CAAC;AACL,CAAC;AAED,4DAA4D;AAC5D,MAAM,UAAU,qBAAqB,CAAC,OAA+B;IACnE,OAAO,2BAA2B,CAAC,OAAO,CAAC,CAAC;AAC9C,CAAC"}
|
package/dist/hono.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import type { Context, MiddlewareHandler } from "hono";
|
|
2
|
-
import type { OwnerAuthInstance, OwnerAuthSessionIdentity } from "./index.js";
|
|
2
|
+
import type { AuthSessionIdentity, MultiUserAuthInstance, OwnerAuthInstance, OwnerAuthSessionIdentity } from "./index.js";
|
|
3
|
+
export declare const AUTH_SESSION_VARIABLE = "authSession";
|
|
4
|
+
export interface AuthVariables {
|
|
5
|
+
authSession: AuthSessionIdentity;
|
|
6
|
+
}
|
|
7
|
+
export interface AuthEnv {
|
|
8
|
+
Variables: AuthVariables;
|
|
9
|
+
}
|
|
10
|
+
export type AuthResolver<Environment extends AuthEnv> = MultiUserAuthInstance | ((context: Context<Environment>) => MultiUserAuthInstance);
|
|
3
11
|
export declare const OWNER_AUTH_SESSION_VARIABLE = "ownerAuthSession";
|
|
4
12
|
export interface OwnerAuthVariables {
|
|
5
13
|
ownerAuthSession: OwnerAuthSessionIdentity;
|
|
@@ -8,6 +16,8 @@ export interface OwnerAuthEnv {
|
|
|
8
16
|
Variables: OwnerAuthVariables;
|
|
9
17
|
}
|
|
10
18
|
export type OwnerAuthResolver<Environment extends OwnerAuthEnv> = OwnerAuthInstance | ((context: Context<Environment>) => OwnerAuthInstance);
|
|
19
|
+
/** Resolves a consumer-approved session once and exposes it to downstream Hono handlers. */
|
|
20
|
+
export declare function createAuthMiddleware<Environment extends AuthEnv = AuthEnv>(resolver: AuthResolver<Environment>): MiddlewareHandler<Environment>;
|
|
11
21
|
/** Resolves an owner session once and exposes it to downstream Hono handlers. */
|
|
12
22
|
export declare function createOwnerAuthMiddleware<Environment extends OwnerAuthEnv = OwnerAuthEnv>(resolver: OwnerAuthResolver<Environment>): MiddlewareHandler<Environment>;
|
|
13
23
|
//# sourceMappingURL=hono.d.ts.map
|
package/dist/hono.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hono.d.ts","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"hono.d.ts","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,iBAAiB,EAAE,MAAM,MAAM,CAAC;AAEvD,OAAO,KAAK,EACV,mBAAmB,EACnB,qBAAqB,EACrB,iBAAiB,EACjB,wBAAwB,EACzB,MAAM,YAAY,CAAC;AAEpB,eAAO,MAAM,qBAAqB,gBAAgB,CAAC;AAEnD,MAAM,WAAW,aAAa;IAC5B,WAAW,EAAE,mBAAmB,CAAC;CAClC;AAED,MAAM,WAAW,OAAO;IACtB,SAAS,EAAE,aAAa,CAAC;CAC1B;AAED,MAAM,MAAM,YAAY,CAAC,WAAW,SAAS,OAAO,IAClD,qBAAqB,GAAG,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK,qBAAqB,CAAC,CAAC;AAErF,eAAO,MAAM,2BAA2B,qBAAqB,CAAC;AAE9D,MAAM,WAAW,kBAAkB;IACjC,gBAAgB,EAAE,wBAAwB,CAAC;CAC5C;AAED,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,kBAAkB,CAAC;CAC/B;AAED,MAAM,MAAM,iBAAiB,CAAC,WAAW,SAAS,YAAY,IAC5D,iBAAiB,GAAG,CAAC,CAAC,OAAO,EAAE,OAAO,CAAC,WAAW,CAAC,KAAK,iBAAiB,CAAC,CAAC;AAe7E,4FAA4F;AAC5F,wBAAgB,oBAAoB,CAAC,WAAW,SAAS,OAAO,GAAG,OAAO,EACxE,QAAQ,EAAE,YAAY,CAAC,WAAW,CAAC,GAClC,iBAAiB,CAAC,WAAW,CAAC,CAShC;AAED,iFAAiF;AACjF,wBAAgB,yBAAyB,CAAC,WAAW,SAAS,YAAY,GAAG,YAAY,EACvF,QAAQ,EAAE,iBAAiB,CAAC,WAAW,CAAC,GACvC,iBAAiB,CAAC,WAAW,CAAC,CAShC"}
|
package/dist/hono.js
CHANGED
|
@@ -1,20 +1,32 @@
|
|
|
1
|
+
export const AUTH_SESSION_VARIABLE = "authSession";
|
|
1
2
|
export const OWNER_AUTH_SESSION_VARIABLE = "ownerAuthSession";
|
|
2
|
-
function unauthorizedResponse() {
|
|
3
|
+
function unauthorizedResponse(code) {
|
|
3
4
|
return Response.json({
|
|
4
|
-
code
|
|
5
|
-
message:
|
|
5
|
+
code,
|
|
6
|
+
message: code,
|
|
6
7
|
}, {
|
|
7
8
|
headers: { "Cache-Control": "no-store" },
|
|
8
9
|
status: 401,
|
|
9
10
|
});
|
|
10
11
|
}
|
|
12
|
+
/** Resolves a consumer-approved session once and exposes it to downstream Hono handlers. */
|
|
13
|
+
export function createAuthMiddleware(resolver) {
|
|
14
|
+
return async (context, next) => {
|
|
15
|
+
const auth = typeof resolver === "function" ? resolver(context) : resolver;
|
|
16
|
+
const session = await auth.resolveSession(context.req.raw.headers);
|
|
17
|
+
if (!session)
|
|
18
|
+
return unauthorizedResponse("auth_session_required");
|
|
19
|
+
context.set(AUTH_SESSION_VARIABLE, session);
|
|
20
|
+
await next();
|
|
21
|
+
};
|
|
22
|
+
}
|
|
11
23
|
/** Resolves an owner session once and exposes it to downstream Hono handlers. */
|
|
12
24
|
export function createOwnerAuthMiddleware(resolver) {
|
|
13
25
|
return async (context, next) => {
|
|
14
26
|
const auth = typeof resolver === "function" ? resolver(context) : resolver;
|
|
15
27
|
const session = await auth.resolveSession(context.req.raw.headers);
|
|
16
28
|
if (!session)
|
|
17
|
-
return unauthorizedResponse();
|
|
29
|
+
return unauthorizedResponse("owner_auth_session_required");
|
|
18
30
|
context.set(OWNER_AUTH_SESSION_VARIABLE, session);
|
|
19
31
|
await next();
|
|
20
32
|
};
|
package/dist/hono.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hono.js","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"hono.js","sourceRoot":"","sources":["../src/hono.ts"],"names":[],"mappings":"AASA,MAAM,CAAC,MAAM,qBAAqB,GAAG,aAAa,CAAC;AAanD,MAAM,CAAC,MAAM,2BAA2B,GAAG,kBAAkB,CAAC;AAa9D,SAAS,oBAAoB,CAAC,IAA6D;IACzF,OAAO,QAAQ,CAAC,IAAI,CAClB;QACE,IAAI;QACJ,OAAO,EAAE,IAAI;KACd,EACD;QACE,OAAO,EAAE,EAAE,eAAe,EAAE,UAAU,EAAE;QACxC,MAAM,EAAE,GAAG;KACZ,CACF,CAAC;AACJ,CAAC;AAED,4FAA4F;AAC5F,MAAM,UAAU,oBAAoB,CAClC,QAAmC;IAEnC,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QAC3E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACnE,IAAI,CAAC,OAAO;YAAE,OAAO,oBAAoB,CAAC,uBAAuB,CAAC,CAAC;QAEnE,OAAO,CAAC,GAAG,CAAC,qBAAqB,EAAE,OAAO,CAAC,CAAC;QAC5C,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAED,iFAAiF;AACjF,MAAM,UAAU,yBAAyB,CACvC,QAAwC;IAExC,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE;QAC7B,MAAM,IAAI,GAAG,OAAO,QAAQ,KAAK,UAAU,CAAC,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;QAC3E,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QACnE,IAAI,CAAC,OAAO;YAAE,OAAO,oBAAoB,CAAC,6BAA6B,CAAC,CAAC;QAEzE,OAAO,CAAC,GAAG,CAAC,2BAA2B,EAAE,OAAO,CAAC,CAAC;QAClD,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,64 +1,195 @@
|
|
|
1
1
|
import type { D1Database } from "@cloudflare/workers-types";
|
|
2
2
|
import { type BetterAuthOptions } from "better-auth";
|
|
3
|
-
export interface
|
|
3
|
+
export interface AuthTableNames {
|
|
4
|
+
account: string;
|
|
5
|
+
passkey: string;
|
|
6
|
+
rateLimit: string;
|
|
7
|
+
session: string;
|
|
8
|
+
user: string;
|
|
9
|
+
verification: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the table names needed at runtime without making the already-public
|
|
13
|
+
* package depend on the still-private migration package. Contract tests keep
|
|
14
|
+
* this result aligned with `@santi020k/auth-migrations`.
|
|
15
|
+
*/
|
|
16
|
+
export declare function resolveAuthTableNames(tablePrefix?: string): AuthTableNames;
|
|
17
|
+
export interface AuthEmail {
|
|
4
18
|
email: string;
|
|
5
19
|
otp: string;
|
|
6
20
|
}
|
|
7
|
-
export interface
|
|
21
|
+
export interface AuthEmailOtpRateLimit {
|
|
8
22
|
max: number;
|
|
9
23
|
window: number;
|
|
10
24
|
}
|
|
11
|
-
export type
|
|
12
|
-
|
|
25
|
+
export type AuthVersionedSecret = NonNullable<BetterAuthOptions["secrets"]>[number];
|
|
26
|
+
export type OwnerAuthEmailOtpRateLimit = AuthEmailOtpRateLimit;
|
|
27
|
+
export type OwnerAuthVersionedSecret = AuthVersionedSecret;
|
|
28
|
+
interface AuthSimpleSecretOptions {
|
|
13
29
|
legacySecret?: never;
|
|
14
30
|
secret: string;
|
|
15
31
|
secrets?: never;
|
|
16
32
|
}
|
|
17
|
-
interface
|
|
33
|
+
interface AuthVersionedSecretOptions {
|
|
18
34
|
legacySecret?: string;
|
|
19
35
|
secret?: never;
|
|
20
|
-
secrets:
|
|
36
|
+
secrets: AuthVersionedSecret[];
|
|
21
37
|
}
|
|
22
|
-
type
|
|
23
|
-
|
|
38
|
+
type AuthSecretOptions = AuthSimpleSecretOptions | AuthVersionedSecretOptions;
|
|
39
|
+
/**
|
|
40
|
+
* Observability hook for security-relevant occurrences. Consumers own
|
|
41
|
+
* storage and alerting; this package only reports what happened. A listener
|
|
42
|
+
* that throws never breaks the authentication flow — the failure is
|
|
43
|
+
* swallowed after the listener runs.
|
|
44
|
+
*/
|
|
45
|
+
export type AuthSecurityEvent = {
|
|
46
|
+
email: string;
|
|
47
|
+
path: string;
|
|
48
|
+
type: "credentials_rejected";
|
|
49
|
+
} | {
|
|
50
|
+
email: string;
|
|
51
|
+
type: "email_otp_delivery_failed";
|
|
52
|
+
} | {
|
|
53
|
+
email: string;
|
|
54
|
+
type: "email_otp_delivery_suppressed";
|
|
55
|
+
} | {
|
|
56
|
+
email: string;
|
|
57
|
+
type: "email_otp_requested";
|
|
58
|
+
} | {
|
|
59
|
+
revokedSessions: number;
|
|
60
|
+
type: "emergency_lockout";
|
|
61
|
+
userId: string;
|
|
62
|
+
} | {
|
|
63
|
+
origin: string | null;
|
|
64
|
+
path: string;
|
|
65
|
+
type: "request_origin_rejected";
|
|
66
|
+
} | {
|
|
67
|
+
path: string;
|
|
68
|
+
type: "request_rate_limited";
|
|
69
|
+
} | {
|
|
70
|
+
sessionId: string;
|
|
71
|
+
type: "session_created";
|
|
72
|
+
userId: string;
|
|
73
|
+
} | {
|
|
74
|
+
sessionId: string;
|
|
75
|
+
type: "session_revoked";
|
|
76
|
+
userId: string;
|
|
77
|
+
} | {
|
|
78
|
+
revokedSessions: number;
|
|
79
|
+
type: "sessions_revoked_all";
|
|
80
|
+
userId: string;
|
|
81
|
+
};
|
|
82
|
+
export type AuthSecurityEventListener = (event: AuthSecurityEvent) => Promise<void> | void;
|
|
83
|
+
interface BaseAuthPolicyConfiguration {
|
|
24
84
|
appName: string;
|
|
25
|
-
|
|
26
|
-
|
|
85
|
+
/** @deprecated Use `browserOrigin`. */
|
|
86
|
+
applicationOrigin?: string;
|
|
27
87
|
basePath?: `/${string}`;
|
|
88
|
+
baseURL?: string;
|
|
89
|
+
browserOrigin?: string;
|
|
28
90
|
cookiePrefix: string;
|
|
29
|
-
|
|
30
|
-
|
|
91
|
+
/** @deprecated Use `baseURL`. */
|
|
92
|
+
authServerURL?: string;
|
|
93
|
+
emailOtpRateLimit?: AuthEmailOtpRateLimit;
|
|
94
|
+
/** Optional Better Auth social providers. Credentials remain consumer-owned secrets. */
|
|
95
|
+
socialProviders?: BetterAuthOptions["socialProviders"];
|
|
96
|
+
tablePrefix?: string;
|
|
97
|
+
/** Optional Cloudflare Turnstile protection for authentication endpoints. */
|
|
98
|
+
turnstile?: AuthTurnstileOptions;
|
|
31
99
|
}
|
|
32
|
-
|
|
33
|
-
export
|
|
100
|
+
type BaseAuthPolicyOptions = BaseAuthPolicyConfiguration & AuthSecretOptions;
|
|
101
|
+
export interface AuthTurnstileOptions {
|
|
102
|
+
allowedHostnames?: readonly string[];
|
|
103
|
+
endpoints?: readonly string[];
|
|
104
|
+
expectedAction?: string;
|
|
105
|
+
secretKey: string;
|
|
106
|
+
}
|
|
107
|
+
interface AuthRuntimeOptions {
|
|
34
108
|
database: D1Database;
|
|
35
|
-
|
|
109
|
+
onSecurityEvent?: AuthSecurityEventListener;
|
|
110
|
+
sendVerificationOTP(email: AuthEmail): Promise<void>;
|
|
36
111
|
sessionExpiresIn?: number;
|
|
37
112
|
sessionUpdateAge?: number;
|
|
38
113
|
waitUntil(task: Promise<void>): void;
|
|
39
|
-
}
|
|
40
|
-
export interface
|
|
114
|
+
}
|
|
115
|
+
export interface AuthPolicy {
|
|
116
|
+
/** Compatibility alias for `origin`. */
|
|
41
117
|
applicationOrigin: string;
|
|
118
|
+
/** Compatibility alias for `baseURL`. */
|
|
42
119
|
authServerOrigin: string;
|
|
43
120
|
basePath: string;
|
|
121
|
+
baseURL: string;
|
|
44
122
|
cookiePrefix: string;
|
|
45
|
-
emailOtpRateLimit:
|
|
46
|
-
|
|
123
|
+
emailOtpRateLimit: AuthEmailOtpRateLimit;
|
|
124
|
+
origin: string;
|
|
47
125
|
relyingPartyId: string;
|
|
48
126
|
secureCookies: boolean;
|
|
127
|
+
tableNames: AuthTableNames;
|
|
49
128
|
}
|
|
50
|
-
export interface
|
|
129
|
+
export interface AuthSessionIdentity {
|
|
130
|
+
authenticatedAt?: string;
|
|
51
131
|
email: string;
|
|
132
|
+
expiresAt?: string;
|
|
133
|
+
sessionId?: string;
|
|
52
134
|
userId: string;
|
|
53
135
|
}
|
|
54
|
-
export interface
|
|
136
|
+
export interface ResolvedAuthSessionIdentity extends AuthSessionIdentity {
|
|
137
|
+
authenticatedAt: string;
|
|
138
|
+
expiresAt: string;
|
|
139
|
+
sessionId: string;
|
|
140
|
+
}
|
|
141
|
+
/** One stored session row, safe to render in a "your devices" UI. Never includes the session token. */
|
|
142
|
+
export interface AuthSessionSummary {
|
|
143
|
+
createdAt: string;
|
|
144
|
+
expiresAt: string;
|
|
145
|
+
id: string;
|
|
146
|
+
ipAddress: string | null;
|
|
147
|
+
updatedAt: string;
|
|
148
|
+
userAgent: string | null;
|
|
149
|
+
}
|
|
150
|
+
interface AuthInstance<TPolicy extends AuthPolicy> {
|
|
151
|
+
/** Revokes every session for `userId` immediately and reports an `emergency_lockout` event. Returns the count revoked. */
|
|
152
|
+
emergencyLockout(userId: string): Promise<number>;
|
|
55
153
|
handler(request: Request): Promise<Response>;
|
|
56
|
-
|
|
57
|
-
|
|
154
|
+
/** Lists `userId`'s active sessions, newest first. */
|
|
155
|
+
listSessions(userId: string): Promise<AuthSessionSummary[]>;
|
|
156
|
+
policy: TPolicy;
|
|
157
|
+
/** Deletes expired rate-limit counters. Call from consumer-owned scheduled maintenance. */
|
|
158
|
+
pruneRateLimits(options?: {
|
|
159
|
+
now?: number;
|
|
160
|
+
retentionSeconds?: number;
|
|
161
|
+
}): Promise<number>;
|
|
162
|
+
resolveSession(headers: Headers): Promise<ResolvedAuthSessionIdentity | null>;
|
|
163
|
+
/** Revokes every session for `userId`. Returns the count revoked. */
|
|
164
|
+
revokeAllSessions(userId: string): Promise<number>;
|
|
165
|
+
/** Revokes one session owned by `userId`. Returns `false` if no matching session existed. */
|
|
166
|
+
revokeSession(userId: string, sessionId: string): Promise<boolean>;
|
|
167
|
+
}
|
|
168
|
+
export type MultiUserAuthPolicyOptions = BaseAuthPolicyOptions;
|
|
169
|
+
export type MultiUserAuthOptions = BaseAuthPolicyOptions & AuthRuntimeOptions & {
|
|
170
|
+
authorizeEmail(email: string): boolean | Promise<boolean>;
|
|
171
|
+
};
|
|
172
|
+
export type MultiUserAuthInstance = AuthInstance<AuthPolicy>;
|
|
173
|
+
export type OwnerAuthPolicyOptions = BaseAuthPolicyOptions & {
|
|
174
|
+
ownerEmail: string;
|
|
175
|
+
};
|
|
176
|
+
export type OwnerAuthOptions = OwnerAuthPolicyOptions & AuthRuntimeOptions;
|
|
177
|
+
export interface OwnerAuthPolicy extends AuthPolicy {
|
|
178
|
+
ownerEmail: string;
|
|
58
179
|
}
|
|
180
|
+
export type OwnerAuthEmail = AuthEmail;
|
|
181
|
+
export type OwnerAuthInstance = AuthInstance<OwnerAuthPolicy>;
|
|
182
|
+
export type OwnerAuthSessionIdentity = AuthSessionIdentity;
|
|
183
|
+
export declare function normalizeAuthEmail(value: string): string;
|
|
59
184
|
export declare function normalizeOwnerEmail(value: string): string;
|
|
185
|
+
/** Returns whether a session is recent enough for a consumer-defined step-up boundary. */
|
|
186
|
+
export declare function isRecentAuthentication(identity: {
|
|
187
|
+
authenticatedAt: string;
|
|
188
|
+
}, maxAgeSeconds: number, now?: number): boolean;
|
|
189
|
+
export declare function resolveMultiUserAuthPolicy(options: MultiUserAuthPolicyOptions): AuthPolicy;
|
|
60
190
|
export declare function resolveOwnerAuthPolicy(options: OwnerAuthPolicyOptions): OwnerAuthPolicy;
|
|
61
191
|
export declare function enforceOwnerAuthRequest(policy: OwnerAuthPolicy, request: Request): Promise<Response | null>;
|
|
192
|
+
export declare function createMultiUserAuth(options: MultiUserAuthOptions): MultiUserAuthInstance;
|
|
62
193
|
export declare function createOwnerAuth(options: OwnerAuthOptions): OwnerAuthInstance;
|
|
63
194
|
export {};
|
|
64
195
|
//# sourceMappingURL=index.d.ts.map
|