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
@@ -0,0 +1,101 @@
1
+ ---
2
+ name: direct-auth
3
+ description: Add sign-in to a project with the direct-auth package (Sign in with Apple, Google, GitHub, email and SMS codes, passwords, passkeys, guests, checked by the project's own backend, with accounts and sessions on its own database). Use when the user wants sign-in or sign-up, to replace Supabase Auth, Firebase Auth or Auth0, account deletion with Apple's revocation, or the iOS, Android or web client that signs in.
4
+ ---
5
+
6
+ # direct-auth
7
+
8
+ Sign-in from the project's own backend: Apple, Google, GitHub, email and SMS codes, passwords, passkeys and guests, with
9
+ accounts and sessions
10
+ on the project's database (SQLite, Postgres, D1). Zero dependencies. The full manual is the package's README:
11
+ `node_modules/direct-auth/README.md`, or https://whitebear.io/en/oss/direct-auth/docs; the HTTP contract is
12
+ `docs/API.md` in the repository. Read the section you need before writing code.
13
+
14
+ ## Start with the wizards
15
+
16
+ 1. `npx direct-auth init` where the server runs: where it runs, the address of the sign-in (`auth.example.com`), which
17
+ ways in, the Apple `.p8` (checked with Apple), the Google client IDs, the GitHub OAuth app (where to make it, its callback URL), the mail and SMS senders (with a test send),
18
+ the DNS records (shown, made at Cloudflare with a token if the user wants, waited for), the signing key; it writes
19
+ `.env` and for Supabase or Cloudflare offers to upload the secrets. Let the user run it when it asks for files,
20
+ tokens or codes; never print a key's value.
21
+ 2. `npx direct-auth app` in each app's folder: it adds the Swift package, the Android library or the web client.
22
+ 3. `npx direct-auth dns example.com` before sending mail: SPF, DKIM and DMARC (MX on the root domain is only for
23
+ receiving and isn't needed).
24
+ 4. Leaving Supabase Auth is a hard stop, in this order: the user turns Supabase Auth's sign-ins and sign-ups off,
25
+ then `npx direct-auth migrate` (dry run, rollback file, a typed `move`, checks; it runs once), then
26
+ `npx direct-auth supabase-trust --project <ref> --issuer <issuer>`, then the app's new version (with direct-auth)
27
+ is released as the minimum version; everyone signs in once more as the same user; no session comes over. From
28
+ the dry run until the move the server answers sign-ins with `503 migration_pending`. In the new version, an app holding Supabase's
29
+ anonymous session calls `claimGuest(supabaseRefreshToken)` once at launch, before anything else, then forgets
30
+ it. A failed `Shared` check means two accounts of one person: run the rollback file, don't go on. The user types
31
+ the database URL; never put it in a command line or a file. Move the app's `handle_new_user` trigger from
32
+ `auth.users` to `auth_users`. Once the update is out, have the user revoke Supabase's JWT signing key (or the
33
+ legacy secret) in the dashboard.
34
+ Second factors stop the move; `--without-mfa` is the user's decision, not yours. A database elsewhere needs its
35
+ CA file (`sslrootcert`) or a server offering SCRAM-SHA-256-PLUS; never suggest `channel_binding=prefer` to get
36
+ past that.
37
+
38
+ ## Moving users from another service
39
+
40
+ `npx direct-auth migrate --from <source>` (or no flags: it asks). Every source is the same hard stop: the old
41
+ service's sign-ups off first, then the move (dry run, rollback file, a typed `move`, checks; it runs once), then the
42
+ app's new version with direct-auth released as the minimum. Everyone signs in again as the same user; no session
43
+ comes over. Let the user run it and type the database URL and secrets; never print a key, a hash or an address. A
44
+ failed `Shared` check means two accounts of one person: run the rollback file (`psql "$DATABASE_URL" -f FILE`).
45
+ Where ids aren't UUIDs, users get new ones and `auth_users.source_id` keeps the old: map the app's tables with it
46
+ (`update t set user_id = u.id::text from auth_users u where u.source_id = t.user_id`) before the new version reads
47
+ them. `--keep-unproven-passwords` (moving unverified addresses as checked) is the user's decision, not yours.
48
+
49
+ - **Supabase**: step 4 above.
50
+ - **Firebase**: `firebase auth:export users.json --format=json --project <id>`, after Authentication › Settings › User
51
+ actions › Enable create is off. Hash parameters from Authentication › Users › ⋮ › Password hash parameters into a
52
+ file outside the repository: `--from firebase --file users.json --hash-config hash.txt --db <the server's
53
+ Postgres>`. Without them passwords stay behind (email codes). Guests move but start over (no claim).
54
+ - **Auth.js**: same database as its adapter's tables: `--from authjs`. No passwords; unverified (OAuth) addresses
55
+ stay behind. The dry run names the app's foreign keys on Auth.js's users table: map them with `source_id`.
56
+ - **Better Auth**: same database: `--from better-auth` (after `disableSignUp`). Its scrypt passwords move; UUID ids
57
+ stay; anonymous users become guests (no claim).
58
+ - **Clerk**: Dashboard › Users › Export (after Restrictions › Sign-up mode: Restricted): `--from clerk --file
59
+ users.csv`. bcrypt digests move; Google, Apple and GitHub accounts aren't in the export, so those users tap the
60
+ same button and join by their verified address.
61
+ - **Auth0**: a users export job (format json, fields user_id, email, email_verified, phone_number, phone_verified,
62
+ name, picture, blocked, created_at, last_login, identities): `--from auth0 --file users.json [--hashes FILE]`.
63
+ Without the hashes (only through Auth0's support) database users sign in with an email code and can set a
64
+ password.
65
+
66
+ Delete the export files after the move: they hold hashes and addresses.
67
+
68
+ ## Where it runs
69
+
70
+ | Runtime | Server | Database |
71
+ | --- | --- | --- |
72
+ | Supabase Edge Functions, Deno | `createAuthServer` from `npm:direct-auth@0`, `Deno.serve(server.fetch)` | `npm:direct-auth@0/postgres` with postgres.js |
73
+ | Node, Bun (a VPS, Docker) | `serve(server)` from `direct-auth/node` | `direct-auth/sqlite` or `direct-auth/postgres` |
74
+ | Cloudflare Workers | `server.fetch` | `direct-auth/d1` |
75
+ | Browser | `createAuthClient` from `direct-auth/client` | |
76
+
77
+ Options come from the environment with `authFromEnv(env)`; only the database is passed by hand.
78
+
79
+ ## Rules
80
+
81
+ - The issuer URL (`DIRECT_AUTH_ISSUER`) never changes once apps use it: it is in every token, and in Supabase's
82
+ Third-Party Auth setting. Prefer a name of the project's own (`https://auth.example.com/auth`): moving the server
83
+ is then pointing that name elsewhere, with the same database and the same `DIRECT_AUTH_SIGNING_KEYS`.
84
+ - On Supabase: the issuer is trusted through the Management API (`supabase-trust`; the dashboard has no generic
85
+ OIDC entry); `auth.uid()` then reads the token's `sub`, and `is_anonymous` is there for guests.
86
+ - Guests (`POST /guest`), linking (`/link/apple`, `/link/google` with the request's nonce, `/link/github`, `/link/email`, `POST /password`), unlinking
87
+ (`DELETE /identity/<id>`) and changing the address (`/email/change`) keep the user's id. `409 identity_in_use`
88
+ means that way in belongs to another account: let the user choose (switch accounts, or keep this one); never merge
89
+ accounts in code. `401 reauth_needed` means sign in again first.
90
+ - Passwords need email codes (`PASSWORDS=1` with `MAIL_SENDER`): sign-up and reset take a code before the password.
91
+ - Sign in with Apple in an app needs the `.p8` on the server (`APPLE_*`) so account deletion can revoke it, as App
92
+ Store guideline 5.1.1(v) asks. The app sends the authorization code with the ID token.
93
+ - GitHub needs an OAuth app (`GITHUB_CLIENT_ID`, `GITHUB_CLIENT_SECRET`) whose callback URL is the app's own: a web
94
+ page that calls `finishGitHub()`, `myapp://github` on iOS, an App Link on Android. Use the clients' helpers: they make
95
+ the state and the PKCE verifier and check the state; never send the secret to an app.
96
+ - SMS goes only to the calling codes in `SMS_COUNTRIES`: never set it wider than the countries the app serves (SMS
97
+ pumping fraud bills per message).
98
+ - APIs check the access token with the issuer's JWKS (`/.well-known/jwks.json`) or `verifyAccessToken`; never trust a
99
+ user id sent in a body.
100
+ - Keys live in the environment or the platform's secrets. Never commit a `.p8`, `DIRECT_AUTH_SIGNING_KEYS` or a
101
+ sender's key, and never echo them.