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,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SMS senders for the codes, each one HTTP call: three hosted providers, and an Android phone with a SIM as the
|
|
3
|
+
* gateway (the open-source SMS Gateway for Android app, in its local mode on the phone's network or through its
|
|
4
|
+
* cloud relay). Numbers arrive in E.164.
|
|
5
|
+
*/
|
|
6
|
+
import { type AwsCredentials } from "./aws.ts";
|
|
7
|
+
import { type HttpOptions, type Sender } from "./types.ts";
|
|
8
|
+
/** Twilio: from a number of the account, or a Messaging Service (which picks the number). */
|
|
9
|
+
export declare function twilio(o: HttpOptions & {
|
|
10
|
+
accountSid: string;
|
|
11
|
+
authToken: string;
|
|
12
|
+
from?: string;
|
|
13
|
+
messagingServiceSid?: string;
|
|
14
|
+
}): Sender;
|
|
15
|
+
/** Vonage's SMS API. It answers 200 with a status per message, so the status is read from the body. */
|
|
16
|
+
export declare function vonage(o: HttpOptions & {
|
|
17
|
+
apiKey: string;
|
|
18
|
+
apiSecret: string;
|
|
19
|
+
from: string;
|
|
20
|
+
}): Sender;
|
|
21
|
+
/** Amazon SNS's Publish to a number, with an access key allowed `sns:Publish`. */
|
|
22
|
+
export declare function sns(o: HttpOptions & AwsCredentials & {
|
|
23
|
+
senderId?: string;
|
|
24
|
+
}): Sender;
|
|
25
|
+
/**
|
|
26
|
+
* An Android phone as the gateway, with SMS Gateway for Android: `url` is the phone's local address
|
|
27
|
+
* (`http://192.168.1.20:8080`) or the app's cloud relay (`https://api.sms-gate.app/3rdparty/v1`), and the user and
|
|
28
|
+
* password the app shows. The phone's own plan pays for the messages.
|
|
29
|
+
*/
|
|
30
|
+
export declare function smsGateway(o: HttpOptions & {
|
|
31
|
+
url: string;
|
|
32
|
+
username: string;
|
|
33
|
+
password: string;
|
|
34
|
+
}): Sender;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SMS senders for the codes, each one HTTP call: three hosted providers, and an Android phone with a SIM as the
|
|
3
|
+
* gateway (the open-source SMS Gateway for Android app, in its local mode on the phone's network or through its
|
|
4
|
+
* cloud relay). Numbers arrive in E.164.
|
|
5
|
+
*/
|
|
6
|
+
import { signAws } from "./aws.js";
|
|
7
|
+
import { basic, form, post } from "./types.js";
|
|
8
|
+
/** Twilio: from a number of the account, or a Messaging Service (which picks the number). */
|
|
9
|
+
export function twilio(o) {
|
|
10
|
+
if (!o.from && !o.messagingServiceSid)
|
|
11
|
+
throw new Error("twilio: from or messagingServiceSid is needed");
|
|
12
|
+
return (m) => post(`https://api.twilio.com/2010-04-01/Accounts/${encodeURIComponent(o.accountSid)}/Messages.json`, {
|
|
13
|
+
headers: { authorization: basic(o.accountSid, o.authToken), "content-type": "application/x-www-form-urlencoded" },
|
|
14
|
+
body: form({ To: m.to, From: o.from, MessagingServiceSid: o.messagingServiceSid, Body: m.text }),
|
|
15
|
+
}, o, "twilio");
|
|
16
|
+
}
|
|
17
|
+
/** Vonage's SMS API. It answers 200 with a status per message, so the status is read from the body. */
|
|
18
|
+
export function vonage(o) {
|
|
19
|
+
return async (m) => {
|
|
20
|
+
try {
|
|
21
|
+
const res = await (o.fetch ?? fetch)("https://rest.nexmo.com/sms/json", {
|
|
22
|
+
method: "POST",
|
|
23
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
24
|
+
body: form({ api_key: o.apiKey, api_secret: o.apiSecret, from: o.from, to: m.to.replace(/^\+/, ""), text: m.text, type: "unicode" }),
|
|
25
|
+
signal: AbortSignal.timeout(o.timeoutMs ?? 15_000),
|
|
26
|
+
});
|
|
27
|
+
const body = (await res.json().catch(() => null));
|
|
28
|
+
const first = body?.messages?.[0];
|
|
29
|
+
if (res.ok && first?.status === "0")
|
|
30
|
+
return { ok: true };
|
|
31
|
+
return { ok: false, reason: `vonage: ${first?.["error-text"] ?? res.statusText}`, status: res.status };
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return { ok: false, reason: "vonage: network" };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** Amazon SNS's Publish to a number, with an access key allowed `sns:Publish`. */
|
|
39
|
+
export function sns(o) {
|
|
40
|
+
const url = `https://sns.${o.region}.amazonaws.com/`;
|
|
41
|
+
return async (m) => {
|
|
42
|
+
const fields = { Action: "Publish", Version: "2010-03-31", PhoneNumber: m.to, Message: m.text };
|
|
43
|
+
fields["MessageAttributes.entry.1.Name"] = "AWS.SNS.SMS.SMSType";
|
|
44
|
+
fields["MessageAttributes.entry.1.Value.DataType"] = "String";
|
|
45
|
+
fields["MessageAttributes.entry.1.Value.StringValue"] = "Transactional";
|
|
46
|
+
if (o.senderId) {
|
|
47
|
+
fields["MessageAttributes.entry.2.Name"] = "AWS.SNS.SMS.SenderID";
|
|
48
|
+
fields["MessageAttributes.entry.2.Value.DataType"] = "String";
|
|
49
|
+
fields["MessageAttributes.entry.2.Value.StringValue"] = o.senderId;
|
|
50
|
+
}
|
|
51
|
+
const body = form(fields);
|
|
52
|
+
const headers = await signAws(o, "sns", url, body, "application/x-www-form-urlencoded");
|
|
53
|
+
return post(url, { headers, body }, o, "sns");
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* An Android phone as the gateway, with SMS Gateway for Android: `url` is the phone's local address
|
|
58
|
+
* (`http://192.168.1.20:8080`) or the app's cloud relay (`https://api.sms-gate.app/3rdparty/v1`), and the user and
|
|
59
|
+
* password the app shows. The phone's own plan pays for the messages.
|
|
60
|
+
*/
|
|
61
|
+
export function smsGateway(o) {
|
|
62
|
+
// The phone serves `/message`; the cloud relay, under `/3rdparty/v1`, takes `/messages`.
|
|
63
|
+
const base = o.url.replace(/\/+$/, "");
|
|
64
|
+
const url = /\/3rdparty\/v1$/.test(base) ? `${base}/messages` : `${base}/message`;
|
|
65
|
+
return (m) => post(url, {
|
|
66
|
+
headers: { authorization: basic(o.username, o.password), "content-type": "application/json" },
|
|
67
|
+
body: JSON.stringify({ textMessage: { text: m.text }, phoneNumbers: [m.to] }),
|
|
68
|
+
}, o, "sms-gateway");
|
|
69
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email over SMTP, to any server or relay (a VPS's own Postfix, a provider's SMTP endpoint): implicit TLS on port
|
|
3
|
+
* 465, or STARTTLS on 587 (and 25), then AUTH PLAIN when a user is given. On Node, Bun and Deno, through `node:net`
|
|
4
|
+
* and `node:tls`; Workers have no such sockets here, so a Worker uses one of the HTTP senders.
|
|
5
|
+
*
|
|
6
|
+
* One connection per code, closed after: codes are few, and a kept connection is one more thing to go stale.
|
|
7
|
+
*/
|
|
8
|
+
import type { Sender } from "./types.ts";
|
|
9
|
+
export interface SmtpOptions {
|
|
10
|
+
host: string;
|
|
11
|
+
/** 465 (TLS from the start) or 587 (STARTTLS). */
|
|
12
|
+
port?: number;
|
|
13
|
+
user?: string;
|
|
14
|
+
pass?: string;
|
|
15
|
+
/** `"Acme <hello@acme.com>"`. */
|
|
16
|
+
from: string;
|
|
17
|
+
/** The name this side gives in EHLO (the from address's domain). */
|
|
18
|
+
hostname?: string;
|
|
19
|
+
/** Milliseconds for the whole exchange (20 s). */
|
|
20
|
+
timeoutMs?: number;
|
|
21
|
+
}
|
|
22
|
+
/** The message, as the DATA command sends it (dot-stuffed, ending with the lone dot). */
|
|
23
|
+
export declare function mime(o: {
|
|
24
|
+
from: string;
|
|
25
|
+
to: string;
|
|
26
|
+
subject: string;
|
|
27
|
+
text: string;
|
|
28
|
+
html: string;
|
|
29
|
+
date?: Date;
|
|
30
|
+
id?: string;
|
|
31
|
+
}): string;
|
|
32
|
+
export declare function smtp(o: SmtpOptions): Sender;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Email over SMTP, to any server or relay (a VPS's own Postfix, a provider's SMTP endpoint): implicit TLS on port
|
|
3
|
+
* 465, or STARTTLS on 587 (and 25), then AUTH PLAIN when a user is given. On Node, Bun and Deno, through `node:net`
|
|
4
|
+
* and `node:tls`; Workers have no such sockets here, so a Worker uses one of the HTTP senders.
|
|
5
|
+
*
|
|
6
|
+
* One connection per code, closed after: codes are few, and a kept connection is one more thing to go stale.
|
|
7
|
+
*/
|
|
8
|
+
import { base64, utf8 } from "../base64.js";
|
|
9
|
+
/** Reads SMTP replies (`250-…` lines until `250 …`) off a socket, one at a time. */
|
|
10
|
+
function replies(socket) {
|
|
11
|
+
let buffer = "";
|
|
12
|
+
const waiting = [];
|
|
13
|
+
const ready = [];
|
|
14
|
+
let lines = [];
|
|
15
|
+
const give = (r) => {
|
|
16
|
+
const w = waiting.shift();
|
|
17
|
+
if (w)
|
|
18
|
+
w(r);
|
|
19
|
+
else
|
|
20
|
+
ready.push(r);
|
|
21
|
+
};
|
|
22
|
+
socket.on("data", (chunk) => {
|
|
23
|
+
buffer += new TextDecoder().decode(chunk);
|
|
24
|
+
let i;
|
|
25
|
+
while ((i = buffer.indexOf("\r\n")) >= 0) {
|
|
26
|
+
const line = buffer.slice(0, i);
|
|
27
|
+
buffer = buffer.slice(i + 2);
|
|
28
|
+
lines.push(line);
|
|
29
|
+
if (/^\d{3} /.test(line) || /^\d{3}$/.test(line)) {
|
|
30
|
+
give({ code: Number(line.slice(0, 3)), text: lines.join("\n") });
|
|
31
|
+
lines = [];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
socket.on("error", () => give(null));
|
|
36
|
+
socket.on("close", () => give(null));
|
|
37
|
+
return {
|
|
38
|
+
next() {
|
|
39
|
+
if (ready.length)
|
|
40
|
+
return Promise.resolve(ready.shift());
|
|
41
|
+
return new Promise((resolve) => waiting.push(resolve));
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const address = (from) => /<([^>]+)>/.exec(from)?.[1]?.trim() ?? from.trim();
|
|
46
|
+
/** A header value in UTF-8, as RFC 2047 writes one that isn't plain ASCII. */
|
|
47
|
+
const header = (s) => (/^[\x20-\x7e]*$/.test(s) ? s : `=?UTF-8?B?${base64(s)}?=`);
|
|
48
|
+
function fromHeader(from) {
|
|
49
|
+
const m = /^(.*)<([^>]+)>\s*$/.exec(from);
|
|
50
|
+
if (!m)
|
|
51
|
+
return from.trim();
|
|
52
|
+
const name = m[1].trim().replace(/^"|"$/g, "");
|
|
53
|
+
return name ? `${header(name)} <${m[2].trim()}>` : `<${m[2].trim()}>`;
|
|
54
|
+
}
|
|
55
|
+
/** base64 in lines of 76, as MIME wants it. */
|
|
56
|
+
const wrap = (s) => base64(utf8(s)).replace(/.{1,76}/g, "$&\r\n");
|
|
57
|
+
/** The message, as the DATA command sends it (dot-stuffed, ending with the lone dot). */
|
|
58
|
+
export function mime(o) {
|
|
59
|
+
const boundary = `b${crypto.randomUUID().replace(/-/g, "")}`;
|
|
60
|
+
const domain = address(o.from).split("@")[1] ?? "localhost";
|
|
61
|
+
const lines = [
|
|
62
|
+
`From: ${fromHeader(o.from)}`,
|
|
63
|
+
`To: <${o.to}>`,
|
|
64
|
+
`Subject: ${header(o.subject)}`,
|
|
65
|
+
`Date: ${(o.date ?? new Date()).toUTCString()}`,
|
|
66
|
+
`Message-ID: <${o.id ?? crypto.randomUUID()}@${domain}>`,
|
|
67
|
+
"MIME-Version: 1.0",
|
|
68
|
+
`Content-Type: multipart/alternative; boundary="${boundary}"`,
|
|
69
|
+
"",
|
|
70
|
+
`--${boundary}`,
|
|
71
|
+
"Content-Type: text/plain; charset=utf-8",
|
|
72
|
+
"Content-Transfer-Encoding: base64",
|
|
73
|
+
"",
|
|
74
|
+
wrap(o.text),
|
|
75
|
+
`--${boundary}`,
|
|
76
|
+
"Content-Type: text/html; charset=utf-8",
|
|
77
|
+
"Content-Transfer-Encoding: base64",
|
|
78
|
+
"",
|
|
79
|
+
wrap(o.html),
|
|
80
|
+
`--${boundary}--`,
|
|
81
|
+
"",
|
|
82
|
+
];
|
|
83
|
+
return lines.join("\r\n").replace(/\r\n\./g, "\r\n..") + "\r\n.\r\n";
|
|
84
|
+
}
|
|
85
|
+
export function smtp(o) {
|
|
86
|
+
const port = o.port ?? 587;
|
|
87
|
+
const ehlo = o.hostname ?? address(o.from).split("@")[1] ?? "localhost";
|
|
88
|
+
if (/[\r\n]/.test(o.from))
|
|
89
|
+
throw new Error("smtp: from has a line break");
|
|
90
|
+
return async (m) => {
|
|
91
|
+
if (/[\r\n<>]/.test(m.to))
|
|
92
|
+
return { ok: false, reason: "smtp: the address has characters SMTP can't take" };
|
|
93
|
+
const net = await import("node:net");
|
|
94
|
+
const tls = await import("node:tls");
|
|
95
|
+
let socket = null;
|
|
96
|
+
const deadline = setTimeout(() => socket?.destroy(), o.timeoutMs ?? 20_000);
|
|
97
|
+
try {
|
|
98
|
+
socket = (port === 465
|
|
99
|
+
? tls.connect({ host: o.host, port, servername: o.host })
|
|
100
|
+
: net.connect({ host: o.host, port }));
|
|
101
|
+
let read = replies(socket);
|
|
102
|
+
const expect = async (codes, what) => {
|
|
103
|
+
const r = await read.next();
|
|
104
|
+
if (!r)
|
|
105
|
+
throw new Error(`${what}: the connection closed`);
|
|
106
|
+
if (!codes.includes(r.code))
|
|
107
|
+
throw new Error(`${what}: ${r.text.split("\n").pop()}`);
|
|
108
|
+
return r;
|
|
109
|
+
};
|
|
110
|
+
const say = async (line, codes, what = line.split(" ")[0]) => {
|
|
111
|
+
socket.write(`${line}\r\n`);
|
|
112
|
+
return expect(codes, what);
|
|
113
|
+
};
|
|
114
|
+
await expect([220], "greeting");
|
|
115
|
+
let hello = await say(`EHLO ${ehlo}`, [250]);
|
|
116
|
+
if (port !== 465) {
|
|
117
|
+
if (!/\bSTARTTLS\b/i.test(hello.text))
|
|
118
|
+
throw new Error("the server offers no STARTTLS, and the code won't go in the clear");
|
|
119
|
+
await say("STARTTLS", [220]);
|
|
120
|
+
const plain = socket;
|
|
121
|
+
plain.removeAllListeners("data");
|
|
122
|
+
socket = tls.connect({ socket: plain, servername: o.host });
|
|
123
|
+
read = replies(socket);
|
|
124
|
+
hello = await say(`EHLO ${ehlo}`, [250]);
|
|
125
|
+
}
|
|
126
|
+
if (o.user) {
|
|
127
|
+
await say(`AUTH PLAIN ${base64(`\0${o.user}\0${o.pass ?? ""}`)}`, [235], "AUTH");
|
|
128
|
+
}
|
|
129
|
+
await say(`MAIL FROM:<${address(o.from)}>`, [250], "MAIL FROM");
|
|
130
|
+
await say(`RCPT TO:<${m.to}>`, [250, 251], "RCPT TO");
|
|
131
|
+
await say("DATA", [354]);
|
|
132
|
+
socket.write(mime({ from: o.from, to: m.to, subject: m.subject, text: m.text, html: m.html }));
|
|
133
|
+
await expect([250], "DATA");
|
|
134
|
+
socket.write("QUIT\r\n");
|
|
135
|
+
return { ok: true };
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
return { ok: false, reason: `smtp: ${e.message}` };
|
|
139
|
+
}
|
|
140
|
+
finally {
|
|
141
|
+
clearTimeout(deadline);
|
|
142
|
+
socket?.end();
|
|
143
|
+
}
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** What every sender gets and answers: one code, to one address or number, already written. */
|
|
2
|
+
export interface Outgoing {
|
|
3
|
+
channel: "email" | "sms";
|
|
4
|
+
/** An email address, or a number in E.164. */
|
|
5
|
+
to: string;
|
|
6
|
+
code: string;
|
|
7
|
+
subject: string;
|
|
8
|
+
text: string;
|
|
9
|
+
html: string;
|
|
10
|
+
locale: string;
|
|
11
|
+
}
|
|
12
|
+
export type SendResult = {
|
|
13
|
+
ok: true;
|
|
14
|
+
} | {
|
|
15
|
+
ok: false;
|
|
16
|
+
reason: string;
|
|
17
|
+
status?: number;
|
|
18
|
+
};
|
|
19
|
+
export type Sender = (message: Outgoing) => Promise<SendResult>;
|
|
20
|
+
export interface HttpOptions {
|
|
21
|
+
fetch?: typeof fetch;
|
|
22
|
+
/** Milliseconds per request (15 s). */
|
|
23
|
+
timeoutMs?: number;
|
|
24
|
+
}
|
|
25
|
+
/** A POST that never throws: the provider's status and a short reason when it refuses, `network` when unreachable. */
|
|
26
|
+
export declare function post(url: string, init: {
|
|
27
|
+
headers: Record<string, string>;
|
|
28
|
+
body: string;
|
|
29
|
+
}, o: HttpOptions, provider: string): Promise<SendResult>;
|
|
30
|
+
export declare const basic: (user: string, pass: string) => string;
|
|
31
|
+
export declare const form: (fields: Record<string, string | undefined>) => string;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** What every sender gets and answers: one code, to one address or number, already written. */
|
|
2
|
+
/** A POST that never throws: the provider's status and a short reason when it refuses, `network` when unreachable. */
|
|
3
|
+
export async function post(url, init, o, provider) {
|
|
4
|
+
try {
|
|
5
|
+
const res = await (o.fetch ?? fetch)(url, {
|
|
6
|
+
method: "POST",
|
|
7
|
+
headers: init.headers,
|
|
8
|
+
body: init.body,
|
|
9
|
+
signal: AbortSignal.timeout(o.timeoutMs ?? 15_000),
|
|
10
|
+
});
|
|
11
|
+
const body = await res.text().catch(() => "");
|
|
12
|
+
if (res.ok)
|
|
13
|
+
return { ok: true };
|
|
14
|
+
return { ok: false, reason: `${provider}: ${short(body) || res.statusText || "refused"}`, status: res.status };
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return { ok: false, reason: `${provider}: network` };
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** The start of a provider's error, without anything that looks like a key. */
|
|
21
|
+
function short(body) {
|
|
22
|
+
return body.replace(/\s+/g, " ").trim().slice(0, 200);
|
|
23
|
+
}
|
|
24
|
+
export const basic = (user, pass) => `Basic ${btoa(`${user}:${pass}`)}`;
|
|
25
|
+
export const form = (fields) => new URLSearchParams(Object.entries(fields).filter((e) => e[1] !== undefined)).toString();
|
package/dist/server.d.ts
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The HTTP API (docs/API.md) as one `fetch(request) → response`, for anything that speaks Request and Response:
|
|
3
|
+
* Supabase's Edge Functions, Deno, Bun, Cloudflare Workers, Vercel and Netlify functions; on Node, `serve()` from
|
|
4
|
+
* `direct-auth/node`.
|
|
5
|
+
*
|
|
6
|
+
* const server = await createAuthServer({ sql, issuer, keys, app: "Example", apple: { … }, email: { … } });
|
|
7
|
+
* Deno.serve(server.fetch);
|
|
8
|
+
*/
|
|
9
|
+
import { type Auth, type AuthOptions } from "./auth.ts";
|
|
10
|
+
export interface AuthServerOptions extends AuthOptions {
|
|
11
|
+
/** Where the API answers (`/auth`). The issuer's URL ends with it. */
|
|
12
|
+
basePath?: string;
|
|
13
|
+
/** The browser origins allowed to call it (`https://example.com`). Same-origin and apps need nothing here. */
|
|
14
|
+
origins?: readonly string[];
|
|
15
|
+
/**
|
|
16
|
+
* The caller's IP, for the rate limits. By default, only what can't be forged: on Cloudflare Workers
|
|
17
|
+
* `cf-connecting-ip` (Cloudflare sets it); on Supabase's Edge Functions the last `x-forwarded-for` entry, which
|
|
18
|
+
* Supabase's gateway appends (an assumption about a gateway we don't run: for per-IP limits that hold whatever it
|
|
19
|
+
* does, put the proxy Worker `direct-auth init` makes in front, with `proxySecret`, and leave `trustProxy` off);
|
|
20
|
+
* under `Deno.serve` and `Bun.serve` the connection's own address, which they hand
|
|
21
|
+
* `fetch` as its second argument (`Deno.serve(server.fetch)`, `Bun.serve({ fetch: server.fetch })`); under `serve()`
|
|
22
|
+
* from `direct-auth/node`, the socket's (or, with its `behindProxy`, the proxy's `x-forwarded-for`). Behind a proxy
|
|
23
|
+
* of your own in front of Deno or Bun (nginx, Caddy, a load balancer), or on Vercel or Netlify, set `trustProxy`.
|
|
24
|
+
* Behind Cloudflare's proxy on a server of your own, give `(req) => req.headers.get("cf-connecting-ip")`. Without
|
|
25
|
+
* an IP the per-IP limits don't apply (the per-address ones still do). IPv6 addresses count by their /64, an IPv4
|
|
26
|
+
* one written as IPv6 (`::ffff:203.0.113.7`) as the IPv4 it is.
|
|
27
|
+
*/
|
|
28
|
+
ip?: (req: Request) => string | null | undefined;
|
|
29
|
+
/**
|
|
30
|
+
* A proxy you run (or the platform's) is in front and sets `x-forwarded-for`: its last entry is the caller's IP,
|
|
31
|
+
* then `x-real-ip`. Off by default, as a client sets those headers to anything when nothing in front replaces them.
|
|
32
|
+
*/
|
|
33
|
+
trustProxy?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* A proxy of your own in front (the Worker `direct-auth init` makes for an address of your own on Supabase): the
|
|
36
|
+
* secret it sends in `x-direct-auth-proxy`. With it, `x-direct-auth-ip` is the caller's IP; without it, those
|
|
37
|
+
* headers are ignored.
|
|
38
|
+
*/
|
|
39
|
+
proxySecret?: string;
|
|
40
|
+
/**
|
|
41
|
+
* A check that a person is asking, before a code is sent (a Turnstile or hCaptcha token in the body, say): false
|
|
42
|
+
* answers `403 human_check_failed`. Worth it for SMS, which costs per message.
|
|
43
|
+
*/
|
|
44
|
+
humanCheck?: (req: Request, body: Record<string, unknown>, channel: "email" | "sms" | "guest") => boolean | Promise<boolean>;
|
|
45
|
+
/**
|
|
46
|
+
* Minutes since sign-in within which a session may change how the account is reached (10): add a passkey, link
|
|
47
|
+
* or unlink a way in, set a password, change the address. Later it answers `401 reauth_needed`. A guest, who has
|
|
48
|
+
* nothing to sign in again with, is never asked.
|
|
49
|
+
*/
|
|
50
|
+
freshMinutes?: number;
|
|
51
|
+
/** The refresh cookie's name in cookie mode (`direct_auth_refresh`). */
|
|
52
|
+
cookieName?: string;
|
|
53
|
+
/** Told of each failure worth a look (a provider refusing, a send failing), without tokens or codes. */
|
|
54
|
+
observe?: (event: {
|
|
55
|
+
route: string;
|
|
56
|
+
reason: string;
|
|
57
|
+
detail?: string;
|
|
58
|
+
}) => void;
|
|
59
|
+
}
|
|
60
|
+
export interface AuthServer {
|
|
61
|
+
/** `info`: what `Deno.serve` or `Bun.serve` pass a handler, from which the caller's address is read. */
|
|
62
|
+
fetch(req: Request, info?: unknown): Promise<Response>;
|
|
63
|
+
auth: Auth;
|
|
64
|
+
}
|
|
65
|
+
/** An IP as a limit's key: IPv6 by its /64 (one host holds a whole one), an IPv4 one written as IPv6 as itself. */
|
|
66
|
+
export declare function ipKey(ip: string | null | undefined): string | undefined;
|
|
67
|
+
export declare function createAuthServer(o: AuthServerOptions): Promise<AuthServer>;
|