@protobase/server 0.123.6 → 0.124.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/dist/better-auth/auth-settings.d.ts +37 -6
- package/dist/better-auth/create-auth.d.ts +49 -31
- package/dist/better-auth/create-auth.js +14 -5
- package/dist/better-auth/last-admin.js +1 -1
- package/dist/better-auth/mcp/mcp-auth-plugins.d.ts +2 -2
- package/dist/better-auth/names/modules/index.d.ts +51 -1
- package/dist/better-auth/names/modules/mail-senders.js +36 -2
- package/dist/better-auth/names/resolve-names.d.ts +1 -1
- package/dist/better-auth/openid-provider/openid-provider-plugins.d.ts +1 -1
- package/dist/better-auth/organizations/accept-invitation.js +1 -1
- package/dist/better-auth/organizations/add-every-user.js +1 -1
- package/dist/better-auth/organizations/host.js +1 -1
- package/dist/better-auth/organizations/options.d.ts +1 -1
- package/dist/better-auth/staff-checks.js +2 -1
- package/dist/better-auth/status-route.js +3 -2
- package/dist/better-auth/store/steps/index.js +6 -1
- package/dist/better-auth/store/steps/legacy-versions.js +4 -0
- package/dist/better-auth/store/steps/mail-senders/003-sender-address-evidence.js +81 -0
- package/dist/better-auth/store/store-sql.d.ts +3 -0
- package/dist/better-auth/users.js +1 -1
- package/dist/functions/define-function.d.ts +7 -5
- package/dist/functions/own-mailbox.js +2 -1
- package/dist/functions/routes.js +2 -1
- package/dist/index.d.ts +7 -3
- package/dist/index.js +3 -1
- package/dist/package.js +1 -1
- package/dist/personal-mail/alias-checks.js +2 -2
- package/dist/personal-mail/gmail-errors.js +22 -0
- package/dist/personal-mail/gmail-sender-addresses.d.ts +4 -0
- package/dist/personal-mail/gmail-sender-addresses.js +74 -0
- package/dist/personal-mail/gmail.js +9 -22
- package/dist/personal-mail/listed-mail-senders.d.ts +31 -0
- package/dist/personal-mail/listed-mail-senders.js +30 -0
- package/dist/personal-mail/mail-sender-book.js +28 -0
- package/dist/personal-mail/mail-sender.d.ts +9 -3
- package/dist/personal-mail/mail-sender.js +43 -32
- package/dist/personal-mail/mail-senders-plugin.js +92 -78
- package/dist/personal-mail/mailbox-connections.d.ts +1 -0
- package/dist/personal-mail/mailbox-connections.js +11 -0
- package/dist/personal-mail/personal-mail-provider.d.ts +32 -0
- package/dist/personal-mail/personal-mail-provider.js +7 -0
- package/dist/personal-mail/personal-mail-providers.d.ts +9 -0
- package/dist/personal-mail/personal-mail-providers.js +12 -5
- package/dist/personal-mail/personal-mailer.d.ts +18 -9
- package/dist/personal-mail/personal-mailer.js +62 -20
- package/dist/personal-mail/sender-address-error.d.ts +21 -0
- package/dist/personal-mail/sender-address-error.js +27 -0
- package/dist/personal-mail/sender-address-input.js +15 -0
- package/dist/personal-mail/sender-address-state.d.ts +12 -0
- package/dist/personal-mail/sender-address-state.js +20 -0
- package/dist/personal-mail/sender-listing.d.ts +22 -0
- package/dist/personal-mail/sender-listing.js +66 -0
- package/dist/personal-mail/sender-store.d.ts +1 -1
- package/dist/personal-mail/sender-store.js +86 -55
- package/dist/webhooks/table/owner-roles.js +1 -1
- package/package.json +6 -6
- package/dist/personal-mail/mail-senders-plugin.d.ts +0 -14
|
@@ -1,44 +1,55 @@
|
|
|
1
1
|
import { MailSenderRefusedError } from "./mail-sender-refused.js";
|
|
2
|
+
import { SenderAddressError } from "./sender-address-error.js";
|
|
3
|
+
import { senderRowState } from "./sender-address-state.js";
|
|
2
4
|
//#region src/personal-mail/mail-sender.ts
|
|
3
|
-
const
|
|
5
|
+
const connected = (book, row) => book.connections.some(({ account }) => account.id === row.connectionId);
|
|
6
|
+
/** The state of a row of `book`'s (`senderRowState`). */
|
|
7
|
+
const stateIn = (book, row) => senderRowState(row, connected(book, row));
|
|
8
|
+
/** The rows a person may choose: neither removed nor detached, oldest first. */
|
|
9
|
+
const usableRows = (book) => book.rows.flatMap((row) => {
|
|
10
|
+
const state = stateIn(book, row);
|
|
11
|
+
return state === "removed" || state === "detached" ? [] : [{
|
|
12
|
+
row,
|
|
13
|
+
state
|
|
14
|
+
}];
|
|
15
|
+
});
|
|
16
|
+
const senderOf = (row, state) => ({
|
|
17
|
+
id: row.id,
|
|
4
18
|
address: row.address,
|
|
5
19
|
...row.name && { name: row.name },
|
|
6
20
|
ownerId: row.userId,
|
|
7
|
-
...row.signature && { signature: row.signature }
|
|
21
|
+
...row.signature && { signature: row.signature },
|
|
22
|
+
state
|
|
23
|
+
});
|
|
24
|
+
const mailboxSender = (book) => ({
|
|
25
|
+
id: null,
|
|
26
|
+
...book.ownAddress && { address: book.ownAddress },
|
|
27
|
+
ownerId: book.userId,
|
|
28
|
+
state: "verified"
|
|
8
29
|
});
|
|
30
|
+
const sameAddress = (a, b) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
9
31
|
/**
|
|
10
|
-
* The sender `
|
|
11
|
-
* mailbox's own address
|
|
12
|
-
* `MailSenderRefusedError` any other address, one someone else entered included
|
|
32
|
+
* The sender `book`'s person sends as for `address`, matched without regard to case: one of their usable records;
|
|
33
|
+
* else their mailbox's own address. Without `address`, their default record, else their mailbox's own. Refuses with
|
|
34
|
+
* `MailSenderRefusedError` any other address, one someone else entered included, and with `sender_address_ambiguous`
|
|
35
|
+
* one that is a record on more than one of their connections.
|
|
13
36
|
*/
|
|
14
|
-
const chooseSender =
|
|
15
|
-
const
|
|
16
|
-
if (
|
|
17
|
-
const chosen =
|
|
18
|
-
|
|
19
|
-
const address = await ownAddress();
|
|
20
|
-
return {
|
|
21
|
-
...address && { address },
|
|
22
|
-
ownerId: userId
|
|
23
|
-
};
|
|
37
|
+
const chooseSender = (book, address) => {
|
|
38
|
+
const usable = usableRows(book);
|
|
39
|
+
if (address === void 0) {
|
|
40
|
+
const chosen = usable.find(({ row }) => row.isDefault);
|
|
41
|
+
return chosen ? senderOf(chosen.row, chosen.state) : mailboxSender(book);
|
|
24
42
|
}
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
if (
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
address: mailbox,
|
|
31
|
-
ownerId: userId
|
|
32
|
-
};
|
|
33
|
-
throw new MailSenderRefusedError(from);
|
|
43
|
+
const matching = usable.filter(({ row }) => sameAddress(row.address, address));
|
|
44
|
+
if (matching.length > 1) throw new SenderAddressError("sender_address_ambiguous");
|
|
45
|
+
if (matching[0]) return senderOf(matching[0].row, matching[0].state);
|
|
46
|
+
if (book.ownAddress && sameAddress(book.ownAddress, address)) return mailboxSender(book);
|
|
47
|
+
throw new MailSenderRefusedError(address);
|
|
34
48
|
};
|
|
35
|
-
/** Every sender
|
|
36
|
-
const usableSenders =
|
|
37
|
-
const
|
|
38
|
-
return [...
|
|
39
|
-
address: mailbox,
|
|
40
|
-
ownerId: userId
|
|
41
|
-
}] : [], ...own.map(senderOf)];
|
|
49
|
+
/** Every sender the person may choose: their mailbox's own address, when the link names it and no record holds it, then their records. */
|
|
50
|
+
const usableSenders = (book) => {
|
|
51
|
+
const usable = usableRows(book);
|
|
52
|
+
return [...book.ownAddress && !usable.some(({ row }) => sameAddress(row.address, book.ownAddress)) ? [mailboxSender(book)] : [], ...usable.map(({ row, state }) => senderOf(row, state))];
|
|
42
53
|
};
|
|
43
54
|
//#endregion
|
|
44
|
-
export { chooseSender, usableSenders };
|
|
55
|
+
export { chooseSender, senderOf, stateIn, usableRows, usableSenders };
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { senderAddressInput } from "./sender-address-input.js";
|
|
2
|
+
import { isRemoved } from "./sender-address-state.js";
|
|
2
3
|
import { linkedMailbox } from "./personal-mailer.js";
|
|
3
4
|
import { headersLocale } from "../locale/request-locale.js";
|
|
4
|
-
import {
|
|
5
|
+
import { storeSqlOf } from "../better-auth/store/store-sql.js";
|
|
6
|
+
import { linkedAccountsOf, senderBook } from "./mail-sender-book.js";
|
|
7
|
+
import { listedMailSenders, listedSender } from "./listed-mail-senders.js";
|
|
8
|
+
import { mailSenderModel, sqlSenderStore } from "./sender-store.js";
|
|
5
9
|
import * as z$1 from "zod";
|
|
6
10
|
import { APIError, createAuthEndpoint, createAuthMiddleware, getSessionFromCtx, sessionMiddleware } from "better-auth/api";
|
|
7
11
|
//#region src/personal-mail/mail-senders-plugin.ts
|
|
@@ -11,21 +15,15 @@ const mailSenderPaths = {
|
|
|
11
15
|
add: "/account/mail-senders/add",
|
|
12
16
|
update: "/account/mail-senders/update",
|
|
13
17
|
remove: "/account/mail-senders/remove",
|
|
18
|
+
refresh: "/account/mail-senders/refresh",
|
|
14
19
|
check: "/account/mail-senders/check",
|
|
15
20
|
checkResult: "/account/mail-senders/check-result"
|
|
16
21
|
};
|
|
17
22
|
/** The longest signature, in characters: a closing and a few lines of contact details. */
|
|
18
23
|
const maxSignatureLength = 2e3;
|
|
19
|
-
const listed = (row) => ({
|
|
20
|
-
id: row.id,
|
|
21
|
-
address: row.address,
|
|
22
|
-
...row.name && { name: row.name },
|
|
23
|
-
...row.signature && { signature: row.signature },
|
|
24
|
-
isDefault: row.isDefault
|
|
25
|
-
});
|
|
26
24
|
const tooMany = {
|
|
27
25
|
code: "TOO_MANY_MAIL_SENDERS",
|
|
28
|
-
message: `You can
|
|
26
|
+
message: `You can have at most 50 From addresses.`
|
|
29
27
|
};
|
|
30
28
|
const taken = {
|
|
31
29
|
code: "MAIL_SENDER_EXISTS",
|
|
@@ -33,33 +31,43 @@ const taken = {
|
|
|
33
31
|
};
|
|
34
32
|
const notFound = {
|
|
35
33
|
code: "MAIL_SENDER_NOT_FOUND",
|
|
36
|
-
message: "This address is not one
|
|
34
|
+
message: "This address is not one of yours."
|
|
37
35
|
};
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
providerId: account.providerId,
|
|
42
|
-
scopes: storedScopes(account.scope),
|
|
43
|
-
createdAt: account.createdAt
|
|
44
|
-
})), providers);
|
|
45
|
-
if (!linked) return;
|
|
46
|
-
const address = idTokenAddress(accounts.find((account) => account.id === linked.account.id)?.idToken);
|
|
47
|
-
return {
|
|
48
|
-
accountId: linked.account.id,
|
|
49
|
-
...address && { address }
|
|
50
|
-
};
|
|
36
|
+
const noMailbox = {
|
|
37
|
+
code: "MAILBOX_NOT_FOUND",
|
|
38
|
+
message: "Connect a mailbox first."
|
|
51
39
|
};
|
|
52
40
|
const unlinkPath = "/unlink-account";
|
|
53
41
|
const signatureField = z$1.string().max(maxSignatureLength).transform((value) => value.trim() ? value : null);
|
|
54
42
|
const nameField = z$1.string().max(100).regex(/^[^\r\n]*$/).transform((value) => value.trim() || null);
|
|
43
|
+
const idField = z$1.string().min(1).max(200);
|
|
44
|
+
const bookOf = async (ctx, providers) => {
|
|
45
|
+
const userId = ctx.context.session.user.id;
|
|
46
|
+
const store = sqlSenderStore(async () => storeSqlOf(ctx.context));
|
|
47
|
+
const [rows, accounts] = await Promise.all([store.ofUser(userId), ctx.context.internalAdapter.findAccounts(userId)]);
|
|
48
|
+
return {
|
|
49
|
+
userId,
|
|
50
|
+
store,
|
|
51
|
+
accounts,
|
|
52
|
+
book: senderBook(userId, rows, accounts, providers)
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const ownRow = (rows, id) => {
|
|
56
|
+
const row = rows.find((candidate) => candidate.id === id);
|
|
57
|
+
if (!row || isRemoved(row)) throw new APIError("NOT_FOUND", notFound);
|
|
58
|
+
return row;
|
|
59
|
+
};
|
|
55
60
|
/**
|
|
56
|
-
* The From addresses the signed-in person sends mail
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
61
|
+
* The From addresses the signed-in person sends mail as, each on one of their mailbox connections, with its state:
|
|
62
|
+
* `GET /account/mail-senders` lists them, oldest first, with their mailbox's own `address` when its link names it and
|
|
63
|
+
* their `connections`; `POST /account/mail-senders/add { address, name?, signature?, connection? }` types one on the
|
|
64
|
+
* connection given, else the one mail goes through, `unverified` until the provider lists it; `POST
|
|
65
|
+
* /account/mail-senders/update { id, name?, signature?, isDefault? }` changes one, `name: null` dropping its name,
|
|
66
|
+
* `signature: null` dropping its own, and `isDefault` making it, or no longer making it, the address mail goes out from
|
|
67
|
+
* when the app names none; `POST /account/mail-senders/remove { id }` removes one; `POST /account/mail-senders/refresh`
|
|
68
|
+
* lists the send-as addresses of each connection whose provider can, answering how each went. Each acts on the
|
|
69
|
+
* session's own addresses and connections only: any other id answers 404 before anything is done. While records of
|
|
70
|
+
* the app hold an address (`inUse`), removing it, or disconnecting the mailbox whose own address it is
|
|
63
71
|
* (`/unlink-account`), is refused with a 409, and changes nothing.
|
|
64
72
|
*
|
|
65
73
|
* With `checks`, the list says `checks: true`, and `POST /account/mail-senders/check { id }` sends a test message from
|
|
@@ -67,7 +75,7 @@ const nameField = z$1.string().max(100).regex(/^[^\r\n]*$/).transform((value) =>
|
|
|
67
75
|
* /account/mail-senders/check-result { id, check }` answers where it stands (`AliasCheckResult`). Without, the check
|
|
68
76
|
* answers `{ status: 'unchecked' }` and sends nothing.
|
|
69
77
|
*/
|
|
70
|
-
const mailSendersPlugin = (providers, names, inUse, checks) => ({
|
|
78
|
+
const mailSendersPlugin = (providers, names, mailOf, inUse, checks) => ({
|
|
71
79
|
id: "protobase-mail-senders",
|
|
72
80
|
schema: names.tables(mailSenderModel),
|
|
73
81
|
hooks: { before: [{
|
|
@@ -76,8 +84,8 @@ const mailSendersPlugin = (providers, names, inUse, checks) => ({
|
|
|
76
84
|
const accountId = ctx.body?.accountId;
|
|
77
85
|
const session = inUse && typeof accountId === "string" ? await getSessionFromCtx(ctx) : null;
|
|
78
86
|
if (!session) return;
|
|
79
|
-
const mailbox =
|
|
80
|
-
if (mailbox?.address && mailbox.
|
|
87
|
+
const mailbox = linkedMailbox(linkedAccountsOf(await ctx.context.internalAdapter.findAccounts(session.user.id)), providers);
|
|
88
|
+
if (mailbox?.account.address && mailbox.account.id === accountId) await inUse.refuse(mailbox.account.address, headersLocale(ctx.headers));
|
|
81
89
|
})
|
|
82
90
|
}] },
|
|
83
91
|
endpoints: {
|
|
@@ -85,12 +93,9 @@ const mailSendersPlugin = (providers, names, inUse, checks) => ({
|
|
|
85
93
|
method: "GET",
|
|
86
94
|
use: [sessionMiddleware]
|
|
87
95
|
}, async (ctx) => {
|
|
88
|
-
const
|
|
89
|
-
const [rows, accounts] = await Promise.all([sendersOf(ctx.context.adapter, userId), ctx.context.internalAdapter.findAccounts(userId)]);
|
|
90
|
-
const address = mailboxOf(accounts, providers)?.address;
|
|
96
|
+
const { book } = await bookOf(ctx, providers);
|
|
91
97
|
return ctx.json({
|
|
92
|
-
...
|
|
93
|
-
senders: rows.map(listed),
|
|
98
|
+
...listedMailSenders(book),
|
|
94
99
|
checks: Boolean(checks)
|
|
95
100
|
});
|
|
96
101
|
}),
|
|
@@ -98,73 +103,82 @@ const mailSendersPlugin = (providers, names, inUse, checks) => ({
|
|
|
98
103
|
method: "POST",
|
|
99
104
|
use: [sessionMiddleware],
|
|
100
105
|
body: z$1.object({
|
|
101
|
-
address:
|
|
106
|
+
address: senderAddressInput,
|
|
102
107
|
name: nameField.optional(),
|
|
103
|
-
signature: signatureField.optional()
|
|
108
|
+
signature: signatureField.optional(),
|
|
109
|
+
connection: idField.optional()
|
|
104
110
|
})
|
|
105
111
|
}, async (ctx) => {
|
|
106
|
-
const userId = ctx
|
|
107
|
-
const address = ctx.body
|
|
108
|
-
const
|
|
109
|
-
if (
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
112
|
+
const { userId, store, book } = await bookOf(ctx, providers);
|
|
113
|
+
const { address, name = null, signature = null, connection } = ctx.body;
|
|
114
|
+
const chosen = connection === void 0 ? linkedMailbox(book.connections.map(({ account }) => account), providers) : book.connections.find(({ account }) => account.id === connection);
|
|
115
|
+
if (!chosen) throw new APIError(connection === void 0 ? "BAD_REQUEST" : "NOT_FOUND", noMailbox);
|
|
116
|
+
const live = book.rows.filter((row) => !isRemoved(row));
|
|
117
|
+
if (live.length >= 50) throw new APIError("BAD_REQUEST", tooMany);
|
|
118
|
+
if (live.some((row) => row.connectionId === chosen.account.id && row.address === address && row.typedAt !== null)) throw new APIError("BAD_REQUEST", taken);
|
|
119
|
+
const row = await store.type({
|
|
113
120
|
userId,
|
|
121
|
+
connectionId: chosen.account.id,
|
|
114
122
|
address,
|
|
115
123
|
name,
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
124
|
+
signature
|
|
125
|
+
}, /* @__PURE__ */ new Date());
|
|
126
|
+
return ctx.json({ sender: listedSender({
|
|
127
|
+
...book,
|
|
128
|
+
rows: [row]
|
|
129
|
+
}, row) });
|
|
122
130
|
}),
|
|
123
131
|
updateMailSender: createAuthEndpoint(mailSenderPaths.update, {
|
|
124
132
|
method: "POST",
|
|
125
133
|
use: [sessionMiddleware],
|
|
126
134
|
body: z$1.object({
|
|
127
|
-
id:
|
|
135
|
+
id: idField,
|
|
128
136
|
name: nameField.nullable().optional(),
|
|
129
137
|
signature: signatureField.nullable().optional(),
|
|
130
138
|
isDefault: z$1.boolean().optional()
|
|
131
139
|
})
|
|
132
140
|
}, async (ctx) => {
|
|
133
|
-
const userId = ctx
|
|
141
|
+
const { userId, store, book } = await bookOf(ctx, providers);
|
|
134
142
|
const { id, isDefault, ...update } = ctx.body;
|
|
135
|
-
|
|
136
|
-
if (
|
|
137
|
-
const
|
|
143
|
+
ownRow(book.rows, id);
|
|
144
|
+
if (isDefault === true) await store.clearDefault(userId);
|
|
145
|
+
const row = await store.change(userId, id, {
|
|
138
146
|
...update,
|
|
139
147
|
...isDefault !== void 0 && { isDefault }
|
|
140
|
-
};
|
|
141
|
-
if (Object.keys(changes).length === 0) return ctx.json({ sender: listed(found) });
|
|
142
|
-
if (isDefault === true) await clearDefault(ctx.context.adapter, userId);
|
|
143
|
-
const row = await changeSender(ctx.context.adapter, userId, id, changes);
|
|
148
|
+
});
|
|
144
149
|
if (!row) throw new APIError("NOT_FOUND", notFound);
|
|
145
|
-
return ctx.json({ sender:
|
|
150
|
+
return ctx.json({ sender: listedSender(book, row) });
|
|
146
151
|
}),
|
|
147
152
|
removeMailSender: createAuthEndpoint(mailSenderPaths.remove, {
|
|
148
153
|
method: "POST",
|
|
149
154
|
use: [sessionMiddleware],
|
|
150
|
-
body: z$1.object({ id:
|
|
155
|
+
body: z$1.object({ id: idField })
|
|
151
156
|
}, async (ctx) => {
|
|
152
|
-
const userId = ctx
|
|
153
|
-
const
|
|
154
|
-
if (
|
|
155
|
-
await
|
|
157
|
+
const { userId, store, book } = await bookOf(ctx, providers);
|
|
158
|
+
const row = ownRow(book.rows, ctx.body.id);
|
|
159
|
+
if (inUse) await inUse.refuse(row.address, headersLocale(ctx.headers));
|
|
160
|
+
await store.remove(userId, ctx.body.id, /* @__PURE__ */ new Date());
|
|
156
161
|
return ctx.json({ removed: true });
|
|
157
162
|
}),
|
|
163
|
+
refreshMailSenders: createAuthEndpoint(mailSenderPaths.refresh, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
use: [sessionMiddleware]
|
|
166
|
+
}, async (ctx) => {
|
|
167
|
+
const connections = await mailOf(ctx.context.session.user.id).refresh();
|
|
168
|
+
return ctx.json({ connections });
|
|
169
|
+
}),
|
|
158
170
|
checkMailSender: createAuthEndpoint(mailSenderPaths.check, {
|
|
159
171
|
method: "POST",
|
|
160
172
|
use: [sessionMiddleware],
|
|
161
|
-
body: z$1.object({ id:
|
|
173
|
+
body: z$1.object({ id: idField })
|
|
162
174
|
}, async (ctx) => {
|
|
163
|
-
const userId = ctx
|
|
164
|
-
const
|
|
165
|
-
if (!found) throw new APIError("NOT_FOUND", notFound);
|
|
175
|
+
const { userId, book } = await bookOf(ctx, providers);
|
|
176
|
+
const row = ownRow(book.rows, ctx.body.id);
|
|
166
177
|
if (!checks) return ctx.json({ status: "unchecked" });
|
|
167
|
-
const check = await checks.start(userId,
|
|
178
|
+
const check = await checks.start(userId, {
|
|
179
|
+
id: row.id,
|
|
180
|
+
address: row.address
|
|
181
|
+
}, headersLocale(ctx.headers));
|
|
168
182
|
return ctx.json({
|
|
169
183
|
status: "waiting",
|
|
170
184
|
check
|
|
@@ -174,14 +188,14 @@ const mailSendersPlugin = (providers, names, inUse, checks) => ({
|
|
|
174
188
|
method: "POST",
|
|
175
189
|
use: [sessionMiddleware],
|
|
176
190
|
body: z$1.object({
|
|
177
|
-
id:
|
|
191
|
+
id: idField,
|
|
178
192
|
check: z$1.string().min(1).max(200)
|
|
179
193
|
})
|
|
180
194
|
}, async (ctx) => {
|
|
181
|
-
const
|
|
182
|
-
|
|
195
|
+
const { book } = await bookOf(ctx, providers);
|
|
196
|
+
const row = ownRow(book.rows, ctx.body.id);
|
|
183
197
|
if (!checks) return ctx.json({ status: "unchecked" });
|
|
184
|
-
return ctx.json(await checks.result(ctx.body.check,
|
|
198
|
+
return ctx.json(await checks.result(ctx.body.check, row.address));
|
|
185
199
|
})
|
|
186
200
|
}
|
|
187
201
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./linked-accounts.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
//#region src/personal-mail/mailbox-connections.ts
|
|
2
|
+
/** The connections among `linked`, each account with the first of `providers` whose send scopes it holds. */
|
|
3
|
+
const mailboxConnections = (linked, providers) => linked.flatMap((account) => {
|
|
4
|
+
const provider = providers.find((candidate) => candidate.signInProvider === account.providerId && candidate.scopes.every((scope) => account.scopes.includes(scope)));
|
|
5
|
+
return provider ? [{
|
|
6
|
+
account,
|
|
7
|
+
provider
|
|
8
|
+
}] : [];
|
|
9
|
+
});
|
|
10
|
+
//#endregion
|
|
11
|
+
export { mailboxConnections };
|
|
@@ -19,6 +19,13 @@ export type PersonalMailProvider = {
|
|
|
19
19
|
signInProvider: string;
|
|
20
20
|
/** The scopes linking asks for, which the account must hold to send. */
|
|
21
21
|
scopes: string[];
|
|
22
|
+
/**
|
|
23
|
+
* The scopes an app may add to the link for more abilities, such as reading the mailbox's send-as addresses; an app
|
|
24
|
+
* names those it wants in `auth.personalMail`, and a scope not listed here stops startup.
|
|
25
|
+
*/
|
|
26
|
+
optionalScopes: string[];
|
|
27
|
+
/** The optional scopes this app adds to the link (`auth.personalMail`): none until the app names them. */
|
|
28
|
+
addedScopes: string[];
|
|
22
29
|
/** More authorization parameters of the link, such as those that ask for a refresh token. */
|
|
23
30
|
linkParams: Record<string, string>;
|
|
24
31
|
/**
|
|
@@ -27,5 +34,30 @@ export type PersonalMailProvider = {
|
|
|
27
34
|
* sees when the provider can tell it.
|
|
28
35
|
*/
|
|
29
36
|
send: (message: MailMessage, accessToken: string, from?: MailFrom) => Promise<SentMail>;
|
|
37
|
+
/**
|
|
38
|
+
* The From addresses the mailbox may send as, each with what the provider says of it, when the provider can list
|
|
39
|
+
* them; a provider without the ability leaves it out, and callers tell it by its absence. Answers `not_permitted`
|
|
40
|
+
* with the scopes it lacks when the connection does not hold them, and rejects with a `PersonalMailError` of
|
|
41
|
+
* `link_expired` or a `SenderListingError` when the listing fails or comes back incomplete, recording nothing.
|
|
42
|
+
*/
|
|
43
|
+
list_sender_addresses?: (connection: ListingConnection) => Promise<SenderAddressListing>;
|
|
44
|
+
};
|
|
45
|
+
/** A connected mailbox as a provider's abilities read it: a valid access token and the scopes the link holds. */
|
|
46
|
+
export type ListingConnection = {
|
|
47
|
+
accessToken: string;
|
|
48
|
+
scopes: string[];
|
|
49
|
+
};
|
|
50
|
+
/** What a provider says of a listed address: `verified` when it may send as it, `pending` while its owner still has to confirm it. */
|
|
51
|
+
export type ProviderAddressState = 'verified' | 'pending';
|
|
52
|
+
/** A provider's listing: every address it lists, complete, or the scopes the connection lacks to list them. */
|
|
53
|
+
export type SenderAddressListing = {
|
|
54
|
+
kind: 'listed';
|
|
55
|
+
addresses: {
|
|
56
|
+
address: string;
|
|
57
|
+
provider_state?: ProviderAddressState;
|
|
58
|
+
}[];
|
|
59
|
+
} | {
|
|
60
|
+
kind: 'not_permitted';
|
|
61
|
+
missing_scopes: string[];
|
|
30
62
|
};
|
|
31
63
|
//#endregion
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
//#region src/personal-mail/personal-mail-provider.ts
|
|
2
|
+
/** Thrown when a provider's listing fails or comes back incomplete: nothing is recorded of it. */
|
|
3
|
+
var SenderListingError = class extends Error {
|
|
4
|
+
code = "listing_failed";
|
|
5
|
+
};
|
|
6
|
+
//#endregion
|
|
7
|
+
export { SenderListingError };
|
|
@@ -5,4 +5,13 @@ export declare const personalMailProviders: {
|
|
|
5
5
|
gmail: (fetcher?: typeof fetch) => PersonalMailProvider;
|
|
6
6
|
};
|
|
7
7
|
export type PersonalMailProviderId = keyof typeof personalMailProviders;
|
|
8
|
+
/**
|
|
9
|
+
* A mail provider people may link, as `auth.personalMail` names it: its id alone, such as `'gmail'`, or with optional
|
|
10
|
+
* scopes the link adds for more abilities, such as `{ provider: 'gmail', scopes: [gmailSettingsScope] }` to read the
|
|
11
|
+
* mailbox's send-as addresses.
|
|
12
|
+
*/
|
|
13
|
+
export type PersonalMailEntry = PersonalMailProviderId | {
|
|
14
|
+
provider: PersonalMailProviderId;
|
|
15
|
+
scopes?: string[];
|
|
16
|
+
};
|
|
8
17
|
//#endregion
|
|
@@ -3,17 +3,24 @@ import { gmail } from "./gmail.js";
|
|
|
3
3
|
/** The mail providers Protobase links mailboxes with, by the id `auth.personalMail` names them with. */
|
|
4
4
|
const personalMailProviders = { gmail };
|
|
5
5
|
/**
|
|
6
|
-
* The providers of `
|
|
6
|
+
* The providers of `entries`, each checked against the app's sign-in providers: linking one goes through its sign-in
|
|
7
7
|
* provider, so an app without it is a configuration error at startup. So is one whose OAuth tokens are not encrypted
|
|
8
8
|
* (`encryptOAuthTokens`): a linked account's tokens send mail as the person, so they are never stored in plain text.
|
|
9
|
+
* So is an added scope the provider does not offer, as a link must never ask for more than its abilities need.
|
|
9
10
|
*/
|
|
10
|
-
const resolvePersonalMail = (
|
|
11
|
-
if (
|
|
12
|
-
return
|
|
11
|
+
const resolvePersonalMail = (entries, signInProviders, encryptOAuthTokens) => {
|
|
12
|
+
if (entries.length > 0 && !encryptOAuthTokens) throw new Error("auth.personalMail: the linked mailboxes' tokens would be stored in plain text; turn on auth.encryptOAuthTokens");
|
|
13
|
+
return entries.map((entry) => {
|
|
14
|
+
const { provider: id, scopes = [] } = typeof entry === "string" ? { provider: entry } : entry;
|
|
13
15
|
if (!Object.hasOwn(personalMailProviders, id)) throw new Error(`auth.personalMail: "${id}" is not a mail provider; use one of ${Object.keys(personalMailProviders).join(", ")}`);
|
|
14
16
|
const provider = personalMailProviders[id]();
|
|
15
17
|
if (!signInProviders.includes(provider.signInProvider)) throw new Error(`auth.personalMail: ${provider.name} links through the sign-in provider "${provider.signInProvider}"; add it to auth.socialProviders`);
|
|
16
|
-
|
|
18
|
+
const unknown = scopes.find((scope) => !provider.optionalScopes.includes(scope));
|
|
19
|
+
if (unknown) throw new Error(`auth.personalMail: ${provider.name} does not take the scope "${unknown}"; use one of ${provider.optionalScopes.join(", ")}`);
|
|
20
|
+
return {
|
|
21
|
+
...provider,
|
|
22
|
+
addedScopes: [...new Set(scopes)]
|
|
23
|
+
};
|
|
17
24
|
});
|
|
18
25
|
};
|
|
19
26
|
//#endregion
|
|
@@ -1,23 +1,32 @@
|
|
|
1
1
|
import { MailMessage, SentMail } from "../mail/mail-message.js";
|
|
2
|
-
import "./linked-accounts.js";
|
|
3
2
|
import "./sender-store.js";
|
|
3
|
+
import "./linked-accounts.js";
|
|
4
4
|
import { MailSender } from "./mail-sender.js";
|
|
5
|
+
import { ConnectionListing } from "./sender-listing.js";
|
|
5
6
|
//#region src/personal-mail/personal-mailer.d.ts
|
|
6
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* A message from someone's own mailbox: from the From address record `sender_address_id` names, null for the
|
|
9
|
+
* mailbox's own address; without it, from their default address, else their mailbox's own.
|
|
10
|
+
*/
|
|
7
11
|
export type PersonalMailMessage = MailMessage & {
|
|
8
|
-
|
|
12
|
+
sender_address_id?: string | null;
|
|
9
13
|
};
|
|
10
14
|
/** Mail from someone's own mailbox, and the From addresses they may send it from. */
|
|
11
15
|
export type PersonalMail = {
|
|
12
16
|
/**
|
|
13
|
-
* Sends `message`
|
|
14
|
-
*
|
|
15
|
-
*
|
|
17
|
+
* Sends `message` through the mailbox connection of the From address it names, from that address, with its display
|
|
18
|
+
* name: the record decides both, so they cannot disagree. Rejects, sending nothing, with a 404 problem for an id
|
|
19
|
+
* that is not one of the person's, and with a `SenderAddressError` for an address the provider says the mailbox
|
|
20
|
+
* cannot send as (`failed`), one still `pending`, or one on no connection they can send through (`detached`). An
|
|
21
|
+
* address no listing read yet is listed first, when its provider can. Resolves to the `Message-ID` the recipient
|
|
22
|
+
* sees when the mail provider can tell it (see `SentMail`).
|
|
16
23
|
*/
|
|
17
24
|
send: (message: PersonalMailMessage) => Promise<SentMail>;
|
|
18
|
-
/** The sender a
|
|
19
|
-
sender: (
|
|
20
|
-
/** Every sender the person may choose, as a send dialog lists them. */
|
|
25
|
+
/** The sender a From address stands for, with its record's id and own signature, or refuses as `chooseSender` does. */
|
|
26
|
+
sender: (address?: string) => Promise<MailSender>;
|
|
27
|
+
/** Every sender the person may choose, as a send dialog lists them, each with its state. */
|
|
21
28
|
senders: () => Promise<MailSender[]>;
|
|
29
|
+
/** Lists the send-as addresses of each of the person's connections whose provider can, each on its own (`listConnections`). */
|
|
30
|
+
refresh: () => Promise<ConnectionListing[]>;
|
|
22
31
|
};
|
|
23
32
|
//#endregion
|
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { PersonalMailError } from "./personal-mail-error.js";
|
|
2
|
+
import { SenderAddressError, SenderAddressNotFoundError } from "./sender-address-error.js";
|
|
3
|
+
import { hasListing, isRemoved, senderRowState } from "./sender-address-state.js";
|
|
2
4
|
import { chooseSender, usableSenders } from "./mail-sender.js";
|
|
5
|
+
import { mailboxConnections } from "./mailbox-connections.js";
|
|
6
|
+
import { listConnection, listConnections } from "./sender-listing.js";
|
|
3
7
|
//#region src/personal-mail/personal-mailer.ts
|
|
4
8
|
/**
|
|
5
9
|
* The mail provider of `linked`, the accounts linked to a person, of the app's `providers` in their order, with the
|
|
@@ -16,30 +20,68 @@ const linkedMailbox = (linked, providers) => {
|
|
|
16
20
|
};
|
|
17
21
|
}
|
|
18
22
|
};
|
|
19
|
-
|
|
20
|
-
const
|
|
23
|
+
const refuseOwnFrom = (message) => {
|
|
24
|
+
const named = [...Object.keys(message), ...Object.keys(message.headers ?? {})].find((name) => ["from", "sender"].includes(name.toLowerCase()));
|
|
25
|
+
if (named) throw new Error(`personalMail.send takes the From address as sender_address_id, never as "${named}"`);
|
|
26
|
+
};
|
|
21
27
|
/**
|
|
22
|
-
* Mail from `userId`'s own mailbox: each message goes out through the
|
|
23
|
-
* refreshed when it expired, from a From address
|
|
24
|
-
*
|
|
28
|
+
* Mail from `userId`'s own mailbox: each message goes out through one of the connections they linked, with an access
|
|
29
|
+
* token refreshed when it expired, from a From address record of theirs. Rejects with a `PersonalMailError` and sends
|
|
30
|
+
* nothing when the mailbox it goes through is not linked (`not_linked`) or the provider no longer honours the link (`link_expired`).
|
|
25
31
|
*/
|
|
26
|
-
const personalMailer = (accounts, providers, senders, userId) => {
|
|
27
|
-
const
|
|
28
|
-
|
|
32
|
+
const personalMailer = (accounts, providers, senders, userId, now = () => /* @__PURE__ */ new Date()) => {
|
|
33
|
+
const lister = {
|
|
34
|
+
accounts,
|
|
35
|
+
senders,
|
|
36
|
+
userId,
|
|
37
|
+
now
|
|
38
|
+
};
|
|
39
|
+
const bookOf = async () => {
|
|
40
|
+
const [rows, linked] = await Promise.all([senders.ofUser(userId), accounts.accounts(userId)]);
|
|
41
|
+
const ownAddress = linkedMailbox(linked, providers)?.account.address;
|
|
42
|
+
return {
|
|
43
|
+
userId,
|
|
44
|
+
rows,
|
|
45
|
+
connections: mailboxConnections(linked, providers),
|
|
46
|
+
...ownAddress && { ownAddress }
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const sendThrough = async ({ account, provider }, message, row) => {
|
|
50
|
+
const accessToken = await accounts.accessToken(userId, account, provider.id);
|
|
51
|
+
return provider.send(message, accessToken, row && {
|
|
52
|
+
address: row.address,
|
|
53
|
+
...row.name && { name: row.name }
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
const sendAsMailbox = async (book, message) => {
|
|
57
|
+
const linked = linkedMailbox(book.connections.map(({ account }) => account), providers);
|
|
58
|
+
if (!linked) throw new PersonalMailError("not_linked");
|
|
59
|
+
return sendThrough(linked, message);
|
|
60
|
+
};
|
|
61
|
+
const checked = async (row, connection) => {
|
|
62
|
+
if (!connection.provider.list_sender_addresses || hasListing(row)) return row;
|
|
63
|
+
if ((await listConnection(lister, connection)).status !== "listed") return row;
|
|
64
|
+
return (await senders.ofUser(userId)).find((candidate) => candidate.id === row.id) ?? row;
|
|
65
|
+
};
|
|
29
66
|
return {
|
|
30
|
-
send: async ({
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
if (
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
67
|
+
send: async ({ sender_address_id, ...message }) => {
|
|
68
|
+
refuseOwnFrom(message);
|
|
69
|
+
const book = await bookOf();
|
|
70
|
+
if (sender_address_id === null) return sendAsMailbox(book, message);
|
|
71
|
+
const row = sender_address_id === void 0 ? book.rows.find((candidate) => candidate.isDefault && !isRemoved(candidate)) : book.rows.find((candidate) => candidate.id === sender_address_id);
|
|
72
|
+
if (!row && sender_address_id === void 0) return sendAsMailbox(book, message);
|
|
73
|
+
if (!row || isRemoved(row)) throw new SenderAddressNotFoundError();
|
|
74
|
+
const connection = book.connections.find(({ account }) => account.id === row.connectionId);
|
|
75
|
+
if (!connection) throw new SenderAddressError("sender_address_detached");
|
|
76
|
+
const state = senderRowState(await checked(row, connection), true);
|
|
77
|
+
if (state === "failed") throw new SenderAddressError("sender_address_failed");
|
|
78
|
+
if (state === "pending") throw new SenderAddressError("sender_address_pending");
|
|
79
|
+
return sendThrough(connection, message, row);
|
|
39
80
|
},
|
|
40
|
-
sender,
|
|
41
|
-
senders: () => usableSenders(
|
|
81
|
+
sender: async (address) => chooseSender(await bookOf(), address),
|
|
82
|
+
senders: async () => usableSenders(await bookOf()),
|
|
83
|
+
refresh: async () => listConnections(lister, (await bookOf()).connections)
|
|
42
84
|
};
|
|
43
85
|
};
|
|
44
86
|
//#endregion
|
|
45
|
-
export { linkedMailbox,
|
|
87
|
+
export { linkedMailbox, personalMailer };
|