@aglyn/tenant-data-admin 1.0.0-beta.270 → 1.0.0-beta.272
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/package.json +7 -7
- package/src/lib/server/append-only-history-write.d.ts +38 -0
- package/src/lib/server/append-only-history-write.js +60 -0
- package/src/lib/server/append-only-history-write.js.map +1 -0
- package/src/lib/server/billing-addons.d.ts +58 -32
- package/src/lib/server/billing-addons.js +100 -86
- package/src/lib/server/billing-addons.js.map +1 -1
- package/src/lib/server/bundle-links.d.ts +166 -0
- package/src/lib/server/bundle-links.js +286 -0
- package/src/lib/server/bundle-links.js.map +1 -0
- package/src/lib/server/client-build.d.ts +74 -0
- package/src/lib/server/client-build.js +264 -0
- package/src/lib/server/client-build.js.map +1 -0
- package/src/lib/server/domain-ownership.d.ts +205 -0
- package/src/lib/server/domain-ownership.js +575 -0
- package/src/lib/server/domain-ownership.js.map +1 -0
- package/src/lib/server/erase.d.ts +17 -0
- package/src/lib/server/erase.js +21 -0
- package/src/lib/server/erase.js.map +1 -1
- package/src/lib/server/host-memberships.d.ts +35 -2
- package/src/lib/server/host-memberships.js +63 -2
- package/src/lib/server/host-memberships.js.map +1 -1
- package/src/lib/server/org-billing.d.ts +4 -2
- package/src/lib/server/org-billing.js +5 -0
- package/src/lib/server/org-billing.js.map +1 -1
- package/src/lib/server/site-care-emails.d.ts +53 -0
- package/src/lib/server/site-care-emails.js +97 -0
- package/src/lib/server/site-care-emails.js.map +1 -0
- package/src/lib/server/site-care-inbound.d.ts +123 -0
- package/src/lib/server/site-care-inbound.js +197 -0
- package/src/lib/server/site-care-inbound.js.map +1 -0
- package/src/lib/server/site-care-project-reconcile.d.ts +64 -0
- package/src/lib/server/site-care-project-reconcile.js +211 -0
- package/src/lib/server/site-care-project-reconcile.js.map +1 -0
- package/src/lib/server/site-care-project-stripe.d.ts +114 -0
- package/src/lib/server/site-care-project-stripe.js +305 -0
- package/src/lib/server/site-care-project-stripe.js.map +1 -0
- package/src/lib/server/site-care-projects.d.ts +169 -0
- package/src/lib/server/site-care-projects.js +882 -0
- package/src/lib/server/site-care-projects.js.map +1 -0
- package/src/lib/server/site-care-reconcile.d.ts +81 -0
- package/src/lib/server/site-care-reconcile.js +349 -0
- package/src/lib/server/site-care-reconcile.js.map +1 -0
- package/src/lib/server/site-care-requests.d.ts +243 -0
- package/src/lib/server/site-care-requests.js +1414 -0
- package/src/lib/server/site-care-requests.js.map +1 -0
- package/src/lib/server/site-care-stripe.d.ts +175 -0
- package/src/lib/server/site-care-stripe.js +375 -0
- package/src/lib/server/site-care-stripe.js.map +1 -0
- package/src/lib/server/sso-provisioning.d.ts +6 -0
- package/src/lib/server/sso-provisioning.js +7 -0
- package/src/lib/server/sso-provisioning.js.map +1 -1
- package/src/lib/server/upgrade-proposal.d.ts +40 -1
- package/src/lib/server/upgrade-proposal.js +92 -50
- package/src/lib/server/upgrade-proposal.js.map +1 -1
- package/src/tenancy.d.ts +1 -0
- package/src/tenancy.js +2 -0
- package/src/tenancy.js.map +1 -1
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
2
|
+
/**
|
|
3
|
+
* @license
|
|
4
|
+
* Copyright 2026 Aglyn LLC
|
|
5
|
+
*
|
|
6
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
* you may not use this file except in compliance with the License.
|
|
8
|
+
* You may obtain a copy of the License at
|
|
9
|
+
*
|
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
*
|
|
12
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
* See the License for the specific language governing permissions and
|
|
16
|
+
* limitations under the License.
|
|
17
|
+
*/ /*==========================================
|
|
18
|
+
* DOMAIN OWNERSHIP CLAIMS (AGL-3974, parent AGL-3973).
|
|
19
|
+
*
|
|
20
|
+
* "This workspace proved it controls example.com." A site import pulls a
|
|
21
|
+
* person's existing website into Aglyn, and an import is a one-click clone
|
|
22
|
+
* of whatever address is typed, so the content and images are pulled only
|
|
23
|
+
* after the workspace has proved the domain is its own. Payment alone is not
|
|
24
|
+
* a control: the first paying customer was a phisher on a stolen card.
|
|
25
|
+
*
|
|
26
|
+
* ## Why this is not the SSO claim
|
|
27
|
+
*
|
|
28
|
+
* `orgs/{orgId}/ssoDomains/{domain}` (sso-provisioning.ts) is one org per
|
|
29
|
+
* domain, because a verified SSO domain ROUTES every sign-in on it. Owning a
|
|
30
|
+
* site is not exclusive in that way: an agency and its client may both hold
|
|
31
|
+
* a proof for the client's domain, and neither proof routes anything. So the
|
|
32
|
+
* claim here is keyed by (org, domain) and carries no global index:
|
|
33
|
+
* `orgs/{orgId}/domainOwnershipClaims/{domain}`. Rules: an owner or admin of
|
|
34
|
+
* the org reads it (it holds the token they publish); only this module, over
|
|
35
|
+
* the Admin SDK, writes it.
|
|
36
|
+
*
|
|
37
|
+
* ## Three ways to prove it, any one of which is enough
|
|
38
|
+
*
|
|
39
|
+
* - **DNS**: a TXT record `aglyn-domain-verification=<token>` at
|
|
40
|
+
* `_aglyn-challenge.<domain>`, the same host and prefix SSO uses, matched as
|
|
41
|
+
* a whole record (`recordsProveOwnership`).
|
|
42
|
+
* - **Meta tag**: `<meta name="aglyn-site-verification" content="<token>">` on
|
|
43
|
+
* the home page, read by the caller through the site-import crawler's safe
|
|
44
|
+
* fetch and handed in as `readVerificationMeta`; this module never opens a
|
|
45
|
+
* socket to a site.
|
|
46
|
+
* - **Email**: a one-time code sent to an address AT the domain (the person
|
|
47
|
+
* types owner@theirdomain.com). Reading mail there is control of the
|
|
48
|
+
* domain's mail. The code is stored only as an HMAC under the server's
|
|
49
|
+
* signing secret, expires in {@link DOMAIN_OWNERSHIP_CODE_TTL_MS}, and is
|
|
50
|
+
* burned after {@link DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS} wrong tries. The
|
|
51
|
+
* HMAC matters because an owner or admin can read the claim document: a
|
|
52
|
+
* plain hash of a six-digit code is reversed by trying a million values.
|
|
53
|
+
*
|
|
54
|
+
* ## Three answers, never two
|
|
55
|
+
*
|
|
56
|
+
* Every check answers `proven`, `not-yet` or `unreachable`. `unreachable`
|
|
57
|
+
* (nobody answered the DNS question, the home page could not be read) is
|
|
58
|
+
* evidence of nothing, the same third state `dns-probe.ts` exists for, and it
|
|
59
|
+
* never demotes a proof already recorded.
|
|
60
|
+
*=========================================*/ import { createHmac, randomBytes, randomInt } from "crypto";
|
|
61
|
+
import { resolveBrandingProfile } from "@aglyn/aglyn/server";
|
|
62
|
+
import { isEmailConfigured, sendEmail } from "@aglyn/shared-util-email";
|
|
63
|
+
import { lookalikeBrandForHost } from "@aglyn/shared-util-email/outbound-phishing-screen";
|
|
64
|
+
import { lookupTxt as defaultLookupTxt } from "./dns-probe.js";
|
|
65
|
+
import firebaseAdmin from "./firebase-admin.js";
|
|
66
|
+
import { tokenSigningSecret } from "./media-signing.js";
|
|
67
|
+
import { safeEqual } from "./safe-equal.js";
|
|
68
|
+
import { challengeValue, isPublicEmailDomain, recordsProveOwnership, SSO_CHALLENGE_PREFIX } from "./sso-provisioning.js";
|
|
69
|
+
/** The subcollection under `orgs/{orgId}`; the document id is the domain. */ export const DOMAIN_OWNERSHIP_CLAIMS_COLLECTION = 'domainOwnershipClaims';
|
|
70
|
+
/** The `name` of the home page meta tag that carries the token. */ export const DOMAIN_OWNERSHIP_META_NAME = 'aglyn-site-verification';
|
|
71
|
+
/** The system email that carries the one-time code. */ export const DOMAIN_OWNERSHIP_CODE_EMAIL_KEY = 'domain-ownership-code';
|
|
72
|
+
/** How long an emailed code works. */ export const DOMAIN_OWNERSHIP_CODE_TTL_MS = 15 * 60 * 1000;
|
|
73
|
+
/** Wrong codes allowed before the code is burned and a new one must be sent. */ export const DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS = 5;
|
|
74
|
+
/** The least time between two codes for one claim. */ export const DOMAIN_OWNERSHIP_CODE_RESEND_MS = 60 * 1000;
|
|
75
|
+
/** Codes one workspace may send in an hour, across every claim. */ export const DOMAIN_OWNERSHIP_CODES_PER_ORG_PER_HOUR = 10;
|
|
76
|
+
/** Codes one address may receive in an hour, from every workspace. */ export const DOMAIN_OWNERSHIP_CODES_PER_ADDRESS_PER_HOUR = 5;
|
|
77
|
+
/**
|
|
78
|
+
* How long a proof stands before an import asks for it again. A domain can
|
|
79
|
+
* lapse and be bought by someone else; a month is short enough that the new
|
|
80
|
+
* owner's content is not imported on the old owner's proof.
|
|
81
|
+
*/ export const DOMAIN_OWNERSHIP_PROOF_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000;
|
|
82
|
+
const DOMAIN_PATTERN = /^(?!-)[a-z0-9-]{1,63}(\.[a-z0-9-]{1,63})+$/;
|
|
83
|
+
const EMAIL_LOCAL_PATTERN = /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]{1,64}$/i;
|
|
84
|
+
/*------------------------------------------
|
|
85
|
+
* Pure helpers
|
|
86
|
+
*-----------------------------------------*/ /**
|
|
87
|
+
* The domain a claim is about: lowercased, any scheme, path, port or address
|
|
88
|
+
* prefix a person pasted removed, and a leading `www.` folded into the bare
|
|
89
|
+
* name, because the site's two names are one site to its owner and the
|
|
90
|
+
* crawler treats them as one origin.
|
|
91
|
+
*/ export function normalizeOwnershipDomain(input) {
|
|
92
|
+
var _raw_split_;
|
|
93
|
+
let raw = String(input != null ? input : '').trim().toLowerCase();
|
|
94
|
+
const at = raw.lastIndexOf('@');
|
|
95
|
+
if (at >= 0 && !raw.includes('/')) raw = raw.slice(at + 1);
|
|
96
|
+
raw = raw.replace(/^[a-z][a-z0-9+.-]*:\/\//, '');
|
|
97
|
+
raw = (_raw_split_ = raw.split(/[/?#]/)[0]) != null ? _raw_split_ : '';
|
|
98
|
+
raw = raw.replace(/:\d+$/, '').replace(/\.+$/, '').replace(/^www\./, '');
|
|
99
|
+
if (!raw || !DOMAIN_PATTERN.test(raw)) {
|
|
100
|
+
return {
|
|
101
|
+
domain: null,
|
|
102
|
+
error: 'Enter your website’s address, for example example.com'
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (isPublicEmailDomain(raw)) {
|
|
106
|
+
return {
|
|
107
|
+
domain: null,
|
|
108
|
+
error: 'That is an email provider’s domain, not a website you own.'
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (lookalikeBrandForHost(raw)) {
|
|
112
|
+
return {
|
|
113
|
+
domain: null,
|
|
114
|
+
error: 'That address looks like another company’s website.'
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
domain: raw,
|
|
119
|
+
error: null
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** The TXT record host for a domain. */ export function domainOwnershipRecordHost(domain) {
|
|
123
|
+
return `${SSO_CHALLENGE_PREFIX}.${domain}`;
|
|
124
|
+
}
|
|
125
|
+
/** The meta tag a person pastes into their home page's `<head>`. */ export function domainOwnershipMetaTag(token) {
|
|
126
|
+
return `<meta name="${DOMAIN_OWNERSHIP_META_NAME}" content="${token}">`;
|
|
127
|
+
}
|
|
128
|
+
/** Exact, whole-value match, for the reason `recordsProveOwnership` gives. */ export function metaValuesProveOwnership(values, token) {
|
|
129
|
+
if (!token) return false;
|
|
130
|
+
return values.some((value)=>safeEqual(String(value != null ? value : '').trim(), token));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The address a code may go to: a plausible mailbox whose domain IS the
|
|
134
|
+
* claim's domain (the `www.` twin included), and nothing else. A parent or a
|
|
135
|
+
* sibling domain proves nothing about this one.
|
|
136
|
+
*/ export function ownershipEmailAddress(input, domain) {
|
|
137
|
+
const address = String(input != null ? input : '').trim().toLowerCase();
|
|
138
|
+
const at = address.lastIndexOf('@');
|
|
139
|
+
if (at <= 0) return null;
|
|
140
|
+
const local = address.slice(0, at);
|
|
141
|
+
const host = address.slice(at + 1);
|
|
142
|
+
if (!EMAIL_LOCAL_PATTERN.test(local) || local.startsWith('.') || local.endsWith('.')) {
|
|
143
|
+
return null;
|
|
144
|
+
}
|
|
145
|
+
if (host !== domain && host !== `www.${domain}`) return null;
|
|
146
|
+
return address;
|
|
147
|
+
}
|
|
148
|
+
/** HMAC of a code, bound to the claim and the address it was sent to. */ export function hashOwnershipCode(secret, input) {
|
|
149
|
+
return createHmac('sha256', secret).update(`domain-ownership-code\n${input.orgId}\n${input.domain}\n${input.address}\n${input.code}`).digest('hex');
|
|
150
|
+
}
|
|
151
|
+
/** Whether a stored proof is still one an import may rely on. */ export function isOwnershipProofCurrent(claim, now) {
|
|
152
|
+
if (!claim || claim.status !== 'proven' || typeof claim.provenAt !== 'number') return false;
|
|
153
|
+
return now - claim.provenAt <= DOMAIN_OWNERSHIP_PROOF_MAX_AGE_MS;
|
|
154
|
+
}
|
|
155
|
+
export function domainOwnershipClaimView(claim, now) {
|
|
156
|
+
var _claim_emailCode, _claim_provenMethod, _claim_provenAt, _claim_provenDetail, _claim_lastChecks, _ref, _ref1;
|
|
157
|
+
const code = (_claim_emailCode = claim.emailCode) != null ? _claim_emailCode : null;
|
|
158
|
+
const codeLive = Boolean(code && code.expiresAt > now && code.attempts < DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS);
|
|
159
|
+
return {
|
|
160
|
+
domain: claim.domain,
|
|
161
|
+
status: claim.status,
|
|
162
|
+
current: isOwnershipProofCurrent(claim, now),
|
|
163
|
+
provenMethod: (_claim_provenMethod = claim.provenMethod) != null ? _claim_provenMethod : null,
|
|
164
|
+
provenAt: (_claim_provenAt = claim.provenAt) != null ? _claim_provenAt : null,
|
|
165
|
+
provenDetail: (_claim_provenDetail = claim.provenDetail) != null ? _claim_provenDetail : null,
|
|
166
|
+
lastChecks: (_claim_lastChecks = claim.lastChecks) != null ? _claim_lastChecks : {},
|
|
167
|
+
dns: {
|
|
168
|
+
host: domainOwnershipRecordHost(claim.domain),
|
|
169
|
+
type: 'TXT',
|
|
170
|
+
value: challengeValue(claim.token)
|
|
171
|
+
},
|
|
172
|
+
meta: {
|
|
173
|
+
name: DOMAIN_OWNERSHIP_META_NAME,
|
|
174
|
+
content: claim.token,
|
|
175
|
+
tag: domainOwnershipMetaTag(claim.token)
|
|
176
|
+
},
|
|
177
|
+
email: {
|
|
178
|
+
domain: claim.domain,
|
|
179
|
+
pendingAddress: codeLive ? (_ref = code == null ? void 0 : code.address) != null ? _ref : null : null,
|
|
180
|
+
expiresAt: codeLive ? (_ref1 = code == null ? void 0 : code.expiresAt) != null ? _ref1 : null : null,
|
|
181
|
+
attemptsLeft: codeLive && code ? DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS - code.attempts : null
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
/*------------------------------------------
|
|
186
|
+
* Firestore
|
|
187
|
+
*-----------------------------------------*/ function resolved(deps) {
|
|
188
|
+
var _deps_firestore, _deps_lookupTxt, _deps_now, _deps_secret, _deps_mintToken, _deps_mintCode;
|
|
189
|
+
return {
|
|
190
|
+
firestore: (_deps_firestore = deps.firestore) != null ? _deps_firestore : firebaseAdmin.app().firestore(),
|
|
191
|
+
lookupTxt: (_deps_lookupTxt = deps.lookupTxt) != null ? _deps_lookupTxt : defaultLookupTxt,
|
|
192
|
+
now: (_deps_now = deps.now) != null ? _deps_now : Date.now,
|
|
193
|
+
secret: (_deps_secret = deps.secret) != null ? _deps_secret : tokenSigningSecret,
|
|
194
|
+
mintToken: (_deps_mintToken = deps.mintToken) != null ? _deps_mintToken : ()=>randomBytes(24).toString('base64url'),
|
|
195
|
+
mintCode: (_deps_mintCode = deps.mintCode) != null ? _deps_mintCode : ()=>String(randomInt(0, 1000000)).padStart(6, '0')
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
function claimRef(firestore, orgId, domain) {
|
|
199
|
+
return firestore.collection('orgs').doc(orgId).collection(DOMAIN_OWNERSHIP_CLAIMS_COLLECTION).doc(domain);
|
|
200
|
+
}
|
|
201
|
+
async function readClaim(firestore, orgId, domain) {
|
|
202
|
+
const snapshot = await claimRef(firestore, orgId, domain).get();
|
|
203
|
+
return snapshot.exists ? snapshot.data() : null;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Start (or re-read) a claim. Idempotent: an existing claim keeps its token,
|
|
207
|
+
* because a new one would break a record or tag the person already published.
|
|
208
|
+
*/ export async function issueDomainOwnershipClaim(orgId, rawDomain, uid, deps = {}) {
|
|
209
|
+
const { domain, error } = normalizeOwnershipDomain(rawDomain);
|
|
210
|
+
if (!domain) return {
|
|
211
|
+
claim: null,
|
|
212
|
+
error
|
|
213
|
+
};
|
|
214
|
+
const io = resolved(deps);
|
|
215
|
+
const ref = claimRef(io.firestore, orgId, domain);
|
|
216
|
+
const existing = await readClaim(io.firestore, orgId, domain);
|
|
217
|
+
if (existing == null ? void 0 : existing.token) return {
|
|
218
|
+
claim: domainOwnershipClaimView(existing, io.now()),
|
|
219
|
+
error: null
|
|
220
|
+
};
|
|
221
|
+
const claim = {
|
|
222
|
+
domain,
|
|
223
|
+
token: io.mintToken(),
|
|
224
|
+
status: 'pending',
|
|
225
|
+
createdAt: io.now(),
|
|
226
|
+
createdBy: uid,
|
|
227
|
+
lastChecks: {},
|
|
228
|
+
emailCode: null
|
|
229
|
+
};
|
|
230
|
+
await ref.set(claim);
|
|
231
|
+
return {
|
|
232
|
+
claim: domainOwnershipClaimView(claim, io.now()),
|
|
233
|
+
error: null
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
/** Every claim the org holds, newest first. */ export async function listDomainOwnershipClaims(orgId, deps = {}) {
|
|
237
|
+
const io = resolved(deps);
|
|
238
|
+
const snapshot = await io.firestore.collection('orgs').doc(orgId).collection(DOMAIN_OWNERSHIP_CLAIMS_COLLECTION).get();
|
|
239
|
+
return snapshot.docs.map((doc)=>doc.data()).sort((a, b)=>{
|
|
240
|
+
var _b_createdAt, _a_createdAt;
|
|
241
|
+
return ((_b_createdAt = b.createdAt) != null ? _b_createdAt : 0) - ((_a_createdAt = a.createdAt) != null ? _a_createdAt : 0);
|
|
242
|
+
}).map((claim)=>domainOwnershipClaimView(claim, io.now()));
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Whether the org holds a CURRENT proof for the domain: the one question an
|
|
246
|
+
* import asks before it pulls a page or an image.
|
|
247
|
+
*/ export async function isDomainOwnershipProven(orgId, rawDomain, deps = {}) {
|
|
248
|
+
const { domain } = normalizeOwnershipDomain(rawDomain);
|
|
249
|
+
if (!domain) return false;
|
|
250
|
+
const io = resolved(deps);
|
|
251
|
+
return isOwnershipProofCurrent(await readClaim(io.firestore, orgId, domain), io.now());
|
|
252
|
+
}
|
|
253
|
+
/** Writes one check's outcome; a proof is only ever added, never removed. */ async function recordCheck(io, orgId, claim, method, status, detail, extra = {}) {
|
|
254
|
+
var _claim_lastChecks;
|
|
255
|
+
const now = io.now();
|
|
256
|
+
const next = _extends({}, claim, extra, {
|
|
257
|
+
lastCheckedAt: now,
|
|
258
|
+
lastChecks: _extends({}, (_claim_lastChecks = claim.lastChecks) != null ? _claim_lastChecks : {}, {
|
|
259
|
+
[method]: status
|
|
260
|
+
})
|
|
261
|
+
}, status === 'proven' ? {
|
|
262
|
+
status: 'proven',
|
|
263
|
+
provenMethod: method,
|
|
264
|
+
provenAt: now,
|
|
265
|
+
provenDetail: detail
|
|
266
|
+
} : {});
|
|
267
|
+
await claimRef(io.firestore, orgId, claim.domain).set(next);
|
|
268
|
+
return next;
|
|
269
|
+
}
|
|
270
|
+
const CHECK_MESSAGES = {
|
|
271
|
+
dns: {
|
|
272
|
+
proven: 'Found the DNS record. Your site is verified.',
|
|
273
|
+
'not-yet': 'The DNS record is not there yet. Changes can take a few minutes to an hour to appear; try again soon.',
|
|
274
|
+
unreachable: 'DNS did not answer just now. Nothing has changed; try again in a minute.'
|
|
275
|
+
},
|
|
276
|
+
meta: {
|
|
277
|
+
proven: 'Found the tag on your home page. Your site is verified.',
|
|
278
|
+
'not-yet': 'The tag is not on your home page yet. Publish the page with the tag in its head section, then try again.',
|
|
279
|
+
unreachable: 'Your home page could not be read just now. Nothing has changed; try again in a minute.'
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
/** Look for the DNS record or the meta tag now. */ export async function checkDomainOwnership(orgId, rawDomain, method, deps = {}) {
|
|
283
|
+
const { domain, error } = normalizeOwnershipDomain(rawDomain);
|
|
284
|
+
if (!domain) return {
|
|
285
|
+
result: null,
|
|
286
|
+
error
|
|
287
|
+
};
|
|
288
|
+
const io = resolved(deps);
|
|
289
|
+
const claim = await readClaim(io.firestore, orgId, domain);
|
|
290
|
+
if (!(claim == null ? void 0 : claim.token)) return {
|
|
291
|
+
result: null,
|
|
292
|
+
error: 'Add the site first.'
|
|
293
|
+
};
|
|
294
|
+
let status;
|
|
295
|
+
let detail;
|
|
296
|
+
if (method === 'dns') {
|
|
297
|
+
detail = domainOwnershipRecordHost(domain);
|
|
298
|
+
const lookup = await io.lookupTxt(detail).catch(()=>({
|
|
299
|
+
answered: false,
|
|
300
|
+
records: []
|
|
301
|
+
}));
|
|
302
|
+
status = !lookup.answered ? 'unreachable' : recordsProveOwnership(lookup.records, claim.token) ? 'proven' : 'not-yet';
|
|
303
|
+
} else {
|
|
304
|
+
detail = `https://${domain}/`;
|
|
305
|
+
const read = deps.readVerificationMeta;
|
|
306
|
+
const found = read ? await read(domain).catch(()=>({
|
|
307
|
+
answered: false,
|
|
308
|
+
values: [],
|
|
309
|
+
url: undefined
|
|
310
|
+
})) : {
|
|
311
|
+
answered: false,
|
|
312
|
+
values: [],
|
|
313
|
+
url: undefined
|
|
314
|
+
};
|
|
315
|
+
if (found.url) detail = found.url;
|
|
316
|
+
status = !found.answered ? 'unreachable' : metaValuesProveOwnership(found.values, claim.token) ? 'proven' : 'not-yet';
|
|
317
|
+
}
|
|
318
|
+
const next = await recordCheck(io, orgId, claim, method, status, detail);
|
|
319
|
+
return {
|
|
320
|
+
error: null,
|
|
321
|
+
result: {
|
|
322
|
+
method,
|
|
323
|
+
status,
|
|
324
|
+
message: CHECK_MESSAGES[method][status],
|
|
325
|
+
claim: domainOwnershipClaimView(next, io.now())
|
|
326
|
+
}
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
/** Email a one-time code to an address at the domain. */ export async function sendDomainOwnershipCode(orgId, rawDomain, rawAddress, context, deps = {}) {
|
|
330
|
+
var _deps_deliverCode;
|
|
331
|
+
const { domain, error } = normalizeOwnershipDomain(rawDomain);
|
|
332
|
+
if (!domain) return {
|
|
333
|
+
claim: null,
|
|
334
|
+
error,
|
|
335
|
+
status: 400
|
|
336
|
+
};
|
|
337
|
+
const io = resolved(deps);
|
|
338
|
+
const claim = await readClaim(io.firestore, orgId, domain);
|
|
339
|
+
if (!(claim == null ? void 0 : claim.token)) return {
|
|
340
|
+
claim: null,
|
|
341
|
+
error: 'Add the site first.',
|
|
342
|
+
status: 404
|
|
343
|
+
};
|
|
344
|
+
const address = ownershipEmailAddress(rawAddress, domain);
|
|
345
|
+
if (!address) {
|
|
346
|
+
return {
|
|
347
|
+
claim: null,
|
|
348
|
+
error: `Enter an email address that ends in @${domain}.`,
|
|
349
|
+
status: 400
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
const now = io.now();
|
|
353
|
+
if (claim.emailCode && now - claim.emailCode.sentAt < DOMAIN_OWNERSHIP_CODE_RESEND_MS) {
|
|
354
|
+
return {
|
|
355
|
+
claim: null,
|
|
356
|
+
error: 'A code was just sent. Wait a minute before asking for another.',
|
|
357
|
+
status: 429
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
const code = io.mintCode();
|
|
361
|
+
const deliver = (_deps_deliverCode = deps.deliverCode) != null ? _deps_deliverCode : deliverDomainOwnershipCode;
|
|
362
|
+
const sent = await deliver({
|
|
363
|
+
orgId,
|
|
364
|
+
org: context.org,
|
|
365
|
+
domain,
|
|
366
|
+
address,
|
|
367
|
+
code,
|
|
368
|
+
expiresInMinutes: Math.round(DOMAIN_OWNERSHIP_CODE_TTL_MS / 60000)
|
|
369
|
+
});
|
|
370
|
+
if (!sent.sent) {
|
|
371
|
+
var _sent_error, _sent_status;
|
|
372
|
+
return {
|
|
373
|
+
claim: null,
|
|
374
|
+
error: (_sent_error = sent.error) != null ? _sent_error : 'The code could not be sent.',
|
|
375
|
+
status: (_sent_status = sent.status) != null ? _sent_status : 502
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
const next = _extends({}, claim, {
|
|
379
|
+
emailCode: {
|
|
380
|
+
address,
|
|
381
|
+
hash: hashOwnershipCode(io.secret(), {
|
|
382
|
+
orgId,
|
|
383
|
+
domain,
|
|
384
|
+
address,
|
|
385
|
+
code
|
|
386
|
+
}),
|
|
387
|
+
sentAt: now,
|
|
388
|
+
expiresAt: now + DOMAIN_OWNERSHIP_CODE_TTL_MS,
|
|
389
|
+
attempts: 0
|
|
390
|
+
}
|
|
391
|
+
});
|
|
392
|
+
await claimRef(io.firestore, orgId, domain).set(next);
|
|
393
|
+
return {
|
|
394
|
+
claim: domainOwnershipClaimView(next, now),
|
|
395
|
+
error: null,
|
|
396
|
+
status: 200
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Check a code the person typed. Counted in a transaction, so parallel
|
|
401
|
+
* guesses cannot get past the attempt limit between a read and a write.
|
|
402
|
+
*/ export async function confirmDomainOwnershipCode(orgId, rawDomain, rawCode, deps = {}) {
|
|
403
|
+
const { domain, error } = normalizeOwnershipDomain(rawDomain);
|
|
404
|
+
if (!domain) return {
|
|
405
|
+
result: null,
|
|
406
|
+
error
|
|
407
|
+
};
|
|
408
|
+
const io = resolved(deps);
|
|
409
|
+
const ref = claimRef(io.firestore, orgId, domain);
|
|
410
|
+
const typed = String(rawCode != null ? rawCode : '').replace(/\s+/g, '');
|
|
411
|
+
const outcome = await io.firestore.runTransaction(async (tx)=>{
|
|
412
|
+
var _claim_lastChecks;
|
|
413
|
+
const snapshot = await tx.get(ref);
|
|
414
|
+
const claim = snapshot.exists ? snapshot.data() : null;
|
|
415
|
+
if (!(claim == null ? void 0 : claim.token)) return {
|
|
416
|
+
claim: null,
|
|
417
|
+
status: null,
|
|
418
|
+
message: 'Add the site first.'
|
|
419
|
+
};
|
|
420
|
+
const now = io.now();
|
|
421
|
+
const code = claim.emailCode;
|
|
422
|
+
if (!code) {
|
|
423
|
+
return {
|
|
424
|
+
claim,
|
|
425
|
+
status: 'not-yet',
|
|
426
|
+
message: 'Send a code first.'
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
if (code.expiresAt <= now || code.attempts >= DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS) {
|
|
430
|
+
return {
|
|
431
|
+
claim,
|
|
432
|
+
status: 'not-yet',
|
|
433
|
+
message: 'That code has expired. Send a new one.'
|
|
434
|
+
};
|
|
435
|
+
}
|
|
436
|
+
const expected = code.hash;
|
|
437
|
+
const presented = /^\d{6}$/.test(typed) ? hashOwnershipCode(io.secret(), {
|
|
438
|
+
orgId,
|
|
439
|
+
domain,
|
|
440
|
+
address: code.address,
|
|
441
|
+
code: typed
|
|
442
|
+
}) : '';
|
|
443
|
+
if (presented && safeEqual(presented, expected)) {
|
|
444
|
+
var _claim_lastChecks1;
|
|
445
|
+
const next = _extends({}, claim, {
|
|
446
|
+
status: 'proven',
|
|
447
|
+
provenMethod: 'email',
|
|
448
|
+
provenAt: now,
|
|
449
|
+
provenDetail: code.address,
|
|
450
|
+
lastCheckedAt: now,
|
|
451
|
+
lastChecks: _extends({}, (_claim_lastChecks1 = claim.lastChecks) != null ? _claim_lastChecks1 : {}, {
|
|
452
|
+
email: 'proven'
|
|
453
|
+
}),
|
|
454
|
+
emailCode: null
|
|
455
|
+
});
|
|
456
|
+
tx.set(ref, next);
|
|
457
|
+
return {
|
|
458
|
+
claim: next,
|
|
459
|
+
status: 'proven',
|
|
460
|
+
message: 'Code accepted. Your site is verified.'
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
const attempts = code.attempts + 1;
|
|
464
|
+
const left = DOMAIN_OWNERSHIP_CODE_MAX_ATTEMPTS - attempts;
|
|
465
|
+
const next = _extends({}, claim, {
|
|
466
|
+
lastCheckedAt: now,
|
|
467
|
+
lastChecks: _extends({}, (_claim_lastChecks = claim.lastChecks) != null ? _claim_lastChecks : {}, {
|
|
468
|
+
email: 'not-yet'
|
|
469
|
+
}),
|
|
470
|
+
emailCode: _extends({}, code, {
|
|
471
|
+
attempts
|
|
472
|
+
})
|
|
473
|
+
});
|
|
474
|
+
tx.set(ref, next);
|
|
475
|
+
return {
|
|
476
|
+
claim: next,
|
|
477
|
+
status: 'not-yet',
|
|
478
|
+
message: left > 0 ? `That code is not right. ${left} ${left === 1 ? 'try' : 'tries'} left.` : 'That code is not right, and it can no longer be used. Send a new one.'
|
|
479
|
+
};
|
|
480
|
+
});
|
|
481
|
+
if (!outcome.claim || !outcome.status) return {
|
|
482
|
+
result: null,
|
|
483
|
+
error: outcome.message
|
|
484
|
+
};
|
|
485
|
+
return {
|
|
486
|
+
error: null,
|
|
487
|
+
result: {
|
|
488
|
+
method: 'email',
|
|
489
|
+
status: outcome.status,
|
|
490
|
+
message: outcome.message,
|
|
491
|
+
claim: domainOwnershipClaimView(outcome.claim, io.now())
|
|
492
|
+
}
|
|
493
|
+
};
|
|
494
|
+
}
|
|
495
|
+
/*------------------------------------------
|
|
496
|
+
* The email
|
|
497
|
+
*-----------------------------------------*/ /**
|
|
498
|
+
* Sends the `domain-ownership-code` system email in the workspace's brand,
|
|
499
|
+
* after two ceilings: per workspace, so one cannot mail a domain's whole
|
|
500
|
+
* staff, and per address, so no address is flooded from many workspaces.
|
|
501
|
+
*/ export const deliverDomainOwnershipCode = async (input)=>{
|
|
502
|
+
var _input_org, _ref, _input_org1;
|
|
503
|
+
var _input_org2;
|
|
504
|
+
if (!isEmailConfigured()) {
|
|
505
|
+
return {
|
|
506
|
+
sent: false,
|
|
507
|
+
status: 501,
|
|
508
|
+
error: 'Email is not configured on this deployment.'
|
|
509
|
+
};
|
|
510
|
+
}
|
|
511
|
+
const { consumeRateLimit } = await import("./rate-limit-store.js");
|
|
512
|
+
const perOrg = await consumeRateLimit(`domain-ownership-code:${input.orgId}`, {
|
|
513
|
+
limit: DOMAIN_OWNERSHIP_CODES_PER_ORG_PER_HOUR,
|
|
514
|
+
windowMs: 60 * 60 * 1000
|
|
515
|
+
});
|
|
516
|
+
if (!perOrg.allowed) {
|
|
517
|
+
return {
|
|
518
|
+
sent: false,
|
|
519
|
+
status: 429,
|
|
520
|
+
error: 'Too many codes sent. Try again in an hour.'
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
const perAddress = await consumeRateLimit(`domain-ownership-code-target:${input.address}`, {
|
|
524
|
+
limit: DOMAIN_OWNERSHIP_CODES_PER_ADDRESS_PER_HOUR,
|
|
525
|
+
windowMs: 60 * 60 * 1000
|
|
526
|
+
});
|
|
527
|
+
if (!perAddress.allowed) {
|
|
528
|
+
return {
|
|
529
|
+
sent: false,
|
|
530
|
+
status: 429,
|
|
531
|
+
error: 'Too many codes for that address. Try again later.'
|
|
532
|
+
};
|
|
533
|
+
}
|
|
534
|
+
const { renderSystemEmailContent, systemEmailBrand } = await import("./render-system-email.js");
|
|
535
|
+
const branding = resolveBrandingProfile((_input_org = input.org) != null ? _input_org : null);
|
|
536
|
+
const workspace = String((_ref = (_input_org2 = input.org) == null ? void 0 : _input_org2['name']) != null ? _ref : '').trim() || 'A workspace';
|
|
537
|
+
const text = [
|
|
538
|
+
`${workspace} asked to confirm that ${input.domain} belongs to them, so they can bring the ` + `site into ${branding.productName}.`,
|
|
539
|
+
'',
|
|
540
|
+
`Your code: ${input.code}`,
|
|
541
|
+
'',
|
|
542
|
+
`It works for ${input.expiresInMinutes} minutes. Type it where you asked for it.`,
|
|
543
|
+
'',
|
|
544
|
+
'If you did not ask for this, ignore this email. Nothing is copied from your site without the code.'
|
|
545
|
+
].join('\n');
|
|
546
|
+
const content = await renderSystemEmailContent(DOMAIN_OWNERSHIP_CODE_EMAIL_KEY, {
|
|
547
|
+
'org.name': workspace,
|
|
548
|
+
domain: input.domain,
|
|
549
|
+
code: input.code,
|
|
550
|
+
expiresInMinutes: String(input.expiresInMinutes)
|
|
551
|
+
}, systemEmailBrand((_input_org1 = input.org) != null ? _input_org1 : null), {
|
|
552
|
+
subject: `Your code to verify ${input.domain}`,
|
|
553
|
+
text
|
|
554
|
+
});
|
|
555
|
+
const result = await sendEmail(_extends({
|
|
556
|
+
to: input.address
|
|
557
|
+
}, content, {
|
|
558
|
+
fromName: branding.fromName,
|
|
559
|
+
context: DOMAIN_OWNERSHIP_CODE_EMAIL_KEY
|
|
560
|
+
}));
|
|
561
|
+
if (!result.sent) {
|
|
562
|
+
return {
|
|
563
|
+
sent: false,
|
|
564
|
+
status: 502,
|
|
565
|
+
error: 'The code could not be sent. Try again.'
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
const { meterOrgEmail } = await import("./email-metering.js");
|
|
569
|
+
await meterOrgEmail(input.orgId).catch(()=>undefined);
|
|
570
|
+
return {
|
|
571
|
+
sent: true
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
|
|
575
|
+
//# sourceMappingURL=domain-ownership.js.map
|