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,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign in with Apple: the identity token an app gets from `ASAuthorizationAppleIDCredential` (or a website from
|
|
3
|
+
* Sign in with Apple JS), checked against Apple's published keys. Its audience is the client that asked: an app's
|
|
4
|
+
* bundle ID, a website's Services ID.
|
|
5
|
+
*
|
|
6
|
+
* The app gives Apple the SHA-256 hex of a random nonce and sends the raw one here: only the hash is in the token.
|
|
7
|
+
* (A website with Sign in with Apple JS hashes it the same way before handing it to `AppleID.auth.init`.)
|
|
8
|
+
*
|
|
9
|
+
* Apple gives the person's name only to the app, and only the first time: the app sends it along, and it is taken
|
|
10
|
+
* as given (it is a display name, nothing more).
|
|
11
|
+
*/
|
|
12
|
+
import { type Jwks, type JwksOptions } from "../jwks.ts";
|
|
13
|
+
import { type IdentityResult } from "./identity.ts";
|
|
14
|
+
export declare const APPLE_ISSUER = "https://appleid.apple.com";
|
|
15
|
+
export declare const APPLE_JWKS = "https://appleid.apple.com/auth/keys";
|
|
16
|
+
export interface AppleOptions extends JwksOptions {
|
|
17
|
+
/** The bundle IDs and Services IDs whose tokens are accepted. */
|
|
18
|
+
clientIds: readonly string[];
|
|
19
|
+
jwks?: Jwks;
|
|
20
|
+
}
|
|
21
|
+
export interface AppleVerifier {
|
|
22
|
+
verify(idToken: string, o?: {
|
|
23
|
+
nonce?: string;
|
|
24
|
+
name?: string;
|
|
25
|
+
now?: number;
|
|
26
|
+
}): Promise<IdentityResult>;
|
|
27
|
+
}
|
|
28
|
+
export declare function apple(options: AppleOptions): AppleVerifier;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sign in with Apple: the identity token an app gets from `ASAuthorizationAppleIDCredential` (or a website from
|
|
3
|
+
* Sign in with Apple JS), checked against Apple's published keys. Its audience is the client that asked: an app's
|
|
4
|
+
* bundle ID, a website's Services ID.
|
|
5
|
+
*
|
|
6
|
+
* The app gives Apple the SHA-256 hex of a random nonce and sends the raw one here: only the hash is in the token.
|
|
7
|
+
* (A website with Sign in with Apple JS hashes it the same way before handing it to `AppleID.auth.init`.)
|
|
8
|
+
*
|
|
9
|
+
* Apple gives the person's name only to the app, and only the first time: the app sends it along, and it is taken
|
|
10
|
+
* as given (it is a display name, nothing more).
|
|
11
|
+
*/
|
|
12
|
+
import { remoteJwks } from "../jwks.js";
|
|
13
|
+
import { verifyJwt } from "../jwt.js";
|
|
14
|
+
import { text, truthy } from "./identity.js";
|
|
15
|
+
export const APPLE_ISSUER = "https://appleid.apple.com";
|
|
16
|
+
export const APPLE_JWKS = "https://appleid.apple.com/auth/keys";
|
|
17
|
+
export function apple(options) {
|
|
18
|
+
if (!options.clientIds.length)
|
|
19
|
+
throw new Error("apple: clientIds needs the app's bundle ID or the website's Services ID");
|
|
20
|
+
const jwks = options.jwks ?? remoteJwks(APPLE_JWKS, options);
|
|
21
|
+
return {
|
|
22
|
+
async verify(idToken, o = {}) {
|
|
23
|
+
const r = await verifyJwt(idToken, {
|
|
24
|
+
key: (kid, alg) => jwks.key(kid, alg),
|
|
25
|
+
issuer: APPLE_ISSUER,
|
|
26
|
+
audience: options.clientIds,
|
|
27
|
+
algorithms: ["RS256"],
|
|
28
|
+
nonce: o.nonce,
|
|
29
|
+
nonceHashed: true,
|
|
30
|
+
now: o.now,
|
|
31
|
+
});
|
|
32
|
+
if (!r.ok)
|
|
33
|
+
return { ok: false, reason: `apple_${r.reason}` };
|
|
34
|
+
const sub = text(r.claims.sub);
|
|
35
|
+
if (!sub)
|
|
36
|
+
return { ok: false, reason: "apple_malformed" };
|
|
37
|
+
const email = text(r.claims.email)?.toLowerCase();
|
|
38
|
+
return {
|
|
39
|
+
ok: true,
|
|
40
|
+
identity: {
|
|
41
|
+
provider: "apple",
|
|
42
|
+
subject: sub,
|
|
43
|
+
email,
|
|
44
|
+
emailVerified: !!email && truthy(r.claims.email_verified),
|
|
45
|
+
name: text(o.name),
|
|
46
|
+
clientId: typeof r.claims.aud === "string" ? r.claims.aud : undefined,
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub: not OpenID Connect, so the server finishes the OAuth flow itself. The app sends the code GitHub gave its
|
|
3
|
+
* redirect, the redirect URI and the PKCE verifier; the server trades them for an access token (with the client
|
|
4
|
+
* secret), reads who it is (`/user`) and the addresses GitHub checked (`/user/emails`), and keeps nothing of
|
|
5
|
+
* GitHub's: the token is used for those two reads and dropped. The subject is GitHub's numeric id, which never
|
|
6
|
+
* changes (a login name can).
|
|
7
|
+
*/
|
|
8
|
+
import { type IdentityResult } from "./identity.ts";
|
|
9
|
+
export interface GitHubOptions {
|
|
10
|
+
/** The OAuth app's client ID (GitHub › Settings › Developer settings › OAuth Apps). */
|
|
11
|
+
clientId: string;
|
|
12
|
+
clientSecret: string;
|
|
13
|
+
fetch?: typeof fetch;
|
|
14
|
+
/** Where GitHub lives (`https://github.com`, `https://api.github.com`): a test's own. */
|
|
15
|
+
web?: string;
|
|
16
|
+
api?: string;
|
|
17
|
+
/** How long each of GitHub's answers may take: 10 s, then `github_unavailable`. */
|
|
18
|
+
timeoutMs?: number;
|
|
19
|
+
}
|
|
20
|
+
export interface GitHubExchange {
|
|
21
|
+
code: string;
|
|
22
|
+
redirectUri?: string;
|
|
23
|
+
codeVerifier?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface GitHubVerifier {
|
|
26
|
+
exchange(a: GitHubExchange): Promise<IdentityResult>;
|
|
27
|
+
}
|
|
28
|
+
export declare function github(o: GitHubOptions): GitHubVerifier;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub: not OpenID Connect, so the server finishes the OAuth flow itself. The app sends the code GitHub gave its
|
|
3
|
+
* redirect, the redirect URI and the PKCE verifier; the server trades them for an access token (with the client
|
|
4
|
+
* secret), reads who it is (`/user`) and the addresses GitHub checked (`/user/emails`), and keeps nothing of
|
|
5
|
+
* GitHub's: the token is used for those two reads and dropped. The subject is GitHub's numeric id, which never
|
|
6
|
+
* changes (a login name can).
|
|
7
|
+
*/
|
|
8
|
+
import { text } from "./identity.js";
|
|
9
|
+
export function github(o) {
|
|
10
|
+
if (!o.clientId || !o.clientSecret)
|
|
11
|
+
throw new Error("github: clientId and clientSecret are both needed");
|
|
12
|
+
const web = (o.web ?? "https://github.com").replace(/\/+$/, "");
|
|
13
|
+
const api = (o.api ?? "https://api.github.com").replace(/\/+$/, "");
|
|
14
|
+
// Each call waits a while at most: a GitHub that never answers can't hold the sign-in, or the server, forever.
|
|
15
|
+
const f = (url, init) => (o.fetch ?? globalThis.fetch)(url, { ...init, signal: AbortSignal.timeout(o.timeoutMs ?? 10_000) });
|
|
16
|
+
const headers = (token) => ({
|
|
17
|
+
authorization: `Bearer ${token}`,
|
|
18
|
+
accept: "application/vnd.github+json",
|
|
19
|
+
"x-github-api-version": "2022-11-28",
|
|
20
|
+
"user-agent": "direct-auth",
|
|
21
|
+
});
|
|
22
|
+
return {
|
|
23
|
+
async exchange(a) {
|
|
24
|
+
let token;
|
|
25
|
+
try {
|
|
26
|
+
const r = await f(`${web}/login/oauth/access_token`, {
|
|
27
|
+
method: "POST",
|
|
28
|
+
headers: { accept: "application/json", "content-type": "application/json", "user-agent": "direct-auth" },
|
|
29
|
+
body: JSON.stringify({ client_id: o.clientId, client_secret: o.clientSecret, code: a.code, redirect_uri: a.redirectUri, code_verifier: a.codeVerifier }),
|
|
30
|
+
});
|
|
31
|
+
if (r.status >= 500)
|
|
32
|
+
return { ok: false, reason: "github_unavailable" };
|
|
33
|
+
const j = (await r.json().catch(() => ({})));
|
|
34
|
+
token = text(j.access_token);
|
|
35
|
+
// GitHub answers 200 with { error: "bad_verification_code" } and the like.
|
|
36
|
+
if (!token)
|
|
37
|
+
return { ok: false, reason: "github_invalid_code" };
|
|
38
|
+
const [u, e] = await Promise.all([f(`${api}/user`, { headers: headers(token) }), f(`${api}/user/emails`, { headers: headers(token) })]);
|
|
39
|
+
if (u.status >= 500 || e.status >= 500)
|
|
40
|
+
return { ok: false, reason: "github_unavailable" };
|
|
41
|
+
if (!u.ok)
|
|
42
|
+
return { ok: false, reason: "github_invalid_code" };
|
|
43
|
+
const user = (await u.json());
|
|
44
|
+
if (typeof user.id !== "number" && typeof user.id !== "string")
|
|
45
|
+
return { ok: false, reason: "github_malformed" };
|
|
46
|
+
// The primary address, only when GitHub checked it (the user:email scope lists them; without it, none).
|
|
47
|
+
const emails = e.ok ? (await e.json().catch(() => [])) : [];
|
|
48
|
+
const primary = Array.isArray(emails) ? emails.find((m) => m.primary === true && m.verified === true) : undefined;
|
|
49
|
+
const email = text(primary?.email)?.toLowerCase();
|
|
50
|
+
return {
|
|
51
|
+
ok: true,
|
|
52
|
+
identity: {
|
|
53
|
+
provider: "github",
|
|
54
|
+
subject: String(user.id),
|
|
55
|
+
email,
|
|
56
|
+
emailVerified: !!email,
|
|
57
|
+
name: text(user.name) ?? text(user.login),
|
|
58
|
+
picture: text(user.avatar_url),
|
|
59
|
+
},
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return { ok: false, reason: "github_unavailable" };
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google: the ID token from Credential Manager (Android), Google Sign-In (iOS) or Google Identity Services (the web),
|
|
3
|
+
* checked against Google's published keys. Its audience is the OAuth client it was asked for: on Android that is the
|
|
4
|
+
* web client ID given as `serverClientId`, on iOS the iOS client ID, on the web the web client ID.
|
|
5
|
+
*/
|
|
6
|
+
import { type Jwks, type JwksOptions } from "../jwks.ts";
|
|
7
|
+
import { type IdentityResult } from "./identity.ts";
|
|
8
|
+
export declare const GOOGLE_ISSUERS: readonly ["https://accounts.google.com", "accounts.google.com"];
|
|
9
|
+
export declare const GOOGLE_JWKS = "https://www.googleapis.com/oauth2/v3/certs";
|
|
10
|
+
export interface GoogleOptions extends JwksOptions {
|
|
11
|
+
/** The OAuth client IDs whose tokens are accepted. */
|
|
12
|
+
clientIds: readonly string[];
|
|
13
|
+
/** Only accounts of these Google Workspace domains (the token's `hd`), when given. */
|
|
14
|
+
domains?: readonly string[];
|
|
15
|
+
jwks?: Jwks;
|
|
16
|
+
}
|
|
17
|
+
export interface GoogleVerifier {
|
|
18
|
+
verify(idToken: string, o?: {
|
|
19
|
+
nonce?: string;
|
|
20
|
+
now?: number;
|
|
21
|
+
}): Promise<IdentityResult>;
|
|
22
|
+
}
|
|
23
|
+
export declare function google(options: GoogleOptions): GoogleVerifier;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Google: the ID token from Credential Manager (Android), Google Sign-In (iOS) or Google Identity Services (the web),
|
|
3
|
+
* checked against Google's published keys. Its audience is the OAuth client it was asked for: on Android that is the
|
|
4
|
+
* web client ID given as `serverClientId`, on iOS the iOS client ID, on the web the web client ID.
|
|
5
|
+
*/
|
|
6
|
+
import { remoteJwks } from "../jwks.js";
|
|
7
|
+
import { verifyJwt } from "../jwt.js";
|
|
8
|
+
import { text, truthy } from "./identity.js";
|
|
9
|
+
export const GOOGLE_ISSUERS = ["https://accounts.google.com", "accounts.google.com"];
|
|
10
|
+
export const GOOGLE_JWKS = "https://www.googleapis.com/oauth2/v3/certs";
|
|
11
|
+
export function google(options) {
|
|
12
|
+
if (!options.clientIds.length)
|
|
13
|
+
throw new Error("google: clientIds needs at least one OAuth client ID");
|
|
14
|
+
const jwks = options.jwks ?? remoteJwks(GOOGLE_JWKS, options);
|
|
15
|
+
return {
|
|
16
|
+
async verify(idToken, o = {}) {
|
|
17
|
+
const r = await verifyJwt(idToken, {
|
|
18
|
+
key: (kid, alg) => jwks.key(kid, alg),
|
|
19
|
+
issuer: GOOGLE_ISSUERS,
|
|
20
|
+
audience: options.clientIds,
|
|
21
|
+
algorithms: ["RS256"],
|
|
22
|
+
nonce: o.nonce,
|
|
23
|
+
now: o.now,
|
|
24
|
+
});
|
|
25
|
+
if (!r.ok)
|
|
26
|
+
return { ok: false, reason: `google_${r.reason}` };
|
|
27
|
+
const sub = text(r.claims.sub);
|
|
28
|
+
if (!sub)
|
|
29
|
+
return { ok: false, reason: "google_malformed" };
|
|
30
|
+
if (options.domains?.length && !options.domains.includes(String(r.claims.hd ?? ""))) {
|
|
31
|
+
return { ok: false, reason: "google_domain" };
|
|
32
|
+
}
|
|
33
|
+
const email = text(r.claims.email)?.toLowerCase();
|
|
34
|
+
return {
|
|
35
|
+
ok: true,
|
|
36
|
+
identity: {
|
|
37
|
+
provider: "google",
|
|
38
|
+
subject: sub,
|
|
39
|
+
email,
|
|
40
|
+
emailVerified: !!email && truthy(r.claims.email_verified),
|
|
41
|
+
name: text(r.claims.name),
|
|
42
|
+
picture: text(r.claims.picture),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/** Who a provider says someone is: the provider's own id for them, and what it vouches for. */
|
|
2
|
+
export interface Identity {
|
|
3
|
+
provider: "apple" | "google" | "github" | "email" | "phone" | "passkey";
|
|
4
|
+
/** The provider's stable id for the person (Apple's and Google's `sub`, GitHub's numeric id, the address, the number). */
|
|
5
|
+
subject: string;
|
|
6
|
+
email?: string;
|
|
7
|
+
/** True only when the provider checked the address itself. */
|
|
8
|
+
emailVerified: boolean;
|
|
9
|
+
phone?: string;
|
|
10
|
+
name?: string;
|
|
11
|
+
picture?: string;
|
|
12
|
+
/** The client the provider's token was for (its `aud`): an app's bundle ID, a Services ID, an OAuth client. */
|
|
13
|
+
clientId?: string;
|
|
14
|
+
}
|
|
15
|
+
export type IdentityResult = {
|
|
16
|
+
ok: true;
|
|
17
|
+
identity: Identity;
|
|
18
|
+
} | {
|
|
19
|
+
ok: false;
|
|
20
|
+
reason: string;
|
|
21
|
+
};
|
|
22
|
+
/** Apple and Google write a claim as a boolean or as the text "true". */
|
|
23
|
+
export declare const truthy: (v: unknown) => v is true | "true";
|
|
24
|
+
export declare const text: (v: unknown) => string | undefined;
|
package/dist/proxy.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sign-in on an address of your own while the server runs on Supabase: a small Cloudflare Worker on that
|
|
3
|
+
* address (`auth.example.com`) that passes `/auth/…` to the Edge Function. The issuer is then
|
|
4
|
+
* `https://auth.example.com/auth`, in every token and every app, and moving the server later (a VPS, a container) is
|
|
5
|
+
* pointing that address somewhere else: nobody signs in again.
|
|
6
|
+
*
|
|
7
|
+
* The Worker tells the server who is asking (`x-direct-auth-ip`, for the rate limits), with a secret only the two
|
|
8
|
+
* share (`DIRECT_AUTH_PROXY_SECRET`): a request that reaches the function another way can't claim an IP.
|
|
9
|
+
*/
|
|
10
|
+
export interface ProxyOptions {
|
|
11
|
+
/** The address, `auth.example.com`. */
|
|
12
|
+
domain: string;
|
|
13
|
+
/** Where the requests go: `https://<ref>.supabase.co/functions/v1/auth`. */
|
|
14
|
+
target: string;
|
|
15
|
+
/** The Worker's name (`direct-auth-proxy`). */
|
|
16
|
+
name?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare const PROXY_WORKER = "// direct-auth's proxy: `https://<your domain>/auth/...` to the server (made by npx direct-auth init).\nexport default {\n async fetch(request, env) {\n const url = new URL(request.url);\n if (url.pathname !== \"/auth\" && !url.pathname.startsWith(\"/auth/\")) return new Response(\"Not found\", { status: 404 });\n const target = new URL(env.TARGET.replace(/\\/+$/, \"\") + url.pathname.slice(\"/auth\".length) + url.search);\n const headers = new Headers(request.headers);\n headers.delete(\"host\");\n // Who is asking, vouched for with the secret the server shares: what a client sends under these names goes.\n headers.set(\"x-direct-auth-ip\", request.headers.get(\"cf-connecting-ip\") ?? \"\");\n headers.set(\"x-direct-auth-proxy\", env.DIRECT_AUTH_PROXY_SECRET ?? \"\");\n const body = request.method === \"GET\" || request.method === \"HEAD\" ? undefined : await request.arrayBuffer();\n return fetch(target, { method: request.method, headers, body, redirect: \"manual\" });\n },\n};\n";
|
|
19
|
+
export declare function proxyFiles(o: ProxyOptions): Record<string, string>;
|
package/dist/proxy.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The sign-in on an address of your own while the server runs on Supabase: a small Cloudflare Worker on that
|
|
3
|
+
* address (`auth.example.com`) that passes `/auth/…` to the Edge Function. The issuer is then
|
|
4
|
+
* `https://auth.example.com/auth`, in every token and every app, and moving the server later (a VPS, a container) is
|
|
5
|
+
* pointing that address somewhere else: nobody signs in again.
|
|
6
|
+
*
|
|
7
|
+
* The Worker tells the server who is asking (`x-direct-auth-ip`, for the rate limits), with a secret only the two
|
|
8
|
+
* share (`DIRECT_AUTH_PROXY_SECRET`): a request that reaches the function another way can't claim an IP.
|
|
9
|
+
*/
|
|
10
|
+
export const PROXY_WORKER = `// direct-auth's proxy: ${"`"}https://<your domain>/auth/...${"`"} to the server (made by npx direct-auth init).
|
|
11
|
+
export default {
|
|
12
|
+
async fetch(request, env) {
|
|
13
|
+
const url = new URL(request.url);
|
|
14
|
+
if (url.pathname !== "/auth" && !url.pathname.startsWith("/auth/")) return new Response("Not found", { status: 404 });
|
|
15
|
+
const target = new URL(env.TARGET.replace(/\\/+$/, "") + url.pathname.slice("/auth".length) + url.search);
|
|
16
|
+
const headers = new Headers(request.headers);
|
|
17
|
+
headers.delete("host");
|
|
18
|
+
// Who is asking, vouched for with the secret the server shares: what a client sends under these names goes.
|
|
19
|
+
headers.set("x-direct-auth-ip", request.headers.get("cf-connecting-ip") ?? "");
|
|
20
|
+
headers.set("x-direct-auth-proxy", env.DIRECT_AUTH_PROXY_SECRET ?? "");
|
|
21
|
+
const body = request.method === "GET" || request.method === "HEAD" ? undefined : await request.arrayBuffer();
|
|
22
|
+
return fetch(target, { method: request.method, headers, body, redirect: "manual" });
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
`;
|
|
26
|
+
export function proxyFiles(o) {
|
|
27
|
+
return {
|
|
28
|
+
"src/index.js": PROXY_WORKER,
|
|
29
|
+
"wrangler.toml": [
|
|
30
|
+
`name = "${o.name ?? "direct-auth-proxy"}"`,
|
|
31
|
+
`main = "src/index.js"`,
|
|
32
|
+
`compatibility_date = "2026-11-01"`,
|
|
33
|
+
// A custom domain: Cloudflare makes its DNS record and certificate when the Worker is deployed.
|
|
34
|
+
`routes = [{ pattern = "${o.domain}", custom_domain = true }]`,
|
|
35
|
+
"",
|
|
36
|
+
"[vars]",
|
|
37
|
+
`TARGET = "${o.target}"`,
|
|
38
|
+
"",
|
|
39
|
+
"# The secret the server shares (DIRECT_AUTH_PROXY_SECRET): npx wrangler secret put DIRECT_AUTH_PROXY_SECRET",
|
|
40
|
+
"",
|
|
41
|
+
].join("\n"),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The DNS records a setup needs, ready to copy: the sign-in's own address, and what mail from the codes' sender
|
|
3
|
+
* needs to land in the inbox. Each record is asked about over DNS-over-HTTPS (`present`), so the wizard can wait for
|
|
4
|
+
* them; on a domain at Cloudflare, with a token the person gives (Zone DNS Edit), they can be made from here
|
|
5
|
+
* (`cloudflare`). The token is only used for those calls: never printed, never written anywhere.
|
|
6
|
+
*/
|
|
7
|
+
export type RecordType = "A" | "AAAA" | "CNAME" | "TXT" | "MX";
|
|
8
|
+
export interface DnsRecord {
|
|
9
|
+
type: RecordType;
|
|
10
|
+
/** The full name, `send.example.com`. */
|
|
11
|
+
name: string;
|
|
12
|
+
value: string;
|
|
13
|
+
priority?: number;
|
|
14
|
+
/** What it is for, a few words. */
|
|
15
|
+
why: string;
|
|
16
|
+
/**
|
|
17
|
+
* How a record already there counts as this one: `exact` (the same value), `spf` (an SPF record that includes the
|
|
18
|
+
* same mechanism), `dmarc` (any DMARC policy).
|
|
19
|
+
*/
|
|
20
|
+
match?: "exact" | "spf" | "dmarc";
|
|
21
|
+
}
|
|
22
|
+
/** The records as aligned lines: type, name, value (priority before an MX's), and what for, dim. */
|
|
23
|
+
export declare function recordLines(records: readonly DnsRecord[], o?: {
|
|
24
|
+
dim?: (s: string) => string;
|
|
25
|
+
code?: (s: string) => string;
|
|
26
|
+
width?: number;
|
|
27
|
+
}): string[];
|
|
28
|
+
/** Whether the record answers as it should over DNS-over-HTTPS. */
|
|
29
|
+
export declare function present(r: DnsRecord, f?: typeof fetch): Promise<boolean>;
|
|
30
|
+
/** Which of the records aren't there yet. */
|
|
31
|
+
export declare function missing(records: readonly DnsRecord[], f?: typeof fetch): Promise<DnsRecord[]>;
|
|
32
|
+
/**
|
|
33
|
+
* Asked again every `everyMs` until all answer, or `timeoutMs` passes, or `stop()` says so; `tick` hears what is
|
|
34
|
+
* still missing each round. What is still missing at the end, or none.
|
|
35
|
+
*/
|
|
36
|
+
export declare function waitFor(records: readonly DnsRecord[], o?: {
|
|
37
|
+
f?: typeof fetch;
|
|
38
|
+
everyMs?: number;
|
|
39
|
+
timeoutMs?: number;
|
|
40
|
+
tick?: (left: DnsRecord[]) => void;
|
|
41
|
+
stop?: () => boolean;
|
|
42
|
+
sleep?: (ms: number) => Promise<void>;
|
|
43
|
+
}): Promise<DnsRecord[]>;
|
|
44
|
+
/**
|
|
45
|
+
* What the mail sender needs on `domain`, asked of the sender itself where its API says (Resend, Mailgun, SendGrid,
|
|
46
|
+
* SES), or the parts that are always the same where it doesn't. `note` says what can't be known from here.
|
|
47
|
+
*/
|
|
48
|
+
export declare function mailRecords(sender: string, domain: string, env: Record<string, string | undefined>, f?: typeof fetch): Promise<{
|
|
49
|
+
records: DnsRecord[];
|
|
50
|
+
note?: string;
|
|
51
|
+
}>;
|
|
52
|
+
/** The record that points the sign-in's address at a server of one's own. */
|
|
53
|
+
export declare function serverRecords(domain: string, ip: string): DnsRecord[];
|
|
54
|
+
export interface Cloudflare {
|
|
55
|
+
/** The zone `name` is in (its id and name), or null when the token can't see one. */
|
|
56
|
+
zoneOf(name: string): Promise<{
|
|
57
|
+
id: string;
|
|
58
|
+
name: string;
|
|
59
|
+
} | null>;
|
|
60
|
+
/** The records of `type` at `name`. */
|
|
61
|
+
list(zone: string, type: RecordType, name: string): Promise<{
|
|
62
|
+
id: string;
|
|
63
|
+
content: string;
|
|
64
|
+
}[]>;
|
|
65
|
+
create(zone: string, r: DnsRecord): Promise<void>;
|
|
66
|
+
update(zone: string, id: string, r: DnsRecord): Promise<void>;
|
|
67
|
+
/** Whether the token works at all (Cloudflare's own check). */
|
|
68
|
+
verify(): Promise<boolean>;
|
|
69
|
+
}
|
|
70
|
+
export declare class CloudflareError extends Error {
|
|
71
|
+
name: string;
|
|
72
|
+
}
|
|
73
|
+
export declare function cloudflare(token: string, f?: typeof fetch): Cloudflare;
|
|
74
|
+
/**
|
|
75
|
+
* What to do at Cloudflare to have `records`: those already right are left alone; a record of the same type and name
|
|
76
|
+
* with another value is replaced only for an exact one (an A, a CNAME, a DKIM key), and an SPF one gets the
|
|
77
|
+
* mechanism added; the rest are made.
|
|
78
|
+
*/
|
|
79
|
+
export declare function planCloudflare(cf: Cloudflare, zone: string, records: readonly DnsRecord[]): Promise<{
|
|
80
|
+
record: DnsRecord;
|
|
81
|
+
action: "create" | "update" | "keep";
|
|
82
|
+
id?: string;
|
|
83
|
+
value?: string;
|
|
84
|
+
}[]>;
|
|
85
|
+
/** Whether the domain's DNS is at Cloudflare: the zone's name servers, found walking up from `name`. */
|
|
86
|
+
export declare function onCloudflare(name: string, f?: typeof fetch): Promise<boolean>;
|
|
87
|
+
/**
|
|
88
|
+
* Whether the sign-in answers at its own address: the name resolves (over DNS-over-HTTPS, so a cache here doesn't
|
|
89
|
+
* fool it), and `<issuer>/.well-known/openid-configuration` says that issuer. What is wrong, or nothing.
|
|
90
|
+
*/
|
|
91
|
+
export declare function issuerProblem(issuer: string, f?: typeof fetch): Promise<string | undefined>;
|