@pithy-sh/auth 0.8.0 → 0.9.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.
Files changed (85) hide show
  1. package/README.md +4 -0
  2. package/dist/caller/certificate.d.ts +83 -0
  3. package/dist/caller/certificate.d.ts.map +1 -0
  4. package/dist/caller/certificate.js +92 -0
  5. package/dist/caller/request.d.ts +66 -0
  6. package/dist/caller/request.d.ts.map +1 -0
  7. package/dist/caller/request.js +71 -0
  8. package/dist/caller/token.d.ts +130 -0
  9. package/dist/caller/token.d.ts.map +1 -0
  10. package/dist/caller/token.js +207 -0
  11. package/dist/capability.d.ts +44 -1
  12. package/dist/capability.d.ts.map +1 -1
  13. package/dist/capability.js +7 -4
  14. package/dist/data/betterAuth.d.ts +1 -1
  15. package/dist/data/betterAuth.d.ts.map +1 -1
  16. package/dist/data/betterAuth.js +1 -1
  17. package/dist/data/tables.d.ts +1 -1
  18. package/dist/http/credentialResign.d.ts +46 -0
  19. package/dist/http/credentialResign.d.ts.map +1 -0
  20. package/dist/http/credentialResign.js +203 -0
  21. package/dist/http/errorCallbackUrl.d.ts.map +1 -1
  22. package/dist/http/errorCallbackUrl.js +15 -9
  23. package/dist/http/middleware.d.ts.map +1 -1
  24. package/dist/http/middleware.js +66 -15
  25. package/dist/http/providerRefusal.d.ts +8 -7
  26. package/dist/http/providerRefusal.d.ts.map +1 -1
  27. package/dist/http/providerRefusal.js +29 -32
  28. package/dist/http/resolve.d.ts.map +1 -1
  29. package/dist/http/resolve.js +18 -3
  30. package/dist/instance/auth.d.ts +1225 -2
  31. package/dist/instance/auth.d.ts.map +1 -1
  32. package/dist/instance/auth.js +6 -2
  33. package/dist/instance/jwksReencryption.d.ts +75 -0
  34. package/dist/instance/jwksReencryption.d.ts.map +1 -0
  35. package/dist/instance/jwksReencryption.js +160 -0
  36. package/dist/instance/providerSignInGate.d.ts +7 -6
  37. package/dist/instance/providerSignInGate.d.ts.map +1 -1
  38. package/dist/instance/providerSignInGate.js +16 -14
  39. package/dist/instance/secrets.d.ts +168 -1
  40. package/dist/instance/secrets.d.ts.map +1 -1
  41. package/dist/instance/secrets.js +86 -3
  42. package/dist/instance/sessionSecrets.d.ts +45 -0
  43. package/dist/instance/sessionSecrets.d.ts.map +1 -0
  44. package/dist/instance/sessionSecrets.js +29 -0
  45. package/dist/migrations/0001_init.d.ts.map +1 -1
  46. package/dist/migrations/0003_account_issuer_nullable.d.ts +5 -0
  47. package/dist/migrations/0003_account_issuer_nullable.d.ts.map +1 -0
  48. package/dist/migrations/0003_account_issuer_nullable.js +131 -0
  49. package/dist/migrations/set.d.ts +2 -1
  50. package/dist/migrations/set.d.ts.map +1 -1
  51. package/dist/migrations/set.js +4 -2
  52. package/dist/test-utils/liveApp.d.ts +5 -0
  53. package/dist/test-utils/liveApp.d.ts.map +1 -1
  54. package/dist/test-utils/schemaDrift.d.ts +49 -0
  55. package/dist/test-utils/schemaDrift.d.ts.map +1 -0
  56. package/dist/test-utils/schemaDrift.js +35 -0
  57. package/dist/test-utils/scopedBackend.d.ts +183 -0
  58. package/dist/test-utils/scopedBackend.d.ts.map +1 -0
  59. package/dist/test-utils/scopedBackend.js +160 -0
  60. package/dist/version.generated.d.ts +1 -1
  61. package/dist/version.generated.js +1 -1
  62. package/docs/server-authoritative.md +182 -0
  63. package/package.json +11 -8
  64. package/src/caller/certificate.ts +133 -0
  65. package/src/caller/request.ts +84 -0
  66. package/src/caller/token.ts +276 -0
  67. package/src/capability.ts +65 -4
  68. package/src/data/betterAuth.ts +2 -1
  69. package/src/http/credentialResign.ts +268 -0
  70. package/src/http/errorCallbackUrl.ts +21 -13
  71. package/src/http/middleware.ts +100 -35
  72. package/src/http/providerRefusal.ts +30 -32
  73. package/src/http/resolve.ts +27 -3
  74. package/src/http/routes.ts +1 -1
  75. package/src/instance/auth.ts +22 -4
  76. package/src/instance/jwksReencryption.ts +206 -0
  77. package/src/instance/providerSignInGate.ts +23 -21
  78. package/src/instance/secrets.ts +94 -1
  79. package/src/instance/sessionSecrets.ts +64 -0
  80. package/src/migrations/0001_init.ts +8 -10
  81. package/src/migrations/0003_account_issuer_nullable.ts +199 -0
  82. package/src/migrations/set.ts +3 -1
  83. package/src/test-utils/schemaDrift.ts +82 -0
  84. package/src/test-utils/scopedBackend.ts +136 -0
  85. package/src/version.generated.ts +1 -1
@@ -0,0 +1,136 @@
1
+ // SPDX-FileCopyrightText: 2026 Pithy
2
+ // SPDX-License-Identifier: MIT
3
+
4
+ import type { D1Database } from "@cloudflare/workers-types";
5
+ import { createMigrationRegistry } from "@pithy-sh/core/src/migrations/registry";
6
+ import { runMigrations } from "@pithy-sh/core/src/migrations/runner";
7
+ import { EMAIL_MIGRATION_ORDER } from "@pithy-sh/email/src/capability";
8
+ import { emailSigningRegistry } from "@pithy-sh/email/src/crypto/signingKey";
9
+ import { email_0001_init } from "@pithy-sh/email/src/migrations/0001_init";
10
+ import type { SecretsStoreEnv } from "@pithy-sh/secrets/src/env/bindings";
11
+ import { type SecretFixture, seedSecrets } from "@pithy-sh/secrets/src/test-utils/secretFixtures";
12
+ import type { Migration } from "kysely/migration";
13
+ import { authDatabase } from "../data/tables";
14
+ import { makeAuth } from "../instance/auth";
15
+ import { NO_SOCIAL_PROVIDERS } from "../instance/providers";
16
+ import { authSecretsRegistry } from "../instance/secrets";
17
+ import { AUTH_MIGRATION_ORDER } from "../migrations/0001_init";
18
+ import { AUTH_MIGRATIONS } from "../migrations/set";
19
+
20
+ /**
21
+ * The fixtures a workers suite needs to put a **real** `auth()` in front of a request (#560).
22
+ *
23
+ * The scope gates in `leaderboard`, `rating` and `ledger` were each tested against a stub middleware that
24
+ * wrote `c.var.auth` itself, so every one of them passed while nothing real could ever open it. A suite
25
+ * built on this composes the actual capability through `createBackend` and signs a user in with the actual
26
+ * Better Auth instance, so what reaches a gate is what a deployed Worker would put there.
27
+ *
28
+ * It holds the pieces and not the composition. Which capabilities sit beside auth is the suite's own
29
+ * statement, and so is the probe it reads the request through.
30
+ */
31
+
32
+ /** The session secret these suites sign with. A throwaway: it authenticates nothing anywhere. */
33
+ export const SCOPED_SESSION_SECRET = "scoped-backend-secret-please-rotate-000";
34
+
35
+ /** The origin the composed auth serves on, and the audience a caller token is minted for. */
36
+ export const SCOPED_ORIGIN = "http://localhost";
37
+
38
+ /** Auth's registry and the email link key, which every composition with `email()` resolves. */
39
+ export const SCOPED_REGISTRY = { ...authSecretsRegistry, ...emailSigningRegistry };
40
+
41
+ /** Every secret the composed backend resolves, since the shared accessor resolves the registry in one batch. */
42
+ export const SCOPED_SECRETS: SecretFixture<typeof SCOPED_REGISTRY> = {
43
+ "auth-session-secret": SCOPED_SESSION_SECRET,
44
+ "auth-google-credentials": { clientId: "g", clientSecret: "g" },
45
+ "auth-apple-credentials": { clientId: "a", clientSecret: "a" },
46
+ "auth-facebook-credentials": { clientId: "f", clientSecret: "f" },
47
+ "auth-github-credentials": { clientId: "h", clientSecret: "h" },
48
+ "email-link-signing-key": "dev-link-signing-key",
49
+ };
50
+
51
+ /** One capability's migrations, as the registry takes them, for a suite composing more than auth. */
52
+ export interface ExtraMigrations {
53
+ namespace: string;
54
+ order: number;
55
+ migrations: Record<string, Migration>;
56
+ }
57
+
58
+ /**
59
+ * Drop every table, re-run every migration the composition needs, then seed the secrets it resolves.
60
+ *
61
+ * Every table rather than a list of them, so a suite composing a capability beside auth does not have to
62
+ * restate what that capability's migrations create, and a second test never meets the first one's rows.
63
+ */
64
+ export async function prepareScopedBackend(
65
+ env: SecretsStoreEnv & { DB: D1Database },
66
+ extra: readonly ExtraMigrations[] = [],
67
+ ): Promise<void> {
68
+ const tables = await env.DB.prepare(
69
+ "select name from sqlite_master where type = 'table' and name not like 'sqlite_%' and name not like '\\_cf\\_%' escape '\\'",
70
+ ).all<{ name: string }>();
71
+ for (const { name } of tables.results) await env.DB.prepare(`drop table if exists "${name}"`).run();
72
+ const provider = createMigrationRegistry([
73
+ { database: "app", namespace: "auth", order: AUTH_MIGRATION_ORDER, migrations: AUTH_MIGRATIONS },
74
+ { database: "app", namespace: "email", order: EMAIL_MIGRATION_ORDER, migrations: { "0001_init": email_0001_init } },
75
+ ...extra.map((entry) => ({ database: "app", ...entry })),
76
+ ]).app;
77
+ if (!provider) throw new Error('expected a migration provider for database "app"');
78
+ await runMigrations(env.DB, provider);
79
+ await seedSecrets(env, SCOPED_REGISTRY, SCOPED_SECRETS);
80
+ }
81
+
82
+ /**
83
+ * The Worker env one request runs against: the real `DB` and `SECRETS`, and stand-ins for the three
84
+ * bindings this composition requires and never exercises — no email is sent and the limiter always allows.
85
+ */
86
+ export function scopedWorkerEnv(env: Record<string, unknown>, db: unknown = env.DB): Record<string, unknown> {
87
+ return {
88
+ ...env,
89
+ DB: db,
90
+ ENVIRONMENT: "dev",
91
+ EMAIL_SUPPRESSIONS: env.DB,
92
+ EMAIL_SENDER: {},
93
+ AUTH_RATE_LIMITER: { limit: async () => ({ success: true }) },
94
+ };
95
+ }
96
+
97
+ /**
98
+ * Sign a user in by OTP against the same database the composed backend reads, and return a bearer token.
99
+ *
100
+ * Directly through Better Auth rather than over HTTP: the sign-in flow is not what these suites are about,
101
+ * and the session row it writes is exactly the one a request presenting the token resolves.
102
+ */
103
+ export async function signInScoped(
104
+ db: D1Database,
105
+ address = "player@test.com",
106
+ ): Promise<{ token: string; userId: string }> {
107
+ const mailbox: { template: string; code?: string }[] = [];
108
+ const instance = makeAuth({
109
+ db: authDatabase(db),
110
+ secret: SCOPED_SESSION_SECRET,
111
+ baseURL: SCOPED_ORIGIN,
112
+ basePath: "/auth",
113
+ trustedOrigins: [SCOPED_ORIGIN],
114
+ ...NO_SOCIAL_PROVIDERS,
115
+ sendEmail: async (m) => {
116
+ mailbox.push(m.template === "otp" ? { template: "otp", code: m.code } : { template: m.template });
117
+ return { delivery: "queued" };
118
+ },
119
+ sessionExpiresIn: 604800,
120
+ sessionUpdateAge: 86400,
121
+ verificationExpiresIn: 300,
122
+ otpLength: 6,
123
+ disableSignUp: false,
124
+ providerSignUp: { google: true, apple: true, facebook: true, github: true },
125
+ emit: async () => {},
126
+ plugins: [],
127
+ });
128
+ await instance.api.sendVerificationOTP({ body: { email: address, type: "sign-in" }, headers: new Headers() });
129
+ const otp = mailbox.find((m) => m.template === "otp");
130
+ if (!otp?.code) throw new Error("no OTP was mailed");
131
+ const signedIn = await instance.api.signInEmailOTP({
132
+ body: { email: address, otp: otp.code },
133
+ headers: new Headers(),
134
+ });
135
+ return { token: signedIn.token, userId: signedIn.user.id };
136
+ }
@@ -13,4 +13,4 @@
13
13
  export const PACKAGE_NAME = "@pithy-sh/auth";
14
14
 
15
15
  /** This package's version, stamped from its own package.json at generation time. */
16
- export const PACKAGE_VERSION = "0.8.0";
16
+ export const PACKAGE_VERSION = "0.9.0";