@pithy-sh/auth 0.1.2 → 0.1.3
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/dist/admin/users.d.ts +155 -0
- package/dist/admin/users.d.ts.map +1 -0
- package/dist/admin/users.js +205 -0
- package/dist/audit/actions.d.ts +66 -0
- package/dist/audit/actions.d.ts.map +1 -0
- package/dist/audit/actions.js +66 -0
- package/dist/audit/emit.d.ts +101 -0
- package/dist/audit/emit.d.ts.map +1 -0
- package/dist/audit/emit.js +170 -0
- package/dist/capability.d.ts +60 -0
- package/dist/capability.d.ts.map +1 -0
- package/dist/capability.js +169 -0
- package/dist/client/api.d.ts +299 -0
- package/dist/client/api.d.ts.map +1 -0
- package/dist/client/api.js +385 -0
- package/dist/client/projection.d.ts +51 -0
- package/dist/client/projection.d.ts.map +1 -0
- package/dist/client/projection.js +0 -0
- package/dist/data/betterAuth.d.ts +75 -0
- package/dist/data/betterAuth.d.ts.map +1 -0
- package/dist/data/betterAuth.js +92 -0
- package/dist/data/device.d.ts +36 -0
- package/dist/data/device.d.ts.map +1 -0
- package/dist/data/device.js +33 -0
- package/dist/data/kitFields.d.ts +78 -0
- package/dist/data/kitFields.d.ts.map +1 -0
- package/dist/data/kitFields.js +77 -0
- package/dist/data/rotatedToken.d.ts +20 -0
- package/dist/data/rotatedToken.d.ts.map +1 -0
- package/dist/data/rotatedToken.js +22 -0
- package/dist/data/tables.d.ts +104 -0
- package/dist/data/tables.d.ts.map +1 -0
- package/dist/data/tables.js +30 -0
- package/dist/device/registry.d.ts +54 -0
- package/dist/device/registry.d.ts.map +1 -0
- package/dist/device/registry.js +85 -0
- package/dist/email/send.d.ts +38 -0
- package/dist/email/send.d.ts.map +1 -0
- package/dist/email/send.js +48 -0
- package/dist/http/adminRoutes.d.ts +9 -0
- package/dist/http/adminRoutes.d.ts.map +1 -0
- package/dist/http/adminRoutes.js +274 -0
- package/dist/http/baseUrl.d.ts +41 -0
- package/dist/http/baseUrl.d.ts.map +1 -0
- package/dist/http/baseUrl.js +96 -0
- package/dist/http/csrf.d.ts +18 -0
- package/dist/http/csrf.d.ts.map +1 -0
- package/dist/http/csrf.js +81 -0
- package/dist/http/devLoginRoute.d.ts +42 -0
- package/dist/http/devLoginRoute.d.ts.map +1 -0
- package/dist/http/devLoginRoute.js +112 -0
- package/dist/http/errors.d.ts +24 -0
- package/dist/http/errors.d.ts.map +1 -0
- package/dist/http/errors.js +67 -0
- package/dist/http/guards.d.ts +103 -0
- package/dist/http/guards.d.ts.map +1 -0
- package/dist/http/guards.js +148 -0
- package/dist/http/middleware.d.ts +17 -0
- package/dist/http/middleware.d.ts.map +1 -0
- package/dist/http/middleware.js +47 -0
- package/dist/http/rateLimit.d.ts +17 -0
- package/dist/http/rateLimit.d.ts.map +1 -0
- package/dist/http/rateLimit.js +31 -0
- package/dist/http/resolve.d.ts +9 -0
- package/dist/http/resolve.d.ts.map +1 -0
- package/dist/http/resolve.js +92 -0
- package/dist/http/responses.d.ts +152 -0
- package/dist/http/responses.d.ts.map +1 -0
- package/dist/http/responses.js +121 -0
- package/dist/http/routes.d.ts +34 -0
- package/dist/http/routes.d.ts.map +1 -0
- package/dist/http/routes.js +268 -0
- package/dist/http/schemas.d.ts +66 -0
- package/dist/http/schemas.d.ts.map +1 -0
- package/dist/http/schemas.js +59 -0
- package/dist/http/views.d.ts +53 -0
- package/dist/http/views.d.ts.map +1 -0
- package/dist/http/views.js +86 -0
- package/dist/i18n/errorCopy.d.ts +24 -0
- package/dist/i18n/errorCopy.d.ts.map +1 -0
- package/dist/i18n/errorCopy.es.d.ts +31 -0
- package/dist/i18n/errorCopy.es.d.ts.map +1 -0
- package/dist/i18n/errorCopy.es.js +33 -0
- package/dist/i18n/errorCopy.js +91 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/instance/auth.d.ts +1122 -0
- package/dist/instance/auth.d.ts.map +1 -0
- package/dist/instance/auth.js +176 -0
- package/dist/instance/plugins.d.ts +87 -0
- package/dist/instance/plugins.d.ts.map +1 -0
- package/dist/instance/plugins.js +116 -0
- package/dist/instance/providers.d.ts +117 -0
- package/dist/instance/providers.d.ts.map +1 -0
- package/dist/instance/providers.js +111 -0
- package/dist/instance/secrets.d.ts +163 -0
- package/dist/instance/secrets.d.ts.map +1 -0
- package/dist/instance/secrets.js +169 -0
- package/dist/migrations/0001_init.d.ts +33 -0
- package/dist/migrations/0001_init.d.ts.map +1 -0
- package/dist/migrations/0001_init.js +77 -0
- package/dist/migrations/pluginTables.d.ts +126 -0
- package/dist/migrations/pluginTables.d.ts.map +1 -0
- package/dist/migrations/pluginTables.js +233 -0
- package/dist/seeds/devSession.d.ts +124 -0
- package/dist/seeds/devSession.d.ts.map +1 -0
- package/dist/seeds/devSession.js +211 -0
- package/dist/seeds/example.d.ts +12 -0
- package/dist/seeds/example.d.ts.map +1 -0
- package/dist/seeds/example.js +30 -0
- package/dist/test-utils/liveApp.d.ts +227 -0
- package/dist/test-utils/liveApp.d.ts.map +1 -0
- package/dist/test-utils/liveApp.js +240 -0
- package/dist/token/rotation.d.ts +56 -0
- package/dist/token/rotation.d.ts.map +1 -0
- package/dist/token/rotation.js +68 -0
- package/dist/version.generated.d.ts +5 -0
- package/dist/version.generated.d.ts.map +1 -0
- package/dist/version.generated.js +7 -0
- package/package.json +24 -13
- package/src/version.generated.ts +1 -1
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
import type { AuditEventInput } from "@pithy-sh/core/src/audit/auditEvent";
|
|
2
|
+
/**
|
|
3
|
+
* A real auth Worker on a real port, for the live suites (#84).
|
|
4
|
+
*
|
|
5
|
+
* The live E2E questions this package has are about an **origin**: which `redirect_uri` Better Auth
|
|
6
|
+
* hands Google, whether a token posted from a browser survives the humanity gate, whether a callback
|
|
7
|
+
* arriving with a forged `state` sets a cookie. None of those are answerable inside the workers pool,
|
|
8
|
+
* which has no listening socket and therefore no port a redirect URI could name; and none are
|
|
9
|
+
* answerable against a hand-built Better Auth instance either, because the thing under test is the
|
|
10
|
+
* route stack the capability composes — the turnstile gate on two paths and not a third, the CSRF
|
|
11
|
+
* publication, the rate limiter — rather than the instance underneath it.
|
|
12
|
+
*
|
|
13
|
+
* So this boots the real stack in Node:
|
|
14
|
+
*
|
|
15
|
+
* - **Miniflare supplies D1**, from Node rather than from inside a worker. Two databases, as a deployed
|
|
16
|
+
* worker has: `DB` for the `pithy_auth_*` and `pithy_email_*` tables, `SECRETS` for the encrypted
|
|
17
|
+
* rows every secret is read from (#153). No plaintext binding stands in for either.
|
|
18
|
+
* - **The migrations are the real ones**, run through the real registry and runner.
|
|
19
|
+
* - **`node:http` gives the port.** Vitest's `fetch` reaches the app the way a browser would, so an
|
|
20
|
+
* `Origin` header is a real one and a `Set-Cookie` is a real one.
|
|
21
|
+
*
|
|
22
|
+
* It reaches Cloudflare for nothing. What is live in a suite built on this is the third party the suite
|
|
23
|
+
* names — Google's token endpoint, Turnstile's siteverify — and nothing else.
|
|
24
|
+
*/
|
|
25
|
+
/** The registry these suites resolve through: auth's slice plus turnstile's, since one route stacks the gate. */
|
|
26
|
+
export declare const LIVE_REGISTRY: {
|
|
27
|
+
"auth-session-secret": {
|
|
28
|
+
readonly backend: "d1";
|
|
29
|
+
readonly scope: "environment";
|
|
30
|
+
readonly rotatable: true;
|
|
31
|
+
readonly valueType: "text";
|
|
32
|
+
readonly devValue: "random";
|
|
33
|
+
readonly origin: {
|
|
34
|
+
readonly kind: "minted";
|
|
35
|
+
readonly recipe: {
|
|
36
|
+
readonly kind: "random";
|
|
37
|
+
readonly bytes: 32;
|
|
38
|
+
readonly encoding: "base64url";
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
readonly rotation: {
|
|
42
|
+
readonly kind: "local";
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
"auth-google-credentials": {
|
|
46
|
+
readonly backend: "d1";
|
|
47
|
+
readonly scope: "environment";
|
|
48
|
+
readonly rotatable: false;
|
|
49
|
+
readonly valueType: "json";
|
|
50
|
+
readonly schema: import("zod").ZodObject<{
|
|
51
|
+
clientId: import("zod").ZodString;
|
|
52
|
+
clientSecret: import("zod").ZodString;
|
|
53
|
+
}, import("zod/v4/core").$strip>;
|
|
54
|
+
readonly origin: {
|
|
55
|
+
readonly kind: "obtained";
|
|
56
|
+
readonly issuer: "google";
|
|
57
|
+
readonly documentation: "https://console.cloud.google.com/apis/credentials";
|
|
58
|
+
};
|
|
59
|
+
readonly rotation: {
|
|
60
|
+
readonly kind: "manual";
|
|
61
|
+
readonly issuer: "google";
|
|
62
|
+
readonly documentation: "https://console.cloud.google.com/apis/credentials";
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
"auth-apple-credentials": {
|
|
66
|
+
readonly backend: "d1";
|
|
67
|
+
readonly scope: "environment";
|
|
68
|
+
readonly rotatable: true;
|
|
69
|
+
readonly valueType: "json";
|
|
70
|
+
readonly schema: import("zod").ZodObject<{
|
|
71
|
+
clientId: import("zod").ZodString;
|
|
72
|
+
clientSecret: import("zod").ZodString;
|
|
73
|
+
appBundleIdentifier: import("zod").ZodOptional<import("zod").ZodString>;
|
|
74
|
+
}, import("zod/v4/core").$strip>;
|
|
75
|
+
readonly origin: {
|
|
76
|
+
readonly kind: "obtained";
|
|
77
|
+
readonly issuer: "apple";
|
|
78
|
+
readonly documentation: "https://developer.apple.com/account/resources/authkeys/list";
|
|
79
|
+
};
|
|
80
|
+
readonly rotation: {
|
|
81
|
+
readonly kind: "manual";
|
|
82
|
+
readonly issuer: "apple";
|
|
83
|
+
readonly documentation: "https://developer.apple.com/account/resources/authkeys/list";
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
"auth-facebook-credentials": {
|
|
87
|
+
readonly backend: "d1";
|
|
88
|
+
readonly scope: "environment";
|
|
89
|
+
readonly rotatable: false;
|
|
90
|
+
readonly valueType: "json";
|
|
91
|
+
readonly schema: import("zod").ZodObject<{
|
|
92
|
+
clientId: import("zod").ZodString;
|
|
93
|
+
clientSecret: import("zod").ZodString;
|
|
94
|
+
}, import("zod/v4/core").$strip>;
|
|
95
|
+
readonly origin: {
|
|
96
|
+
readonly kind: "obtained";
|
|
97
|
+
readonly issuer: "facebook";
|
|
98
|
+
readonly documentation: "https://developers.facebook.com/apps/";
|
|
99
|
+
};
|
|
100
|
+
readonly rotation: {
|
|
101
|
+
readonly kind: "manual";
|
|
102
|
+
readonly issuer: "facebook";
|
|
103
|
+
readonly documentation: "https://developers.facebook.com/apps/";
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
"auth-github-credentials": {
|
|
107
|
+
readonly backend: "d1";
|
|
108
|
+
readonly scope: "environment";
|
|
109
|
+
readonly rotatable: false;
|
|
110
|
+
readonly valueType: "json";
|
|
111
|
+
readonly schema: import("zod").ZodObject<{
|
|
112
|
+
clientId: import("zod").ZodString;
|
|
113
|
+
clientSecret: import("zod").ZodString;
|
|
114
|
+
}, import("zod/v4/core").$strip>;
|
|
115
|
+
readonly origin: {
|
|
116
|
+
readonly kind: "obtained";
|
|
117
|
+
readonly issuer: "github";
|
|
118
|
+
readonly documentation: "https://github.com/settings/developers";
|
|
119
|
+
};
|
|
120
|
+
readonly rotation: {
|
|
121
|
+
readonly kind: "manual";
|
|
122
|
+
readonly issuer: "github";
|
|
123
|
+
readonly documentation: "https://github.com/settings/developers";
|
|
124
|
+
};
|
|
125
|
+
};
|
|
126
|
+
"turnstile-secret-keys": {
|
|
127
|
+
readonly backend: "d1";
|
|
128
|
+
readonly scope: "environment";
|
|
129
|
+
readonly rotatable: true;
|
|
130
|
+
readonly valueType: "json";
|
|
131
|
+
readonly schema: import("zod").ZodObject<{
|
|
132
|
+
visible: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
133
|
+
key: import("zod").ZodString;
|
|
134
|
+
lastRotatedAt: import("zod").ZodOptional<import("zod").ZodString>;
|
|
135
|
+
}, import("zod/v4/core").$strip>>;
|
|
136
|
+
invisible: import("zod").ZodOptional<import("zod").ZodObject<{
|
|
137
|
+
key: import("zod").ZodString;
|
|
138
|
+
lastRotatedAt: import("zod").ZodOptional<import("zod").ZodString>;
|
|
139
|
+
}, import("zod/v4/core").$strip>>;
|
|
140
|
+
}, import("zod/v4/core").$strict>;
|
|
141
|
+
readonly origin: {
|
|
142
|
+
readonly kind: "obtained";
|
|
143
|
+
readonly issuer: "cloudflare";
|
|
144
|
+
readonly documentation: "https://developers.cloudflare.com/turnstile/get-started/widget-management/dashboard/";
|
|
145
|
+
};
|
|
146
|
+
readonly rotation: {
|
|
147
|
+
readonly kind: "provider";
|
|
148
|
+
readonly issuer: "cloudflare";
|
|
149
|
+
readonly documentation: "https://developers.cloudflare.com/api/resources/turnstile/subresources/widgets/methods/rotate_secret/";
|
|
150
|
+
};
|
|
151
|
+
};
|
|
152
|
+
};
|
|
153
|
+
/** Unconfigure the module-scoped shared secrets store. For the end of a file, never for one app's close. */
|
|
154
|
+
export declare function resetLiveSecrets(): void;
|
|
155
|
+
/** An email job the app wrote: which template, and to whom. The body is never read — it carries a token. */
|
|
156
|
+
export interface EnqueuedEmail {
|
|
157
|
+
/** The job's template — `magic-link`, `otp`, … as `pithy_email_jobs` recorded it. */
|
|
158
|
+
template: string;
|
|
159
|
+
/** The recipient address. */
|
|
160
|
+
to: string;
|
|
161
|
+
}
|
|
162
|
+
/** What a suite may change about the app it boots. Everything else is the capability's own default. */
|
|
163
|
+
export interface LiveAppOptions {
|
|
164
|
+
/**
|
|
165
|
+
* The mount path, **always stated**.
|
|
166
|
+
*
|
|
167
|
+
* `AuthConfig` defaults it to `/auth`, and that default is precisely what a live suite must not lean
|
|
168
|
+
* on: every `redirect_uri` registered with an OAuth provider embeds this path, so moving it silently
|
|
169
|
+
* invalidates all of them at once, and a suite that took the default could not tell that it had moved.
|
|
170
|
+
* Required here, so the pin is in the suite where its reader can see it.
|
|
171
|
+
*/
|
|
172
|
+
basePath: string;
|
|
173
|
+
/** Whether Google is enabled. When true, `google` credentials must be supplied. */
|
|
174
|
+
google?: {
|
|
175
|
+
clientId: string;
|
|
176
|
+
clientSecret: string;
|
|
177
|
+
} | undefined;
|
|
178
|
+
/** When set, the turnstile gate is composed at this widget mode, exactly as `auth.compose` stacks it. */
|
|
179
|
+
turnstile?: {
|
|
180
|
+
mode: "visible" | "invisible";
|
|
181
|
+
secretKey: string;
|
|
182
|
+
} | undefined;
|
|
183
|
+
/**
|
|
184
|
+
* The `ENVIRONMENT` var this Worker is stamped with, as `pithy init` writes into every wrangler
|
|
185
|
+
* stanza. `dev` by default, because that is what these suites are: a developer's machine, running the
|
|
186
|
+
* stack the way `pithy dev` does.
|
|
187
|
+
*
|
|
188
|
+
* It is an option because one thing now reads it — the turnstile gate refuses a Cloudflare test key
|
|
189
|
+
* outside dev and staging (#374) — so a suite has to be able to say it is prod to prove that.
|
|
190
|
+
*/
|
|
191
|
+
environment?: string;
|
|
192
|
+
}
|
|
193
|
+
/** A booted app: where it listens, what it was pinned to, and what it recorded. */
|
|
194
|
+
export interface LiveApp {
|
|
195
|
+
/** The origin the app is actually serving on — `http://localhost:<port>`, port assigned by the OS. */
|
|
196
|
+
origin: string;
|
|
197
|
+
/** The mount path the suite pinned. Every asserted URL is composed from this, never from a literal. */
|
|
198
|
+
basePath: string;
|
|
199
|
+
/** The OAuth callback URL for a provider, composed from this run's origin and pinned base path. */
|
|
200
|
+
callbackUrl(provider: string): string;
|
|
201
|
+
/** Every audit event the app emitted, in order. */
|
|
202
|
+
events: AuditEventInput[];
|
|
203
|
+
/** Read the email jobs the app enqueued — the evidence a gated route did or did not reach its handler. */
|
|
204
|
+
enqueued(): Promise<EnqueuedEmail[]>;
|
|
205
|
+
/**
|
|
206
|
+
* The one-time code this app last mailed to an address, read out of the job payload.
|
|
207
|
+
*
|
|
208
|
+
* **What it is for is a signed-in browser.** A suite about a route that needs a session has to get
|
|
209
|
+
* one the way a reader does — ask for a code, then post it back — and the code exists nowhere a
|
|
210
|
+
* browser can see it. Reading the enqueued job is the harness standing in for the mailbox, and it is
|
|
211
|
+
* the only stand-in available: no Workflow runs here, so nothing is ever delivered or redacted.
|
|
212
|
+
*
|
|
213
|
+
* `null` when nothing was mailed to that address, which is a suite asserting on a send that never
|
|
214
|
+
* happened rather than a code that was blank.
|
|
215
|
+
*/
|
|
216
|
+
mailedOtp(to: string): Promise<string | null>;
|
|
217
|
+
/** Stop the server and dispose the runtime. Always from a `finally` or an `afterAll`. */
|
|
218
|
+
close(): Promise<void>;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Boot the auth Worker on an ephemeral port.
|
|
222
|
+
*
|
|
223
|
+
* The caller owns the teardown: `close()` stops the listener and disposes Miniflare, and a suite that
|
|
224
|
+
* forgets leaves a workerd process behind.
|
|
225
|
+
*/
|
|
226
|
+
export declare function startLiveApp(options: LiveAppOptions): Promise<LiveApp>;
|
|
227
|
+
//# sourceMappingURL=liveApp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"liveApp.d.ts","sourceRoot":"","sources":["../../src/test-utils/liveApp.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qCAAqC,CAAC;AAsB3E;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH,iHAAiH;AACjH,eAAO,MAAM,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAA0D,CAAC;AAErF,4GAA4G;AAC5G,wBAAgB,gBAAgB,IAAI,IAAI,CAEvC;AAwBD,4GAA4G;AAC5G,MAAM,WAAW,aAAa;IAC5B,qFAAqF;IACrF,QAAQ,EAAE,MAAM,CAAC;IACjB,6BAA6B;IAC7B,EAAE,EAAE,MAAM,CAAC;CACZ;AAED,uGAAuG;AACvG,MAAM,WAAW,cAAc;IAC7B;;;;;;;OAOG;IACH,QAAQ,EAAE,MAAM,CAAC;IACjB,mFAAmF;IACnF,MAAM,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IAChE,yGAAyG;IACzG,SAAS,CAAC,EAAE;QAAE,IAAI,EAAE,SAAS,GAAG,WAAW,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,GAAG,SAAS,CAAC;IAC7E;;;;;;;OAOG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,mFAAmF;AACnF,MAAM,WAAW,OAAO;IACtB,sGAAsG;IACtG,MAAM,EAAE,MAAM,CAAC;IACf,uGAAuG;IACvG,QAAQ,EAAE,MAAM,CAAC;IACjB,mGAAmG;IACnG,WAAW,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAC;IACtC,mDAAmD;IACnD,MAAM,EAAE,eAAe,EAAE,CAAC;IAC1B,0GAA0G;IAC1G,QAAQ,IAAI,OAAO,CAAC,aAAa,EAAE,CAAC,CAAC;IACrC;;;;;;;;;;OAUG;IACH,SAAS,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAC9C,yFAAyF;IACzF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED;;;;;GAKG;AACH,wBAAsB,YAAY,CAAC,OAAO,EAAE,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,CAsF5E"}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { publishSameOrigin } from "../http/csrf.js";
|
|
2
|
+
import { authSecretsRegistry } from "../instance/secrets.js";
|
|
3
|
+
import { createSessionMiddleware } from "../http/middleware.js";
|
|
4
|
+
import { createRateLimitMiddleware } from "../http/rateLimit.js";
|
|
5
|
+
import { createAuthRoutes } from "../http/routes.js";
|
|
6
|
+
import { auth_0001_init } from "../migrations/0001_init.js";
|
|
7
|
+
import { AuthConfig } from "../capability.js";
|
|
8
|
+
import { email } from "@pithy-sh/email/src/capability";
|
|
9
|
+
import { configureSharedSecrets, resetSharedSecrets } from "@pithy-sh/secrets/src/sharedSecretsStore";
|
|
10
|
+
import { createServer } from "node:http";
|
|
11
|
+
import { pithyErrorHandler } from "@pithy-sh/core/src/error/http";
|
|
12
|
+
import { createMigrationRegistry } from "@pithy-sh/core/src/migrations/registry";
|
|
13
|
+
import { runMigrations } from "@pithy-sh/core/src/migrations/runner";
|
|
14
|
+
import { email_0001_init } from "@pithy-sh/email/src/migrations/0001_init";
|
|
15
|
+
import { devEncryptionKeys } from "@pithy-sh/secrets/src/test-utils/devEncryptionKeys";
|
|
16
|
+
import { seedSecrets } from "@pithy-sh/secrets/src/test-utils/secretFixtures";
|
|
17
|
+
import { turnstileSecretsRegistry } from "@pithy-sh/turnstile/src/secret/registry";
|
|
18
|
+
import { Hono } from "hono";
|
|
19
|
+
import { Miniflare } from "miniflare";
|
|
20
|
+
//#region src/test-utils/liveApp.ts
|
|
21
|
+
/**
|
|
22
|
+
* A real auth Worker on a real port, for the live suites (#84).
|
|
23
|
+
*
|
|
24
|
+
* The live E2E questions this package has are about an **origin**: which `redirect_uri` Better Auth
|
|
25
|
+
* hands Google, whether a token posted from a browser survives the humanity gate, whether a callback
|
|
26
|
+
* arriving with a forged `state` sets a cookie. None of those are answerable inside the workers pool,
|
|
27
|
+
* which has no listening socket and therefore no port a redirect URI could name; and none are
|
|
28
|
+
* answerable against a hand-built Better Auth instance either, because the thing under test is the
|
|
29
|
+
* route stack the capability composes — the turnstile gate on two paths and not a third, the CSRF
|
|
30
|
+
* publication, the rate limiter — rather than the instance underneath it.
|
|
31
|
+
*
|
|
32
|
+
* So this boots the real stack in Node:
|
|
33
|
+
*
|
|
34
|
+
* - **Miniflare supplies D1**, from Node rather than from inside a worker. Two databases, as a deployed
|
|
35
|
+
* worker has: `DB` for the `pithy_auth_*` and `pithy_email_*` tables, `SECRETS` for the encrypted
|
|
36
|
+
* rows every secret is read from (#153). No plaintext binding stands in for either.
|
|
37
|
+
* - **The migrations are the real ones**, run through the real registry and runner.
|
|
38
|
+
* - **`node:http` gives the port.** Vitest's `fetch` reaches the app the way a browser would, so an
|
|
39
|
+
* `Origin` header is a real one and a `Set-Cookie` is a real one.
|
|
40
|
+
*
|
|
41
|
+
* It reaches Cloudflare for nothing. What is live in a suite built on this is the third party the suite
|
|
42
|
+
* names — Google's token endpoint, Turnstile's siteverify — and nothing else.
|
|
43
|
+
*/
|
|
44
|
+
/** The registry these suites resolve through: auth's slice plus turnstile's, since one route stacks the gate. */
|
|
45
|
+
const LIVE_REGISTRY = {
|
|
46
|
+
...authSecretsRegistry,
|
|
47
|
+
...turnstileSecretsRegistry
|
|
48
|
+
};
|
|
49
|
+
/** Unconfigure the module-scoped shared secrets store. For the end of a file, never for one app's close. */
|
|
50
|
+
function resetLiveSecrets() {
|
|
51
|
+
resetSharedSecrets();
|
|
52
|
+
}
|
|
53
|
+
/** Tables the app database holds once both capabilities' migrations have run. */
|
|
54
|
+
const TABLES = [
|
|
55
|
+
"pithy_auth_accounts",
|
|
56
|
+
"pithy_auth_devices",
|
|
57
|
+
"pithy_auth_jwks",
|
|
58
|
+
"pithy_auth_rate_limit",
|
|
59
|
+
"pithy_auth_rotated_tokens",
|
|
60
|
+
"pithy_auth_sessions",
|
|
61
|
+
"pithy_auth_users",
|
|
62
|
+
"pithy_auth_verifications",
|
|
63
|
+
"pithy_email_jobs",
|
|
64
|
+
"pithy_email_events"
|
|
65
|
+
];
|
|
66
|
+
/**
|
|
67
|
+
* The session secret these suites sign with.
|
|
68
|
+
*
|
|
69
|
+
* A fixed throwaway rather than a random one, so a failure is reproducible; it authenticates nothing
|
|
70
|
+
* anywhere, and it is not a value the fixture report or an error message ever carries.
|
|
71
|
+
*/
|
|
72
|
+
const SESSION_SECRET = "live-e2e-secret-please-rotate-000000";
|
|
73
|
+
/**
|
|
74
|
+
* Boot the auth Worker on an ephemeral port.
|
|
75
|
+
*
|
|
76
|
+
* The caller owns the teardown: `close()` stops the listener and disposes Miniflare, and a suite that
|
|
77
|
+
* forgets leaves a workerd process behind.
|
|
78
|
+
*/
|
|
79
|
+
async function startLiveApp(options) {
|
|
80
|
+
const miniflare = new Miniflare({
|
|
81
|
+
modules: true,
|
|
82
|
+
script: "export default {};",
|
|
83
|
+
d1Databases: {
|
|
84
|
+
DB: "DB",
|
|
85
|
+
SECRETS: "SECRETS"
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
const db = await miniflare.getD1Database("DB");
|
|
89
|
+
const env = {
|
|
90
|
+
DB: db,
|
|
91
|
+
SECRETS: await miniflare.getD1Database("SECRETS"),
|
|
92
|
+
SECRETS_ENCRYPTION_KEYS: devEncryptionKeys(),
|
|
93
|
+
ENVIRONMENT: options.environment ?? "dev",
|
|
94
|
+
AUTH_RATE_LIMITER: { limit: async () => ({ success: true }) }
|
|
95
|
+
};
|
|
96
|
+
await migrate(db);
|
|
97
|
+
const fixture = { "auth-session-secret": SESSION_SECRET };
|
|
98
|
+
if (options.google) fixture["auth-google-credentials"] = options.google;
|
|
99
|
+
if (options.turnstile) fixture["turnstile-secret-keys"] = { [options.turnstile.mode]: { key: options.turnstile.secretKey } };
|
|
100
|
+
configureSharedSecrets({ registry: LIVE_REGISTRY });
|
|
101
|
+
await seedSecrets(env, LIVE_REGISTRY, fixture);
|
|
102
|
+
const events = [];
|
|
103
|
+
const server = createServer();
|
|
104
|
+
const origin = await listen(server);
|
|
105
|
+
const emailCapability = email({
|
|
106
|
+
fromAddress: "no@reply.test",
|
|
107
|
+
fromName: "Live E2E",
|
|
108
|
+
baseUrl: origin
|
|
109
|
+
});
|
|
110
|
+
const app = buildApp({
|
|
111
|
+
config: AuthConfig.parse({
|
|
112
|
+
baseURL: origin,
|
|
113
|
+
basePath: options.basePath,
|
|
114
|
+
trustedOrigins: [origin],
|
|
115
|
+
google: { enabled: Boolean(options.google) }
|
|
116
|
+
}),
|
|
117
|
+
enqueueEmail: emailCapability.enqueue,
|
|
118
|
+
turnstile: options.turnstile ? { mode: options.turnstile.mode } : void 0
|
|
119
|
+
}, events);
|
|
120
|
+
server.on("request", (request, response) => {
|
|
121
|
+
serve(app, env, origin, request, response);
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
origin,
|
|
125
|
+
basePath: options.basePath,
|
|
126
|
+
callbackUrl: (provider) => `${origin}${options.basePath}/callback/${provider}`,
|
|
127
|
+
events,
|
|
128
|
+
enqueued: async () => {
|
|
129
|
+
return (await db.prepare("select template, to_address from pithy_email_jobs order by rowid").all()).results.map((row) => ({
|
|
130
|
+
template: row.template,
|
|
131
|
+
to: row.to_address
|
|
132
|
+
}));
|
|
133
|
+
},
|
|
134
|
+
mailedOtp: async (to) => {
|
|
135
|
+
const row = await db.prepare("select payload from pithy_email_jobs where to_address = ? and template = 'otp' order by rowid desc limit 1").bind(to).first();
|
|
136
|
+
if (!row) return null;
|
|
137
|
+
const parsed = JSON.parse(row.payload);
|
|
138
|
+
const code = typeof parsed === "object" && parsed !== null ? parsed.code : void 0;
|
|
139
|
+
return typeof code === "string" ? code : null;
|
|
140
|
+
},
|
|
141
|
+
close: async () => {
|
|
142
|
+
await new Promise((resolve) => server.close(() => resolve()));
|
|
143
|
+
await miniflare.dispose();
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
/** Run auth's and email's real migrations against a fresh database. */
|
|
148
|
+
async function migrate(db) {
|
|
149
|
+
for (const table of [
|
|
150
|
+
...TABLES,
|
|
151
|
+
"pithy_migrations",
|
|
152
|
+
"pithy_migrations_lock"
|
|
153
|
+
]) await db.prepare(`drop table if exists ${table}`).run();
|
|
154
|
+
const provider = createMigrationRegistry([{
|
|
155
|
+
database: "app",
|
|
156
|
+
namespace: "auth",
|
|
157
|
+
order: 300,
|
|
158
|
+
migrations: { "0001_init": auth_0001_init }
|
|
159
|
+
}, {
|
|
160
|
+
database: "app",
|
|
161
|
+
namespace: "email",
|
|
162
|
+
order: 200,
|
|
163
|
+
migrations: { "0001_init": email_0001_init }
|
|
164
|
+
}]).app;
|
|
165
|
+
if (!provider) throw new Error("expected a migration provider for database \"app\"");
|
|
166
|
+
await runMigrations(db, provider);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* The capability's middleware order, mirrored: same-origin publication, the tier-1 limiter, session
|
|
170
|
+
* resolution, then the routes.
|
|
171
|
+
*
|
|
172
|
+
* Mirrored rather than obtained from `auth().compose()` because composing the capability needs a
|
|
173
|
+
* `pithy.config.ts`, a project on disk and a CLI to resolve it. The order here is the order
|
|
174
|
+
* `capability.ts` registers, and `routeContract.test.ts` is what holds that claim honest.
|
|
175
|
+
*/
|
|
176
|
+
function buildApp(wiring, events) {
|
|
177
|
+
const app = new Hono();
|
|
178
|
+
app.onError(pithyErrorHandler);
|
|
179
|
+
app.use("*", async (c, next) => {
|
|
180
|
+
if (c.get("emit") === void 0) c.set("emit", async (event) => {
|
|
181
|
+
events.push(event);
|
|
182
|
+
});
|
|
183
|
+
if (c.get("auth") === void 0) c.set("auth", null);
|
|
184
|
+
await next();
|
|
185
|
+
});
|
|
186
|
+
publishSameOrigin(wiring)(app);
|
|
187
|
+
app.use(`${wiring.config.basePath}/*`, createRateLimitMiddleware(wiring.config.rateLimiterBinding));
|
|
188
|
+
createSessionMiddleware(wiring)(app);
|
|
189
|
+
createAuthRoutes(wiring)(app);
|
|
190
|
+
return app;
|
|
191
|
+
}
|
|
192
|
+
/** Listen on an OS-assigned port and answer with the origin that names it. */
|
|
193
|
+
function listen(server) {
|
|
194
|
+
return new Promise((resolve, reject) => {
|
|
195
|
+
server.once("error", reject);
|
|
196
|
+
server.listen(0, "127.0.0.1", () => {
|
|
197
|
+
resolve(`http://localhost:${server.address().port}`);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
/** Hand one Node request to the Hono app and write back what it answered. */
|
|
202
|
+
async function serve(app, env, origin, request, response) {
|
|
203
|
+
try {
|
|
204
|
+
const answer = await app.fetch(await toRequest(request, origin), env);
|
|
205
|
+
const headers = [];
|
|
206
|
+
for (const [key, value] of answer.headers) if (key.toLowerCase() !== "set-cookie") headers.push([key, value]);
|
|
207
|
+
for (const cookie of answer.headers.getSetCookie()) headers.push(["set-cookie", cookie]);
|
|
208
|
+
response.writeHead(answer.status, headers);
|
|
209
|
+
response.end(answer.body ? Buffer.from(await answer.arrayBuffer()) : void 0);
|
|
210
|
+
} catch (cause) {
|
|
211
|
+
response.writeHead(500, { "content-type": "text/plain" });
|
|
212
|
+
response.end(`live harness failed: ${cause instanceof Error ? cause.message : String(cause)}`);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
/** A Node request as a Web `Request`, body and all. */
|
|
216
|
+
async function toRequest(request, origin) {
|
|
217
|
+
const method = request.method ?? "GET";
|
|
218
|
+
const headers = new Headers();
|
|
219
|
+
for (const [key, value] of Object.entries(request.headers)) {
|
|
220
|
+
if (value === void 0) continue;
|
|
221
|
+
for (const one of Array.isArray(value) ? value : [value]) headers.append(key, one);
|
|
222
|
+
}
|
|
223
|
+
const body = method !== "GET" && method !== "HEAD" ? await readBody(request) : void 0;
|
|
224
|
+
return new Request(new URL(request.url ?? "/", origin), {
|
|
225
|
+
method,
|
|
226
|
+
headers,
|
|
227
|
+
body
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
/** Collect a request body. */
|
|
231
|
+
function readBody(request) {
|
|
232
|
+
return new Promise((resolve, reject) => {
|
|
233
|
+
const chunks = [];
|
|
234
|
+
request.on("data", (chunk) => chunks.push(chunk));
|
|
235
|
+
request.on("end", () => resolve(Buffer.concat(chunks)));
|
|
236
|
+
request.on("error", reject);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
//#endregion
|
|
240
|
+
export { LIVE_REGISTRY, resetLiveSecrets, startLiveApp };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { AuthDatabase } from "../data/tables";
|
|
2
|
+
/**
|
|
3
|
+
* Refresh-token rotation primitives: the atomic consume that makes rotation race-safe, and the
|
|
4
|
+
* reuse-detection ledger that turns a replayed refresh token into a family-wide revocation
|
|
5
|
+
* (OAuth refresh rotation, RFC 6819 §5.2.2.3). The HTTP route (`http/routes.ts`) composes these; they
|
|
6
|
+
* stay here as small, directly testable units over the shared Kysely.
|
|
7
|
+
*/
|
|
8
|
+
/** The outcome of consuming a session: whether THIS call removed the row, and the consumed family. */
|
|
9
|
+
export interface ConsumeResult {
|
|
10
|
+
/** True when this call deleted the session row — the single winner of a concurrent rotation. */
|
|
11
|
+
won: boolean;
|
|
12
|
+
/** The consumed session's family id, or null when it had none (a fresh, never-rotated session). */
|
|
13
|
+
familyId: string | null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Atomically consume a live session by its token. The conditional `DELETE ... RETURNING` is the single
|
|
17
|
+
* race gate: of N concurrent rotations presenting the same token, exactly one deletes the row and sees
|
|
18
|
+
* `won: true`; the rest see `won: false` and must not mint a successor. One presented token yields one
|
|
19
|
+
* successor.
|
|
20
|
+
*/
|
|
21
|
+
export declare function consumeSession(db: AuthDatabase, token: string): Promise<ConsumeResult>;
|
|
22
|
+
/**
|
|
23
|
+
* Record a consumed token in the reuse-detection ledger, tagged with the family it belonged to. Idempotent
|
|
24
|
+
* on the token PK — a redundant record (e.g. a retried write) is a no-op, never an error.
|
|
25
|
+
*/
|
|
26
|
+
export declare function recordConsumedToken(db: AuthDatabase, entry: {
|
|
27
|
+
token: string;
|
|
28
|
+
familyId: string;
|
|
29
|
+
userId: string;
|
|
30
|
+
rotatedAt: Date;
|
|
31
|
+
}): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* The family/owner a consumed token belonged to and when it was consumed, or null when the token was
|
|
34
|
+
* never consumed. `rotatedAt` lets the caller tell a benign concurrent/retried rotation (a fresh
|
|
35
|
+
* consume) from a genuinely replayed refresh token (an old one).
|
|
36
|
+
*/
|
|
37
|
+
export declare function findConsumedToken(db: AuthDatabase, token: string): Promise<{
|
|
38
|
+
familyId: string;
|
|
39
|
+
userId: string;
|
|
40
|
+
rotatedAt: Date;
|
|
41
|
+
} | null>;
|
|
42
|
+
/** The tokens of every live session in a family — the set a family revocation must sign out. */
|
|
43
|
+
export declare function familySessionTokens(db: AuthDatabase, familyId: string): Promise<string[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Revoke an entire refresh-token family: sign out every live session sharing the id. Deletion goes
|
|
46
|
+
* through Better Auth's `deleteSession` (injected) so any adapter-side bookkeeping stays consistent —
|
|
47
|
+
* the same path device-revoke uses. Returns the number of sessions revoked.
|
|
48
|
+
*/
|
|
49
|
+
export declare function revokeFamily(db: AuthDatabase, familyId: string, deleteSession: (token: string) => Promise<unknown>): Promise<number>;
|
|
50
|
+
/**
|
|
51
|
+
* Prune ledger entries consumed before `cutoff`, bounding the table's growth. A token consumed longer
|
|
52
|
+
* ago than a full session lifetime can no longer match any live session, so its reuse-detection value
|
|
53
|
+
* has lapsed. The `rotatedAt` index keeps this an index range delete. Returns the number of rows removed.
|
|
54
|
+
*/
|
|
55
|
+
export declare function pruneConsumedTokens(db: AuthDatabase, cutoff: Date): Promise<number>;
|
|
56
|
+
//# sourceMappingURL=rotation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rotation.d.ts","sourceRoot":"","sources":["../../src/token/rotation.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAEnD;;;;;GAKG;AAEH,sGAAsG;AACtG,MAAM,WAAW,aAAa;IAC5B,gGAAgG;IAChG,GAAG,EAAE,OAAO,CAAC;IACb,mGAAmG;IACnG,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;CACzB;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,EAAE,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC,CAO5F;AAED;;;GAGG;AACH,wBAAsB,mBAAmB,CACvC,EAAE,EAAE,YAAY,EAChB,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE,GAC1E,OAAO,CAAC,IAAI,CAAC,CAMf;AAED;;;;GAIG;AACH,wBAAsB,iBAAiB,CACrC,EAAE,EAAE,YAAY,EAChB,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,IAAI,CAAA;CAAE,GAAG,IAAI,CAAC,CAOvE;AAED,gGAAgG;AAChG,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAG/F;AAED;;;;GAIG;AACH,wBAAsB,YAAY,CAChC,EAAE,EAAE,YAAY,EAChB,QAAQ,EAAE,MAAM,EAChB,aAAa,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,GACjD,OAAO,CAAC,MAAM,CAAC,CAMjB;AAED;;;;GAIG;AACH,wBAAsB,mBAAmB,CAAC,EAAE,EAAE,YAAY,EAAE,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAMzF"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { RotatedToken } from "../data/rotatedToken.js";
|
|
2
|
+
import { SQLiteDate } from "@pithy-sh/core/src/data/codecs";
|
|
3
|
+
//#region src/token/rotation.ts
|
|
4
|
+
/**
|
|
5
|
+
* Atomically consume a live session by its token. The conditional `DELETE ... RETURNING` is the single
|
|
6
|
+
* race gate: of N concurrent rotations presenting the same token, exactly one deletes the row and sees
|
|
7
|
+
* `won: true`; the rest see `won: false` and must not mint a successor. One presented token yields one
|
|
8
|
+
* successor.
|
|
9
|
+
*/
|
|
10
|
+
async function consumeSession(db, token) {
|
|
11
|
+
const row = await db.deleteFrom("pithyAuthSessions").where("token", "=", token).returning("familyId").executeTakeFirst();
|
|
12
|
+
return row ? {
|
|
13
|
+
won: true,
|
|
14
|
+
familyId: row.familyId ?? null
|
|
15
|
+
} : {
|
|
16
|
+
won: false,
|
|
17
|
+
familyId: null
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Record a consumed token in the reuse-detection ledger, tagged with the family it belonged to. Idempotent
|
|
22
|
+
* on the token PK — a redundant record (e.g. a retried write) is a no-op, never an error.
|
|
23
|
+
*/
|
|
24
|
+
async function recordConsumedToken(db, entry) {
|
|
25
|
+
await db.insertInto("pithyAuthRotatedTokens").values(RotatedToken.encode(entry)).onConflict((oc) => oc.column("token").doNothing()).execute();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The family/owner a consumed token belonged to and when it was consumed, or null when the token was
|
|
29
|
+
* never consumed. `rotatedAt` lets the caller tell a benign concurrent/retried rotation (a fresh
|
|
30
|
+
* consume) from a genuinely replayed refresh token (an old one).
|
|
31
|
+
*/
|
|
32
|
+
async function findConsumedToken(db, token) {
|
|
33
|
+
const row = await db.selectFrom("pithyAuthRotatedTokens").select([
|
|
34
|
+
"familyId",
|
|
35
|
+
"userId",
|
|
36
|
+
"rotatedAt"
|
|
37
|
+
]).where("token", "=", token).executeTakeFirst();
|
|
38
|
+
return row ? {
|
|
39
|
+
familyId: row.familyId,
|
|
40
|
+
userId: row.userId,
|
|
41
|
+
rotatedAt: SQLiteDate.parse(row.rotatedAt)
|
|
42
|
+
} : null;
|
|
43
|
+
}
|
|
44
|
+
/** The tokens of every live session in a family — the set a family revocation must sign out. */
|
|
45
|
+
async function familySessionTokens(db, familyId) {
|
|
46
|
+
return (await db.selectFrom("pithyAuthSessions").select("token").where("familyId", "=", familyId).execute()).map((row) => row.token);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Revoke an entire refresh-token family: sign out every live session sharing the id. Deletion goes
|
|
50
|
+
* through Better Auth's `deleteSession` (injected) so any adapter-side bookkeeping stays consistent —
|
|
51
|
+
* the same path device-revoke uses. Returns the number of sessions revoked.
|
|
52
|
+
*/
|
|
53
|
+
async function revokeFamily(db, familyId, deleteSession) {
|
|
54
|
+
const tokens = await familySessionTokens(db, familyId);
|
|
55
|
+
for (const token of tokens) await deleteSession(token);
|
|
56
|
+
return tokens.length;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Prune ledger entries consumed before `cutoff`, bounding the table's growth. A token consumed longer
|
|
60
|
+
* ago than a full session lifetime can no longer match any live session, so its reuse-detection value
|
|
61
|
+
* has lapsed. The `rotatedAt` index keeps this an index range delete. Returns the number of rows removed.
|
|
62
|
+
*/
|
|
63
|
+
async function pruneConsumedTokens(db, cutoff) {
|
|
64
|
+
const result = await db.deleteFrom("pithyAuthRotatedTokens").where("rotatedAt", "<", SQLiteDate.encode(cutoff)).executeTakeFirst();
|
|
65
|
+
return Number(result.numDeletedRows ?? 0n);
|
|
66
|
+
}
|
|
67
|
+
//#endregion
|
|
68
|
+
export { consumeSession, familySessionTokens, findConsumedToken, pruneConsumedTokens, recordConsumedToken, revokeFamily };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/** This package's npm name — the join key against a release feed. */
|
|
2
|
+
export declare const PACKAGE_NAME = "@pithy-sh/auth";
|
|
3
|
+
/** This package's version, stamped from its own package.json at generation time. */
|
|
4
|
+
export declare const PACKAGE_VERSION = "0.1.3";
|
|
5
|
+
//# sourceMappingURL=version.generated.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"version.generated.d.ts","sourceRoot":"","sources":["../src/version.generated.ts"],"names":[],"mappings":"AAWA,qEAAqE;AACrE,eAAO,MAAM,YAAY,mBAAmB,CAAC;AAE7C,oFAAoF;AACpF,eAAO,MAAM,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/version.generated.ts
|
|
2
|
+
/** This package's npm name — the join key against a release feed. */
|
|
3
|
+
const PACKAGE_NAME = "@pithy-sh/auth";
|
|
4
|
+
/** This package's version, stamped from its own package.json at generation time. */
|
|
5
|
+
const PACKAGE_VERSION = "0.1.3";
|
|
6
|
+
//#endregion
|
|
7
|
+
export { PACKAGE_NAME, PACKAGE_VERSION };
|