@pithy-sh/auth 0.14.1 → 0.15.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 +4 -0
- package/dist/audit/actions.d.ts +32 -28
- package/dist/audit/actions.d.ts.map +1 -1
- package/dist/audit/actions.js +32 -28
- package/dist/audit/emit.d.ts +40 -34
- package/dist/audit/emit.d.ts.map +1 -1
- package/dist/audit/emit.js +45 -32
- package/dist/capability.js +1 -1
- package/dist/http/middleware.js +1 -1
- package/dist/http/providerFreshness.d.ts +30 -31
- package/dist/http/providerFreshness.d.ts.map +1 -1
- package/dist/http/providerFreshness.js +26 -31
- package/dist/http/recentSignIn.d.ts +57 -0
- package/dist/http/recentSignIn.d.ts.map +1 -0
- package/dist/http/recentSignIn.js +88 -0
- package/dist/http/routes.js +2 -2
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/instance/auth.js +2 -2
- package/dist/mcp/http/routes.js +2 -2
- package/dist/test-utils/dropOrder.d.ts +26 -0
- package/dist/test-utils/dropOrder.d.ts.map +1 -0
- package/dist/test-utils/dropOrder.js +120 -0
- package/dist/test-utils/scopedBackend.d.ts +10 -6
- package/dist/test-utils/scopedBackend.d.ts.map +1 -1
- package/dist/test-utils/scopedBackend.js +40 -12
- package/dist/version.generated.d.ts +1 -1
- package/dist/version.generated.js +1 -1
- package/docs/account-lifecycle.md +1 -1
- package/docs/recent-sign-in.md +90 -0
- package/package.json +5 -5
- package/src/audit/actions.ts +32 -28
- package/src/audit/emit.ts +50 -34
- package/src/http/providerFreshness.ts +40 -42
- package/src/http/recentSignIn.ts +114 -0
- package/src/index.ts +4 -2
- package/src/test-utils/dropOrder.ts +139 -0
- package/src/test-utils/scopedBackend.ts +51 -13
- package/src/version.generated.ts +1 -1
|
@@ -16,14 +16,15 @@ import { NO_SOCIAL_PROVIDERS } from "../instance/providers";
|
|
|
16
16
|
import { authSecretsRegistry } from "../instance/secrets";
|
|
17
17
|
import { AUTH_MIGRATION_ORDER } from "../migrations/0001_init";
|
|
18
18
|
import { AUTH_MIGRATIONS } from "../migrations/set";
|
|
19
|
+
import { dropOrder } from "./dropOrder";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
|
-
* The fixtures a workers suite needs to put a **real** `auth()` in front of a request
|
|
22
|
+
* The fixtures a workers suite needs to put a **real** `auth()` in front of a request.
|
|
22
23
|
*
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
24
|
+
* A gate tested against a stub middleware that writes `c.var.auth` itself passes whether or not anything
|
|
25
|
+
* real can open it. A suite built on this composes the actual capability through `createBackend` and signs
|
|
26
|
+
* a user in with the actual Better Auth instance, so what reaches a gate is what a deployed Worker would put
|
|
27
|
+
* there.
|
|
27
28
|
*
|
|
28
29
|
* It holds the pieces and not the composition. Which capabilities sit beside auth is the suite's own
|
|
29
30
|
* statement, and so is the probe it reads the request through.
|
|
@@ -61,15 +62,14 @@ export interface ExtraMigrations {
|
|
|
61
62
|
*
|
|
62
63
|
* Every table rather than a list of them, so a suite composing a capability beside auth does not have to
|
|
63
64
|
* restate what that capability's migrations create, and a second test never meets the first one's rows.
|
|
65
|
+
* Whatever the foreign keys between an app's own tables, children go before the tables they reference; a
|
|
66
|
+
* cycle no order can drop is refused with its tables named, before anything is dropped.
|
|
64
67
|
*/
|
|
65
68
|
export async function prepareScopedBackend(
|
|
66
69
|
env: SecretsStoreEnv & { DB: D1Database },
|
|
67
70
|
extra: readonly ExtraMigrations[] = [],
|
|
68
71
|
): Promise<void> {
|
|
69
|
-
|
|
70
|
-
"select name from sqlite_master where type = 'table' and name not like 'sqlite_%' and name not like '\\_cf\\_%' escape '\\'",
|
|
71
|
-
).all<{ name: string }>();
|
|
72
|
-
for (const { name } of tables.results) await env.DB.prepare(`drop table if exists "${name}"`).run();
|
|
72
|
+
await dropEveryTable(env.DB);
|
|
73
73
|
const provider = createMigrationRegistry([
|
|
74
74
|
{ database: "app", namespace: "auth", order: AUTH_MIGRATION_ORDER, migrations: AUTH_MIGRATIONS },
|
|
75
75
|
{ database: "app", namespace: "email", order: EMAIL_MIGRATION_ORDER, migrations: { "0001_init": email_0001_init } },
|
|
@@ -80,6 +80,38 @@ export async function prepareScopedBackend(
|
|
|
80
80
|
await seedSecrets(env, SCOPED_REGISTRY, SCOPED_SECRETS);
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
+
/**
|
|
84
|
+
* Drop every table but SQLite's and D1's own, in the order `dropOrder` reads off the schema's foreign keys.
|
|
85
|
+
*
|
|
86
|
+
* D1 will not turn `foreign_keys` off, so the keys stay on and the order carries the drop. One batch, so
|
|
87
|
+
* `defer_foreign_keys` holds across every drop in it. Outside a batch it lasts one statement, since each
|
|
88
|
+
* statement commits on its own.
|
|
89
|
+
*/
|
|
90
|
+
async function dropEveryTable(db: D1Database): Promise<void> {
|
|
91
|
+
const tables = await db
|
|
92
|
+
.prepare(
|
|
93
|
+
"select name from sqlite_master where type = 'table' and name not like 'sqlite_%' and name not like '\\_cf\\_%' escape '\\'",
|
|
94
|
+
)
|
|
95
|
+
.all<{ name: string }>();
|
|
96
|
+
const names = tables.results.map(({ name }) => name);
|
|
97
|
+
if (names.length === 0) return;
|
|
98
|
+
const keyLists = await db.batch<{ table: string; on_delete: string }>(
|
|
99
|
+
names.map((name) => db.prepare(`pragma foreign_key_list(${quoted(name)})`)),
|
|
100
|
+
);
|
|
101
|
+
const foreignKeys = names.flatMap((name, index) =>
|
|
102
|
+
(keyLists[index]?.results ?? []).map((key) => ({ table: name, references: key.table, onDelete: key.on_delete })),
|
|
103
|
+
);
|
|
104
|
+
await db.batch([
|
|
105
|
+
db.prepare("pragma defer_foreign_keys = on"),
|
|
106
|
+
...dropOrder(names, foreignKeys).map((name) => db.prepare(`drop table if exists ${quoted(name)}`)),
|
|
107
|
+
]);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** A table name as an SQL identifier. */
|
|
111
|
+
function quoted(name: string): string {
|
|
112
|
+
return `"${name.replaceAll('"', '""')}"`;
|
|
113
|
+
}
|
|
114
|
+
|
|
83
115
|
/**
|
|
84
116
|
* The Worker env one request runs against: the real `DB` and `SECRETS`, and stand-ins for the three
|
|
85
117
|
* bindings this composition requires and never exercises — no email is sent and the limiter always allows.
|
|
@@ -96,7 +128,8 @@ export function scopedWorkerEnv(env: Record<string, unknown>, db: unknown = env.
|
|
|
96
128
|
}
|
|
97
129
|
|
|
98
130
|
/**
|
|
99
|
-
* Sign a user in by OTP against the same database the composed backend reads, and return a bearer token
|
|
131
|
+
* Sign a user in by OTP against the same database the composed backend reads, and return a bearer token and
|
|
132
|
+
* the session cookie the same sign-in sets, as a `cookie` header value.
|
|
100
133
|
*
|
|
101
134
|
* Directly through Better Auth rather than over HTTP: the sign-in flow is not what these suites are about,
|
|
102
135
|
* and the session row it writes is exactly the one a request presenting the token resolves.
|
|
@@ -104,7 +137,7 @@ export function scopedWorkerEnv(env: Record<string, unknown>, db: unknown = env.
|
|
|
104
137
|
export async function signInScoped(
|
|
105
138
|
db: D1Database,
|
|
106
139
|
address = "player@test.com",
|
|
107
|
-
): Promise<{ token: string; userId: string }> {
|
|
140
|
+
): Promise<{ token: string; userId: string; cookie: string }> {
|
|
108
141
|
const mailbox: { template: string; code?: string }[] = [];
|
|
109
142
|
const instance = makeAuth({
|
|
110
143
|
db: authDatabase(db),
|
|
@@ -129,9 +162,14 @@ export async function signInScoped(
|
|
|
129
162
|
await instance.api.sendVerificationOTP({ body: { email: address, type: "sign-in" }, headers: new Headers() });
|
|
130
163
|
const otp = mailbox.find((m) => m.template === "otp");
|
|
131
164
|
if (!otp?.code) throw new Error("no OTP was mailed");
|
|
132
|
-
const
|
|
165
|
+
const { headers, response } = await instance.api.signInEmailOTP({
|
|
133
166
|
body: { email: address, otp: otp.code },
|
|
134
167
|
headers: new Headers(),
|
|
168
|
+
returnHeaders: true,
|
|
135
169
|
});
|
|
136
|
-
|
|
170
|
+
const cookie = headers
|
|
171
|
+
.getSetCookie()
|
|
172
|
+
.map((line) => line.split(";")[0])
|
|
173
|
+
.join("; ");
|
|
174
|
+
return { token: response.token, userId: response.user.id, cookie };
|
|
137
175
|
}
|
package/src/version.generated.ts
CHANGED