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
|
@@ -0,0 +1,326 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Password hashes. New ones are PBKDF2-SHA256 with WebCrypto (`$pbkdf2-sha256$i=600000$<salt>$<hash>`, base64url,
|
|
3
|
+
* the iteration count inside, so a later default can raise it and old hashes still check). Hashes moved from
|
|
4
|
+
* Supabase Auth, Clerk and Auth0 are bcrypt (`$2a$10$…`), Firebase Auth's its modified scrypt
|
|
5
|
+
* (`$fbscrypt$r=8,m=14$<signer key>$<salt separator>$<salt>$<hash>`, standard base64, the project's parameters with
|
|
6
|
+
* each hash) and Better Auth's scrypt (`$bascrypt$<salt>$<key>`, hex); they are checked here, in plain TypeScript,
|
|
7
|
+
* and replaced with PBKDF2 the first time their owner signs in.
|
|
8
|
+
*
|
|
9
|
+
* Cloudflare Workers caps PBKDF2 at 100,000 iterations, so a hash made there uses that, and a Worker can't check a
|
|
10
|
+
* hash made elsewhere with more. Pick the count for the host that will check the hashes (`passwords.iterations`).
|
|
11
|
+
* A bcrypt check (cost 10, Supabase's) is about 80 ms of plain CPU: more than the Workers free plan's 10 ms a
|
|
12
|
+
* request, so a Worker checking moved hashes needs the paid plan (its limit is seconds). Elsewhere it gives way to
|
|
13
|
+
* other requests as it goes, every few milliseconds.
|
|
14
|
+
*/
|
|
15
|
+
import { b64url, fromBase64, utf8 } from "./base64.js";
|
|
16
|
+
import { betterAuthScrypt, firebaseScrypt } from "./scrypt.js";
|
|
17
|
+
export const DEFAULT_ITERATIONS = 600_000;
|
|
18
|
+
export const WORKERS_MAX_ITERATIONS = 100_000;
|
|
19
|
+
const onWorkers = () => globalThis.navigator?.userAgent === "Cloudflare-Workers";
|
|
20
|
+
/** The most iterations a check here can do: 100,000 on Workers (more throws there), no limit elsewhere. */
|
|
21
|
+
export function maxIterations() {
|
|
22
|
+
return onWorkers() ? WORKERS_MAX_ITERATIONS : Number.POSITIVE_INFINITY;
|
|
23
|
+
}
|
|
24
|
+
/** The iteration count new hashes get here: 600,000, or 100,000 on Workers. */
|
|
25
|
+
export function defaultIterations() {
|
|
26
|
+
return onWorkers() ? WORKERS_MAX_ITERATIONS : DEFAULT_ITERATIONS;
|
|
27
|
+
}
|
|
28
|
+
async function pbkdf2(password, salt, iterations) {
|
|
29
|
+
const key = await crypto.subtle.importKey("raw", utf8(password.normalize("NFKC")), "PBKDF2", false, ["deriveBits"]);
|
|
30
|
+
return new Uint8Array(await crypto.subtle.deriveBits({ name: "PBKDF2", hash: "SHA-256", salt: salt, iterations }, key, 256));
|
|
31
|
+
}
|
|
32
|
+
function equalBytes(a, b) {
|
|
33
|
+
if (a.length !== b.length)
|
|
34
|
+
return false;
|
|
35
|
+
let d = 0;
|
|
36
|
+
for (let i = 0; i < a.length; i += 1)
|
|
37
|
+
d |= a[i] ^ b[i];
|
|
38
|
+
return d === 0;
|
|
39
|
+
}
|
|
40
|
+
export async function hashPassword(password, iterations = defaultIterations()) {
|
|
41
|
+
const salt = crypto.getRandomValues(new Uint8Array(16));
|
|
42
|
+
return `$pbkdf2-sha256$i=${iterations}$${b64url(salt)}$${b64url(await pbkdf2(password, salt, iterations))}`;
|
|
43
|
+
}
|
|
44
|
+
const PBKDF2 = /^\$pbkdf2-sha256\$i=(\d{1,9})\$([A-Za-z0-9_-]{16,64})\$([A-Za-z0-9_-]{40,90})$/;
|
|
45
|
+
const BCRYPT = /^\$2[abxy]\$(\d\d)\$([./A-Za-z0-9]{22})([./A-Za-z0-9]{31})$/;
|
|
46
|
+
const B64 = "[A-Za-z0-9+/]+={0,2}";
|
|
47
|
+
const FIREBASE = new RegExp(`^\\$fbscrypt\\$r=(\\d{1,2}),m=(\\d{1,2})\\$(${B64})\\$(${B64}|)\\$(${B64}|)\\$(${B64})$`);
|
|
48
|
+
const BETTER_AUTH = /^\$bascrypt\$([0-9a-f]{2,128})\$([0-9a-f]{128})$/;
|
|
49
|
+
const std64 = (s) => Uint8Array.from(atob(s), (c) => c.charCodeAt(0));
|
|
50
|
+
/** What kind of moved hash `stored` is: `bcrypt`, `firebase` or `better-auth`; null for PBKDF2 or anything else. */
|
|
51
|
+
export function movedKind(stored) {
|
|
52
|
+
return BCRYPT.test(stored) ? "bcrypt" : FIREBASE.test(stored) ? "firebase" : BETTER_AUTH.test(stored) ? "better-auth" : null;
|
|
53
|
+
}
|
|
54
|
+
/** Firebase's parameters in a moved hash (the same for a whole project), for checks that have no hash to check. */
|
|
55
|
+
export function firebaseParams(stored) {
|
|
56
|
+
const f = FIREBASE.exec(stored);
|
|
57
|
+
const rounds = Number(f?.[1]), memCost = Number(f?.[2]);
|
|
58
|
+
return f && rounds >= 1 && rounds <= 8 && memCost >= 1 && memCost <= 14 ? { rounds, memCost } : null;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Whether `password` is the one `stored` was made from. `rehash` is true when the hash should be replaced (a bcrypt
|
|
62
|
+
* one, or fewer iterations than `iterations`). A hash this code can't read is simply not a match.
|
|
63
|
+
*/
|
|
64
|
+
export async function verifyPassword(password, stored, iterations = defaultIterations()) {
|
|
65
|
+
const p = PBKDF2.exec(stored);
|
|
66
|
+
if (p) {
|
|
67
|
+
const n = Number(p[1]);
|
|
68
|
+
if (n < 1)
|
|
69
|
+
return { ok: false, rehash: false };
|
|
70
|
+
// More than a Worker can do: not a match, after the most it can do (as long as a check of a missing account).
|
|
71
|
+
if (n > maxIterations()) {
|
|
72
|
+
await pbkdf2(password, new Uint8Array(16), maxIterations());
|
|
73
|
+
return { ok: false, rehash: false };
|
|
74
|
+
}
|
|
75
|
+
const ok = equalBytes(await pbkdf2(password, fromBase64(p[2]), n), fromBase64(p[3]));
|
|
76
|
+
// Fewer iterations, or more, than now: remade with today's count, so every check costs the same.
|
|
77
|
+
return { ok, rehash: ok && n !== iterations };
|
|
78
|
+
}
|
|
79
|
+
const b = BCRYPT.exec(stored);
|
|
80
|
+
if (b) {
|
|
81
|
+
const cost = Number(b[1]);
|
|
82
|
+
if (cost < 4 || cost > 16)
|
|
83
|
+
return { ok: false, rehash: false };
|
|
84
|
+
const ok = equalBytes(utf8(await bcryptAsync(password, cost, decodeBcrypt64(b[2], 16))), utf8(b[3]));
|
|
85
|
+
return { ok, rehash: ok };
|
|
86
|
+
}
|
|
87
|
+
const f = FIREBASE.exec(stored);
|
|
88
|
+
if (f) {
|
|
89
|
+
const rounds = Number(f[1]);
|
|
90
|
+
const memCost = Number(f[2]);
|
|
91
|
+
// Firebase's own range: a hash claiming more is no match, and costs no more than the most Firebase uses.
|
|
92
|
+
if (rounds < 1 || rounds > 8 || memCost < 1 || memCost > 14)
|
|
93
|
+
return { ok: false, rehash: false };
|
|
94
|
+
let parts;
|
|
95
|
+
try {
|
|
96
|
+
parts = [f[3], f[4], f[5], f[6]].map(std64);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
// Base64 that doesn't decode: no match, as for a hash this code can't read.
|
|
100
|
+
return { ok: false, rehash: false };
|
|
101
|
+
}
|
|
102
|
+
const [signerKey, saltSeparator, salt, hash] = parts;
|
|
103
|
+
const ok = equalBytes(await firebaseScrypt(password, { rounds, memCost, signerKey, saltSeparator, salt }), hash);
|
|
104
|
+
return { ok, rehash: ok };
|
|
105
|
+
}
|
|
106
|
+
const a = BETTER_AUTH.exec(stored);
|
|
107
|
+
if (a) {
|
|
108
|
+
const key = await betterAuthScrypt(password, a[1]);
|
|
109
|
+
const ok = equalBytes(utf8([...key].map((x) => x.toString(16).padStart(2, "0")).join("")), utf8(a[2]));
|
|
110
|
+
return { ok, rehash: ok };
|
|
111
|
+
}
|
|
112
|
+
return { ok: false, rehash: false };
|
|
113
|
+
}
|
|
114
|
+
/** The work a check does when there is no hash to check against, so a missing account takes as long as a wrong password. */
|
|
115
|
+
export async function burnPassword(password, iterations = defaultIterations()) {
|
|
116
|
+
await pbkdf2(password, new Uint8Array(16), Math.min(iterations, maxIterations()));
|
|
117
|
+
}
|
|
118
|
+
/** The work of checking a bcrypt hash of `cost`, done when there is none to check: see `burnPassword`. */
|
|
119
|
+
export async function burnBcrypt(password, cost = 10) {
|
|
120
|
+
if (cost >= 4 && cost <= 16)
|
|
121
|
+
await bcryptAsync(password, cost, new Uint8Array(16));
|
|
122
|
+
}
|
|
123
|
+
/** The work of checking a Firebase hash with these parameters, or a Better Auth one, done when there is none to check. */
|
|
124
|
+
export async function burnScrypt(password, kind, p = { rounds: 8, memCost: 14 }) {
|
|
125
|
+
if (kind === "better-auth")
|
|
126
|
+
await betterAuthScrypt(password, "00");
|
|
127
|
+
else
|
|
128
|
+
await firebaseScrypt(password, { ...p, salt: new Uint8Array(16), saltSeparator: new Uint8Array(1), signerKey: new Uint8Array(64) });
|
|
129
|
+
}
|
|
130
|
+
/** The cost of a bcrypt hash (10 in `$2a$10$…`), or null for another kind. */
|
|
131
|
+
export function bcryptCost(stored) {
|
|
132
|
+
const b = BCRYPT.exec(stored);
|
|
133
|
+
return b ? Number(b[1]) : null;
|
|
134
|
+
}
|
|
135
|
+
/** The iteration count of a PBKDF2 hash, or null for another kind. */
|
|
136
|
+
export function pbkdf2Iterations(stored) {
|
|
137
|
+
const p = PBKDF2.exec(stored);
|
|
138
|
+
return p ? Number(p[1]) : null;
|
|
139
|
+
}
|
|
140
|
+
/** PBKDF2 work on top of a check that did `done` iterations, so it costs `target` in all. */
|
|
141
|
+
export async function topUp(password, done, target) {
|
|
142
|
+
const cap = maxIterations();
|
|
143
|
+
if (Math.min(target, cap) > Math.min(done, cap))
|
|
144
|
+
await pbkdf2(password, new Uint8Array(16), Math.min(target, cap) - Math.min(done, cap));
|
|
145
|
+
}
|
|
146
|
+
// The next turn of the event loop, so a long bcrypt check lets other requests through.
|
|
147
|
+
const pause = () => new Promise((resolve) => {
|
|
148
|
+
const g = globalThis;
|
|
149
|
+
if (typeof g.setImmediate === "function")
|
|
150
|
+
g.setImmediate(resolve);
|
|
151
|
+
else
|
|
152
|
+
setTimeout(resolve, 0);
|
|
153
|
+
});
|
|
154
|
+
async function bcryptAsync(password, cost, salt) {
|
|
155
|
+
const steps = bcryptSteps(password, cost, salt);
|
|
156
|
+
for (;;) {
|
|
157
|
+
const step = steps.next();
|
|
158
|
+
if (step.done)
|
|
159
|
+
return step.value;
|
|
160
|
+
await pause();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
// --- bcrypt, only to check hashes moved from elsewhere (Supabase Auth's are `$2a$10$`) ---
|
|
164
|
+
const BCRYPT_ALPHABET = "./ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
165
|
+
function decodeBcrypt64(text, length) {
|
|
166
|
+
const out = new Uint8Array(length);
|
|
167
|
+
let bits = 0;
|
|
168
|
+
let value = 0;
|
|
169
|
+
let at = 0;
|
|
170
|
+
for (const ch of text) {
|
|
171
|
+
const v = BCRYPT_ALPHABET.indexOf(ch);
|
|
172
|
+
if (v < 0)
|
|
173
|
+
throw new Error("bcrypt: bad character");
|
|
174
|
+
value = (value << 6) | v;
|
|
175
|
+
bits += 6;
|
|
176
|
+
if (bits >= 8) {
|
|
177
|
+
bits -= 8;
|
|
178
|
+
if (at < length)
|
|
179
|
+
out[at++] = (value >> bits) & 0xff;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
function encodeBcrypt64(bytes) {
|
|
185
|
+
let out = "";
|
|
186
|
+
let bits = 0;
|
|
187
|
+
let value = 0;
|
|
188
|
+
for (const byte of bytes) {
|
|
189
|
+
value = ((value << 8) | byte) & 0xffffff;
|
|
190
|
+
bits += 8;
|
|
191
|
+
while (bits >= 6) {
|
|
192
|
+
bits -= 6;
|
|
193
|
+
out += BCRYPT_ALPHABET[(value >> bits) & 63];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (bits > 0)
|
|
197
|
+
out += BCRYPT_ALPHABET[(value << (6 - bits)) & 63];
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Blowfish's initial P-array and S-boxes are the hexadecimal digits of pi: 1,042 words, worked out once (Machin's
|
|
202
|
+
* formula over BigInt) rather than pasted in.
|
|
203
|
+
*/
|
|
204
|
+
let PI_WORDS = null;
|
|
205
|
+
function piWords() {
|
|
206
|
+
if (PI_WORDS)
|
|
207
|
+
return PI_WORDS;
|
|
208
|
+
const words = 18 + 4 * 256;
|
|
209
|
+
const bits = BigInt(words * 32 + 64);
|
|
210
|
+
const one = 1n << bits;
|
|
211
|
+
const atanInv = (x) => {
|
|
212
|
+
let sum = 0n;
|
|
213
|
+
let term = one / x;
|
|
214
|
+
const x2 = x * x;
|
|
215
|
+
for (let k = 1n; term !== 0n; k += 2n) {
|
|
216
|
+
sum += (k & 2n ? -term : term) / k;
|
|
217
|
+
term /= x2;
|
|
218
|
+
}
|
|
219
|
+
return sum;
|
|
220
|
+
};
|
|
221
|
+
const pi = 16n * atanInv(5n) - 4n * atanInv(239n);
|
|
222
|
+
// The fraction's first `words * 32` bits, below the leading 3.
|
|
223
|
+
const fraction = (pi - (3n << bits)) >> 64n;
|
|
224
|
+
const out = new Uint32Array(words);
|
|
225
|
+
for (let i = 0; i < words; i += 1)
|
|
226
|
+
out[i] = Number((fraction >> BigInt((words - 1 - i) * 32)) & 0xffffffffn);
|
|
227
|
+
PI_WORDS = out;
|
|
228
|
+
return out;
|
|
229
|
+
}
|
|
230
|
+
/** The 31 characters of a bcrypt hash after its salt. */
|
|
231
|
+
function bcrypt(password, cost, salt) {
|
|
232
|
+
const steps = bcryptSteps(password, cost, salt);
|
|
233
|
+
for (;;) {
|
|
234
|
+
const step = steps.next();
|
|
235
|
+
if (step.done)
|
|
236
|
+
return step.value;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
/** bcrypt, stopping every 32 rounds (a few milliseconds) so the caller can let others run. */
|
|
240
|
+
function* bcryptSteps(password, cost, salt) {
|
|
241
|
+
const init = piWords();
|
|
242
|
+
const P = Int32Array.from(init.subarray(0, 18));
|
|
243
|
+
const S = Int32Array.from(init.subarray(18));
|
|
244
|
+
// The key: the password's bytes with a NUL after them, at most 72 bytes.
|
|
245
|
+
const pw = utf8(password);
|
|
246
|
+
const key = new Uint8Array(Math.min(pw.length + 1, 72));
|
|
247
|
+
key.set(pw.subarray(0, key.length));
|
|
248
|
+
const lr = new Int32Array(2);
|
|
249
|
+
const encrypt = () => {
|
|
250
|
+
let l = lr[0];
|
|
251
|
+
let r = lr[1];
|
|
252
|
+
for (let i = 0; i < 16; i += 2) {
|
|
253
|
+
l ^= P[i];
|
|
254
|
+
r ^= (((S[l >>> 24] + S[256 | ((l >>> 16) & 255)]) ^ S[512 | ((l >>> 8) & 255)]) + S[768 | (l & 255)]) | 0;
|
|
255
|
+
r ^= P[i + 1];
|
|
256
|
+
l ^= (((S[r >>> 24] + S[256 | ((r >>> 16) & 255)]) ^ S[512 | ((r >>> 8) & 255)]) + S[768 | (r & 255)]) | 0;
|
|
257
|
+
}
|
|
258
|
+
lr[0] = r ^ P[17];
|
|
259
|
+
lr[1] = l ^ P[16];
|
|
260
|
+
};
|
|
261
|
+
const stream = (data, at) => {
|
|
262
|
+
let w = 0;
|
|
263
|
+
for (let k = 0; k < 4; k += 1) {
|
|
264
|
+
w = (w << 8) | data[at.i];
|
|
265
|
+
at.i = (at.i + 1) % data.length;
|
|
266
|
+
}
|
|
267
|
+
return w;
|
|
268
|
+
};
|
|
269
|
+
const expand = (data, salted) => {
|
|
270
|
+
const k = { i: 0 };
|
|
271
|
+
for (let i = 0; i < 18; i += 1)
|
|
272
|
+
P[i] = P[i] ^ stream(data, k);
|
|
273
|
+
const s = { i: 0 };
|
|
274
|
+
lr[0] = 0;
|
|
275
|
+
lr[1] = 0;
|
|
276
|
+
const next = () => {
|
|
277
|
+
if (salted) {
|
|
278
|
+
lr[0] = lr[0] ^ stream(salted, s);
|
|
279
|
+
lr[1] = lr[1] ^ stream(salted, s);
|
|
280
|
+
}
|
|
281
|
+
encrypt();
|
|
282
|
+
};
|
|
283
|
+
for (let i = 0; i < 18; i += 2) {
|
|
284
|
+
next();
|
|
285
|
+
P[i] = lr[0];
|
|
286
|
+
P[i + 1] = lr[1];
|
|
287
|
+
}
|
|
288
|
+
for (let i = 0; i < 1024; i += 2) {
|
|
289
|
+
next();
|
|
290
|
+
S[i] = lr[0];
|
|
291
|
+
S[i + 1] = lr[1];
|
|
292
|
+
}
|
|
293
|
+
};
|
|
294
|
+
expand(key, salt);
|
|
295
|
+
for (let n = 0, rounds = 2 ** cost; n < rounds; n += 1) {
|
|
296
|
+
expand(key, null);
|
|
297
|
+
expand(salt, null);
|
|
298
|
+
if ((n & 31) === 31)
|
|
299
|
+
yield;
|
|
300
|
+
}
|
|
301
|
+
const text = utf8("OrpheanBeholderScryDoubt");
|
|
302
|
+
const c = new Int32Array(6);
|
|
303
|
+
for (let i = 0; i < 6; i += 1)
|
|
304
|
+
c[i] = (text[i * 4] << 24) | (text[i * 4 + 1] << 16) | (text[i * 4 + 2] << 8) | text[i * 4 + 3];
|
|
305
|
+
for (let n = 0; n < 64; n += 1) {
|
|
306
|
+
for (let i = 0; i < 6; i += 2) {
|
|
307
|
+
lr[0] = c[i];
|
|
308
|
+
lr[1] = c[i + 1];
|
|
309
|
+
encrypt();
|
|
310
|
+
c[i] = lr[0];
|
|
311
|
+
c[i + 1] = lr[1];
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
const out = new Uint8Array(24);
|
|
315
|
+
for (let i = 0; i < 6; i += 1) {
|
|
316
|
+
out[i * 4] = c[i] >>> 24;
|
|
317
|
+
out[i * 4 + 1] = (c[i] >>> 16) & 255;
|
|
318
|
+
out[i * 4 + 2] = (c[i] >>> 8) & 255;
|
|
319
|
+
out[i * 4 + 3] = c[i] & 255;
|
|
320
|
+
}
|
|
321
|
+
return encodeBcrypt64(out.subarray(0, 23));
|
|
322
|
+
}
|
|
323
|
+
/** A bcrypt hash of `password`, for tests and tools (new hashes here are PBKDF2). */
|
|
324
|
+
export function bcryptHash(password, cost = 10, salt = crypto.getRandomValues(new Uint8Array(16))) {
|
|
325
|
+
return `$2b$${String(cost).padStart(2, "0")}$${encodeBcrypt64(salt).slice(0, 22)}${bcrypt(password, cost, salt)}`;
|
|
326
|
+
}
|
package/dist/pgwire.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A Postgres connection for the command line (`direct-auth migrate`), with nothing but Node: the wire protocol's
|
|
3
|
+
* simple queries, SCRAM-SHA-256 (or MD5) passwords, and TLS when the server offers it. Not for the server, which
|
|
4
|
+
* takes whatever driver the app already uses.
|
|
5
|
+
*
|
|
6
|
+
* `sslmode` in the URL as libpq reads it: `disable`, `prefer` (TLS when the server has it), `require` (TLS or
|
|
7
|
+
* nothing), `verify-ca` (TLS with the certificate's chain checked against `sslrootcert`), `verify-full` (the chain
|
|
8
|
+
* and the host's name). `sslrootcert` is a CA file, such as the one Supabase gives under Database Settings › SSL
|
|
9
|
+
* Configuration, or `system` for the system's CAs. With no sslmode given: `verify-full` when there is an
|
|
10
|
+
* `sslrootcert`, `require` for a database elsewhere (what moves is password hashes: no one on the way may turn TLS
|
|
11
|
+
* off), `prefer` on this machine.
|
|
12
|
+
*
|
|
13
|
+
* Who is at the other end is proven one of two ways, and nothing is sent or believed until it is:
|
|
14
|
+
*
|
|
15
|
+
* - the certificate, checked strongly: `verify-full`, or `verify-ca` against a CA file of its own (any certificate
|
|
16
|
+
* the system's CAs signed, for any name, would pass `verify-ca` with `system`, so that one doesn't count);
|
|
17
|
+
* - or SCRAM bound to the TLS channel (SCRAM-SHA-256-PLUS, tls-server-end-point): the server proves it knows the
|
|
18
|
+
* password for this very connection, so one in between can neither pose as the database nor pass the sign-in
|
|
19
|
+
* through to it. Without a strongly checked certificate this is required (`channel_binding=require`, the
|
|
20
|
+
* default then). Supabase's pooler doesn't offer it: give it the CA file (`sslrootcert=prod-ca-2021.crt`), or
|
|
21
|
+
* use the direct connection. `channel_binding=prefer` or `disable` in the URL goes without, and the connection
|
|
22
|
+
* says it is open to a server in between.
|
|
23
|
+
*
|
|
24
|
+
* A password sent as it is or as MD5, or none asked for, only goes to this machine's database with TLS not asked
|
|
25
|
+
* for, or to a strongly checked certificate. Until the server says the sign-in is done (AuthenticationOk, after
|
|
26
|
+
* one of those proofs), nothing else it sends is taken: a server that says it is ready without it is refused.
|
|
27
|
+
*/
|
|
28
|
+
export interface PgResult {
|
|
29
|
+
rows: Record<string, string | null>[];
|
|
30
|
+
/** Each statement's tag (`INSERT 0 3`), in order. */
|
|
31
|
+
commands: string[];
|
|
32
|
+
/** What `raise notice` said, in order. */
|
|
33
|
+
notices: string[];
|
|
34
|
+
}
|
|
35
|
+
export declare class PgError extends Error {
|
|
36
|
+
name: string;
|
|
37
|
+
code: string;
|
|
38
|
+
constructor(message: string, code: string);
|
|
39
|
+
}
|
|
40
|
+
export interface PgConnection {
|
|
41
|
+
query(text: string): Promise<PgResult>;
|
|
42
|
+
close(): Promise<void>;
|
|
43
|
+
/** How the connection is protected, for saying so. */
|
|
44
|
+
security: {
|
|
45
|
+
tls: boolean;
|
|
46
|
+
verified: boolean;
|
|
47
|
+
channelBound: boolean;
|
|
48
|
+
summary: string;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/** The most SCRAM iterations a server may ask for: past this it is a way to keep this tool busy, not a password. */
|
|
52
|
+
export declare const MAX_SCRAM_ITERATIONS = 1000000;
|
|
53
|
+
/** The parts of a database URL, and the URL with its password hidden (for saying which database). */
|
|
54
|
+
export declare function parseDatabaseUrl(url: string): {
|
|
55
|
+
host: string;
|
|
56
|
+
/** This machine's database, with TLS not asked for: the old ways of sending a password are allowed. */
|
|
57
|
+
local: boolean;
|
|
58
|
+
strong: boolean;
|
|
59
|
+
sslrootcert: string | undefined;
|
|
60
|
+
/** Required unless the other end is proven some other way (this machine, a strongly checked certificate). */
|
|
61
|
+
channelBinding: string;
|
|
62
|
+
port: number;
|
|
63
|
+
user: string;
|
|
64
|
+
password: string;
|
|
65
|
+
database: string;
|
|
66
|
+
sslmode: string;
|
|
67
|
+
shown: string;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* SCRAM-SHA-256's client side (RFC 5802, 7677): the final message and the server signature to expect. `gs2` is the
|
|
71
|
+
* header the first message began with (`n,,` none, `y,,` able but not offered, `p=tls-server-end-point,,` bound),
|
|
72
|
+
* and `binding` the channel's bytes when bound (RFC 5929).
|
|
73
|
+
*/
|
|
74
|
+
export declare function scramFinal(password: string, clientNonce: string, serverFirst: string, user?: string, gs2?: string, binding?: Uint8Array): {
|
|
75
|
+
message: string;
|
|
76
|
+
serverSignature: string;
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* The hash tls-server-end-point uses for a certificate (RFC 5929): its signature's, or SHA-256 for MD5 and SHA-1.
|
|
80
|
+
* Read from the DER: Certificate ::= SEQUENCE { tbsCertificate, signatureAlgorithm, signature }.
|
|
81
|
+
*/
|
|
82
|
+
export declare function endPointHash(der: Uint8Array): "sha256" | "sha384" | "sha512";
|
|
83
|
+
export declare function pgConnect(url: string, o?: {
|
|
84
|
+
timeoutMs?: number;
|
|
85
|
+
local?: boolean;
|
|
86
|
+
}): Promise<PgConnection>;
|