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.
- package/LICENSE +21 -0
- package/README.md +486 -2
- package/dist/accounts.d.ts +118 -0
- package/dist/accounts.js +314 -0
- package/dist/apps.d.ts +85 -0
- package/dist/apps.js +449 -0
- package/dist/auth.d.ts +396 -0
- package/dist/auth.js +699 -0
- package/dist/base64.d.ts +20 -0
- package/dist/base64.js +45 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +463 -0
- package/dist/client.d.ts +170 -0
- package/dist/client.js +438 -0
- package/dist/codes.d.ts +81 -0
- package/dist/codes.js +91 -0
- package/dist/crypto.d.ts +11 -0
- package/dist/crypto.js +37 -0
- package/dist/d1.d.ts +2 -0
- package/dist/d1.js +2 -0
- package/dist/db/limits.d.ts +16 -0
- package/dist/db/limits.js +23 -0
- package/dist/db/schema.d.ts +25 -0
- package/dist/db/schema.js +136 -0
- package/dist/db/sql.d.ts +52 -0
- package/dist/db/sql.js +121 -0
- package/dist/dns.d.ts +48 -0
- package/dist/dns.js +133 -0
- package/dist/env.d.ts +39 -0
- package/dist/env.js +169 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +14 -0
- package/dist/init.d.ts +134 -0
- package/dist/init.js +912 -0
- package/dist/jwks.d.ts +18 -0
- package/dist/jwks.js +63 -0
- package/dist/jwt.d.ts +90 -0
- package/dist/jwt.js +99 -0
- package/dist/keys.d.ts +33 -0
- package/dist/keys.js +54 -0
- package/dist/messages.d.ts +26 -0
- package/dist/messages.js +57 -0
- package/dist/migrate-cli.d.ts +82 -0
- package/dist/migrate-cli.js +564 -0
- package/dist/migrate.d.ts +99 -0
- package/dist/migrate.js +396 -0
- package/dist/node.d.ts +16 -0
- package/dist/node.js +63 -0
- package/dist/passkeys.d.ts +126 -0
- package/dist/passkeys.js +383 -0
- package/dist/passwords.d.ts +53 -0
- package/dist/passwords.js +326 -0
- package/dist/pgwire.d.ts +86 -0
- package/dist/pgwire.js +445 -0
- package/dist/phone.d.ts +11 -0
- package/dist/phone.js +18 -0
- package/dist/postgres.d.ts +2 -0
- package/dist/postgres.js +2 -0
- package/dist/providers/apple-revoke.d.ts +55 -0
- package/dist/providers/apple-revoke.js +113 -0
- package/dist/providers/apple.d.ts +28 -0
- package/dist/providers/apple.js +51 -0
- package/dist/providers/github.d.ts +28 -0
- package/dist/providers/github.js +67 -0
- package/dist/providers/google.d.ts +23 -0
- package/dist/providers/google.js +47 -0
- package/dist/providers/identity.d.ts +24 -0
- package/dist/providers/identity.js +3 -0
- package/dist/proxy.d.ts +19 -0
- package/dist/proxy.js +43 -0
- package/dist/records.d.ts +91 -0
- package/dist/records.js +264 -0
- package/dist/scrypt.d.ts +26 -0
- package/dist/scrypt.js +179 -0
- package/dist/senders/aws.d.ts +13 -0
- package/dist/senders/aws.js +47 -0
- package/dist/senders/dev.d.ts +6 -0
- package/dist/senders/dev.js +13 -0
- package/dist/senders/index.d.ts +5 -0
- package/dist/senders/index.js +4 -0
- package/dist/senders/mail.d.ts +26 -0
- package/dist/senders/mail.js +66 -0
- package/dist/senders/sms.d.ts +34 -0
- package/dist/senders/sms.js +69 -0
- package/dist/senders/smtp.d.ts +32 -0
- package/dist/senders/smtp.js +145 -0
- package/dist/senders/types.d.ts +31 -0
- package/dist/senders/types.js +25 -0
- package/dist/server.d.ts +67 -0
- package/dist/server.js +494 -0
- package/dist/sessions.d.ts +74 -0
- package/dist/sessions.js +146 -0
- package/dist/skill.d.ts +25 -0
- package/dist/skill.js +66 -0
- package/dist/sources.d.ts +138 -0
- package/dist/sources.js +931 -0
- package/dist/sqlite.d.ts +2 -0
- package/dist/sqlite.js +2 -0
- package/dist/stores/d1.d.ts +22 -0
- package/dist/stores/d1.js +23 -0
- package/dist/stores/postgres.d.ts +33 -0
- package/dist/stores/postgres.js +71 -0
- package/dist/stores/sqlite.d.ts +27 -0
- package/dist/stores/sqlite.js +55 -0
- package/dist/supabase.d.ts +71 -0
- package/dist/supabase.js +283 -0
- package/dist/tui.d.ts +178 -0
- package/dist/tui.js +537 -0
- package/package.json +90 -4
- package/skills/direct-auth/SKILL.md +101 -0
package/dist/migrate.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `direct-auth migrate`: users moved from another auth service into direct-auth's tables, once, in the same
|
|
3
|
+
* database. Each source (Supabase Auth; Firebase Auth, Auth.js, Better Auth, Clerk and Auth0, in sources.ts) says four things:
|
|
4
|
+
*
|
|
5
|
+
* inspect what would move and what needs a hand (counts only, never an address)
|
|
6
|
+
* applySql the move, one transaction; it refuses to run a second time
|
|
7
|
+
* rollbackSql the way back, written before applying
|
|
8
|
+
* verify that what should have moved did
|
|
9
|
+
*
|
|
10
|
+
* and the steps after. A new source is one more object in `SOURCES`.
|
|
11
|
+
*/
|
|
12
|
+
import { READERS, readerSource } from "./sources.js";
|
|
13
|
+
import { B64URL, CLAIM_DAYS, CONFIRMED_AT_ONCE, ELIGIBLE, EMAIL_OF, GUEST_TOKENS, MOVED_PASSWORD, PHONE_OF, SSO, supabaseMoveSql } from "./supabase.js";
|
|
14
|
+
/** An id with its middle taken out: enough to find it again, not to read it off a screen. */
|
|
15
|
+
export const maskId = (id) => (id.length > 10 ? `${id.slice(0, 4)}…${id.slice(-4)}` : "…");
|
|
16
|
+
/** Supabase's own schemas: what isn't the app's. */
|
|
17
|
+
const SYSTEM = ["auth", "storage", "realtime", "supabase_functions", "supabase_migrations", "extensions", "graphql", "graphql_public", "vault", "pgsodium", "pgsodium_masks", "net", "cron", "pgbouncer", "pg_catalog", "information_schema", "_realtime", "_analytics", "pgmq", "pgtle"];
|
|
18
|
+
const notSystem = (col) => `${col} not in (${SYSTEM.map((s) => `'${s}'`).join(", ")}) and ${col} not like 'pg\\_%'`;
|
|
19
|
+
const n = (v) => Number(v ?? 0);
|
|
20
|
+
/** Which identities move: Apple and Google always; an address or number only once confirmed. */
|
|
21
|
+
const IDENTITY_MOVES = `coalesce(i.provider_id, '') <> '' and (i.provider in ('apple', 'google', 'github')
|
|
22
|
+
or (i.provider = 'email' and u.email_confirmed_at is not null and not ${SSO("u")})
|
|
23
|
+
or (i.provider = 'phone' and u.phone_confirmed_at is not null and not ${SSO("u")}))`;
|
|
24
|
+
const ON_DELETE = `case c.confdeltype when 'c' then 'cascade' when 'n' then 'set null' when 'd' then 'set default' when 'r' then 'restrict' else 'no action' end`;
|
|
25
|
+
/** What Postgres prints, with every name in full (a role's search_path with auth in it would leave `auth.` out). */
|
|
26
|
+
const FULL_NAMES = "select pg_catalog.set_config('search_path', '', true);";
|
|
27
|
+
export const supabaseSource = {
|
|
28
|
+
id: "supabase",
|
|
29
|
+
label: "Supabase Auth",
|
|
30
|
+
async inspect(q, o) {
|
|
31
|
+
const t = (name) => `public.${o.prefix}${name}`;
|
|
32
|
+
const one = async (sql) => (await q(sql)).rows[0] ?? {};
|
|
33
|
+
const report = { counts: [], attention: [], notes: [], existing: false, fks: [], has: { guests: false, passwords: false, passkeys: false }, mfa: 0 };
|
|
34
|
+
const ours = await one(`select to_regclass('auth.users') is not null as auth, to_regclass('${t("users")}') is not null as users,
|
|
35
|
+
to_regclass('${t("meta")}') is not null as meta`);
|
|
36
|
+
if (ours.auth !== "t") {
|
|
37
|
+
report.blocked = "there is no auth.users here: this isn't a Supabase database (or not the one with the users)";
|
|
38
|
+
return report;
|
|
39
|
+
}
|
|
40
|
+
if (ours.users === "t" && ours.meta !== "t") {
|
|
41
|
+
report.blocked = `${t("users")} is there and isn't direct-auth's: give another prefix (--prefix da_)`;
|
|
42
|
+
return report;
|
|
43
|
+
}
|
|
44
|
+
if (ours.meta === "t" && (await one(`select count(*) as n from ${t("meta")} where key in ('supabase_moved', 'source_moved')`)).n !== "0") {
|
|
45
|
+
report.blocked = "the users were moved here already: the move runs once. To move again, run the way back (the rollback file) first";
|
|
46
|
+
return report;
|
|
47
|
+
}
|
|
48
|
+
report.existing = ours.users === "t";
|
|
49
|
+
const moving = (u) => `${ELIGIBLE(u)}${report.existing ? ` and not exists (select 1 from ${t("users")} d where d.id = ${u}.id)` : ""}`;
|
|
50
|
+
const taken = (kind, value) => report.existing
|
|
51
|
+
? `or exists (select 1 from ${t("users")} d where d.${kind} = ${value}) or exists (select 1 from ${t("identities")} d where d.provider = '${kind}' and d.subject = ${value})`
|
|
52
|
+
: "";
|
|
53
|
+
const c = await one(`select
|
|
54
|
+
(select count(*) from auth.users) as total,
|
|
55
|
+
(select count(*) from auth.users u where ${moving("u")}) as moving,
|
|
56
|
+
(select count(*) from auth.users where deleted_at is not null) as deleted,
|
|
57
|
+
(select count(*) from auth.users u where u.deleted_at is null and u.banned_until >= now()) as banned,
|
|
58
|
+
(select count(*) from auth.users u where ${moving("u")} and coalesce(u.is_anonymous, false)) as guests,
|
|
59
|
+
(select count(*) from auth.users u where ${moving("u")} and ${MOVED_PASSWORD("u", o.keepUnprovenPasswords)} is not null) as passwords,
|
|
60
|
+
(select count(*) from auth.users u where ${moving("u")} and u.email_confirmed_at is null and u.encrypted_password like '$2%') as unconfirmed_passwords,
|
|
61
|
+
(select count(*) from auth.users u where ${moving("u")} and ${EMAIL_OF("u")} is not null and (
|
|
62
|
+
exists (select 1 from auth.users o where o.id <> u.id and ${moving("o")} and ${EMAIL_OF("o")} = ${EMAIL_OF("u")}
|
|
63
|
+
and (coalesce(o.created_at, '-infinity'), o.id) < (coalesce(u.created_at, '-infinity'), u.id)) ${taken("email", EMAIL_OF("u"))})) as shared_email,
|
|
64
|
+
(select count(*) from auth.users u where ${moving("u")} and ${PHONE_OF("u")} is not null and (
|
|
65
|
+
exists (select 1 from auth.users o where o.id <> u.id and ${moving("o")} and ${PHONE_OF("o")} = ${PHONE_OF("u")}
|
|
66
|
+
and (coalesce(o.created_at, '-infinity'), o.id) < (coalesce(u.created_at, '-infinity'), u.id)) ${taken("phone", PHONE_OF("u"))})) as shared_phone,
|
|
67
|
+
${report.existing
|
|
68
|
+
? `(select count(*) from auth.identities i join auth.users u on u.id = i.user_id where ${moving("u")} and i.provider in ('apple', 'google', 'github') and coalesce(i.provider_id, '') <> ''
|
|
69
|
+
and exists (select 1 from ${t("identities")} d where d.provider = i.provider and d.subject = i.provider_id))`
|
|
70
|
+
: "0"} as shared_identity,
|
|
71
|
+
(select count(*) from auth.users u where ${moving("u")} and ${SSO("u")}) as sso,
|
|
72
|
+
(select count(*) from auth.users u where ${moving("u")} and not ${SSO("u")} and u.encrypted_password like '$2%'
|
|
73
|
+
and exists (select 1 from auth.identities i where i.user_id = u.id and i.provider = 'email')
|
|
74
|
+
and not exists (select 1 from auth.identities i where i.user_id = u.id and i.provider not in ('email', 'phone'))) as email_signups,
|
|
75
|
+
(select count(*) from auth.users u where ${moving("u")} and ${CONFIRMED_AT_ONCE("u")}) as confirmed_at_once,
|
|
76
|
+
(select count(*) from auth.users u where ${moving("u")} and not coalesce(u.is_anonymous, false)
|
|
77
|
+
and ${EMAIL_OF("u")} is null and ${PHONE_OF("u")} is null
|
|
78
|
+
and not exists (select 1 from auth.identities i where i.user_id = u.id and i.provider in ('apple', 'google', 'github'))) as no_way_in`);
|
|
79
|
+
report.counts.push({ label: "Users", count: n(c.moving), note: `of ${n(c.total)} in auth.users` });
|
|
80
|
+
report.has.guests = n(c.guests) > 0;
|
|
81
|
+
report.has.passwords = n(c.passwords) > 0;
|
|
82
|
+
const providers = await q(`select i.provider,
|
|
83
|
+
count(*) filter (where ${IDENTITY_MOVES}) as moving,
|
|
84
|
+
count(*) filter (where not (${IDENTITY_MOVES})) as skipped
|
|
85
|
+
from auth.identities i join auth.users u on u.id = i.user_id
|
|
86
|
+
where ${moving("u")} group by i.provider order by 2 desc, 1`);
|
|
87
|
+
for (const r of providers.rows) {
|
|
88
|
+
const supported = ["apple", "google", "github", "email", "phone"].includes(r.provider ?? "");
|
|
89
|
+
const label = { apple: "Sign in with Apple", google: "Google", github: "GitHub", email: "Email", phone: "Phone" }[r.provider ?? ""] ?? r.provider ?? "?";
|
|
90
|
+
if (!supported)
|
|
91
|
+
report.counts.push({ label, count: 0, note: `${n(r.moving) + n(r.skipped)} not moved: direct-auth doesn't sign in with ${r.provider}`, warn: true });
|
|
92
|
+
else
|
|
93
|
+
report.counts.push({ label, count: n(r.moving), note: n(r.skipped) ? `${n(r.skipped)} unconfirmed left out` : undefined, warn: n(r.skipped) > 0 });
|
|
94
|
+
}
|
|
95
|
+
const shared = n(c.shared_email) + n(c.shared_phone) + n(c.shared_identity);
|
|
96
|
+
if (shared) {
|
|
97
|
+
report.counts.push({
|
|
98
|
+
label: "Shared",
|
|
99
|
+
count: shared,
|
|
100
|
+
note: "an address, number, Apple, Google or GitHub account another account has (an older one, or one here already): it stays with that one, and these move without it. Two accounts of one person, to merge or tell apart by hand",
|
|
101
|
+
warn: true,
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
if (n(c.sso)) {
|
|
105
|
+
report.counts.push({
|
|
106
|
+
label: "SSO (SAML)",
|
|
107
|
+
count: n(c.sso),
|
|
108
|
+
note: "their address is what their company's identity provider said, which proves nothing here: they move without it, as unconfirmed",
|
|
109
|
+
warn: true,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (n(c.confirmed_at_once)) {
|
|
113
|
+
report.counts.push({
|
|
114
|
+
label: "Confirmed at once",
|
|
115
|
+
count: n(c.confirmed_at_once),
|
|
116
|
+
note: `of ${n(c.email_signups)} email sign-ups had their address confirmed within 2 seconds: Supabase's Confirm email looks off (or the admin API made them confirmed), so nobody proved those addresses. Each moves as that address's account here: an email code to it signs in to it. ${o.keepUnprovenPasswords ? "Their passwords move too (--keep-unproven-passwords): whoever set one keeps it" : "Their passwords stay behind (whoever signed up with the address set them): --keep-unproven-passwords to move them"}`,
|
|
117
|
+
warn: true,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
const hasTokens = (await one(`select to_regclass('auth.refresh_tokens') is not null as has`)).has === "t";
|
|
121
|
+
const claimable = hasTokens && n(c.guests)
|
|
122
|
+
? n((await one(`select count(*) as n from auth.users u where ${moving("u")} and coalesce(u.is_anonymous, false) and exists (${GUEST_TOKENS("u")})`)).n)
|
|
123
|
+
: 0;
|
|
124
|
+
report.claimable = claimable;
|
|
125
|
+
report.counts.push({
|
|
126
|
+
label: "Guests (anonymous)",
|
|
127
|
+
count: n(c.guests),
|
|
128
|
+
note: n(c.guests)
|
|
129
|
+
? `as guests, same ids and data; ${claimable} of them get back in once with the Supabase refresh token their app kept (claimGuest, within ${CLAIM_DAYS} days)${claimable < n(c.guests) ? `; the other ${n(c.guests) - claimable} have no session left to claim with` : ""}`
|
|
130
|
+
: undefined,
|
|
131
|
+
warn: claimable < n(c.guests),
|
|
132
|
+
});
|
|
133
|
+
report.counts.push({
|
|
134
|
+
label: "Password hashes",
|
|
135
|
+
count: n(c.passwords),
|
|
136
|
+
note: n(c.unconfirmed_passwords)
|
|
137
|
+
? `${n(c.unconfirmed_passwords)} on unconfirmed addresses left out`
|
|
138
|
+
: n(c.passwords)
|
|
139
|
+
? "bcrypt, checked as they are and made PBKDF2 at each one's first sign-in; GoTrue gives users made without a password a random one, so each counts as a password once it signs in"
|
|
140
|
+
: undefined,
|
|
141
|
+
warn: n(c.unconfirmed_passwords) > 0,
|
|
142
|
+
});
|
|
143
|
+
const hasPasskeys = (await one(`select to_regclass('auth.webauthn_credentials') is not null as has`)).has === "t";
|
|
144
|
+
if (hasPasskeys) {
|
|
145
|
+
const p = await one(`select count(*) as n from auth.webauthn_credentials w join auth.users u on u.id = w.user_id where ${moving("u")}`);
|
|
146
|
+
report.counts.push({ label: "Passkeys", count: n(p.n), note: n(p.n) ? "same relying party (PASSKEY_RP_ID) needed" : undefined });
|
|
147
|
+
report.has.passkeys = n(p.n) > 0;
|
|
148
|
+
}
|
|
149
|
+
if ((await one(`select to_regclass('auth.sessions') is not null as has`)).has === "t") {
|
|
150
|
+
const ss = await one(`select count(*) as n from auth.sessions s join auth.users u on u.id = s.user_id where ${ELIGIBLE("u")} and (s.not_after is null or s.not_after > now())`);
|
|
151
|
+
if (n(ss.n))
|
|
152
|
+
report.counts.push({ label: "Sessions", count: n(ss.n), note: "not moved: everyone signs in again, in the app's new version" });
|
|
153
|
+
}
|
|
154
|
+
report.counts.push({ label: "Banned", count: n(c.banned), note: n(c.banned) ? "left out" : undefined, warn: n(c.banned) > 0 });
|
|
155
|
+
report.counts.push({ label: "Deleted", count: n(c.deleted), note: n(c.deleted) ? "left out" : undefined });
|
|
156
|
+
if (n(c.no_way_in)) {
|
|
157
|
+
report.counts.push({ label: "No way in", count: n(c.no_way_in), note: "nothing confirmed to sign in with: they move, but can't sign in until they're given one", warn: true });
|
|
158
|
+
}
|
|
159
|
+
if ((await one(`select to_regclass('auth.mfa_factors') is not null as has`)).has === "t") {
|
|
160
|
+
const m = await one(`select count(distinct f.user_id) as n from auth.mfa_factors f join auth.users u on u.id = f.user_id where f.status::text = 'verified' and ${moving("u")}`);
|
|
161
|
+
report.mfa = n(m.n);
|
|
162
|
+
if (report.mfa) {
|
|
163
|
+
report.counts.push({
|
|
164
|
+
label: "Second factors",
|
|
165
|
+
count: report.mfa,
|
|
166
|
+
note: "direct-auth doesn't check one: they'd sign in with the first alone. The move stops unless --without-mfa",
|
|
167
|
+
warn: true,
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// Foreign keys on auth.users(id): the script points them at the new table, with all they say.
|
|
172
|
+
const fks = await q(`${FULL_NAMES} select c.conname, c.conrelid::regclass::text as tbl, a.attname as col, ${ON_DELETE} as action, pg_get_constraintdef(c.oid) as def, k.relkind = 'p' as partitioned,
|
|
173
|
+
(array_length(c.conkey, 1) = 1 and c.confkey = array[(select attnum from pg_attribute where attrelid = 'auth.users'::regclass and attname = 'id')]::int2[]) as movable
|
|
174
|
+
from pg_constraint c
|
|
175
|
+
join pg_attribute a on a.attrelid = c.conrelid and a.attnum = c.conkey[1]
|
|
176
|
+
join pg_class k on k.oid = c.conrelid join pg_namespace ns on ns.oid = k.relnamespace
|
|
177
|
+
where c.contype = 'f' and c.confrelid = 'auth.users'::regclass and c.conparentid = 0 and ${notSystem("ns.nspname")}
|
|
178
|
+
order by 2, 3`);
|
|
179
|
+
for (const r of fks.rows) {
|
|
180
|
+
if (r.movable !== "t") {
|
|
181
|
+
report.attention.push({ name: `${r.tbl}.${r.col}`, kind: "foreign key", todo: `${r.def}: not on auth.users(id) alone, so it stays; point it at ${t("users")} by hand` });
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const partitioned = r.partitioned === "t";
|
|
185
|
+
report.fks.push({ table: r.tbl, column: r.col, constraint: r.conname, onDelete: r.action, definition: r.def, ...(partitioned ? { partitioned } : {}) });
|
|
186
|
+
const left = await one(`select count(*) as n from ${r.tbl} x join auth.users u on u.id = x.${quote(r.col)} where not (${ELIGIBLE("u")})`);
|
|
187
|
+
if (partitioned && n(left.n)) {
|
|
188
|
+
report.attention.push({
|
|
189
|
+
name: `${r.tbl}.${r.col}`,
|
|
190
|
+
kind: "foreign key",
|
|
191
|
+
todo: `partitioned, and ${n(left.n)} ${n(left.n) === 1 ? "row belongs" : "rows belong"} to users left out (banned or deleted): its key can't be NOT VALID, so the move stops at it. Delete those rows first, or move the key by hand`,
|
|
192
|
+
});
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
report.attention.push({ name: `${r.tbl}.${r.col}`, kind: "foreign key", todo: `points at ${t("users")} instead (${r.def.replace(/^FOREIGN KEY \([^)]*\) REFERENCES auth\.users\(id\)\s*/, "") || `on delete ${r.action}`})`, automatic: true });
|
|
196
|
+
if (n(left.n)) {
|
|
197
|
+
report.attention.push({
|
|
198
|
+
name: `${r.tbl}.${r.col}`,
|
|
199
|
+
kind: "foreign key",
|
|
200
|
+
todo: `${n(left.n)} ${n(left.n) === 1 ? "row belongs" : "rows belong"} to users left out (banned or deleted): the key is added NOT VALID, so they stay; delete them, then: alter table ${r.tbl} validate constraint ${quote(r.conname)}`,
|
|
201
|
+
});
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
const triggers = await q(`select tgname, tgfoid::regproc::text as fn from pg_trigger where tgrelid = 'auth.users'::regclass and not tgisinternal order by 1`);
|
|
205
|
+
for (const r of triggers.rows) {
|
|
206
|
+
report.attention.push({
|
|
207
|
+
name: `${r.tgname} → ${r.fn}()`,
|
|
208
|
+
kind: "trigger",
|
|
209
|
+
todo: `runs on auth.users, where nobody is made anymore. After the move, make it a trigger after insert on ${t("users")} (new.id, new.email, new.phone, new.name, new.picture) and drop this one`,
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
const fns = await q(`select ns.nspname || '.' || p.proname as name,
|
|
213
|
+
p.prosrc ilike '%auth.users%' as reads_users,
|
|
214
|
+
p.prosrc ~* '(app|user)_metadata' as metadata
|
|
215
|
+
from pg_proc p join pg_namespace ns on ns.oid = p.pronamespace
|
|
216
|
+
where ${notSystem("ns.nspname")} and (p.prosrc ilike '%auth.users%' or p.prosrc ~* '(app|user)_metadata')
|
|
217
|
+
order by 1`);
|
|
218
|
+
for (const r of fns.rows) {
|
|
219
|
+
if (r.reads_users === "t")
|
|
220
|
+
report.attention.push({ name: `${r.name}()`, kind: "function", todo: `reads auth.users: point it at ${t("users")}` });
|
|
221
|
+
if (r.metadata === "t")
|
|
222
|
+
report.attention.push({ name: `${r.name}()`, kind: "claim", todo: "reads app_metadata or user_metadata, which direct-auth's tokens don't carry: add what it needs with the claims option" });
|
|
223
|
+
}
|
|
224
|
+
const policies = await q(`select schemaname || '.' || tablename || ': ' || policyname as name,
|
|
225
|
+
coalesce(qual, '') || ' ' || coalesce(with_check, '') as body
|
|
226
|
+
from pg_policies where ${notSystem("schemaname")} order by 1`);
|
|
227
|
+
let usesUid = 0;
|
|
228
|
+
for (const r of policies.rows) {
|
|
229
|
+
const body = r.body ?? "";
|
|
230
|
+
if (/auth\.users/i.test(body))
|
|
231
|
+
report.attention.push({ name: r.name, kind: "policy", todo: `reads auth.users: read ${t("users")} instead` });
|
|
232
|
+
if (/(app|user)_metadata/i.test(body))
|
|
233
|
+
report.attention.push({ name: r.name, kind: "claim", todo: "reads app_metadata or user_metadata: add it with the claims option, or read a table" });
|
|
234
|
+
if (/auth\.(uid|jwt|role|email)\s*\(/i.test(body))
|
|
235
|
+
usesUid += 1;
|
|
236
|
+
}
|
|
237
|
+
if (usesUid)
|
|
238
|
+
report.notes.push(`${usesUid} ${usesUid === 1 ? "policy reads" : "policies read"} auth.uid(), auth.jwt() or auth.role(): they keep working, filled from direct-auth's token (is_anonymous included).`);
|
|
239
|
+
const views = await q(`select schemaname || '.' || viewname as name from pg_views where ${notSystem("schemaname")} and definition ilike '%auth.users%' order by 1`);
|
|
240
|
+
for (const r of views.rows)
|
|
241
|
+
report.attention.push({ name: r.name, kind: "view", todo: `reads auth.users: read ${t("users")} instead` });
|
|
242
|
+
if ((await one(`select to_regclass('storage.objects') is not null as has`)).has === "t") {
|
|
243
|
+
const s = await one(`select count(*) filter (where owner_id is not null or owner is not null) as owned from storage.objects`).catch(() => ({ owned: "0" }));
|
|
244
|
+
report.notes.push(`Storage: ${n(s.owned)} objects with an owner keep it (the same ids). Storage and Realtime read direct-auth's token once it is a Third-Party Auth issuer.`);
|
|
245
|
+
}
|
|
246
|
+
report.notes.push("A hard stop, in this order: turn Supabase Auth's sign-ins and sign-ups off, move, then point the app's new version at direct-auth and make it the minimum. No session comes over (but a guest's, claimed once): everyone signs in again, as the same user.");
|
|
247
|
+
return report;
|
|
248
|
+
},
|
|
249
|
+
applySql: (o) => supabaseMoveSql({ prefix: o.prefix, allowMfaLoss: o.allowMfaLoss, keepUnprovenPasswords: o.keepUnprovenPasswords }),
|
|
250
|
+
rollbackSql(report, o) {
|
|
251
|
+
const t = (name) => `public.${o.prefix}${name}`;
|
|
252
|
+
const ours = new RegExp(`REFERENCES (?:public\\.)?${o.prefix}users\\(id\\)`);
|
|
253
|
+
const lines = [
|
|
254
|
+
"-- direct-auth: the way back from `direct-auth migrate`, written right before it ran. Read it before running it.",
|
|
255
|
+
"-- The foreign keys go back to auth.users as they were: valid where they were, once no row is of a user only",
|
|
256
|
+
"-- direct-auth has (those stay, and the key with them is left NOT VALID).",
|
|
257
|
+
report.existing
|
|
258
|
+
? "-- direct-auth's tables were there before, so they stay; the users the move took from Supabase go from them."
|
|
259
|
+
: "-- direct-auth's tables didn't exist before, so they go, with whatever they got since (sessions, new users).",
|
|
260
|
+
"begin;",
|
|
261
|
+
"",
|
|
262
|
+
];
|
|
263
|
+
for (const fk of report.fks) {
|
|
264
|
+
const wasValid = !/\sNOT VALID$/.test(fk.definition);
|
|
265
|
+
const back = fk.definition.replace(/\s+NOT VALID$/, "").replace(ours, "REFERENCES auth.users(id)");
|
|
266
|
+
const name = quote(fk.constraint);
|
|
267
|
+
lines.push(`alter table ${fk.table} drop constraint if exists ${name};`);
|
|
268
|
+
if (fk.partitioned) {
|
|
269
|
+
// A partitioned table takes no NOT VALID key: it goes back valid, or the way back stops here.
|
|
270
|
+
lines.push(`alter table ${fk.table} add constraint ${name} ${back};`);
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
lines.push(`alter table ${fk.table} add constraint ${name} ${back} not valid;`);
|
|
274
|
+
if (wasValid) {
|
|
275
|
+
const col = quote(fk.column);
|
|
276
|
+
lines.push(`do $$ begin`, ` if not exists (select 1 from ${fk.table} x where x.${col} is not null and not exists (select 1 from auth.users u where u.id = x.${col})) then`, ` alter table ${fk.table} validate constraint ${name};`, ` else`, ` raise notice '${fk.table.replace(/'/g, "''")}.${fk.column.replace(/'/g, "''")}: rows of users only direct-auth has; the key stays NOT VALID';`, ` end if;`, `end $$;`);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
lines.push("");
|
|
280
|
+
if (report.existing) {
|
|
281
|
+
// Their guest claims and everything else of theirs go with them (on delete cascade).
|
|
282
|
+
lines.push(`delete from ${t("users")} where id in (select id from auth.users);`);
|
|
283
|
+
lines.push(`delete from ${t("meta")} where key = 'supabase_moved';`);
|
|
284
|
+
// Its users wait in Supabase Auth again: nobody is made here until the next move.
|
|
285
|
+
lines.push(`insert into ${t("meta")} (key, value) values ('supabase_pending', 'back') on conflict (key) do nothing;`);
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
for (const name of ["guest_claims", "refresh", "sessions", "identities", "passkeys", "challenges", "codes", "limits", "users", "meta"]) {
|
|
289
|
+
lines.push(`drop table if exists ${t(name)} cascade;`);
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
lines.push("", "commit;", "");
|
|
293
|
+
return lines.join("\n");
|
|
294
|
+
},
|
|
295
|
+
async verify(q, o) {
|
|
296
|
+
const t = (name) => `public.${o.prefix}${name}`;
|
|
297
|
+
const one = async (sql) => (await q(sql)).rows[0] ?? {};
|
|
298
|
+
const checks = [];
|
|
299
|
+
const here = (u) => `exists (select 1 from ${t("users")} d where d.id = ${u}.id)`;
|
|
300
|
+
// Ways in another account has here stayed with it: missing from this one isn't missing.
|
|
301
|
+
const elsewhere = (provider, subject) => `exists (select 1 from ${t("identities")} x where x.provider = ${provider} and x.subject = ${subject})`;
|
|
302
|
+
const c = await one(`select
|
|
303
|
+
(select count(*) from auth.users u where ${ELIGIBLE("u")}) as eligible,
|
|
304
|
+
(select count(*) from auth.users u where ${ELIGIBLE("u")} and not ${here("u")}) as missing,
|
|
305
|
+
(select count(*) from auth.users u where ${ELIGIBLE("u")} and coalesce(u.is_anonymous, false)
|
|
306
|
+
and not exists (select 1 from ${t("users")} d where d.id = u.id and d.is_guest = 1)) as guests_missing,
|
|
307
|
+
(select count(*) from auth.users u join ${t("users")} d on d.id = u.id
|
|
308
|
+
where ${ELIGIBLE("u")} and d.email is not null and d.email = ${EMAIL_OF("u")} and ${MOVED_PASSWORD("u", o.keepUnprovenPasswords)} is not null and d.password_hash is null) as passwords_missing,
|
|
309
|
+
(select count(*) from auth.users u join ${t("users")} d on d.id = u.id
|
|
310
|
+
where ${ELIGIBLE("u")} and ((${EMAIL_OF("u")} is not null and d.email is distinct from ${EMAIL_OF("u")})
|
|
311
|
+
or (${PHONE_OF("u")} is not null and d.phone is distinct from ${PHONE_OF("u")})
|
|
312
|
+
or exists (select 1 from auth.identities i join ${t("identities")} x on x.provider = i.provider and x.subject = i.provider_id
|
|
313
|
+
where i.user_id = u.id and i.provider in ('apple', 'google', 'github') and x.user_id <> u.id))) as shared,
|
|
314
|
+
(select count(*) from auth.identities i join auth.users u on u.id = i.user_id
|
|
315
|
+
where ${ELIGIBLE("u")} and ${here("u")} and i.provider in ('apple', 'google', 'github') and coalesce(i.provider_id, '') <> ''
|
|
316
|
+
and not ${elsewhere("i.provider", "i.provider_id")}) as identities_missing,
|
|
317
|
+
(select count(*) from auth.users u join ${t("users")} d on d.id = u.id
|
|
318
|
+
where ${ELIGIBLE("u")} and ((${EMAIL_OF("u")} is not null and not ${elsewhere("'email'", EMAIL_OF("u"))})
|
|
319
|
+
or (${PHONE_OF("u")} is not null and not ${elsewhere("'phone'", PHONE_OF("u"))}))) as addresses_missing`);
|
|
320
|
+
checks.push({ label: "Users", ok: n(c.missing) === 0, detail: n(c.missing) ? `${n(c.missing)} of ${n(c.eligible)} not moved` : `all ${n(c.eligible)} here` });
|
|
321
|
+
checks.push({
|
|
322
|
+
label: "Ways in",
|
|
323
|
+
ok: n(c.identities_missing) === 0 && n(c.addresses_missing) === 0,
|
|
324
|
+
detail: n(c.identities_missing) || n(c.addresses_missing)
|
|
325
|
+
? `${n(c.identities_missing) + n(c.addresses_missing)} identities missing`
|
|
326
|
+
: "every Apple, Google and GitHub identity, every confirmed address and number",
|
|
327
|
+
});
|
|
328
|
+
// A way in that stayed with another account (one made here before the move, most likely: sign-ins weren't off)
|
|
329
|
+
// is two accounts of one person, the data on one and the address on the other: not a move to go on from.
|
|
330
|
+
checks.push({
|
|
331
|
+
label: "Shared",
|
|
332
|
+
ok: n(c.shared) === 0,
|
|
333
|
+
detail: n(c.shared)
|
|
334
|
+
? `${n(c.shared)} moved without an address, number, Apple, Google or GitHub account another account has: two accounts of one person. Run the way back, find who made the other (sign-ins on before the move?), then move again`
|
|
335
|
+
: "no address, number or account split between two users",
|
|
336
|
+
});
|
|
337
|
+
checks.push({ label: "Guests", ok: n(c.guests_missing) === 0, detail: n(c.guests_missing) ? `${n(c.guests_missing)} missing` : "all there" });
|
|
338
|
+
if ((await one(`select to_regclass('auth.refresh_tokens') is not null as has`)).has === "t") {
|
|
339
|
+
const g = await one(`select
|
|
340
|
+
(select count(*) from auth.users u where ${ELIGIBLE("u")} and coalesce(u.is_anonymous, false) and exists (${GUEST_TOKENS("u")})) as claimable,
|
|
341
|
+
(select count(*) from auth.users u where ${ELIGIBLE("u")} and coalesce(u.is_anonymous, false) and exists (${GUEST_TOKENS("u")})
|
|
342
|
+
and not exists (select 1 from ${t("guest_claims")} gc where gc.user_id = u.id)) as unclaimable`);
|
|
343
|
+
checks.push({
|
|
344
|
+
label: "Guest claims",
|
|
345
|
+
ok: n(g.unclaimable) === 0,
|
|
346
|
+
detail: n(g.unclaimable) ? `${n(g.unclaimable)} of ${n(g.claimable)} guests with a Supabase session have no claim` : `${n(g.claimable)} guests can get back in with their app's Supabase session`,
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
checks.push({ label: "Passwords", ok: n(c.passwords_missing) === 0, detail: n(c.passwords_missing) ? `${n(c.passwords_missing)} hashes missing` : "every confirmed one" });
|
|
350
|
+
if ((await one(`select to_regclass('auth.webauthn_credentials') is not null as has`)).has === "t") {
|
|
351
|
+
const p = await one(`select count(*) as missing from auth.webauthn_credentials w join auth.users u on u.id = w.user_id
|
|
352
|
+
where ${ELIGIBLE("u")} and not exists (select 1 from ${t("passkeys")} d where d.id = ${B64URL("w.credential_id")})`);
|
|
353
|
+
checks.push({ label: "Passkeys", ok: n(p.missing) === 0, detail: n(p.missing) ? `${n(p.missing)} missing` : "all there" });
|
|
354
|
+
}
|
|
355
|
+
// A few users, at random, as themselves: same id, same confirmed address, a guest still a guest.
|
|
356
|
+
const sample = await q(`select u.id,
|
|
357
|
+
(d.id is not null) as there,
|
|
358
|
+
(d.email is null or d.email = ${EMAIL_OF("u")}) as same_email,
|
|
359
|
+
(d.is_guest = case when coalesce(u.is_anonymous, false) then 1 else 0 end) as same_guest
|
|
360
|
+
from auth.users u left join ${t("users")} d on d.id = u.id
|
|
361
|
+
where ${ELIGIBLE("u")} order by random() limit 5`);
|
|
362
|
+
const wrong = sample.rows.filter((r) => r.there !== "t" || r.same_email !== "t" || r.same_guest !== "t");
|
|
363
|
+
checks.push({
|
|
364
|
+
label: "Sample",
|
|
365
|
+
ok: wrong.length === 0,
|
|
366
|
+
detail: sample.rows.length
|
|
367
|
+
? `${sample.rows.map((r) => maskId(r.id ?? "")).join(", ")}${wrong.length ? `: ${wrong.map((r) => maskId(r.id ?? "")).join(", ")} differ` : ": the same users"}`
|
|
368
|
+
: "nobody to sample",
|
|
369
|
+
});
|
|
370
|
+
const f = await one(`select
|
|
371
|
+
(select count(*) from pg_constraint c join pg_class k on k.oid = c.conrelid join pg_namespace ns on ns.oid = k.relnamespace
|
|
372
|
+
where c.contype = 'f' and c.confrelid = 'auth.users'::regclass and c.conparentid = 0 and ${notSystem("ns.nspname")}
|
|
373
|
+
and array_length(c.conkey, 1) = 1
|
|
374
|
+
and c.confkey = array[(select attnum from pg_attribute where attrelid = 'auth.users'::regclass and attname = 'id')]::int2[]) as left_on_auth,
|
|
375
|
+
(select count(*) from pg_constraint c where c.contype = 'f' and c.confrelid = '${t("users")}'::regclass
|
|
376
|
+
and c.conrelid not in (select to_regclass(x) from unnest(array['${t("identities")}', '${t("sessions")}', '${t("passkeys")}', '${t("guest_claims")}']) x)) as on_ours`);
|
|
377
|
+
checks.push({
|
|
378
|
+
label: "Foreign keys",
|
|
379
|
+
ok: n(f.left_on_auth) === 0,
|
|
380
|
+
detail: n(f.left_on_auth) ? `${n(f.left_on_auth)} still on auth.users` : `${n(f.on_ours)} on ${t("users")}, none left on auth.users`,
|
|
381
|
+
});
|
|
382
|
+
return checks;
|
|
383
|
+
},
|
|
384
|
+
};
|
|
385
|
+
const quote = (name) => (/^[a-z_][a-z0-9_]*$/.test(name) ? name : `"${name.replace(/"/g, '""')}"`);
|
|
386
|
+
export const SOURCES = [supabaseSource, ...READERS.map(readerSource)];
|
|
387
|
+
/** The Supabase project's ref, from its database URL (db.<ref>.supabase.co, or the pooler's postgres.<ref> user). */
|
|
388
|
+
export function supabaseRef(url) {
|
|
389
|
+
try {
|
|
390
|
+
const u = new URL(url);
|
|
391
|
+
return /^db\.([a-z0-9]{20})\.supabase\.co$/.exec(u.hostname)?.[1] ?? /^postgres\.([a-z0-9]{20})$/.exec(decodeURIComponent(u.username))?.[1];
|
|
392
|
+
}
|
|
393
|
+
catch {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
}
|
package/dist/node.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node's own HTTP server in front of `createAuthServer`: for a VPS, a container, a laptop. Bun and Deno serve
|
|
3
|
+
* `server.fetch` as it is (`Bun.serve({ fetch: server.fetch })`, `Deno.serve(server.fetch)`).
|
|
4
|
+
*
|
|
5
|
+
* const server = await createAuthServer({ … });
|
|
6
|
+
* serve(server, { port: 8787 });
|
|
7
|
+
*/
|
|
8
|
+
import { type Server } from "node:http";
|
|
9
|
+
import type { AuthServer } from "./server.ts";
|
|
10
|
+
export interface ServeOptions {
|
|
11
|
+
port?: number;
|
|
12
|
+
host?: string;
|
|
13
|
+
/** Behind a proxy that sets `x-forwarded-for` (nginx, Caddy, Traefik): true takes the client's IP from it. */
|
|
14
|
+
behindProxy?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export declare function serve(server: Pick<AuthServer, "fetch">, o?: ServeOptions): Server;
|
package/dist/node.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node's own HTTP server in front of `createAuthServer`: for a VPS, a container, a laptop. Bun and Deno serve
|
|
3
|
+
* `server.fetch` as it is (`Bun.serve({ fetch: server.fetch })`, `Deno.serve(server.fetch)`).
|
|
4
|
+
*
|
|
5
|
+
* const server = await createAuthServer({ … });
|
|
6
|
+
* serve(server, { port: 8787 });
|
|
7
|
+
*/
|
|
8
|
+
import { createServer } from "node:http";
|
|
9
|
+
async function toRequest(req, behindProxy) {
|
|
10
|
+
const chunks = [];
|
|
11
|
+
let size = 0;
|
|
12
|
+
for await (const chunk of req) {
|
|
13
|
+
size += chunk.length;
|
|
14
|
+
if (size > 256 * 1024)
|
|
15
|
+
throw new Error("body too large");
|
|
16
|
+
chunks.push(chunk);
|
|
17
|
+
}
|
|
18
|
+
const headers = new Headers();
|
|
19
|
+
for (const [k, v] of Object.entries(req.headers)) {
|
|
20
|
+
if (v === undefined)
|
|
21
|
+
continue;
|
|
22
|
+
// Only what this process saw, or what its own proxy wrote: a client's own forwarding headers are dropped.
|
|
23
|
+
if (k === "cf-connecting-ip")
|
|
24
|
+
continue;
|
|
25
|
+
if (!behindProxy && (k === "x-forwarded-for" || k === "x-real-ip"))
|
|
26
|
+
continue;
|
|
27
|
+
headers.set(k, Array.isArray(v) ? v.join(", ") : v);
|
|
28
|
+
}
|
|
29
|
+
if (!behindProxy && req.socket.remoteAddress)
|
|
30
|
+
headers.set("x-real-ip", req.socket.remoteAddress);
|
|
31
|
+
const proto = behindProxy ? String(req.headers["x-forwarded-proto"] ?? "http").split(",")[0] : "http";
|
|
32
|
+
const url = `${proto}://${req.headers.host ?? "localhost"}${req.url ?? "/"}`;
|
|
33
|
+
const body = req.method === "GET" || req.method === "HEAD" ? undefined : Buffer.concat(chunks);
|
|
34
|
+
return new Request(url, { method: req.method, headers, body });
|
|
35
|
+
}
|
|
36
|
+
export function serve(server, o = {}) {
|
|
37
|
+
const http = createServer(async (req, res) => {
|
|
38
|
+
try {
|
|
39
|
+
const behind = o.behindProxy ?? false;
|
|
40
|
+
// The caller's address, handed over as Deno.serve hands it: the socket's, or what the proxy in front says.
|
|
41
|
+
const forwarded = String(req.headers["x-forwarded-for"] ?? "").split(",").map((s) => s.trim()).filter(Boolean);
|
|
42
|
+
const ip = behind ? (forwarded.at(-1) ?? String(req.headers["x-real-ip"] ?? "")) : req.socket.remoteAddress;
|
|
43
|
+
const response = await server.fetch(await toRequest(req, behind), { remoteAddr: { hostname: ip || undefined } });
|
|
44
|
+
const headers = {};
|
|
45
|
+
response.headers.forEach((v, k) => {
|
|
46
|
+
if (k === "set-cookie")
|
|
47
|
+
return;
|
|
48
|
+
headers[k] = v;
|
|
49
|
+
});
|
|
50
|
+
const cookies = response.headers.getSetCookie?.() ?? [];
|
|
51
|
+
if (cookies.length)
|
|
52
|
+
headers["set-cookie"] = cookies;
|
|
53
|
+
res.writeHead(response.status, headers);
|
|
54
|
+
res.end(Buffer.from(await response.arrayBuffer()));
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
res.writeHead(400, { "content-type": "application/json" });
|
|
58
|
+
res.end('{"error":"bad_request"}');
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
http.listen(o.port ?? 8787, o.host ?? "0.0.0.0");
|
|
62
|
+
return http;
|
|
63
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Passkeys (WebAuthn), checked with WebCrypto: registering one for a signed-in user, and signing in with one. The
|
|
3
|
+
* options and answers are WebAuthn's JSON forms (base64url), which browsers (`PublicKeyCredential.parseCreationOptionsFromJSON`,
|
|
4
|
+
* `toJSON()`), Android's Credential Manager and the iOS client all speak.
|
|
5
|
+
*
|
|
6
|
+
* Attestation is not asked for (`none`): a passkey is trusted as the key the user's device made, which is what
|
|
7
|
+
* sites do. What is checked: the challenge (once, five minutes), the origin, the relying party's ID hash, the user
|
|
8
|
+
* present flag (and verified, when asked), the signature, and the counter when the authenticator keeps one.
|
|
9
|
+
*/
|
|
10
|
+
import { type Bytes } from "./base64.ts";
|
|
11
|
+
import { type Sql } from "./db/sql.ts";
|
|
12
|
+
export interface PasskeyOptions {
|
|
13
|
+
/** The relying party's ID: the site's domain (`example.com`), the one the apps' associated domains name too. */
|
|
14
|
+
rpId: string;
|
|
15
|
+
/** The name a device shows (`Example`). */
|
|
16
|
+
rpName: string;
|
|
17
|
+
/**
|
|
18
|
+
* The origins an answer may come from: `https://example.com` for the site and the iOS app (iOS gives the RP ID's
|
|
19
|
+
* origin), `android:apk-key-hash:<base64url SHA-256 of the signing certificate>` for each Android app. Defaults to
|
|
20
|
+
* `https://<rpId>`.
|
|
21
|
+
*/
|
|
22
|
+
origins?: readonly string[];
|
|
23
|
+
/** `required` refuses a passkey used without a fingerprint, face or PIN (`preferred`). */
|
|
24
|
+
userVerification?: "required" | "preferred";
|
|
25
|
+
}
|
|
26
|
+
export declare function decodeCbor(bytes: Uint8Array): {
|
|
27
|
+
value: unknown;
|
|
28
|
+
length: number;
|
|
29
|
+
};
|
|
30
|
+
/** A COSE key as the JWK WebCrypto imports, and its algorithm: ES256 (-7) or RS256 (-257). */
|
|
31
|
+
export declare function coseToJwk(cose: Map<unknown, unknown>): {
|
|
32
|
+
jwk: JsonWebKey;
|
|
33
|
+
alg: -7 | -257;
|
|
34
|
+
};
|
|
35
|
+
/** An ECDSA signature from DER (what authenticators give) to r and s side by side (what WebCrypto checks). */
|
|
36
|
+
export declare function derToRaw(der: Uint8Array): Bytes;
|
|
37
|
+
export type PasskeyFailure = "passkey_bad_request" | "passkey_challenge" | "passkey_origin" | "passkey_rp" | "passkey_user_presence" | "passkey_user_verification" | "passkey_unknown" | "passkey_signature" | "passkey_counter" | "passkey_algorithm" | "passkey_exists";
|
|
38
|
+
export declare function passkeys(sql: Sql, o: PasskeyOptions): {
|
|
39
|
+
registrationOptions(user: {
|
|
40
|
+
id: string;
|
|
41
|
+
email: string | null;
|
|
42
|
+
phone: string | null;
|
|
43
|
+
name: string | null;
|
|
44
|
+
}, now?: number): Promise<{
|
|
45
|
+
challengeId: `${string}-${string}-${string}-${string}-${string}`;
|
|
46
|
+
options: {
|
|
47
|
+
rp: {
|
|
48
|
+
id: string;
|
|
49
|
+
name: string;
|
|
50
|
+
};
|
|
51
|
+
user: {
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
displayName: string;
|
|
55
|
+
};
|
|
56
|
+
challenge: string;
|
|
57
|
+
pubKeyCredParams: {
|
|
58
|
+
type: string;
|
|
59
|
+
alg: number;
|
|
60
|
+
}[];
|
|
61
|
+
timeout: number;
|
|
62
|
+
attestation: string;
|
|
63
|
+
authenticatorSelection: {
|
|
64
|
+
residentKey: string;
|
|
65
|
+
requireResidentKey: boolean;
|
|
66
|
+
userVerification: "required" | "preferred";
|
|
67
|
+
};
|
|
68
|
+
excludeCredentials: {
|
|
69
|
+
transports?: any;
|
|
70
|
+
type: string;
|
|
71
|
+
id: string;
|
|
72
|
+
}[];
|
|
73
|
+
};
|
|
74
|
+
}>;
|
|
75
|
+
register(userId: string, a: {
|
|
76
|
+
challengeId: unknown;
|
|
77
|
+
credential: unknown;
|
|
78
|
+
name?: unknown;
|
|
79
|
+
}, now?: number): Promise<{
|
|
80
|
+
ok: true;
|
|
81
|
+
passkey: {
|
|
82
|
+
id: string;
|
|
83
|
+
name: string | null;
|
|
84
|
+
createdAt: number;
|
|
85
|
+
};
|
|
86
|
+
} | {
|
|
87
|
+
ok: false;
|
|
88
|
+
reason: PasskeyFailure;
|
|
89
|
+
}>;
|
|
90
|
+
signInOptions(now?: number): Promise<{
|
|
91
|
+
challengeId: `${string}-${string}-${string}-${string}-${string}`;
|
|
92
|
+
options: {
|
|
93
|
+
challenge: string;
|
|
94
|
+
rpId: string;
|
|
95
|
+
timeout: number;
|
|
96
|
+
userVerification: "required" | "preferred";
|
|
97
|
+
allowCredentials: never[];
|
|
98
|
+
};
|
|
99
|
+
}>;
|
|
100
|
+
/** The user a passkey answer signs in, when every check holds. */
|
|
101
|
+
signIn(a: {
|
|
102
|
+
challengeId: unknown;
|
|
103
|
+
credential: unknown;
|
|
104
|
+
}, now?: number): Promise<{
|
|
105
|
+
ok: true;
|
|
106
|
+
userId: string;
|
|
107
|
+
} | {
|
|
108
|
+
ok: false;
|
|
109
|
+
reason: PasskeyFailure;
|
|
110
|
+
}>;
|
|
111
|
+
list(userId: string): Promise<{
|
|
112
|
+
id: string;
|
|
113
|
+
name: string | null;
|
|
114
|
+
createdAt: number;
|
|
115
|
+
lastUsedAt: number | null;
|
|
116
|
+
}[]>;
|
|
117
|
+
remove(userId: string, id: string): Promise<boolean>;
|
|
118
|
+
};
|
|
119
|
+
export type Passkeys = ReturnType<typeof passkeys>;
|
|
120
|
+
/**
|
|
121
|
+
* Whether a passkey's user handle names this user. Here, and in Supabase Auth (GoTrue's `WebAuthnID()` is
|
|
122
|
+
* `[]byte(user.ID.String())`, internal/models/user.go), it is the id as text: the UUID's 36 characters. The 16 bytes
|
|
123
|
+
* of the UUID, and the text in capitals, without hyphens or in braces, are taken too: the same id, as other servers
|
|
124
|
+
* register it.
|
|
125
|
+
*/
|
|
126
|
+
export declare function sameUser(handle: Uint8Array, userId: string): boolean;
|