direct-auth 0.0.0-stage → 0.1.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 (110) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +486 -2
  3. package/dist/accounts.d.ts +118 -0
  4. package/dist/accounts.js +314 -0
  5. package/dist/apps.d.ts +85 -0
  6. package/dist/apps.js +449 -0
  7. package/dist/auth.d.ts +396 -0
  8. package/dist/auth.js +699 -0
  9. package/dist/base64.d.ts +20 -0
  10. package/dist/base64.js +45 -0
  11. package/dist/cli.d.ts +2 -0
  12. package/dist/cli.js +463 -0
  13. package/dist/client.d.ts +170 -0
  14. package/dist/client.js +438 -0
  15. package/dist/codes.d.ts +81 -0
  16. package/dist/codes.js +91 -0
  17. package/dist/crypto.d.ts +11 -0
  18. package/dist/crypto.js +37 -0
  19. package/dist/d1.d.ts +2 -0
  20. package/dist/d1.js +2 -0
  21. package/dist/db/limits.d.ts +16 -0
  22. package/dist/db/limits.js +23 -0
  23. package/dist/db/schema.d.ts +25 -0
  24. package/dist/db/schema.js +136 -0
  25. package/dist/db/sql.d.ts +52 -0
  26. package/dist/db/sql.js +121 -0
  27. package/dist/dns.d.ts +48 -0
  28. package/dist/dns.js +133 -0
  29. package/dist/env.d.ts +39 -0
  30. package/dist/env.js +169 -0
  31. package/dist/index.d.ts +19 -0
  32. package/dist/index.js +14 -0
  33. package/dist/init.d.ts +134 -0
  34. package/dist/init.js +912 -0
  35. package/dist/jwks.d.ts +18 -0
  36. package/dist/jwks.js +63 -0
  37. package/dist/jwt.d.ts +90 -0
  38. package/dist/jwt.js +99 -0
  39. package/dist/keys.d.ts +33 -0
  40. package/dist/keys.js +54 -0
  41. package/dist/messages.d.ts +26 -0
  42. package/dist/messages.js +57 -0
  43. package/dist/migrate-cli.d.ts +82 -0
  44. package/dist/migrate-cli.js +564 -0
  45. package/dist/migrate.d.ts +99 -0
  46. package/dist/migrate.js +396 -0
  47. package/dist/node.d.ts +16 -0
  48. package/dist/node.js +63 -0
  49. package/dist/passkeys.d.ts +126 -0
  50. package/dist/passkeys.js +383 -0
  51. package/dist/passwords.d.ts +53 -0
  52. package/dist/passwords.js +326 -0
  53. package/dist/pgwire.d.ts +86 -0
  54. package/dist/pgwire.js +445 -0
  55. package/dist/phone.d.ts +11 -0
  56. package/dist/phone.js +18 -0
  57. package/dist/postgres.d.ts +2 -0
  58. package/dist/postgres.js +2 -0
  59. package/dist/providers/apple-revoke.d.ts +55 -0
  60. package/dist/providers/apple-revoke.js +113 -0
  61. package/dist/providers/apple.d.ts +28 -0
  62. package/dist/providers/apple.js +51 -0
  63. package/dist/providers/github.d.ts +28 -0
  64. package/dist/providers/github.js +67 -0
  65. package/dist/providers/google.d.ts +23 -0
  66. package/dist/providers/google.js +47 -0
  67. package/dist/providers/identity.d.ts +24 -0
  68. package/dist/providers/identity.js +3 -0
  69. package/dist/proxy.d.ts +19 -0
  70. package/dist/proxy.js +43 -0
  71. package/dist/records.d.ts +91 -0
  72. package/dist/records.js +264 -0
  73. package/dist/scrypt.d.ts +26 -0
  74. package/dist/scrypt.js +179 -0
  75. package/dist/senders/aws.d.ts +13 -0
  76. package/dist/senders/aws.js +47 -0
  77. package/dist/senders/dev.d.ts +6 -0
  78. package/dist/senders/dev.js +13 -0
  79. package/dist/senders/index.d.ts +5 -0
  80. package/dist/senders/index.js +4 -0
  81. package/dist/senders/mail.d.ts +26 -0
  82. package/dist/senders/mail.js +66 -0
  83. package/dist/senders/sms.d.ts +34 -0
  84. package/dist/senders/sms.js +69 -0
  85. package/dist/senders/smtp.d.ts +32 -0
  86. package/dist/senders/smtp.js +145 -0
  87. package/dist/senders/types.d.ts +31 -0
  88. package/dist/senders/types.js +25 -0
  89. package/dist/server.d.ts +67 -0
  90. package/dist/server.js +494 -0
  91. package/dist/sessions.d.ts +74 -0
  92. package/dist/sessions.js +146 -0
  93. package/dist/skill.d.ts +25 -0
  94. package/dist/skill.js +66 -0
  95. package/dist/sources.d.ts +138 -0
  96. package/dist/sources.js +931 -0
  97. package/dist/sqlite.d.ts +2 -0
  98. package/dist/sqlite.js +2 -0
  99. package/dist/stores/d1.d.ts +22 -0
  100. package/dist/stores/d1.js +23 -0
  101. package/dist/stores/postgres.d.ts +33 -0
  102. package/dist/stores/postgres.js +71 -0
  103. package/dist/stores/sqlite.d.ts +27 -0
  104. package/dist/stores/sqlite.js +55 -0
  105. package/dist/supabase.d.ts +71 -0
  106. package/dist/supabase.js +283 -0
  107. package/dist/tui.d.ts +178 -0
  108. package/dist/tui.js +537 -0
  109. package/package.json +90 -4
  110. package/skills/direct-auth/SKILL.md +101 -0
package/dist/auth.d.ts ADDED
@@ -0,0 +1,396 @@
1
+ /**
2
+ * `createAuth`: everything a backend needs to sign people in, as plain functions over its database. The HTTP routes
3
+ * (`direct-auth/server`) are a thin layer over these, and an app that has its own router can call them directly.
4
+ *
5
+ * const auth = await createAuth({
6
+ * sql: await openSqlite("./auth.db"),
7
+ * issuer: "https://auth.example.com",
8
+ * keys: process.env.DIRECT_AUTH_SIGNING_KEYS,
9
+ * apple: { clientIds: ["com.example.app"] },
10
+ * google: { clientIds: ["123-abc.apps.googleusercontent.com"] },
11
+ * email: { send: resend({ apiKey, from: "Example <hello@example.com>" }) },
12
+ * sms: { send: twilio({ … }), countries: ["+598"] },
13
+ * app: "Example",
14
+ * });
15
+ * const r = await auth.signInWithApple({ idToken, nonce }); // { ok, user, tokens } or { ok: false, reason }
16
+ */
17
+ import { type User } from "./accounts.ts";
18
+ import { type PasskeyOptions } from "./passkeys.ts";
19
+ import { type AppleKeyOptions } from "./providers/apple-revoke.ts";
20
+ import { type CodeLimits, type CodeOptions } from "./codes.ts";
21
+ import { type Sql } from "./db/sql.ts";
22
+ import { type PrivateJwk, type SigningKeys } from "./keys.ts";
23
+ import { type AppleOptions } from "./providers/apple.ts";
24
+ import { type GoogleOptions } from "./providers/google.ts";
25
+ import { type GitHubOptions } from "./providers/github.ts";
26
+ import type { Sender } from "./senders/types.ts";
27
+ import { type RefreshFailure, type SessionOptions, type Tokens } from "./sessions.ts";
28
+ export interface AuthOptions extends Omit<SessionOptions, "issuer"> {
29
+ sql: Sql;
30
+ /** The issuer's public URL (`https://auth.example.com`, or a path under one): the tokens' `iss`. */
31
+ issuer: string;
32
+ /** `DIRECT_AUTH_SIGNING_KEYS`: the JSON array of private JWKs (or base64 of it), or the keys themselves. */
33
+ keys: string | readonly PrivateJwk[] | undefined;
34
+ /** The project's name, as the codes' messages say it. */
35
+ app: string;
36
+ /** The tables' prefix (`auth_`). */
37
+ tablePrefix?: string;
38
+ /**
39
+ * Sign in with Apple. With `revoke` (the team's Sign in with Apple key), the app's authorization code is kept as
40
+ * Apple's refresh token, and deleting the account revokes it, as App Store guideline 5.1.1(v) asks.
41
+ */
42
+ apple?: AppleOptions & {
43
+ revoke?: AppleKeyOptions;
44
+ };
45
+ google?: GoogleOptions;
46
+ /** GitHub's OAuth app: the server trades the app's code for who it is (`POST /github`). */
47
+ github?: GitHubOptions;
48
+ /** Passkeys: the site's domain as the relying party, and the origins the apps' answers come from. */
49
+ passkeys?: PasskeyOptions;
50
+ email?: {
51
+ send: Sender;
52
+ } & CodeOptions;
53
+ sms?: {
54
+ send: Sender;
55
+ /** The calling codes SMS may go to (`["+598", "+54"]`). None by default: SMS goes nowhere until this says where. */
56
+ countries: readonly string[];
57
+ /** The site's domain, for the line that lets browsers and phones fill the code in. */
58
+ domain?: string;
59
+ } & CodeOptions;
60
+ /**
61
+ * Guests: `POST /guest` makes an account with nothing to sign in with yet, for an app that lets people in before
62
+ * they pick a way to sign in. `days`: a guest nobody used for that long is deleted at `prune()` (never, unless set).
63
+ */
64
+ guests?: {
65
+ days?: number;
66
+ };
67
+ /**
68
+ * Email and password, on top of the codes (needs `email`: an address is proven by a code before a password is set
69
+ * on it, at sign-up and at reset). `iterations`: PBKDF2's count for new hashes (600,000; 100,000 on Workers).
70
+ * `minLength`: 8.
71
+ */
72
+ passwords?: {
73
+ iterations?: number;
74
+ minLength?: number;
75
+ };
76
+ limits?: Partial<CodeLimits>;
77
+ /** Runs after a user is deleted (the app's own rows), guests pruned included. */
78
+ onDelete?: (user: User) => Promise<void> | void;
79
+ /**
80
+ * Apple's server-to-server notice about a user (`consent-revoked`, `account-delete`, `email-disabled`,
81
+ * `email-enabled`). For the first two the user is already signed out everywhere and the Apple sign-in forgotten;
82
+ * this is for anything more (deleting the account when it has no other way in, for one).
83
+ */
84
+ onAppleNotice?: (notice: {
85
+ type: string;
86
+ userId: string;
87
+ subject: string;
88
+ email?: string;
89
+ }) => Promise<void> | void;
90
+ /** Creates the tables on start (true). */
91
+ migrate?: boolean;
92
+ }
93
+ export type SignInResult = {
94
+ ok: true;
95
+ user: User;
96
+ tokens: Tokens;
97
+ created: boolean;
98
+ } | {
99
+ ok: false;
100
+ reason: string;
101
+ };
102
+ /** What a change to the signed-in account answers: the user as they are now, and an access token that says so. */
103
+ export type AccountResult = {
104
+ ok: true;
105
+ user: User;
106
+ accessToken: string;
107
+ expiresIn: number;
108
+ } | {
109
+ ok: false;
110
+ reason: string;
111
+ };
112
+ export interface AppleSignIn {
113
+ idToken: string;
114
+ nonce?: string;
115
+ name?: string;
116
+ authorizationCode?: string;
117
+ redirectUri?: string;
118
+ }
119
+ export interface Context {
120
+ ip?: string;
121
+ userAgent?: string;
122
+ now?: number;
123
+ }
124
+ export declare function normalizeEmail(input: string): string | null;
125
+ export declare function createAuth(o: AuthOptions): Promise<{
126
+ keys: SigningKeys;
127
+ sql: Sql;
128
+ issuer: string;
129
+ /** The identity token an app got from Sign in with Apple, with the raw nonce it asked with, and the name Apple gave it the first time. */
130
+ signInWithApple(a: AppleSignIn, c?: Context): Promise<SignInResult>;
131
+ signInWithGoogle(a: {
132
+ idToken: string;
133
+ nonce?: string;
134
+ }, c?: Context): Promise<SignInResult>;
135
+ /** GitHub: the code its redirect gave the app, the redirect URI, and the PKCE verifier the app made. */
136
+ signInWithGitHub(a: {
137
+ code: string;
138
+ redirectUri?: string;
139
+ codeVerifier?: string;
140
+ }, c?: Context): Promise<SignInResult>;
141
+ /** A guest: an account with nothing to sign in with yet. Linking a way in later keeps its id. */
142
+ signInAsGuest(a?: {
143
+ name?: string;
144
+ }, c?: Context): Promise<SignInResult>;
145
+ /**
146
+ * A guest moved from Supabase Auth, back on their device: the refresh token of the anonymous session the app kept
147
+ * from Supabase (the move stored its hash, for 180 days). It works once, for a user who is still a guest, and
148
+ * answers a session for that same user; anything else is `invalid_token`. Supabase Auth still trades a refresh
149
+ * token for a new one with its sign-ins off, so an old app that refreshed after the move holds a token the move
150
+ * never saw: one Supabase has and hasn't revoked counts too, while that user's claim waits.
151
+ */
152
+ claimGuest(a: {
153
+ supabaseRefreshToken: string;
154
+ }, c?: Context): Promise<SignInResult>;
155
+ /**
156
+ * Email and password. Wrong address, wrong password, an account without one: all answer `invalid_credentials`,
157
+ * in the same time, so nobody learns which addresses have accounts.
158
+ */
159
+ signInWithPassword(a: {
160
+ email: string;
161
+ password: string;
162
+ }, c?: Context): Promise<SignInResult>;
163
+ /**
164
+ * Signing up with a password, or resetting it: the code `startEmail` sent proves the address, then the password
165
+ * is set on its account (made if new) and every other session of it ends.
166
+ */
167
+ setPasswordWithCode(a: {
168
+ email: string;
169
+ code: string;
170
+ password: string;
171
+ }, c?: Context): Promise<SignInResult>;
172
+ /** Sets or replaces the signed-in user's password (an address needed), and signs their other devices out. */
173
+ setPassword(userId: string, sessionId: string, password: string, c?: Context): Promise<AccountResult>;
174
+ /** Removes the user's password, unless it is their last way in. */
175
+ removePassword(userId: string, sessionId: string, c?: Context): Promise<AccountResult>;
176
+ /** Adds Sign in with Apple to the signed-in user (a guest becomes a full account, with the same id). */
177
+ linkApple(userId: string, sessionId: string, a: AppleSignIn, c?: Context): Promise<AccountResult>;
178
+ /** Adds GitHub to the signed-in account: a code made for this request (GitHub's codes work once, for ten minutes). */
179
+ linkGitHub(userId: string, sessionId: string, a: {
180
+ code: string;
181
+ redirectUri?: string;
182
+ codeVerifier?: string;
183
+ }, c?: Context): Promise<AccountResult>;
184
+ linkGoogle(userId: string, sessionId: string, a: {
185
+ idToken: string;
186
+ nonce?: string;
187
+ }, c?: Context): Promise<AccountResult>;
188
+ /** Adds an address (proven by the code `startEmail` sent) to the signed-in user. */
189
+ linkEmail(userId: string, sessionId: string, a: {
190
+ email: string;
191
+ code: string;
192
+ }, c?: Context): Promise<AccountResult>;
193
+ /** Changes the signed-in user's address to one proven by a code: email sign-in and the password move to it. */
194
+ changeEmail(userId: string, sessionId: string, a: {
195
+ email: string;
196
+ code: string;
197
+ }, c?: Context): Promise<AccountResult>;
198
+ /** Removes one of the user's ways in (`identities()`'s `id`), unless it is the last. An Apple one is revoked at Apple. */
199
+ unlink(userId: string, sessionId: string, identityId: string, c?: Context): Promise<AccountResult>;
200
+ /** Sends a code to an address. Answers the same whether or not someone has signed in with it. */
201
+ startEmail(a: {
202
+ email: string;
203
+ locale?: string;
204
+ }, c?: Context): Promise<{
205
+ ok: true;
206
+ reason?: undefined;
207
+ detail?: undefined;
208
+ } | {
209
+ ok: false;
210
+ reason: string;
211
+ detail: string;
212
+ } | {
213
+ ok: false;
214
+ reason: string;
215
+ }>;
216
+ verifyEmail(a: {
217
+ email: string;
218
+ code: string;
219
+ }, c?: Context): Promise<SignInResult>;
220
+ /** Sends a code by SMS, only to the calling codes `sms.countries` allows. */
221
+ startSms(a: {
222
+ phone: string;
223
+ locale?: string;
224
+ }, c?: Context): Promise<{
225
+ ok: true;
226
+ reason?: undefined;
227
+ detail?: undefined;
228
+ } | {
229
+ ok: false;
230
+ reason: string;
231
+ detail: string;
232
+ } | {
233
+ ok: false;
234
+ reason: string;
235
+ }>;
236
+ verifySms(a: {
237
+ phone: string;
238
+ code: string;
239
+ }, c?: Context): Promise<SignInResult>;
240
+ refresh(refreshToken: string, c?: Context): Promise<{
241
+ ok: true;
242
+ tokens: Tokens;
243
+ } | {
244
+ ok: false;
245
+ reason: RefreshFailure | "rate_limited";
246
+ }>;
247
+ /** The access token's claims when this server signed it and it is still good. */
248
+ verifyAccessToken(token: string, c?: Context): Promise<import("./jwt.ts").JwtResult>;
249
+ /**
250
+ * The access token's user, only while its session is still on: signed out (here or everywhere) ends it at once,
251
+ * where a check of the token alone would let it work until it expires. For routes that change the account.
252
+ */
253
+ signedIn(token: string, c?: Context): Promise<{
254
+ userId: string;
255
+ sessionId: string;
256
+ sessionCreatedAt: number;
257
+ } | null>;
258
+ signOut: (sessionId: string, c?: Context) => Promise<void>;
259
+ /**
260
+ * Signs out with a refresh token (a web app's cookie): its session, or with `everywhere` every session of its
261
+ * user, which only the session's current token may do (an old copy of it ends nothing but its own session).
262
+ */
263
+ signOutWithRefresh(refreshToken: string, everywhere?: boolean, c?: Context): Promise<boolean>;
264
+ signOutEverywhere(userId: string, c?: Context): Promise<void>;
265
+ getUser: (userId: string) => Promise<User | null>;
266
+ identities: (userId: string) => Promise<import("./accounts.ts").IdentityInfo[]>;
267
+ sessions: (userId: string, c?: Context) => Promise<import("./sessions.ts").SessionInfo[]>;
268
+ /**
269
+ * Deletes the user and everything here that was theirs, then the app's own `onDelete`. Sign in with Apple is
270
+ * revoked first when the server has Apple's key: with the token kept at sign-in, or with `appleAuthorizationCode`
271
+ * (a fresh Sign in with Apple) when none was kept, which is the case of accounts moved from elsewhere. Without
272
+ * either it answers `apple_code_needed` and deletes nothing.
273
+ */
274
+ deleteUser(userId: string, a?: {
275
+ appleAuthorizationCode?: string;
276
+ redirectUri?: string;
277
+ }): Promise<{
278
+ ok: true;
279
+ appleRevoked: boolean | null;
280
+ } | {
281
+ ok: false;
282
+ reason: string;
283
+ }>;
284
+ /**
285
+ * Apple's server-to-server notification (the `payload` Apple posts), checked against Apple's keys. A withdrawn
286
+ * consent or a deleted Apple account signs the user out everywhere and forgets that Apple sign-in.
287
+ */
288
+ appleNotification(payload: string, c?: Context): Promise<{
289
+ ok: true;
290
+ type: string;
291
+ } | {
292
+ ok: false;
293
+ reason: string;
294
+ }>;
295
+ passkeys: {
296
+ registrationOptions(userId: string, c?: Context): Promise<{
297
+ ok: false;
298
+ reason: string;
299
+ } | {
300
+ challengeId: `${string}-${string}-${string}-${string}-${string}`;
301
+ options: {
302
+ rp: {
303
+ id: string;
304
+ name: string;
305
+ };
306
+ user: {
307
+ id: string;
308
+ name: string;
309
+ displayName: string;
310
+ };
311
+ challenge: string;
312
+ pubKeyCredParams: {
313
+ type: string;
314
+ alg: number;
315
+ }[];
316
+ timeout: number;
317
+ attestation: string;
318
+ authenticatorSelection: {
319
+ residentKey: string;
320
+ requireResidentKey: boolean;
321
+ userVerification: "required" | "preferred";
322
+ };
323
+ excludeCredentials: {
324
+ transports?: any;
325
+ type: string;
326
+ id: string;
327
+ }[];
328
+ };
329
+ ok: true;
330
+ reason?: undefined;
331
+ }>;
332
+ /** A guest who adds a passkey becomes a full account. */
333
+ register(userId: string, a: {
334
+ challengeId: unknown;
335
+ credential: unknown;
336
+ name?: unknown;
337
+ }, c?: Context): Promise<{
338
+ ok: true;
339
+ passkey: {
340
+ id: string;
341
+ name: string | null;
342
+ createdAt: number;
343
+ };
344
+ } | {
345
+ ok: false;
346
+ reason: string;
347
+ }>;
348
+ signInOptions(c?: Context): Promise<{
349
+ ok: false;
350
+ reason: string;
351
+ } | {
352
+ challengeId: `${string}-${string}-${string}-${string}-${string}`;
353
+ options: {
354
+ challenge: string;
355
+ rpId: string;
356
+ timeout: number;
357
+ userVerification: "required" | "preferred";
358
+ allowCredentials: never[];
359
+ };
360
+ ok: true;
361
+ reason?: undefined;
362
+ }>;
363
+ signIn(a: {
364
+ challengeId: unknown;
365
+ credential: unknown;
366
+ }, c?: Context): Promise<SignInResult>;
367
+ list: (userId: string) => Promise<{
368
+ id: string;
369
+ name: string | null;
370
+ createdAt: number;
371
+ lastUsedAt: number | null;
372
+ }[]>;
373
+ /** Removes a passkey, unless it is the user's last way in (`last_way_in`). */
374
+ remove(userId: string, id: string): Promise<{
375
+ ok: true;
376
+ } | {
377
+ ok: false;
378
+ reason: "not_found" | "last_way_in";
379
+ }>;
380
+ };
381
+ /** OpenID discovery: what Supabase's Third-Party Auth (or any API) reads to trust these tokens. */
382
+ discovery(): {
383
+ issuer: string;
384
+ jwks_uri: string;
385
+ id_token_signing_alg_values_supported: string[];
386
+ subject_types_supported: string[];
387
+ response_types_supported: string[];
388
+ claims_supported: string[];
389
+ };
390
+ jwks: () => {
391
+ keys: import("./jwt.ts").PublicJwk[];
392
+ };
393
+ /** Clears spent codes, old counters and, with `guests.days`, guests nobody used for that long: once an hour is plenty. */
394
+ prune(c?: Context): Promise<void>;
395
+ }>;
396
+ export type Auth = Awaited<ReturnType<typeof createAuth>>;