@aooth/user 0.1.6 → 0.1.8

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.
@@ -1,246 +0,0 @@
1
- //#region src/types.d.ts
2
- interface UserCredentials {
3
- id: string;
4
- username: string;
5
- /**
6
- * Server-managed optimistic-concurrency counter. Bumped by `UserStore.update`
7
- * on every successful write; checked against `UserStoreUpdate.expectedVersion`
8
- * for CAS. Callers MUST NOT write it directly — atscript-db rejects direct
9
- * writes with `DbError("VERSION_COLUMN_WRITE")`. Optional in TS so pre-OCC
10
- * fixtures keep compiling; the store seeds `0` on insert.
11
- */
12
- version?: number;
13
- password: PasswordData;
14
- account: AccountData;
15
- mfa: MfaData;
16
- /**
17
- * Hashed backup codes (SHA-256, hex-encoded). Generated via
18
- * `UserService.generateBackupCodes`. Undefined when the user has not
19
- * enrolled backup codes; an empty array means all codes were consumed.
20
- */
21
- backupCodes?: string[];
22
- /**
23
- * Persisted device-trust records ("remember this device, skip MFA next
24
- * time"). Managed by `UserService.{issue,add,verify,revoke,list}TrustedDevice`.
25
- * Absent when the user has never opted in.
26
- */
27
- trustedDevices?: TrustedDeviceRecord[];
28
- }
29
- interface TrustedDeviceRecord {
30
- /** `<raw>.<sig>` — what we hand back to the consumer and what they round-trip. */
31
- token: string;
32
- /** Bound IP — set when `deviceTrust.bindsTo === 'cookie+ip'`. */
33
- ip?: string;
34
- issuedAt: number;
35
- expiresAt: number;
36
- /** Optional human-readable label (e.g. user-agent summary). */
37
- name?: string;
38
- }
39
- interface PasswordData {
40
- /** Self-describing scrypt hash: $scrypt$N=...,r=...,p=...,l=...$salt$hash */
41
- hash: string;
42
- /** Previous password hashes (self-describing strings) */
43
- history: string[];
44
- lastChanged: number;
45
- /** True when password was system-generated and user hasn't set their own */
46
- isInitial: boolean;
47
- }
48
- interface AccountData {
49
- active: boolean;
50
- locked: boolean;
51
- lockReason: string;
52
- /** 0 = permanent lock, >0 = timestamp (ms) when lock expires */
53
- lockEnds: number;
54
- failedLoginAttempts: number;
55
- lastLogin: number;
56
- /**
57
- * True while the user record exists from an admin-issued invite but the
58
- * invitee has not yet accepted (set password + activate). Used by
59
- * `InviteWorkflow` to gate the accept tail, reject duplicate invites, and
60
- * power `auth/invite/resend` / `auth/invite/cancel`. Absent / `false` once
61
- * the invite has been accepted.
62
- */
63
- pendingInvitation?: boolean;
64
- }
65
- interface MfaData {
66
- /** Registered MFA methods */
67
- methods: MfaMethod[];
68
- /** Name of the default MFA method */
69
- defaultMethod: string;
70
- /** Auto-send MFA challenge on login */
71
- autoSend: boolean;
72
- }
73
- interface MfaMethod {
74
- /** Method name: 'email', 'sms', 'totp' */
75
- name: string;
76
- /** Whether this method has been verified/confirmed */
77
- confirmed: boolean;
78
- /** The method's value: email address, phone number, or TOTP secret */
79
- value: string;
80
- /**
81
- * Last HOTP counter accepted for this method (TOTP only). Server-managed
82
- * replay guard — `verifyMfa` rejects any code whose matched counter is
83
- * `<= lastUsedWindow`. Never written from user-facing input.
84
- */
85
- lastUsedWindow?: number;
86
- }
87
- interface UserServiceConfig {
88
- password?: PasswordConfig;
89
- lockout?: LockoutConfig;
90
- /** Injectable clock for testability. Defaults to Date.now */
91
- clock?: () => number;
92
- /**
93
- * Device-trust config. Required (with a non-empty `secret`) when any
94
- * `issueTrustedDevice` / `verifyTrustedDevice` API is called; the methods
95
- * throw clearly when invoked without it.
96
- */
97
- deviceTrust?: {
98
- /** HMAC-SHA256 signing secret for trust-device tokens. */secret: string;
99
- };
100
- }
101
- interface PasswordConfig {
102
- /** Pepper string prepended to password before hashing */
103
- pepper?: string;
104
- /** Number of historical hashes to retain (0 = disabled) */
105
- historyLength?: number;
106
- /** scrypt cost parameter N (default 16384) */
107
- scryptN?: number;
108
- /** scrypt block size r (default 8) */
109
- scryptR?: number;
110
- /** scrypt parallelism p (default 1) */
111
- scryptP?: number;
112
- /** Hash output length in bytes (default 64) */
113
- keyLength?: number;
114
- /** Password policy rules */
115
- policies?: (PasswordPolicyDef | PasswordPolicyInstance)[];
116
- }
117
- interface LockoutConfig {
118
- /** Lock after this many failed attempts (0 = disabled) */
119
- threshold?: number;
120
- /** Lock duration in ms (0 = permanent) */
121
- duration?: number;
122
- }
123
- interface PasswordPolicyDef {
124
- /**
125
- * Backend evaluator. Function only — executed directly with no sandbox.
126
- * String rules were removed: `@prostojs/ftring`'s sandbox does NOT block
127
- * prototype-chain escapes (`constructor.constructor("return process")()`,
128
- * `__proto__.x = ...`), so accepting strings was an RCE vector. Authors
129
- * use `definePasswordPolicy({ rule, args })` to get both this fn AND the
130
- * serialized form for free.
131
- */
132
- rule: PasswordPolicyEvalFn;
133
- /**
134
- * Pre-baked function-literal text shipped to clients for cross-tier
135
- * validation via `getTransferablePolicies()`. Authored as
136
- * `(v) => (${ruleSource})(v, ${args.map(JSON.stringify).join(', ')})` by
137
- * `definePasswordPolicy`. Absent → the policy is backend-only (frontend
138
- * skips it; server-side check remains authoritative).
139
- */
140
- serialized?: string;
141
- description?: string;
142
- errorMessage?: string;
143
- }
144
- type PasswordPolicyEvalFn = (password: string, context?: PasswordPolicyContext) => boolean | Promise<boolean>;
145
- interface PasswordPolicyContext {
146
- passwordData?: PasswordData;
147
- passwordConfig?: PasswordConfig;
148
- }
149
- /** Interface satisfied by the PasswordPolicy class (avoids circular import) */
150
- interface PasswordPolicyInstance extends PasswordPolicyDef {
151
- evaluate(password: string, context?: PasswordPolicyContext): boolean | Promise<boolean>;
152
- transferable: boolean;
153
- }
154
- type DeepPartial<T> = { [P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P] };
155
- interface UserStoreUpdate {
156
- /** Partial object with fields to set (deep-merged) */
157
- set?: DeepPartial<UserCredentials>;
158
- /** Dot-paths to atomically increment: e.g. {'account.failedLoginAttempts': 1} */
159
- inc?: Record<string, number>;
160
- /**
161
- * Optimistic concurrency control: when supplied, the store applies the
162
- * update iff the row's current `version` equals this value. On mismatch
163
- * the store returns `false` (same shape as "not found") and does NOT
164
- * mutate. Service callers treat both states as "stale read, retry".
165
- */
166
- expectedVersion?: number;
167
- }
168
- type UserAuthErrorType = "NOT_FOUND" | "ALREADY_EXISTS" | "LOCKED" | "INACTIVE" | "INVALID_CREDENTIALS" | "POLICY_VIOLATION" | "PASSWORDS_MISMATCH" | "PASSWORD_IN_HISTORY" | "MFA_REQUIRED" | "MFA_INVALID" | "MFA_NOT_CONFIGURED" | "CAS_EXHAUSTED";
169
- interface LoginResult<T extends object = object> {
170
- user: UserCredentials & T;
171
- /** Whether MFA verification is required before granting full access */
172
- mfaRequired: boolean;
173
- }
174
- interface LockStatus {
175
- locked: boolean;
176
- /** True when lock has a non-zero lockEnds that is in the past */
177
- expired: boolean;
178
- reason: string;
179
- lockEnds: number;
180
- }
181
- interface PolicyCheckResult {
182
- passed: boolean;
183
- policies: {
184
- description: string;
185
- passed: boolean;
186
- }[];
187
- errors: string[];
188
- }
189
- interface TransferablePolicy {
190
- rule: string;
191
- description?: string;
192
- errorMessage?: string;
193
- }
194
- interface MfaMethodInfo {
195
- name: string;
196
- isDefault: boolean;
197
- masked: string;
198
- }
199
- interface TotpConfig {
200
- /** Time step in seconds (default 30) */
201
- period?: number;
202
- /** Number of digits in the code (default 6) */
203
- digits?: number;
204
- /** Verification window — number of steps to check on each side (default 1) */
205
- window?: number;
206
- /** Injectable clock for testability */
207
- clock?: () => number;
208
- }
209
- //#endregion
210
- //#region src/store/user-store.d.ts
211
- interface WithCasOptions {
212
- /**
213
- * Total attempts (1 initial + retries). Default `2` = one retry. Each
214
- * attempt re-reads the row so the mutator runs against fresh state — that
215
- * is the whole point of retry under OCC. Bump for high-contention writers
216
- * (bulk admin scripts); leave at default for normal per-user request flow.
217
- */
218
- maxAttempts?: number;
219
- }
220
- declare abstract class UserStore<T extends object = object> {
221
- abstract exists(username: string): Promise<boolean>;
222
- abstract findByUsername(username: string): Promise<(UserCredentials & T) | null>;
223
- abstract create(data: UserCredentials & T): Promise<void>;
224
- abstract update(username: string, update: UserStoreUpdate): Promise<boolean>;
225
- /**
226
- * Hard-delete the row. Returns `true` when a row was removed, `false` when
227
- * the username was not found. Used by `UserService.deleteUser` (and in turn
228
- * by the invite workflow's `auth/invite/cancel` step).
229
- */
230
- abstract delete(username: string): Promise<boolean>;
231
- /**
232
- * Run a read-modify-write cycle under optimistic concurrency. Each attempt
233
- * fetches the current row, calls `mutator` with it, and applies the returned
234
- * patch under CAS (`expectedVersion = current.version`). On CAS miss the
235
- * cycle repeats up to `opts.maxAttempts`. The mutator MAY return `null` to
236
- * exit early without writing — used for "race-loser detects nothing left to
237
- * do" paths (e.g. the backup code was already consumed by the winner).
238
- *
239
- * Throws `UserAuthError("NOT_FOUND")` when no row matches `username`, or
240
- * `UserAuthError("CAS_EXHAUSTED")` when retries are saturated. Errors
241
- * thrown from inside `mutator` propagate immediately without retry.
242
- */
243
- abstract withCas(username: string, mutator: (current: UserCredentials & T) => UserStoreUpdate | null, opts?: WithCasOptions): Promise<void>;
244
- }
245
- //#endregion
246
- export { UserServiceConfig as C, UserCredentials as S, PolicyCheckResult as _, LockStatus as a, TrustedDeviceRecord as b, MfaData as c, PasswordConfig as d, PasswordData as f, PasswordPolicyInstance as g, PasswordPolicyEvalFn as h, DeepPartial as i, MfaMethod as l, PasswordPolicyDef as m, WithCasOptions as n, LockoutConfig as o, PasswordPolicyContext as p, AccountData as r, LoginResult as s, UserStore as t, MfaMethodInfo as u, TotpConfig as v, UserStoreUpdate as w, UserAuthErrorType as x, TransferablePolicy as y };