@lacecms/application 0.1.0-alpha.4 → 0.1.0-alpha.6
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/accounts.d.ts +301 -0
- package/dist/accounts.d.ts.map +1 -0
- package/dist/accounts.js +342 -0
- package/dist/accounts.js.map +1 -0
- package/dist/configuration-evolution.d.ts +151 -0
- package/dist/configuration-evolution.d.ts.map +1 -0
- package/dist/configuration-evolution.js +568 -0
- package/dist/configuration-evolution.js.map +1 -0
- package/dist/configuration-fields.d.ts +30 -0
- package/dist/configuration-fields.d.ts.map +1 -0
- package/dist/configuration-fields.js +170 -0
- package/dist/configuration-fields.js.map +1 -0
- package/dist/configuration-migration.d.ts +130 -0
- package/dist/configuration-migration.d.ts.map +1 -0
- package/dist/configuration-migration.js +255 -0
- package/dist/configuration-migration.js.map +1 -0
- package/dist/configuration-sync.d.ts +192 -12
- package/dist/configuration-sync.d.ts.map +1 -1
- package/dist/configuration-sync.js +815 -39
- package/dist/configuration-sync.js.map +1 -1
- package/dist/content-use-cases.d.ts +53 -3
- package/dist/content-use-cases.d.ts.map +1 -1
- package/dist/content-use-cases.js +190 -19
- package/dist/content-use-cases.js.map +1 -1
- package/dist/email.d.ts +80 -0
- package/dist/email.d.ts.map +1 -0
- package/dist/email.js +134 -0
- package/dist/email.js.map +1 -0
- package/dist/index.d.ts +139 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +46 -1
- package/dist/index.js.map +1 -1
- package/dist/media-use-cases.d.ts +68 -1
- package/dist/media-use-cases.d.ts.map +1 -1
- package/dist/media-use-cases.js +151 -1
- package/dist/media-use-cases.js.map +1 -1
- package/package.json +4 -4
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { type Actor, type UnixMilliseconds } from "@lacecms/domain";
|
|
2
|
+
import { type EmailDeliveryOutcome, type EmailSender, type RenderedEmail } from "./email.js";
|
|
3
|
+
import type { Clock, ManagedUser, OpaqueTokenSecret, SecurityRole, SecurityService, SensitiveRateLimiter } from "./index.js";
|
|
4
|
+
/** Invitations stay valid for 72 hours after issuance or resend. */
|
|
5
|
+
export declare const INVITATION_TTL_MS: number;
|
|
6
|
+
/** Password-reset links stay valid for one hour. */
|
|
7
|
+
export declare const PASSWORD_RESET_TTL_MS: number;
|
|
8
|
+
export declare const DISPLAY_NAME_MAX_LENGTH = 120;
|
|
9
|
+
/** Closed account-flow failures; the HTTP boundary maps each code to one envelope. */
|
|
10
|
+
export type AccountErrorCode = "CONFLICT" | "INVALID_CREDENTIALS" | "INVITATION_INVALID" | "NOT_FOUND" | "RESET_INVALID";
|
|
11
|
+
export declare class AccountError extends Error {
|
|
12
|
+
readonly code: AccountErrorCode;
|
|
13
|
+
constructor(code: AccountErrorCode);
|
|
14
|
+
}
|
|
15
|
+
/** A sensitive limit was exhausted inside a use case. */
|
|
16
|
+
export declare class RateLimitExceededError extends Error {
|
|
17
|
+
readonly retryAfterSeconds: number;
|
|
18
|
+
constructor(retryAfterSeconds: number);
|
|
19
|
+
}
|
|
20
|
+
/** An unaccepted, unrevoked invitation as stored; never carries its token or digest. */
|
|
21
|
+
export interface InvitationRecord {
|
|
22
|
+
readonly createdAt: UnixMilliseconds;
|
|
23
|
+
readonly email: string;
|
|
24
|
+
readonly expiresAt: UnixMilliseconds;
|
|
25
|
+
readonly id: string;
|
|
26
|
+
readonly invitedBy: string;
|
|
27
|
+
/** The inviter's display name with the shared fallbacks applied. */
|
|
28
|
+
readonly invitedByName: string;
|
|
29
|
+
readonly role: SecurityRole;
|
|
30
|
+
}
|
|
31
|
+
/** One stored session of a user; never carries the session token or IP address. */
|
|
32
|
+
export interface SessionRecord {
|
|
33
|
+
readonly createdAt: UnixMilliseconds;
|
|
34
|
+
readonly id: string;
|
|
35
|
+
readonly lastActiveAt: UnixMilliseconds;
|
|
36
|
+
readonly userAgent?: string;
|
|
37
|
+
}
|
|
38
|
+
export type CreateInvitationResult = {
|
|
39
|
+
readonly invitation: InvitationRecord;
|
|
40
|
+
readonly status: "created";
|
|
41
|
+
readonly token: OpaqueTokenSecret;
|
|
42
|
+
} | {
|
|
43
|
+
readonly status: "conflict";
|
|
44
|
+
};
|
|
45
|
+
export type ReissueInvitationResult = {
|
|
46
|
+
readonly invitation: InvitationRecord;
|
|
47
|
+
readonly status: "reissued";
|
|
48
|
+
readonly token: OpaqueTokenSecret;
|
|
49
|
+
} | {
|
|
50
|
+
readonly status: "conflict";
|
|
51
|
+
} | {
|
|
52
|
+
readonly status: "not_found";
|
|
53
|
+
};
|
|
54
|
+
export type AcceptInvitationResult = {
|
|
55
|
+
readonly status: "accepted";
|
|
56
|
+
readonly user: ManagedUser;
|
|
57
|
+
} | {
|
|
58
|
+
readonly status: "conflict";
|
|
59
|
+
} | {
|
|
60
|
+
readonly status: "invalid";
|
|
61
|
+
};
|
|
62
|
+
export type IssuePasswordResetResult = {
|
|
63
|
+
readonly email: string;
|
|
64
|
+
readonly status: "issued";
|
|
65
|
+
readonly token: OpaqueTokenSecret;
|
|
66
|
+
readonly userId: string;
|
|
67
|
+
} | {
|
|
68
|
+
readonly status: "disabled";
|
|
69
|
+
} | {
|
|
70
|
+
readonly status: "not_found";
|
|
71
|
+
};
|
|
72
|
+
export type ChangePasswordResult = {
|
|
73
|
+
readonly revoked: number;
|
|
74
|
+
readonly status: "changed";
|
|
75
|
+
} | {
|
|
76
|
+
readonly status: "invalid_credentials";
|
|
77
|
+
};
|
|
78
|
+
/**
|
|
79
|
+
* Invitation, reset, profile, password and session commands. Tokens cross this
|
|
80
|
+
* port only as plaintext inputs or once-returned outputs; adapters store digests.
|
|
81
|
+
*/
|
|
82
|
+
export interface AccountSecurityPort {
|
|
83
|
+
/** Conflicts when the email has an account or an unexpired active invitation. */
|
|
84
|
+
createInvitation(input: {
|
|
85
|
+
readonly email: string;
|
|
86
|
+
readonly invitedBy: string;
|
|
87
|
+
readonly now: UnixMilliseconds;
|
|
88
|
+
readonly role: SecurityRole;
|
|
89
|
+
}): Promise<CreateInvitationResult>;
|
|
90
|
+
/** Unaccepted, unrevoked invitations, newest first, including expired ones. */
|
|
91
|
+
listInvitations(): Promise<readonly InvitationRecord[]>;
|
|
92
|
+
/** Null for an unknown, expired, revoked or accepted token. */
|
|
93
|
+
inspectInvitation(input: {
|
|
94
|
+
readonly now: UnixMilliseconds;
|
|
95
|
+
readonly token: OpaqueTokenSecret;
|
|
96
|
+
}): Promise<{
|
|
97
|
+
readonly email: string;
|
|
98
|
+
readonly expiresAt: UnixMilliseconds;
|
|
99
|
+
readonly role: SecurityRole;
|
|
100
|
+
} | null>;
|
|
101
|
+
/** Atomically creates the user and credential and marks the invitation accepted. */
|
|
102
|
+
acceptInvitation(input: {
|
|
103
|
+
readonly displayName?: string;
|
|
104
|
+
readonly now: UnixMilliseconds;
|
|
105
|
+
readonly password: string;
|
|
106
|
+
readonly token: OpaqueTokenSecret;
|
|
107
|
+
}): Promise<AcceptInvitationResult>;
|
|
108
|
+
/** Rotates the token and restarts the expiry of an unaccepted, unrevoked invitation. */
|
|
109
|
+
reissueInvitation(input: {
|
|
110
|
+
readonly invitationId: string;
|
|
111
|
+
readonly now: UnixMilliseconds;
|
|
112
|
+
}): Promise<ReissueInvitationResult>;
|
|
113
|
+
revokeInvitation(input: {
|
|
114
|
+
readonly invitationId: string;
|
|
115
|
+
readonly now: UnixMilliseconds;
|
|
116
|
+
}): Promise<"conflict" | "not_found" | "revoked">;
|
|
117
|
+
/** Issues a token for an enabled account and supersedes its unconsumed tokens. */
|
|
118
|
+
issuePasswordReset(input: {
|
|
119
|
+
readonly now: UnixMilliseconds;
|
|
120
|
+
readonly requestedBy?: string;
|
|
121
|
+
readonly target: {
|
|
122
|
+
readonly email: string;
|
|
123
|
+
} | {
|
|
124
|
+
readonly userId: string;
|
|
125
|
+
};
|
|
126
|
+
}): Promise<IssuePasswordResetResult>;
|
|
127
|
+
/** Replaces the credential, consumes the token and deletes every session. */
|
|
128
|
+
confirmPasswordReset(input: {
|
|
129
|
+
readonly now: UnixMilliseconds;
|
|
130
|
+
readonly password: string;
|
|
131
|
+
readonly token: OpaqueTokenSecret;
|
|
132
|
+
}): Promise<{
|
|
133
|
+
readonly status: "reset";
|
|
134
|
+
readonly userId: string;
|
|
135
|
+
} | {
|
|
136
|
+
readonly status: "invalid";
|
|
137
|
+
}>;
|
|
138
|
+
updateDisplayName(input: {
|
|
139
|
+
readonly displayName: string;
|
|
140
|
+
readonly now: UnixMilliseconds;
|
|
141
|
+
readonly userId: string;
|
|
142
|
+
}): Promise<boolean>;
|
|
143
|
+
/** With `keepSessionId`, every other session of the user is deleted. */
|
|
144
|
+
changePassword(input: {
|
|
145
|
+
readonly currentPassword: string;
|
|
146
|
+
readonly keepSessionId?: string;
|
|
147
|
+
readonly newPassword: string;
|
|
148
|
+
readonly now: UnixMilliseconds;
|
|
149
|
+
readonly userId: string;
|
|
150
|
+
}): Promise<ChangePasswordResult>;
|
|
151
|
+
/** Unexpired sessions of one user, newest activity first. */
|
|
152
|
+
listSessions(input: {
|
|
153
|
+
readonly now: UnixMilliseconds;
|
|
154
|
+
readonly userId: string;
|
|
155
|
+
}): Promise<readonly SessionRecord[]>;
|
|
156
|
+
/** False when the session does not exist or belongs to another user. */
|
|
157
|
+
deleteSession(input: {
|
|
158
|
+
readonly sessionId: string;
|
|
159
|
+
readonly userId: string;
|
|
160
|
+
}): Promise<boolean>;
|
|
161
|
+
deleteOtherSessions(input: {
|
|
162
|
+
readonly keepSessionId: string;
|
|
163
|
+
readonly userId: string;
|
|
164
|
+
}): Promise<number>;
|
|
165
|
+
/** Null when the user does not exist. */
|
|
166
|
+
deleteUserSessions(input: {
|
|
167
|
+
readonly userId: string;
|
|
168
|
+
}): Promise<number | null>;
|
|
169
|
+
}
|
|
170
|
+
export type SessionBrowser = "Chrome" | "Edge" | "Firefox" | "Opera" | "Safari" | "Unknown";
|
|
171
|
+
export type SessionOs = "Android" | "iOS" | "Linux" | "macOS" | "Windows" | "Unknown";
|
|
172
|
+
/** Classifies a stored user agent into a coarse browser and OS; no dependency, no versions. */
|
|
173
|
+
export declare function summarizeUserAgent(userAgent: string | undefined): {
|
|
174
|
+
readonly browser: SessionBrowser;
|
|
175
|
+
readonly os: SessionOs;
|
|
176
|
+
};
|
|
177
|
+
export declare function invitationEmailTemplate(input: {
|
|
178
|
+
readonly expiresInHours: number;
|
|
179
|
+
readonly inviterName: string;
|
|
180
|
+
readonly link: string;
|
|
181
|
+
readonly role: SecurityRole;
|
|
182
|
+
readonly siteUrl: string;
|
|
183
|
+
}): RenderedEmail;
|
|
184
|
+
export declare function passwordResetEmailTemplate(input: {
|
|
185
|
+
readonly expiresInHours: number;
|
|
186
|
+
readonly link: string;
|
|
187
|
+
readonly requestedByAdministrator: boolean;
|
|
188
|
+
readonly siteUrl: string;
|
|
189
|
+
}): RenderedEmail;
|
|
190
|
+
export declare function passwordChangedEmailTemplate(input: {
|
|
191
|
+
readonly siteUrl: string;
|
|
192
|
+
}): RenderedEmail;
|
|
193
|
+
/** Builds a link whose token travels only in the URL fragment. */
|
|
194
|
+
export declare function accountLink(publicBaseUrl: string, path: "admin/accept-invite" | "admin/reset-password", token: string): string;
|
|
195
|
+
/** Normalizes an address the way every adapter stores it. */
|
|
196
|
+
export declare function normalizeAccountEmail(value: string): string;
|
|
197
|
+
/** Trims a display name and enforces the shared bounds. */
|
|
198
|
+
export declare function normalizeDisplayName(value: string): string;
|
|
199
|
+
export type InvitationState = "expired" | "pending";
|
|
200
|
+
export interface InvitationView {
|
|
201
|
+
readonly createdAt: UnixMilliseconds;
|
|
202
|
+
readonly email: string;
|
|
203
|
+
readonly expiresAt: UnixMilliseconds;
|
|
204
|
+
readonly id: string;
|
|
205
|
+
readonly invitedBy: string;
|
|
206
|
+
readonly role: SecurityRole;
|
|
207
|
+
readonly state: InvitationState;
|
|
208
|
+
}
|
|
209
|
+
export interface LinkIssueResult {
|
|
210
|
+
readonly delivery: EmailDeliveryOutcome;
|
|
211
|
+
/** Present exactly when delivery was not `sent`; shown to the administrator once. */
|
|
212
|
+
readonly link?: string;
|
|
213
|
+
}
|
|
214
|
+
export interface InvitationIssueResult extends LinkIssueResult {
|
|
215
|
+
readonly invitation: InvitationView;
|
|
216
|
+
}
|
|
217
|
+
export interface SessionView {
|
|
218
|
+
readonly browser: SessionBrowser;
|
|
219
|
+
readonly createdAt: UnixMilliseconds;
|
|
220
|
+
readonly current: boolean;
|
|
221
|
+
readonly id: string;
|
|
222
|
+
readonly lastActiveAt: UnixMilliseconds;
|
|
223
|
+
readonly os: SessionOs;
|
|
224
|
+
}
|
|
225
|
+
/** Schedules work after the response without delaying it; failures are reported, not raised. */
|
|
226
|
+
export type DeferTask = (task: Promise<unknown>) => void;
|
|
227
|
+
export interface AccountUseCasesInput {
|
|
228
|
+
readonly clock: Clock;
|
|
229
|
+
readonly email: EmailSender;
|
|
230
|
+
/** Per-email reset limit; absent means unlimited (tests and tooling only). */
|
|
231
|
+
readonly limiter?: SensitiveRateLimiter;
|
|
232
|
+
/** Absolute base URL ending in `/`, never derived from request headers. */
|
|
233
|
+
readonly publicBaseUrl: string;
|
|
234
|
+
readonly security: SecurityService;
|
|
235
|
+
}
|
|
236
|
+
/** Invitation, password-recovery and self-service flows shared by both runtimes. */
|
|
237
|
+
export declare class AccountUseCases {
|
|
238
|
+
private readonly input;
|
|
239
|
+
constructor(input: AccountUseCasesInput);
|
|
240
|
+
private now;
|
|
241
|
+
private view;
|
|
242
|
+
private deliver;
|
|
243
|
+
private sendInvitation;
|
|
244
|
+
invite(actor: Actor, request: {
|
|
245
|
+
readonly email: string;
|
|
246
|
+
readonly role: SecurityRole;
|
|
247
|
+
}): Promise<InvitationIssueResult>;
|
|
248
|
+
listInvitations(actor: Actor): Promise<readonly InvitationView[]>;
|
|
249
|
+
resendInvitation(actor: Actor, invitationId: string): Promise<InvitationIssueResult>;
|
|
250
|
+
revokeInvitation(actor: Actor, invitationId: string): Promise<void>;
|
|
251
|
+
inspectInvitation(request: {
|
|
252
|
+
readonly token: string;
|
|
253
|
+
}): Promise<{
|
|
254
|
+
readonly email: string;
|
|
255
|
+
readonly expiresAt: UnixMilliseconds;
|
|
256
|
+
readonly role: SecurityRole;
|
|
257
|
+
}>;
|
|
258
|
+
acceptInvitation(request: {
|
|
259
|
+
readonly displayName?: string;
|
|
260
|
+
readonly password: string;
|
|
261
|
+
readonly token: string;
|
|
262
|
+
}): Promise<{
|
|
263
|
+
readonly email: string;
|
|
264
|
+
}>;
|
|
265
|
+
/**
|
|
266
|
+
* Returns the same outcome for enabled, disabled and unknown addresses. Only
|
|
267
|
+
* an enabled account receives a token, and its email is deferred.
|
|
268
|
+
*/
|
|
269
|
+
requestPasswordReset(request: {
|
|
270
|
+
readonly defer: DeferTask;
|
|
271
|
+
readonly email: string;
|
|
272
|
+
}): Promise<void>;
|
|
273
|
+
confirmPasswordReset(request: {
|
|
274
|
+
readonly password: string;
|
|
275
|
+
readonly token: string;
|
|
276
|
+
}): Promise<void>;
|
|
277
|
+
sendPasswordReset(actor: Actor, userId: string): Promise<LinkIssueResult>;
|
|
278
|
+
signOutUser(actor: Actor, userId: string): Promise<{
|
|
279
|
+
readonly revoked: number;
|
|
280
|
+
}>;
|
|
281
|
+
updateDisplayName(actor: Actor, displayName: string): Promise<void>;
|
|
282
|
+
/**
|
|
283
|
+
* Requires the current password. Signing out other sessions keeps only the
|
|
284
|
+
* session making the request. The changed-password notice is deferred.
|
|
285
|
+
*/
|
|
286
|
+
changePassword(actor: Actor, currentSessionId: string | null, request: {
|
|
287
|
+
readonly currentPassword: string;
|
|
288
|
+
readonly defer: DeferTask;
|
|
289
|
+
readonly newPassword: string;
|
|
290
|
+
readonly signOutOtherSessions: boolean;
|
|
291
|
+
}): Promise<{
|
|
292
|
+
readonly revoked: number;
|
|
293
|
+
}>;
|
|
294
|
+
listSessions(actor: Actor, currentSessionId: string | null): Promise<readonly SessionView[]>;
|
|
295
|
+
/** Ending the current session is refused in favor of sign-out. */
|
|
296
|
+
deleteSession(actor: Actor, currentSessionId: string | null, sessionId: string): Promise<void>;
|
|
297
|
+
deleteOtherSessions(actor: Actor, currentSessionId: string | null): Promise<{
|
|
298
|
+
readonly revoked: number;
|
|
299
|
+
}>;
|
|
300
|
+
}
|
|
301
|
+
//# sourceMappingURL=accounts.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accounts.d.ts","sourceRoot":"","sources":["../src/accounts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,KAAK,EAAE,KAAK,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AACvF,OAAO,EAEL,KAAK,oBAAoB,EACzB,KAAK,WAAW,EAChB,KAAK,aAAa,EACnB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EACV,KAAK,EACL,WAAW,EACX,iBAAiB,EACjB,YAAY,EACZ,eAAe,EACf,oBAAoB,EACrB,MAAM,YAAY,CAAC;AAEpB,oEAAoE;AACpE,eAAO,MAAM,iBAAiB,QAAsB,CAAC;AACrD,oDAAoD;AACpD,eAAO,MAAM,qBAAqB,QAAiB,CAAC;AACpD,eAAO,MAAM,uBAAuB,MAAM,CAAC;AAE3C,sFAAsF;AACtF,MAAM,MAAM,gBAAgB,GACxB,UAAU,GACV,qBAAqB,GACrB,oBAAoB,GACpB,WAAW,GACX,eAAe,CAAC;AAEpB,qBAAa,YAAa,SAAQ,KAAK;aACF,IAAI,EAAE,gBAAgB;IAAzD,YAAmC,IAAI,EAAE,gBAAgB,EAGxD;CACF;AAED,yDAAyD;AACzD,qBAAa,sBAAuB,SAAQ,KAAK;aACZ,iBAAiB,EAAE,MAAM;IAA5D,YAAmC,iBAAiB,EAAE,MAAM,EAG3D;CACF;AAED,wFAAwF;AACxF,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,QAAQ,CAAC,EAAE,EA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,KAAK,EAAE,KAAK,EACZ,gBAAgB,EAAE,MAAM,GAAG,IAAI,GAC9B,OAAO,CAAC,SAAS,WAAW,EAAE,CAAC,CAWjC;IAED,kEAAkE;IACrD,aAAa,CACxB,KAAK,EAAE,KAAK,EACZ,gBAAgB,EAAE,MAAM,GAAG,IAAI,EAC/B,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,IAAI,CAAC,CAIf;IAEY,mBAAmB,CAC9B,KAAK,EAAE,KAAK,EACZ,gBAAgB,EAAE,MAAM,GAAG,IAAI,GAC9B,OAAO,CAAC;QAAE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAA;KAAE,CAAC,CAOvC;CACF"}
|
package/dist/accounts.js
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { requirePermission } from "@lacecms/domain";
|
|
2
|
+
import { renderEmail, } from "./email.js";
|
|
3
|
+
/** Invitations stay valid for 72 hours after issuance or resend. */
|
|
4
|
+
export const INVITATION_TTL_MS = 72 * 60 * 60 * 1000;
|
|
5
|
+
/** Password-reset links stay valid for one hour. */
|
|
6
|
+
export const PASSWORD_RESET_TTL_MS = 60 * 60 * 1000;
|
|
7
|
+
export const DISPLAY_NAME_MAX_LENGTH = 120;
|
|
8
|
+
export class AccountError extends Error {
|
|
9
|
+
code;
|
|
10
|
+
constructor(code) {
|
|
11
|
+
super(code);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.name = "AccountError";
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** A sensitive limit was exhausted inside a use case. */
|
|
17
|
+
export class RateLimitExceededError extends Error {
|
|
18
|
+
retryAfterSeconds;
|
|
19
|
+
constructor(retryAfterSeconds) {
|
|
20
|
+
super("Rate limit exceeded.");
|
|
21
|
+
this.retryAfterSeconds = retryAfterSeconds;
|
|
22
|
+
this.name = "RateLimitExceededError";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Classifies a stored user agent into a coarse browser and OS; no dependency, no versions. */
|
|
26
|
+
export function summarizeUserAgent(userAgent) {
|
|
27
|
+
const value = userAgent ?? "";
|
|
28
|
+
const browser = /\bEdg(?:e|A|iOS)?\//u.test(value)
|
|
29
|
+
? "Edge"
|
|
30
|
+
: /\b(?:OPR|Opera|OPiOS)\//u.test(value)
|
|
31
|
+
? "Opera"
|
|
32
|
+
: /\b(?:Firefox|FxiOS)\//u.test(value)
|
|
33
|
+
? "Firefox"
|
|
34
|
+
: /\b(?:Chrome|CriOS|Chromium)\//u.test(value)
|
|
35
|
+
? "Chrome"
|
|
36
|
+
: /\bVersion\/[\d.]+.*\bSafari\//u.test(value)
|
|
37
|
+
? "Safari"
|
|
38
|
+
: "Unknown";
|
|
39
|
+
const os = /\bWindows\b/u.test(value)
|
|
40
|
+
? "Windows"
|
|
41
|
+
: /\b(?:iPhone|iPad|iPod)\b/u.test(value)
|
|
42
|
+
? "iOS"
|
|
43
|
+
: /\bAndroid\b/u.test(value)
|
|
44
|
+
? "Android"
|
|
45
|
+
: /\b(?:Macintosh|Mac OS X)\b/u.test(value)
|
|
46
|
+
? "macOS"
|
|
47
|
+
: /\b(?:Linux|X11|CrOS)\b/u.test(value)
|
|
48
|
+
? "Linux"
|
|
49
|
+
: "Unknown";
|
|
50
|
+
return Object.freeze({ browser, os });
|
|
51
|
+
}
|
|
52
|
+
const ROLE_LABELS = Object.freeze({
|
|
53
|
+
admin: "Admin",
|
|
54
|
+
editor: "Editor",
|
|
55
|
+
viewer: "Viewer",
|
|
56
|
+
});
|
|
57
|
+
function hours(ms) {
|
|
58
|
+
return Math.round(ms / (60 * 60 * 1000));
|
|
59
|
+
}
|
|
60
|
+
export function invitationEmailTemplate(input) {
|
|
61
|
+
return renderEmail({
|
|
62
|
+
link: input.link,
|
|
63
|
+
paragraphs: [
|
|
64
|
+
`${input.inviterName} invited you to the Lace CMS at ${input.siteUrl} with the ${ROLE_LABELS[input.role]} role.`,
|
|
65
|
+
`Open the link below to choose your password and join. The link works once and expires in ${input.expiresInHours} hours.`,
|
|
66
|
+
],
|
|
67
|
+
postscript: [
|
|
68
|
+
"If you did not expect this invitation, you can ignore this email; no account is created unless the link is used.",
|
|
69
|
+
],
|
|
70
|
+
subject: "You are invited to Lace CMS",
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export function passwordResetEmailTemplate(input) {
|
|
74
|
+
return renderEmail({
|
|
75
|
+
link: input.link,
|
|
76
|
+
paragraphs: [
|
|
77
|
+
input.requestedByAdministrator
|
|
78
|
+
? `An administrator of the Lace CMS at ${input.siteUrl} sent you a link to choose a new password.`
|
|
79
|
+
: `Someone asked to reset the password of your account on the Lace CMS at ${input.siteUrl}.`,
|
|
80
|
+
`Open the link below to choose a new password. The link works once and expires in ${input.expiresInHours} ${input.expiresInHours === 1 ? "hour" : "hours"}. Choosing a new password signs you out everywhere.`,
|
|
81
|
+
],
|
|
82
|
+
postscript: [
|
|
83
|
+
"If you did not ask for this, you can ignore this email; your password stays unchanged.",
|
|
84
|
+
],
|
|
85
|
+
subject: "Reset your Lace CMS password",
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function passwordChangedEmailTemplate(input) {
|
|
89
|
+
return renderEmail({
|
|
90
|
+
paragraphs: [
|
|
91
|
+
`The password of your account on the Lace CMS at ${input.siteUrl} was just changed.`,
|
|
92
|
+
"If you made this change, no action is needed. If you did not, reset your password from the sign-in page and contact your administrator.",
|
|
93
|
+
],
|
|
94
|
+
subject: "Your Lace CMS password was changed",
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
/** Builds a link whose token travels only in the URL fragment. */
|
|
98
|
+
export function accountLink(publicBaseUrl, path, token) {
|
|
99
|
+
if (!/^https?:\/\/[^\s?\x23]+\/$/u.test(publicBaseUrl))
|
|
100
|
+
throw new TypeError("The public base URL must be absolute and end in a slash.");
|
|
101
|
+
return `${publicBaseUrl}${path}#token=${token}`;
|
|
102
|
+
}
|
|
103
|
+
/** Normalizes an address the way every adapter stores it. */
|
|
104
|
+
export function normalizeAccountEmail(value) {
|
|
105
|
+
return value.trim().toLowerCase();
|
|
106
|
+
}
|
|
107
|
+
// oxlint-disable-next-line no-control-regex -- rejects control characters by design.
|
|
108
|
+
const CONTROL_CHARACTERS = /[\u0000-\u001f\u007f-\u009f]/u;
|
|
109
|
+
/** Trims a display name and enforces the shared bounds. */
|
|
110
|
+
export function normalizeDisplayName(value) {
|
|
111
|
+
const name = value.trim();
|
|
112
|
+
if (name.length < 1 || name.length > DISPLAY_NAME_MAX_LENGTH || CONTROL_CHARACTERS.test(name))
|
|
113
|
+
throw new TypeError("Invalid display name.");
|
|
114
|
+
return name;
|
|
115
|
+
}
|
|
116
|
+
function requireUsersManagerActor(actor) {
|
|
117
|
+
requirePermission(actor, "users:manage");
|
|
118
|
+
}
|
|
119
|
+
/** Invitation, password-recovery and self-service flows shared by both runtimes. */
|
|
120
|
+
export class AccountUseCases {
|
|
121
|
+
input;
|
|
122
|
+
constructor(input) {
|
|
123
|
+
this.input = input;
|
|
124
|
+
}
|
|
125
|
+
now() {
|
|
126
|
+
return this.input.clock.now();
|
|
127
|
+
}
|
|
128
|
+
view(record, now) {
|
|
129
|
+
return Object.freeze({
|
|
130
|
+
createdAt: record.createdAt,
|
|
131
|
+
email: record.email,
|
|
132
|
+
expiresAt: record.expiresAt,
|
|
133
|
+
id: record.id,
|
|
134
|
+
invitedBy: record.invitedByName,
|
|
135
|
+
role: record.role,
|
|
136
|
+
state: Number(record.expiresAt) > Number(now) ? "pending" : "expired",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
async deliver(message, to, link) {
|
|
140
|
+
const delivery = await this.input.email.send({ ...message, to });
|
|
141
|
+
return delivery.status === "sent"
|
|
142
|
+
? Object.freeze({ delivery })
|
|
143
|
+
: Object.freeze({ delivery, link });
|
|
144
|
+
}
|
|
145
|
+
async sendInvitation(actor, invitation, token, now) {
|
|
146
|
+
const link = accountLink(this.input.publicBaseUrl, "admin/accept-invite", token);
|
|
147
|
+
const profile = await this.input.security.readUserProfile(actor.id);
|
|
148
|
+
const message = invitationEmailTemplate({
|
|
149
|
+
expiresInHours: hours(INVITATION_TTL_MS),
|
|
150
|
+
inviterName: profile?.name ?? profile?.email ?? "A Lace administrator",
|
|
151
|
+
link,
|
|
152
|
+
role: invitation.role,
|
|
153
|
+
siteUrl: this.input.publicBaseUrl,
|
|
154
|
+
});
|
|
155
|
+
const result = await this.deliver(message, invitation.email, link);
|
|
156
|
+
return Object.freeze({ ...result, invitation: this.view(invitation, now) });
|
|
157
|
+
}
|
|
158
|
+
async invite(actor, request) {
|
|
159
|
+
requireUsersManagerActor(actor);
|
|
160
|
+
const now = this.now();
|
|
161
|
+
const created = await this.input.security.createInvitation({
|
|
162
|
+
email: normalizeAccountEmail(request.email),
|
|
163
|
+
invitedBy: actor.id,
|
|
164
|
+
now,
|
|
165
|
+
role: request.role,
|
|
166
|
+
});
|
|
167
|
+
if (created.status === "conflict")
|
|
168
|
+
throw new AccountError("CONFLICT");
|
|
169
|
+
return this.sendInvitation(actor, created.invitation, created.token, now);
|
|
170
|
+
}
|
|
171
|
+
async listInvitations(actor) {
|
|
172
|
+
requireUsersManagerActor(actor);
|
|
173
|
+
const now = this.now();
|
|
174
|
+
return (await this.input.security.listInvitations()).map((record) => this.view(record, now));
|
|
175
|
+
}
|
|
176
|
+
async resendInvitation(actor, invitationId) {
|
|
177
|
+
requireUsersManagerActor(actor);
|
|
178
|
+
const now = this.now();
|
|
179
|
+
const reissued = await this.input.security.reissueInvitation({ invitationId, now });
|
|
180
|
+
if (reissued.status === "not_found")
|
|
181
|
+
throw new AccountError("NOT_FOUND");
|
|
182
|
+
if (reissued.status === "conflict")
|
|
183
|
+
throw new AccountError("CONFLICT");
|
|
184
|
+
return this.sendInvitation(actor, reissued.invitation, reissued.token, now);
|
|
185
|
+
}
|
|
186
|
+
async revokeInvitation(actor, invitationId) {
|
|
187
|
+
requireUsersManagerActor(actor);
|
|
188
|
+
const outcome = await this.input.security.revokeInvitation({ invitationId, now: this.now() });
|
|
189
|
+
if (outcome === "not_found")
|
|
190
|
+
throw new AccountError("NOT_FOUND");
|
|
191
|
+
if (outcome === "conflict")
|
|
192
|
+
throw new AccountError("CONFLICT");
|
|
193
|
+
}
|
|
194
|
+
async inspectInvitation(request) {
|
|
195
|
+
const found = await this.input.security.inspectInvitation({
|
|
196
|
+
now: this.now(),
|
|
197
|
+
token: request.token,
|
|
198
|
+
});
|
|
199
|
+
if (found === null)
|
|
200
|
+
throw new AccountError("INVITATION_INVALID");
|
|
201
|
+
return found;
|
|
202
|
+
}
|
|
203
|
+
async acceptInvitation(request) {
|
|
204
|
+
const result = await this.input.security.acceptInvitation({
|
|
205
|
+
...(request.displayName === undefined
|
|
206
|
+
? {}
|
|
207
|
+
: { displayName: normalizeDisplayName(request.displayName) }),
|
|
208
|
+
now: this.now(),
|
|
209
|
+
password: request.password,
|
|
210
|
+
token: request.token,
|
|
211
|
+
});
|
|
212
|
+
if (result.status === "invalid")
|
|
213
|
+
throw new AccountError("INVITATION_INVALID");
|
|
214
|
+
if (result.status === "conflict")
|
|
215
|
+
throw new AccountError("CONFLICT");
|
|
216
|
+
return Object.freeze({ email: result.user.email });
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Returns the same outcome for enabled, disabled and unknown addresses. Only
|
|
220
|
+
* an enabled account receives a token, and its email is deferred.
|
|
221
|
+
*/
|
|
222
|
+
async requestPasswordReset(request) {
|
|
223
|
+
const email = normalizeAccountEmail(request.email);
|
|
224
|
+
const now = this.now();
|
|
225
|
+
if (this.input.limiter !== undefined) {
|
|
226
|
+
const decision = await this.input.limiter.check({ now, operation: "reset", subject: email });
|
|
227
|
+
if (!decision.allowed)
|
|
228
|
+
throw new RateLimitExceededError(decision.retryAfterSeconds);
|
|
229
|
+
}
|
|
230
|
+
const issued = await this.input.security.issuePasswordReset({ now, target: { email } });
|
|
231
|
+
if (issued.status !== "issued")
|
|
232
|
+
return;
|
|
233
|
+
const message = passwordResetEmailTemplate({
|
|
234
|
+
expiresInHours: hours(PASSWORD_RESET_TTL_MS),
|
|
235
|
+
link: accountLink(this.input.publicBaseUrl, "admin/reset-password", issued.token),
|
|
236
|
+
requestedByAdministrator: false,
|
|
237
|
+
siteUrl: this.input.publicBaseUrl,
|
|
238
|
+
});
|
|
239
|
+
request.defer(this.input.email.send({ ...message, to: issued.email }));
|
|
240
|
+
}
|
|
241
|
+
async confirmPasswordReset(request) {
|
|
242
|
+
const result = await this.input.security.confirmPasswordReset({
|
|
243
|
+
now: this.now(),
|
|
244
|
+
password: request.password,
|
|
245
|
+
token: request.token,
|
|
246
|
+
});
|
|
247
|
+
if (result.status === "invalid")
|
|
248
|
+
throw new AccountError("RESET_INVALID");
|
|
249
|
+
}
|
|
250
|
+
async sendPasswordReset(actor, userId) {
|
|
251
|
+
requireUsersManagerActor(actor);
|
|
252
|
+
const issued = await this.input.security.issuePasswordReset({
|
|
253
|
+
now: this.now(),
|
|
254
|
+
requestedBy: actor.id,
|
|
255
|
+
target: { userId },
|
|
256
|
+
});
|
|
257
|
+
if (issued.status === "not_found")
|
|
258
|
+
throw new AccountError("NOT_FOUND");
|
|
259
|
+
if (issued.status === "disabled")
|
|
260
|
+
throw new AccountError("CONFLICT");
|
|
261
|
+
const link = accountLink(this.input.publicBaseUrl, "admin/reset-password", issued.token);
|
|
262
|
+
const message = passwordResetEmailTemplate({
|
|
263
|
+
expiresInHours: hours(PASSWORD_RESET_TTL_MS),
|
|
264
|
+
link,
|
|
265
|
+
requestedByAdministrator: true,
|
|
266
|
+
siteUrl: this.input.publicBaseUrl,
|
|
267
|
+
});
|
|
268
|
+
return this.deliver(message, issued.email, link);
|
|
269
|
+
}
|
|
270
|
+
async signOutUser(actor, userId) {
|
|
271
|
+
requireUsersManagerActor(actor);
|
|
272
|
+
const revoked = await this.input.security.deleteUserSessions({ userId });
|
|
273
|
+
if (revoked === null)
|
|
274
|
+
throw new AccountError("NOT_FOUND");
|
|
275
|
+
return Object.freeze({ revoked });
|
|
276
|
+
}
|
|
277
|
+
async updateDisplayName(actor, displayName) {
|
|
278
|
+
const updated = await this.input.security.updateDisplayName({
|
|
279
|
+
displayName: normalizeDisplayName(displayName),
|
|
280
|
+
now: this.now(),
|
|
281
|
+
userId: actor.id,
|
|
282
|
+
});
|
|
283
|
+
if (!updated)
|
|
284
|
+
throw new AccountError("NOT_FOUND");
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Requires the current password. Signing out other sessions keeps only the
|
|
288
|
+
* session making the request. The changed-password notice is deferred.
|
|
289
|
+
*/
|
|
290
|
+
async changePassword(actor, currentSessionId, request) {
|
|
291
|
+
if (request.signOutOtherSessions && currentSessionId === null)
|
|
292
|
+
throw new AccountError("CONFLICT");
|
|
293
|
+
const result = await this.input.security.changePassword({
|
|
294
|
+
currentPassword: request.currentPassword,
|
|
295
|
+
...(request.signOutOtherSessions && currentSessionId !== null
|
|
296
|
+
? { keepSessionId: currentSessionId }
|
|
297
|
+
: {}),
|
|
298
|
+
newPassword: request.newPassword,
|
|
299
|
+
now: this.now(),
|
|
300
|
+
userId: actor.id,
|
|
301
|
+
});
|
|
302
|
+
if (result.status === "invalid_credentials")
|
|
303
|
+
throw new AccountError("INVALID_CREDENTIALS");
|
|
304
|
+
if (this.input.email.provider !== "none") {
|
|
305
|
+
const profile = await this.input.security.readUserProfile(actor.id);
|
|
306
|
+
if (profile !== null && !profile.disabled)
|
|
307
|
+
request.defer(this.input.email.send({
|
|
308
|
+
...passwordChangedEmailTemplate({ siteUrl: this.input.publicBaseUrl }),
|
|
309
|
+
to: profile.email,
|
|
310
|
+
}));
|
|
311
|
+
}
|
|
312
|
+
return Object.freeze({ revoked: result.revoked });
|
|
313
|
+
}
|
|
314
|
+
async listSessions(actor, currentSessionId) {
|
|
315
|
+
const sessions = await this.input.security.listSessions({ now: this.now(), userId: actor.id });
|
|
316
|
+
return sessions.map((session) => Object.freeze({
|
|
317
|
+
...summarizeUserAgent(session.userAgent),
|
|
318
|
+
createdAt: session.createdAt,
|
|
319
|
+
current: session.id === currentSessionId,
|
|
320
|
+
id: session.id,
|
|
321
|
+
lastActiveAt: session.lastActiveAt,
|
|
322
|
+
}));
|
|
323
|
+
}
|
|
324
|
+
/** Ending the current session is refused in favor of sign-out. */
|
|
325
|
+
async deleteSession(actor, currentSessionId, sessionId) {
|
|
326
|
+
if (sessionId === currentSessionId)
|
|
327
|
+
throw new AccountError("CONFLICT");
|
|
328
|
+
const deleted = await this.input.security.deleteSession({ sessionId, userId: actor.id });
|
|
329
|
+
if (!deleted)
|
|
330
|
+
throw new AccountError("NOT_FOUND");
|
|
331
|
+
}
|
|
332
|
+
async deleteOtherSessions(actor, currentSessionId) {
|
|
333
|
+
if (currentSessionId === null)
|
|
334
|
+
throw new AccountError("CONFLICT");
|
|
335
|
+
const revoked = await this.input.security.deleteOtherSessions({
|
|
336
|
+
keepSessionId: currentSessionId,
|
|
337
|
+
userId: actor.id,
|
|
338
|
+
});
|
|
339
|
+
return Object.freeze({ revoked });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
//# sourceMappingURL=accounts.js.map
|