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/jwks.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider's published keys (Apple's, Google's), fetched once and kept as long as the provider's `Cache-Control`
|
|
3
|
+
* says (an hour when it says nothing, a day at most). A `kid` that isn't there makes one fetch, at most once a
|
|
4
|
+
* minute: providers rotate keys, and a token signed with a new one shouldn't wait out the cache. Single-flight, so a
|
|
5
|
+
* cold start with many sign-ins at once fetches once.
|
|
6
|
+
*/
|
|
7
|
+
import type { Algorithm, PublicJwk } from "./jwt.ts";
|
|
8
|
+
export interface JwksOptions {
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
/** Milliseconds per request (10 s). */
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
}
|
|
13
|
+
export interface Jwks {
|
|
14
|
+
key(kid: string | undefined, alg: Algorithm): Promise<PublicJwk | null | "unavailable">;
|
|
15
|
+
}
|
|
16
|
+
export declare function remoteJwks(url: string, o?: JwksOptions): Jwks;
|
|
17
|
+
/** Keys held in memory: this server's own, or a test's. */
|
|
18
|
+
export declare function staticJwks(keys: readonly PublicJwk[]): Jwks;
|
package/dist/jwks.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A provider's published keys (Apple's, Google's), fetched once and kept as long as the provider's `Cache-Control`
|
|
3
|
+
* says (an hour when it says nothing, a day at most). A `kid` that isn't there makes one fetch, at most once a
|
|
4
|
+
* minute: providers rotate keys, and a token signed with a new one shouldn't wait out the cache. Single-flight, so a
|
|
5
|
+
* cold start with many sign-ins at once fetches once.
|
|
6
|
+
*/
|
|
7
|
+
const HOUR = 3_600_000;
|
|
8
|
+
const DAY = 24 * HOUR;
|
|
9
|
+
const MINUTE = 60_000;
|
|
10
|
+
function maxAge(res) {
|
|
11
|
+
const m = /max-age=(\d+)/.exec(res.headers.get("cache-control") ?? "");
|
|
12
|
+
return m ? Math.min(Number(m[1]) * 1000, DAY) : HOUR;
|
|
13
|
+
}
|
|
14
|
+
const kty = (alg) => (alg === "ES256" ? "EC" : "RSA");
|
|
15
|
+
export function remoteJwks(url, o = {}) {
|
|
16
|
+
let keys = [];
|
|
17
|
+
let until = 0;
|
|
18
|
+
let lastFetch = 0;
|
|
19
|
+
let flight = null;
|
|
20
|
+
const refresh = () => (flight ??= (async () => {
|
|
21
|
+
lastFetch = Date.now();
|
|
22
|
+
try {
|
|
23
|
+
const res = await (o.fetch ?? fetch)(url, { signal: AbortSignal.timeout(o.timeoutMs ?? 10_000) });
|
|
24
|
+
if (!res.ok)
|
|
25
|
+
return false;
|
|
26
|
+
const body = (await res.json());
|
|
27
|
+
if (!Array.isArray(body.keys))
|
|
28
|
+
return false;
|
|
29
|
+
keys = body.keys.filter((k) => typeof k === "object" && k !== null);
|
|
30
|
+
until = Date.now() + maxAge(res);
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
finally {
|
|
37
|
+
flight = null;
|
|
38
|
+
}
|
|
39
|
+
})());
|
|
40
|
+
const find = (kid, alg) => keys.find((k) => k.kty === kty(alg) && (k.alg === undefined || k.alg === alg) && (kid === undefined || k.kid === kid)) ?? null;
|
|
41
|
+
return {
|
|
42
|
+
async key(kid, alg) {
|
|
43
|
+
if (Date.now() >= until) {
|
|
44
|
+
const ok = await refresh();
|
|
45
|
+
if (!ok && !keys.length)
|
|
46
|
+
return "unavailable";
|
|
47
|
+
}
|
|
48
|
+
const found = find(kid, alg);
|
|
49
|
+
if (found || Date.now() - lastFetch < MINUTE)
|
|
50
|
+
return found;
|
|
51
|
+
await refresh();
|
|
52
|
+
return find(kid, alg);
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/** Keys held in memory: this server's own, or a test's. */
|
|
57
|
+
export function staticJwks(keys) {
|
|
58
|
+
return {
|
|
59
|
+
async key(kid, alg) {
|
|
60
|
+
return keys.find((k) => k.kty === kty(alg) && (kid === undefined || k.kid === kid)) ?? null;
|
|
61
|
+
},
|
|
62
|
+
};
|
|
63
|
+
}
|
package/dist/jwt.d.ts
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Web Tokens with WebCrypto alone: signing the sessions' access tokens (ES256) and checking the tokens others
|
|
3
|
+
* sign (Apple's and Google's ID tokens, RS256; this server's own, ES256). Nothing here throws on a bad token: a check
|
|
4
|
+
* answers what is wrong with it, so a caller can log the reason without the token.
|
|
5
|
+
*/
|
|
6
|
+
import { type Bytes } from "./base64.ts";
|
|
7
|
+
export type Claims = Record<string, unknown> & {
|
|
8
|
+
iss?: string;
|
|
9
|
+
sub?: string;
|
|
10
|
+
aud?: string | string[];
|
|
11
|
+
exp?: number;
|
|
12
|
+
iat?: number;
|
|
13
|
+
nbf?: number;
|
|
14
|
+
nonce?: string;
|
|
15
|
+
};
|
|
16
|
+
export interface Header {
|
|
17
|
+
alg: string;
|
|
18
|
+
kid?: string;
|
|
19
|
+
typ?: string;
|
|
20
|
+
}
|
|
21
|
+
/** A public key as a JWKS lists it. */
|
|
22
|
+
export interface PublicJwk extends JsonWebKey {
|
|
23
|
+
kid?: string;
|
|
24
|
+
alg?: string;
|
|
25
|
+
use?: string;
|
|
26
|
+
}
|
|
27
|
+
declare const ALGORITHMS: {
|
|
28
|
+
readonly ES256: {
|
|
29
|
+
readonly import: {
|
|
30
|
+
readonly name: "ECDSA";
|
|
31
|
+
readonly namedCurve: "P-256";
|
|
32
|
+
};
|
|
33
|
+
readonly verify: {
|
|
34
|
+
readonly name: "ECDSA";
|
|
35
|
+
readonly hash: "SHA-256";
|
|
36
|
+
};
|
|
37
|
+
};
|
|
38
|
+
readonly RS256: {
|
|
39
|
+
readonly import: {
|
|
40
|
+
readonly name: "RSASSA-PKCS1-v1_5";
|
|
41
|
+
readonly hash: "SHA-256";
|
|
42
|
+
};
|
|
43
|
+
readonly verify: {
|
|
44
|
+
readonly name: "RSASSA-PKCS1-v1_5";
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export type Algorithm = keyof typeof ALGORITHMS;
|
|
49
|
+
export declare function signJwt(header: Header, claims: Claims, key: CryptoKey): Promise<string>;
|
|
50
|
+
/** The parts of a token, read but not checked; null when it isn't three base64url parts with JSON in the first two. */
|
|
51
|
+
export declare function decodeJwt(token: string): {
|
|
52
|
+
header: Header;
|
|
53
|
+
claims: Claims;
|
|
54
|
+
input: string;
|
|
55
|
+
signature: Bytes;
|
|
56
|
+
} | null;
|
|
57
|
+
export type JwtFailure = "malformed" | "algorithm" | "unknown_key" | "signature" | "issuer" | "audience" | "expired" | "not_yet_valid" | "nonce" | "keys_unavailable";
|
|
58
|
+
export type JwtResult = {
|
|
59
|
+
ok: true;
|
|
60
|
+
claims: Claims;
|
|
61
|
+
header: Header;
|
|
62
|
+
} | {
|
|
63
|
+
ok: false;
|
|
64
|
+
reason: JwtFailure;
|
|
65
|
+
};
|
|
66
|
+
export interface VerifyOptions {
|
|
67
|
+
/** The keys that may have signed it, by `kid`: a JWKS, or a function that finds one (and may fetch). */
|
|
68
|
+
key: (kid: string | undefined, alg: Algorithm) => Promise<PublicJwk | null | "unavailable">;
|
|
69
|
+
/** One issuer, or any of several (Google writes it two ways). */
|
|
70
|
+
issuer: string | readonly string[];
|
|
71
|
+
/** The token's `aud` must be one of these. */
|
|
72
|
+
audience: string | readonly string[];
|
|
73
|
+
algorithms?: readonly Algorithm[];
|
|
74
|
+
/**
|
|
75
|
+
* The raw nonce the client asked with. A token that carries a nonce is refused without it. The token's must be
|
|
76
|
+
* this, or with `nonceHashed` its SHA-256 hex and only that (Apple is given the hash, so the raw value never
|
|
77
|
+
* travels with the token, and a copied token's nonce is no use to whoever copied it).
|
|
78
|
+
*/
|
|
79
|
+
nonce?: string;
|
|
80
|
+
nonceHashed?: boolean;
|
|
81
|
+
/** False for tokens that carry no `exp` (Apple's notifications); `maxAge` then bounds their `iat`. */
|
|
82
|
+
requireExp?: boolean;
|
|
83
|
+
/** Seconds a token may be old, by its `iat`. */
|
|
84
|
+
maxAge?: number;
|
|
85
|
+
/** Seconds of clock difference allowed (60). */
|
|
86
|
+
leeway?: number;
|
|
87
|
+
now?: number;
|
|
88
|
+
}
|
|
89
|
+
export declare function verifyJwt(token: string, o: VerifyOptions): Promise<JwtResult>;
|
|
90
|
+
export {};
|
package/dist/jwt.js
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JSON Web Tokens with WebCrypto alone: signing the sessions' access tokens (ES256) and checking the tokens others
|
|
3
|
+
* sign (Apple's and Google's ID tokens, RS256; this server's own, ES256). Nothing here throws on a bad token: a check
|
|
4
|
+
* answers what is wrong with it, so a caller can log the reason without the token.
|
|
5
|
+
*/
|
|
6
|
+
import { b64url, fromBase64, fromUtf8, utf8 } from "./base64.js";
|
|
7
|
+
const ALGORITHMS = {
|
|
8
|
+
ES256: { import: { name: "ECDSA", namedCurve: "P-256" }, verify: { name: "ECDSA", hash: "SHA-256" } },
|
|
9
|
+
RS256: { import: { name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" }, verify: { name: "RSASSA-PKCS1-v1_5" } },
|
|
10
|
+
};
|
|
11
|
+
export async function signJwt(header, claims, key) {
|
|
12
|
+
const input = `${b64url(JSON.stringify(header))}.${b64url(JSON.stringify(claims))}`;
|
|
13
|
+
// WebCrypto's ECDSA signature is r and s side by side (IEEE P1363), which is what a JWT carries.
|
|
14
|
+
const signature = new Uint8Array(await crypto.subtle.sign({ name: "ECDSA", hash: "SHA-256" }, key, utf8(input)));
|
|
15
|
+
return `${input}.${b64url(signature)}`;
|
|
16
|
+
}
|
|
17
|
+
/** The parts of a token, read but not checked; null when it isn't three base64url parts with JSON in the first two. */
|
|
18
|
+
export function decodeJwt(token) {
|
|
19
|
+
const parts = token.split(".");
|
|
20
|
+
if (parts.length !== 3 || parts.some((p) => !/^[A-Za-z0-9_-]*$/.test(p)))
|
|
21
|
+
return null;
|
|
22
|
+
try {
|
|
23
|
+
const header = JSON.parse(fromUtf8(fromBase64(parts[0])));
|
|
24
|
+
const claims = JSON.parse(fromUtf8(fromBase64(parts[1])));
|
|
25
|
+
if (typeof header !== "object" || header === null || typeof claims !== "object" || claims === null)
|
|
26
|
+
return null;
|
|
27
|
+
return { header, claims, input: `${parts[0]}.${parts[1]}`, signature: fromBase64(parts[2]) };
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const imported = new WeakMap();
|
|
34
|
+
function importJwk(jwk, alg) {
|
|
35
|
+
let key = imported.get(jwk);
|
|
36
|
+
if (!key) {
|
|
37
|
+
const { kid: _kid, alg: _alg, use: _use, key_ops: _ops, ...material } = jwk;
|
|
38
|
+
key = crypto.subtle.importKey("jwk", material, ALGORITHMS[alg].import, false, ["verify"]);
|
|
39
|
+
imported.set(jwk, key);
|
|
40
|
+
}
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
const list = (v) => (typeof v === "string" ? [v] : v);
|
|
44
|
+
async function sha256Hex(text) {
|
|
45
|
+
const digest = new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(text)));
|
|
46
|
+
return Array.from(digest, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
47
|
+
}
|
|
48
|
+
export async function verifyJwt(token, o) {
|
|
49
|
+
const parsed = decodeJwt(token);
|
|
50
|
+
if (!parsed)
|
|
51
|
+
return { ok: false, reason: "malformed" };
|
|
52
|
+
const { header, claims, input, signature } = parsed;
|
|
53
|
+
const allowed = o.algorithms ?? ["ES256", "RS256"];
|
|
54
|
+
if (!allowed.includes(header.alg))
|
|
55
|
+
return { ok: false, reason: "algorithm" };
|
|
56
|
+
const alg = header.alg;
|
|
57
|
+
const jwk = await o.key(header.kid, alg);
|
|
58
|
+
if (jwk === "unavailable")
|
|
59
|
+
return { ok: false, reason: "keys_unavailable" };
|
|
60
|
+
if (!jwk)
|
|
61
|
+
return { ok: false, reason: "unknown_key" };
|
|
62
|
+
let valid = false;
|
|
63
|
+
try {
|
|
64
|
+
valid = await crypto.subtle.verify(ALGORITHMS[alg].verify, await importJwk(jwk, alg), signature, utf8(input));
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
valid = false;
|
|
68
|
+
}
|
|
69
|
+
if (!valid)
|
|
70
|
+
return { ok: false, reason: "signature" };
|
|
71
|
+
const now = Math.floor((o.now ?? Date.now()) / 1000);
|
|
72
|
+
const leeway = o.leeway ?? 60;
|
|
73
|
+
if (typeof claims.iss !== "string" || !list(o.issuer).includes(claims.iss))
|
|
74
|
+
return { ok: false, reason: "issuer" };
|
|
75
|
+
const aud = list(claims.aud ?? []);
|
|
76
|
+
if (!aud.some((a) => list(o.audience).includes(a)))
|
|
77
|
+
return { ok: false, reason: "audience" };
|
|
78
|
+
if (o.requireExp === false) {
|
|
79
|
+
if (claims.exp !== undefined && (typeof claims.exp !== "number" || claims.exp + leeway < now))
|
|
80
|
+
return { ok: false, reason: "expired" };
|
|
81
|
+
}
|
|
82
|
+
else if (typeof claims.exp !== "number" || claims.exp + leeway < now)
|
|
83
|
+
return { ok: false, reason: "expired" };
|
|
84
|
+
if (o.maxAge !== undefined && (typeof claims.iat !== "number" || claims.iat + o.maxAge + leeway < now))
|
|
85
|
+
return { ok: false, reason: "expired" };
|
|
86
|
+
if (typeof claims.nbf === "number" && claims.nbf - leeway > now)
|
|
87
|
+
return { ok: false, reason: "not_yet_valid" };
|
|
88
|
+
if (typeof claims.iat === "number" && claims.iat - leeway > now)
|
|
89
|
+
return { ok: false, reason: "not_yet_valid" };
|
|
90
|
+
if (o.nonce !== undefined || claims.nonce !== undefined) {
|
|
91
|
+
const n = claims.nonce;
|
|
92
|
+
if (typeof n !== "string" || o.nonce === undefined)
|
|
93
|
+
return { ok: false, reason: "nonce" };
|
|
94
|
+
const expected = o.nonceHashed ? await sha256Hex(o.nonce) : o.nonce;
|
|
95
|
+
if (n !== expected)
|
|
96
|
+
return { ok: false, reason: "nonce" };
|
|
97
|
+
}
|
|
98
|
+
return { ok: true, claims, header };
|
|
99
|
+
}
|
package/dist/keys.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server's own signing keys: ES256 (P-256) key pairs kept as private JWKs, each with its `kid` (its RFC 7638
|
|
3
|
+
* thumbprint). The first one signs; all of them are published, so a rotation is: add a new key at the front, wait an
|
|
4
|
+
* access token's lifetime, drop the old one.
|
|
5
|
+
*
|
|
6
|
+
* Kept in one secret, `DIRECT_AUTH_SIGNING_KEYS`: a JSON array of private JWKs (one line), or base64 of it.
|
|
7
|
+
*/
|
|
8
|
+
import type { PublicJwk } from "./jwt.ts";
|
|
9
|
+
export interface PrivateJwk extends JsonWebKey {
|
|
10
|
+
kid: string;
|
|
11
|
+
alg: "ES256";
|
|
12
|
+
}
|
|
13
|
+
export interface SigningKeys {
|
|
14
|
+
/** The key that signs, and its kid. */
|
|
15
|
+
signer: {
|
|
16
|
+
kid: string;
|
|
17
|
+
key: CryptoKey;
|
|
18
|
+
};
|
|
19
|
+
/** Every key's public half, for the JWKS. */
|
|
20
|
+
public: PublicJwk[];
|
|
21
|
+
/** The signing key's private scalar: what other secrets here (the codes' HMAC) are derived from. Never sent. */
|
|
22
|
+
secret: string;
|
|
23
|
+
}
|
|
24
|
+
export declare class KeyError extends Error {
|
|
25
|
+
name: string;
|
|
26
|
+
}
|
|
27
|
+
/** The RFC 7638 thumbprint of an EC public key, base64url: a kid that is the same wherever it is computed. */
|
|
28
|
+
export declare function thumbprint(jwk: JsonWebKey): Promise<string>;
|
|
29
|
+
/** A new key pair, as the private JWK to keep. */
|
|
30
|
+
export declare function generateSigningKey(): Promise<PrivateJwk>;
|
|
31
|
+
export declare function publicHalf(jwk: PrivateJwk): PublicJwk;
|
|
32
|
+
/** The keys from the secret's text (a JSON array or base64 of one); a `KeyError` that never quotes the keys. */
|
|
33
|
+
export declare function loadSigningKeys(text: string | readonly PrivateJwk[]): Promise<SigningKeys>;
|
package/dist/keys.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The server's own signing keys: ES256 (P-256) key pairs kept as private JWKs, each with its `kid` (its RFC 7638
|
|
3
|
+
* thumbprint). The first one signs; all of them are published, so a rotation is: add a new key at the front, wait an
|
|
4
|
+
* access token's lifetime, drop the old one.
|
|
5
|
+
*
|
|
6
|
+
* Kept in one secret, `DIRECT_AUTH_SIGNING_KEYS`: a JSON array of private JWKs (one line), or base64 of it.
|
|
7
|
+
*/
|
|
8
|
+
import { b64url, fromBase64, fromUtf8, utf8 } from "./base64.js";
|
|
9
|
+
export class KeyError extends Error {
|
|
10
|
+
name = "KeyError";
|
|
11
|
+
}
|
|
12
|
+
const EC = { name: "ECDSA", namedCurve: "P-256" };
|
|
13
|
+
/** The RFC 7638 thumbprint of an EC public key, base64url: a kid that is the same wherever it is computed. */
|
|
14
|
+
export async function thumbprint(jwk) {
|
|
15
|
+
const canonical = JSON.stringify({ crv: jwk.crv, kty: jwk.kty, x: jwk.x, y: jwk.y });
|
|
16
|
+
return b64url(new Uint8Array(await crypto.subtle.digest("SHA-256", utf8(canonical))));
|
|
17
|
+
}
|
|
18
|
+
/** A new key pair, as the private JWK to keep. */
|
|
19
|
+
export async function generateSigningKey() {
|
|
20
|
+
const pair = (await crypto.subtle.generateKey(EC, true, ["sign", "verify"]));
|
|
21
|
+
const jwk = await crypto.subtle.exportKey("jwk", pair.privateKey);
|
|
22
|
+
return { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y, d: jwk.d, kid: await thumbprint(jwk), alg: "ES256" };
|
|
23
|
+
}
|
|
24
|
+
export function publicHalf(jwk) {
|
|
25
|
+
return { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y, kid: jwk.kid, alg: "ES256", use: "sig" };
|
|
26
|
+
}
|
|
27
|
+
/** The keys from the secret's text (a JSON array or base64 of one); a `KeyError` that never quotes the keys. */
|
|
28
|
+
export async function loadSigningKeys(text) {
|
|
29
|
+
let keys = text;
|
|
30
|
+
if (typeof text === "string") {
|
|
31
|
+
const trimmed = text.trim();
|
|
32
|
+
try {
|
|
33
|
+
keys = JSON.parse(trimmed.startsWith("[") ? trimmed : fromUtf8(fromBase64(trimmed)));
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
throw new KeyError("DIRECT_AUTH_SIGNING_KEYS is not a JSON array of keys (nor base64 of one)");
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (!Array.isArray(keys) || !keys.length)
|
|
40
|
+
throw new KeyError("DIRECT_AUTH_SIGNING_KEYS has no keys");
|
|
41
|
+
const valid = keys.every((k) => typeof k === "object" && k && k.kty === "EC" && k.crv === "P-256" && typeof k.d === "string" && typeof k.kid === "string");
|
|
42
|
+
if (!valid)
|
|
43
|
+
throw new KeyError("every signing key must be a private P-256 JWK with a kid (npx direct-auth keys makes one)");
|
|
44
|
+
const list = keys;
|
|
45
|
+
let signer;
|
|
46
|
+
try {
|
|
47
|
+
const { kid: _kid, alg: _alg, ...material } = list[0];
|
|
48
|
+
signer = await crypto.subtle.importKey("jwk", material, EC, false, ["sign"]);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
throw new KeyError("the first signing key does not load");
|
|
52
|
+
}
|
|
53
|
+
return { signer: { kid: list[0].kid, key: signer }, public: list.map(publicHalf), secret: list[0].d };
|
|
54
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a code says, in English, Spanish, Portuguese and German: the email's subject and text, and the SMS. A
|
|
3
|
+
* project can give its own `message` function instead.
|
|
4
|
+
*
|
|
5
|
+
* The SMS ends with the line browsers and phones read to fill the code in by themselves (`@example.com #123456`, the
|
|
6
|
+
* WebOTP format; iOS reads the code anywhere in the text) when the project says which domain it is.
|
|
7
|
+
*/
|
|
8
|
+
export type Locale = "en" | "es" | "pt" | "de";
|
|
9
|
+
export interface CodeMessage {
|
|
10
|
+
channel: "email" | "sms";
|
|
11
|
+
code: string;
|
|
12
|
+
/** The project's name, as people know it. */
|
|
13
|
+
app: string;
|
|
14
|
+
locale?: string;
|
|
15
|
+
/** Minutes the code lasts. */
|
|
16
|
+
minutes: number;
|
|
17
|
+
/** The site's domain, for the SMS's autofill line. */
|
|
18
|
+
domain?: string;
|
|
19
|
+
}
|
|
20
|
+
export interface Rendered {
|
|
21
|
+
subject: string;
|
|
22
|
+
text: string;
|
|
23
|
+
html: string;
|
|
24
|
+
}
|
|
25
|
+
export declare function localeOf(locale: string | undefined): Locale;
|
|
26
|
+
export declare function renderCode(m: CodeMessage): Rendered;
|
package/dist/messages.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a code says, in English, Spanish, Portuguese and German: the email's subject and text, and the SMS. A
|
|
3
|
+
* project can give its own `message` function instead.
|
|
4
|
+
*
|
|
5
|
+
* The SMS ends with the line browsers and phones read to fill the code in by themselves (`@example.com #123456`, the
|
|
6
|
+
* WebOTP format; iOS reads the code anywhere in the text) when the project says which domain it is.
|
|
7
|
+
*/
|
|
8
|
+
const COPY = {
|
|
9
|
+
en: {
|
|
10
|
+
subject: (app) => `Your ${app} code`,
|
|
11
|
+
line: (app) => `Your code to sign in to ${app}:`,
|
|
12
|
+
lasts: (m) => `It works for ${m} minutes.`,
|
|
13
|
+
ignore: "If you didn't ask for it, ignore this email: nobody can sign in without the code.",
|
|
14
|
+
sms: (code, app) => `${code} is your ${app} code.`,
|
|
15
|
+
},
|
|
16
|
+
es: {
|
|
17
|
+
subject: (app) => `Tu código de ${app}`,
|
|
18
|
+
line: (app) => `Tu código para entrar a ${app}:`,
|
|
19
|
+
lasts: (m) => `Sirve durante ${m} minutos.`,
|
|
20
|
+
ignore: "Si no lo pediste, ignorá este correo: nadie puede entrar sin el código.",
|
|
21
|
+
sms: (code, app) => `${code} es tu código de ${app}.`,
|
|
22
|
+
},
|
|
23
|
+
pt: {
|
|
24
|
+
subject: (app) => `Seu código do ${app}`,
|
|
25
|
+
line: (app) => `Seu código para entrar no ${app}:`,
|
|
26
|
+
lasts: (m) => `Ele vale por ${m} minutos.`,
|
|
27
|
+
ignore: "Se você não pediu, ignore este e-mail: ninguém entra sem o código.",
|
|
28
|
+
sms: (code, app) => `${code} é seu código do ${app}.`,
|
|
29
|
+
},
|
|
30
|
+
de: {
|
|
31
|
+
subject: (app) => `Dein Code für ${app}`,
|
|
32
|
+
line: (app) => `Dein Code für die Anmeldung bei ${app}:`,
|
|
33
|
+
lasts: (m) => `Er gilt ${m} Minuten.`,
|
|
34
|
+
ignore: "Wenn du ihn nicht angefordert hast, ignoriere diese E-Mail: Ohne den Code kann sich niemand anmelden.",
|
|
35
|
+
sms: (code, app) => `${code} ist dein Code für ${app}.`,
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
export function localeOf(locale) {
|
|
39
|
+
const l = (locale ?? "en").slice(0, 2).toLowerCase();
|
|
40
|
+
return l === "es" || l === "pt" || l === "de" ? l : "en";
|
|
41
|
+
}
|
|
42
|
+
const escape = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
43
|
+
export function renderCode(m) {
|
|
44
|
+
const c = COPY[localeOf(m.locale)];
|
|
45
|
+
if (m.channel === "sms") {
|
|
46
|
+
const text = m.domain ? `${c.sms(m.code, m.app)}\n\n@${m.domain} #${m.code}` : c.sms(m.code, m.app);
|
|
47
|
+
return { subject: "", text, html: "" };
|
|
48
|
+
}
|
|
49
|
+
const text = `${c.line(m.app)}\n\n${m.code}\n\n${c.lasts(m.minutes)}\n${c.ignore}\n`;
|
|
50
|
+
const html = `<!doctype html><html><body style="margin:0;padding:32px 16px;background:#f6f6f7;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,Helvetica,Arial,sans-serif;color:#111317">
|
|
51
|
+
<div style="max-width:440px;margin:0 auto;background:#fff;border-radius:16px;padding:32px">
|
|
52
|
+
<p style="margin:0 0 16px;font-size:16px;line-height:1.5">${escape(c.line(m.app))}</p>
|
|
53
|
+
<p style="margin:0 0 16px;font-size:34px;font-weight:600;letter-spacing:6px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace">${escape(m.code)}</p>
|
|
54
|
+
<p style="margin:0;font-size:14px;line-height:1.5;color:#5b5f66">${escape(c.lasts(m.minutes))} ${escape(c.ignore)}</p>
|
|
55
|
+
</div></body></html>`;
|
|
56
|
+
return { subject: c.subject(m.app), text, html };
|
|
57
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `direct-auth migrate`: the four steps on the command line, for a person. Connect (the database URL asked with
|
|
3
|
+
* its password hidden), a dry run (what moves, what needs a hand), the move after a typed yes (the way back written
|
|
4
|
+
* first), and a check that it all arrived; then the next steps for the source. Only counts and masked ids are ever
|
|
5
|
+
* shown: no address, no hash, no URL with its password.
|
|
6
|
+
*/
|
|
7
|
+
import { type Check, type MigrationSource, type Query, type Report } from "./migrate.ts";
|
|
8
|
+
import type { InitIO } from "./init.ts";
|
|
9
|
+
export interface MigrateOptions {
|
|
10
|
+
/** The source; asked from a list when not given. */
|
|
11
|
+
from?: string;
|
|
12
|
+
/** A file source's export (Firebase's JSON, Clerk's CSV, Auth0's NDJSON, gzipped or not); asked when not given. */
|
|
13
|
+
file?: string;
|
|
14
|
+
/** Auth0: the password hashes file from its support, if there is one. */
|
|
15
|
+
hashes?: string;
|
|
16
|
+
/** Firebase: a file with the project's password hash parameters, as the console shows them. */
|
|
17
|
+
hashConfig?: string;
|
|
18
|
+
/** The database URL; asked (hidden) when not given. */
|
|
19
|
+
db?: string;
|
|
20
|
+
prefix: string;
|
|
21
|
+
dryRun: boolean;
|
|
22
|
+
/** No typed confirmation: for a script that read the dry run already. */
|
|
23
|
+
yes: boolean;
|
|
24
|
+
/** Where the way back is written (`direct-auth-rollback-<time>.sql`). */
|
|
25
|
+
rollbackFile?: string;
|
|
26
|
+
/** The sign-in's issuer, for the next steps. */
|
|
27
|
+
issuer?: string;
|
|
28
|
+
/** Move users who have a second factor anyway (direct-auth doesn't check one). */
|
|
29
|
+
withoutMfa?: boolean;
|
|
30
|
+
/** Move the passwords of addresses confirmed the moment they signed up, which nobody proved. */
|
|
31
|
+
keepUnprovenPasswords?: boolean;
|
|
32
|
+
/** Opens the database: the query function, what to say it is (the URL, password hidden), and how to close it. */
|
|
33
|
+
connect(url: string): Promise<{
|
|
34
|
+
query: Query;
|
|
35
|
+
shown: string;
|
|
36
|
+
close(): Promise<void>;
|
|
37
|
+
security?: string;
|
|
38
|
+
}>;
|
|
39
|
+
now?: () => Date;
|
|
40
|
+
/** Reads a file (a test gives its own). */
|
|
41
|
+
readFile?: (path: string) => string;
|
|
42
|
+
}
|
|
43
|
+
/** A file as text, gunzipped first when it is gzip (Auth0's export downloads as `.json.gz`). */
|
|
44
|
+
export declare function readExport(path: string): string;
|
|
45
|
+
/** What each source is, as the list shows it, and where its users come from. */
|
|
46
|
+
export declare const FROM: Record<string, {
|
|
47
|
+
what: string;
|
|
48
|
+
where: string;
|
|
49
|
+
db: string;
|
|
50
|
+
}>;
|
|
51
|
+
export declare function migrate(io: InitIO, o: MigrateOptions): Promise<number>;
|
|
52
|
+
type Line = {
|
|
53
|
+
text: string;
|
|
54
|
+
tone?: "ok" | "note" | "bad" | "dim" | "code";
|
|
55
|
+
};
|
|
56
|
+
/** Progress across the steps: done ones filled. */
|
|
57
|
+
export declare function bar(at: number, of: number): string;
|
|
58
|
+
/** The dry run as aligned lines: the counts, then what needs a hand (each by name, with what to do), then notes. */
|
|
59
|
+
export declare function reportLines(r: Report): Line[];
|
|
60
|
+
export declare function checkLines(checks: readonly Check[]): Line[];
|
|
61
|
+
/**
|
|
62
|
+
* What comes after the move, for Supabase: trusting the issuer (the Management API, as the dashboard has no
|
|
63
|
+
* generic OIDC field), the server's env, and each client swapped, with the snippet.
|
|
64
|
+
*/
|
|
65
|
+
export declare function nextSteps(source: MigrationSource, r: Report, o: {
|
|
66
|
+
issuer?: string;
|
|
67
|
+
ref?: string;
|
|
68
|
+
prefix: string;
|
|
69
|
+
}): Line[];
|
|
70
|
+
/**
|
|
71
|
+
* `direct-auth supabase-trust`: the project takes direct-auth's tokens, as a Third-Party Auth issuer, through
|
|
72
|
+
* Supabase's Management API (the dashboard lists only Firebase, Auth0, Cognito and Clerk). What is there is read
|
|
73
|
+
* first; the change is shown, and made after a yes. The access token is never printed.
|
|
74
|
+
*/
|
|
75
|
+
export declare function trustIssuer(io: InitIO, o: {
|
|
76
|
+
project: string;
|
|
77
|
+
issuer: string;
|
|
78
|
+
token?: string;
|
|
79
|
+
yes: boolean;
|
|
80
|
+
f?: typeof fetch;
|
|
81
|
+
}): Promise<number>;
|
|
82
|
+
export {};
|