@aooth/user 0.2.17 → 0.2.19

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,4 +1,4 @@
1
- import { A as UserStoreUpdate, O as UserCredentials, i as NewFederatedIdentity, n as FederatedIdentityStore, o as UserStore, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity } from "./federated-identity-store-D8BFFhpm.cjs";
1
+ import { A as UserStoreUpdate, O as UserCredentials, i as NewFederatedIdentity, n as FederatedIdentityStore, o as UserStore, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity } from "./federated-identity-store-CuvsHXtJ.cjs";
2
2
  /**
3
3
  * Structural surface of `AtscriptDbTable` covering exactly the methods this
4
4
  * adapter calls — kept loose so `@aooth/user` doesn't pull `@atscript/db` types
@@ -1,4 +1,4 @@
1
- import { A as UserStoreUpdate, O as UserCredentials, i as NewFederatedIdentity, n as FederatedIdentityStore, o as UserStore, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity } from "./federated-identity-store-D8BFFhpm.mjs";
1
+ import { A as UserStoreUpdate, O as UserCredentials, i as NewFederatedIdentity, n as FederatedIdentityStore, o as UserStore, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity } from "./federated-identity-store-CuvsHXtJ.mjs";
2
2
  /**
3
3
  * Structural surface of `AtscriptDbTable` covering exactly the methods this
4
4
  * adapter calls — kept loose so `@aooth/user` doesn't pull `@atscript/db` types
@@ -15,7 +15,8 @@ interface UserCredentials {
15
15
  /**
16
16
  * Persisted device-trust records ("remember this device, skip MFA next
17
17
  * time"). Managed by `UserService.{issue,add,verify,revoke,list}TrustedDevice`.
18
- * Absent when the user has never opted in.
18
+ * Absent when the user has never opted in. Emptied by every password change
19
+ * (`changePassword` / `setPassword`).
19
20
  */
20
21
  trustedDevices?: TrustedDeviceRecord[];
21
22
  /**
@@ -106,6 +107,12 @@ interface MfaMethod {
106
107
  }
107
108
  interface UserServiceConfig {
108
109
  password?: PasswordConfig;
110
+ /**
111
+ * Failed-sign-in lockout (password and TOTP failures share one counter).
112
+ * Omitted: 10 failures lock the account for 15 minutes. Passed: its own
113
+ * fields apply, each defaulting to `0` — `{ threshold: 0 }` turns lockout
114
+ * off, `duration: 0` makes a lock permanent.
115
+ */
109
116
  lockout?: LockoutConfig;
110
117
  /** Injectable clock for testability. Defaults to Date.now */
111
118
  clock?: () => number;
@@ -15,7 +15,8 @@ interface UserCredentials {
15
15
  /**
16
16
  * Persisted device-trust records ("remember this device, skip MFA next
17
17
  * time"). Managed by `UserService.{issue,add,verify,revoke,list}TrustedDevice`.
18
- * Absent when the user has never opted in.
18
+ * Absent when the user has never opted in. Emptied by every password change
19
+ * (`changePassword` / `setPassword`).
19
20
  */
20
21
  trustedDevices?: TrustedDeviceRecord[];
21
22
  /**
@@ -106,6 +107,12 @@ interface MfaMethod {
106
107
  }
107
108
  interface UserServiceConfig {
108
109
  password?: PasswordConfig;
110
+ /**
111
+ * Failed-sign-in lockout (password and TOTP failures share one counter).
112
+ * Omitted: 10 failures lock the account for 15 minutes. Passed: its own
113
+ * fields apply, each defaulting to `0` — `{ threshold: 0 }` turns lockout
114
+ * off, `duration: 0` makes a lock permanent.
115
+ */
109
116
  lockout?: LockoutConfig;
110
117
  /** Injectable clock for testability. Defaults to Date.now */
111
118
  clock?: () => number;
package/dist/index.cjs CHANGED
@@ -287,6 +287,11 @@ var PasswordPolicy = class {
287
287
  }
288
288
  };
289
289
  // src/user-service.ts
290
+ /** The lockout when `UserServiceConfig.lockout` is omitted: 10 failures lock for 15 minutes. */
291
+ const DEFAULT_LOCKOUT = {
292
+ threshold: 10,
293
+ duration: 9e5
294
+ };
290
295
  function resolveConfig(config) {
291
296
  return {
292
297
  password: {
@@ -299,10 +304,10 @@ function resolveConfig(config) {
299
304
  policies: normalizePolicies(config?.password?.policies),
300
305
  maxAgeMs: config?.password?.maxAgeMs ?? 0
301
306
  },
302
- lockout: {
303
- threshold: config?.lockout?.threshold ?? 0,
304
- duration: config?.lockout?.duration ?? 0
305
- },
307
+ lockout: config?.lockout ? {
308
+ threshold: config.lockout.threshold ?? 0,
309
+ duration: config.lockout.duration ?? 0
310
+ } : { ...DEFAULT_LOCKOUT },
306
311
  clock: config?.clock ?? Date.now,
307
312
  ...config?.deviceTrust && { deviceTrust: config.deviceTrust },
308
313
  ...config?.emailField && { emailField: config.emailField }
@@ -546,12 +551,22 @@ var UserService = class {
546
551
  if (!user) throw new require_federated_identity_store.UserAuthError("NOT_FOUND");
547
552
  return this.hasher.verify(password, user.password.hash);
548
553
  }
554
+ /**
555
+ * Change the password after verifying the current one. Applies the password
556
+ * policies and history, and drops every trusted device (remembered MFA skip)
557
+ * in the same write.
558
+ */
549
559
  async changePassword(id, currentPassword, newPassword, repeatPassword) {
550
560
  if (repeatPassword !== void 0 && newPassword !== repeatPassword) throw new require_federated_identity_store.UserAuthError("PASSWORDS_MISMATCH");
551
561
  const user = await this.getUser(id);
552
562
  if (!await this.hasher.verify(currentPassword, user.password.hash)) throw new require_federated_identity_store.UserAuthError("INVALID_CREDENTIALS");
553
563
  await this.applyPasswordChange(id, user, newPassword);
554
564
  }
565
+ /**
566
+ * Set a new password without the current one (recovery, a forced change, an
567
+ * admin reset). Applies the password policies and history, and drops every
568
+ * trusted device (remembered MFA skip) in the same write.
569
+ */
555
570
  async setPassword(id, newPassword) {
556
571
  const user = await this.getUser(id);
557
572
  await this.applyPasswordChange(id, user, newPassword);
@@ -595,6 +610,25 @@ var UserService = class {
595
610
  return accepted;
596
611
  }
597
612
  /**
613
+ * Refresh a pending invitation — a re-invite or a resend — in ONE
614
+ * compare-and-set write: applies `patch` (the new link marker
615
+ * `account.inviteNonce` and, for a re-invite, its roles, extras and issuer)
616
+ * only while the row is still pending. Returns `false` (nothing written)
617
+ * when the invite was accepted — also when the acceptance lands while the
618
+ * refresh runs — so no part of a refresh reaches a live account. Throws
619
+ * `UserAuthError("NOT_FOUND")` when no row matches `id`, and
620
+ * `UserAuthError("CAS_EXHAUSTED")` when concurrent writes outlast the retries.
621
+ */
622
+ async refreshInvitation(id, patch) {
623
+ let refreshed = false;
624
+ await this.store.withCas(id, (user) => {
625
+ refreshed = user.account.pendingInvitation === true;
626
+ if (!refreshed) return null;
627
+ return { set: patch };
628
+ });
629
+ return refreshed;
630
+ }
631
+ /**
598
632
  * Revoke a pending invitation: delete the invited account, so its invite
599
633
  * link stops working (the invite workflow answers it with 410
600
634
  * `INVITE_CANCELLED`). Returns `false` and deletes nothing when the account
@@ -1038,12 +1072,15 @@ var UserService = class {
1038
1072
  const newHash = await this.hasher.hash(newPassword);
1039
1073
  const limit = this.config.password.historyLength;
1040
1074
  const newHistory = limit > 0 ? [user.password.hash, ...user.password.history].filter(Boolean).slice(0, limit) : [];
1041
- await this.store.update(id, { set: { password: {
1042
- hash: newHash,
1043
- history: newHistory,
1044
- lastChanged: this.config.clock(),
1045
- isInitial: false
1046
- } } });
1075
+ await this.store.update(id, { set: {
1076
+ password: {
1077
+ hash: newHash,
1078
+ history: newHistory,
1079
+ lastChanged: this.config.clock(),
1080
+ isInitial: false
1081
+ },
1082
+ ...user.trustedDevices?.length ? { trustedDevices: [] } : {}
1083
+ } });
1047
1084
  }
1048
1085
  hasConfirmedMfaMethods(mfa) {
1049
1086
  return mfa.methods.some((m) => m.confirmed);
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as UserStoreUpdate, C as PolicyCheckResult, D as UserAuthErrorType, E as TrustedDeviceRecord, O as UserCredentials, S as PasswordPolicyParams, T as TransferablePolicy, _ as PasswordData, a as pickDefinedProfile, b as PasswordPolicyEvalFn, c as AccountData, d as LockoutConfig, f as LoginResult, g as PasswordConfig, h as MfaMethodInfo, i as NewFederatedIdentity, k as UserServiceConfig, l as DeepPartial, m as MfaMethod, n as FederatedIdentityStore, o as UserStore, p as MfaData, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity, u as LockStatus, v as PasswordPolicyContext, w as TotpConfig, x as PasswordPolicyInstance, y as PasswordPolicyDef } from "./federated-identity-store-D8BFFhpm.cjs";
1
+ import { A as UserStoreUpdate, C as PolicyCheckResult, D as UserAuthErrorType, E as TrustedDeviceRecord, O as UserCredentials, S as PasswordPolicyParams, T as TransferablePolicy, _ as PasswordData, a as pickDefinedProfile, b as PasswordPolicyEvalFn, c as AccountData, d as LockoutConfig, f as LoginResult, g as PasswordConfig, h as MfaMethodInfo, i as NewFederatedIdentity, k as UserServiceConfig, l as DeepPartial, m as MfaMethod, n as FederatedIdentityStore, o as UserStore, p as MfaData, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity, u as LockStatus, v as PasswordPolicyContext, w as TotpConfig, x as PasswordPolicyInstance, y as PasswordPolicyDef } from "./federated-identity-store-CuvsHXtJ.cjs";
2
2
  /**
3
3
  * The message code of each `UserAuthError` type — the id its text is
4
4
  * registered and translated by, and the `code` of the HTTP error made from it.
@@ -203,7 +203,17 @@ export declare class UserService<T extends object = object> {
203
203
  */
204
204
  recordLogin(id: string): Promise<number>;
205
205
  verifyPassword(id: string, password: string): Promise<boolean>;
206
+ /**
207
+ * Change the password after verifying the current one. Applies the password
208
+ * policies and history, and drops every trusted device (remembered MFA skip)
209
+ * in the same write.
210
+ */
206
211
  changePassword(id: string, currentPassword: string, newPassword: string, repeatPassword?: string): Promise<void>;
212
+ /**
213
+ * Set a new password without the current one (recovery, a forced change, an
214
+ * admin reset). Applies the password policies and history, and drops every
215
+ * trusted device (remembered MFA skip) in the same write.
216
+ */
207
217
  setPassword(id: string, newPassword: string): Promise<void>;
208
218
  /**
209
219
  * Hard-delete the user row by `id`. Returns nothing on success. Throws
@@ -227,6 +237,17 @@ export declare class UserService<T extends object = object> {
227
237
  * the same account can never both complete.
228
238
  */
229
239
  acceptInvitation(id: string, nonce: string | undefined): Promise<boolean>;
240
+ /**
241
+ * Refresh a pending invitation — a re-invite or a resend — in ONE
242
+ * compare-and-set write: applies `patch` (the new link marker
243
+ * `account.inviteNonce` and, for a re-invite, its roles, extras and issuer)
244
+ * only while the row is still pending. Returns `false` (nothing written)
245
+ * when the invite was accepted — also when the acceptance lands while the
246
+ * refresh runs — so no part of a refresh reaches a live account. Throws
247
+ * `UserAuthError("NOT_FOUND")` when no row matches `id`, and
248
+ * `UserAuthError("CAS_EXHAUSTED")` when concurrent writes outlast the retries.
249
+ */
250
+ refreshInvitation(id: string, patch: DeepPartial<UserCredentials & T>): Promise<boolean>;
230
251
  /**
231
252
  * Revoke a pending invitation: delete the invited account, so its invite
232
253
  * link stops working (the invite workflow answers it with 410
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { A as UserStoreUpdate, C as PolicyCheckResult, D as UserAuthErrorType, E as TrustedDeviceRecord, O as UserCredentials, S as PasswordPolicyParams, T as TransferablePolicy, _ as PasswordData, a as pickDefinedProfile, b as PasswordPolicyEvalFn, c as AccountData, d as LockoutConfig, f as LoginResult, g as PasswordConfig, h as MfaMethodInfo, i as NewFederatedIdentity, k as UserServiceConfig, l as DeepPartial, m as MfaMethod, n as FederatedIdentityStore, o as UserStore, p as MfaData, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity, u as LockStatus, v as PasswordPolicyContext, w as TotpConfig, x as PasswordPolicyInstance, y as PasswordPolicyDef } from "./federated-identity-store-D8BFFhpm.mjs";
1
+ import { A as UserStoreUpdate, C as PolicyCheckResult, D as UserAuthErrorType, E as TrustedDeviceRecord, O as UserCredentials, S as PasswordPolicyParams, T as TransferablePolicy, _ as PasswordData, a as pickDefinedProfile, b as PasswordPolicyEvalFn, c as AccountData, d as LockoutConfig, f as LoginResult, g as PasswordConfig, h as MfaMethodInfo, i as NewFederatedIdentity, k as UserServiceConfig, l as DeepPartial, m as MfaMethod, n as FederatedIdentityStore, o as UserStore, p as MfaData, r as FederatedProfileSnapshot, s as WithCasOptions, t as FederatedIdentity, u as LockStatus, v as PasswordPolicyContext, w as TotpConfig, x as PasswordPolicyInstance, y as PasswordPolicyDef } from "./federated-identity-store-CuvsHXtJ.mjs";
2
2
  /**
3
3
  * The message code of each `UserAuthError` type — the id its text is
4
4
  * registered and translated by, and the `code` of the HTTP error made from it.
@@ -203,7 +203,17 @@ export declare class UserService<T extends object = object> {
203
203
  */
204
204
  recordLogin(id: string): Promise<number>;
205
205
  verifyPassword(id: string, password: string): Promise<boolean>;
206
+ /**
207
+ * Change the password after verifying the current one. Applies the password
208
+ * policies and history, and drops every trusted device (remembered MFA skip)
209
+ * in the same write.
210
+ */
206
211
  changePassword(id: string, currentPassword: string, newPassword: string, repeatPassword?: string): Promise<void>;
212
+ /**
213
+ * Set a new password without the current one (recovery, a forced change, an
214
+ * admin reset). Applies the password policies and history, and drops every
215
+ * trusted device (remembered MFA skip) in the same write.
216
+ */
207
217
  setPassword(id: string, newPassword: string): Promise<void>;
208
218
  /**
209
219
  * Hard-delete the user row by `id`. Returns nothing on success. Throws
@@ -227,6 +237,17 @@ export declare class UserService<T extends object = object> {
227
237
  * the same account can never both complete.
228
238
  */
229
239
  acceptInvitation(id: string, nonce: string | undefined): Promise<boolean>;
240
+ /**
241
+ * Refresh a pending invitation — a re-invite or a resend — in ONE
242
+ * compare-and-set write: applies `patch` (the new link marker
243
+ * `account.inviteNonce` and, for a re-invite, its roles, extras and issuer)
244
+ * only while the row is still pending. Returns `false` (nothing written)
245
+ * when the invite was accepted — also when the acceptance lands while the
246
+ * refresh runs — so no part of a refresh reaches a live account. Throws
247
+ * `UserAuthError("NOT_FOUND")` when no row matches `id`, and
248
+ * `UserAuthError("CAS_EXHAUSTED")` when concurrent writes outlast the retries.
249
+ */
250
+ refreshInvitation(id: string, patch: DeepPartial<UserCredentials & T>): Promise<boolean>;
230
251
  /**
231
252
  * Revoke a pending invitation: delete the invited account, so its invite
232
253
  * link stops working (the invite workflow answers it with 410
package/dist/index.mjs CHANGED
@@ -286,6 +286,11 @@ var PasswordPolicy = class {
286
286
  }
287
287
  };
288
288
  // src/user-service.ts
289
+ /** The lockout when `UserServiceConfig.lockout` is omitted: 10 failures lock for 15 minutes. */
290
+ const DEFAULT_LOCKOUT = {
291
+ threshold: 10,
292
+ duration: 9e5
293
+ };
289
294
  function resolveConfig(config) {
290
295
  return {
291
296
  password: {
@@ -298,10 +303,10 @@ function resolveConfig(config) {
298
303
  policies: normalizePolicies(config?.password?.policies),
299
304
  maxAgeMs: config?.password?.maxAgeMs ?? 0
300
305
  },
301
- lockout: {
302
- threshold: config?.lockout?.threshold ?? 0,
303
- duration: config?.lockout?.duration ?? 0
304
- },
306
+ lockout: config?.lockout ? {
307
+ threshold: config.lockout.threshold ?? 0,
308
+ duration: config.lockout.duration ?? 0
309
+ } : { ...DEFAULT_LOCKOUT },
305
310
  clock: config?.clock ?? Date.now,
306
311
  ...config?.deviceTrust && { deviceTrust: config.deviceTrust },
307
312
  ...config?.emailField && { emailField: config.emailField }
@@ -545,12 +550,22 @@ var UserService = class {
545
550
  if (!user) throw new UserAuthError("NOT_FOUND");
546
551
  return this.hasher.verify(password, user.password.hash);
547
552
  }
553
+ /**
554
+ * Change the password after verifying the current one. Applies the password
555
+ * policies and history, and drops every trusted device (remembered MFA skip)
556
+ * in the same write.
557
+ */
548
558
  async changePassword(id, currentPassword, newPassword, repeatPassword) {
549
559
  if (repeatPassword !== void 0 && newPassword !== repeatPassword) throw new UserAuthError("PASSWORDS_MISMATCH");
550
560
  const user = await this.getUser(id);
551
561
  if (!await this.hasher.verify(currentPassword, user.password.hash)) throw new UserAuthError("INVALID_CREDENTIALS");
552
562
  await this.applyPasswordChange(id, user, newPassword);
553
563
  }
564
+ /**
565
+ * Set a new password without the current one (recovery, a forced change, an
566
+ * admin reset). Applies the password policies and history, and drops every
567
+ * trusted device (remembered MFA skip) in the same write.
568
+ */
554
569
  async setPassword(id, newPassword) {
555
570
  const user = await this.getUser(id);
556
571
  await this.applyPasswordChange(id, user, newPassword);
@@ -594,6 +609,25 @@ var UserService = class {
594
609
  return accepted;
595
610
  }
596
611
  /**
612
+ * Refresh a pending invitation — a re-invite or a resend — in ONE
613
+ * compare-and-set write: applies `patch` (the new link marker
614
+ * `account.inviteNonce` and, for a re-invite, its roles, extras and issuer)
615
+ * only while the row is still pending. Returns `false` (nothing written)
616
+ * when the invite was accepted — also when the acceptance lands while the
617
+ * refresh runs — so no part of a refresh reaches a live account. Throws
618
+ * `UserAuthError("NOT_FOUND")` when no row matches `id`, and
619
+ * `UserAuthError("CAS_EXHAUSTED")` when concurrent writes outlast the retries.
620
+ */
621
+ async refreshInvitation(id, patch) {
622
+ let refreshed = false;
623
+ await this.store.withCas(id, (user) => {
624
+ refreshed = user.account.pendingInvitation === true;
625
+ if (!refreshed) return null;
626
+ return { set: patch };
627
+ });
628
+ return refreshed;
629
+ }
630
+ /**
597
631
  * Revoke a pending invitation: delete the invited account, so its invite
598
632
  * link stops working (the invite workflow answers it with 410
599
633
  * `INVITE_CANCELLED`). Returns `false` and deletes nothing when the account
@@ -1037,12 +1071,15 @@ var UserService = class {
1037
1071
  const newHash = await this.hasher.hash(newPassword);
1038
1072
  const limit = this.config.password.historyLength;
1039
1073
  const newHistory = limit > 0 ? [user.password.hash, ...user.password.history].filter(Boolean).slice(0, limit) : [];
1040
- await this.store.update(id, { set: { password: {
1041
- hash: newHash,
1042
- history: newHistory,
1043
- lastChanged: this.config.clock(),
1044
- isInitial: false
1045
- } } });
1074
+ await this.store.update(id, { set: {
1075
+ password: {
1076
+ hash: newHash,
1077
+ history: newHistory,
1078
+ lastChanged: this.config.clock(),
1079
+ isInitial: false
1080
+ },
1081
+ ...user.trustedDevices?.length ? { trustedDevices: [] } : {}
1082
+ } });
1046
1083
  }
1047
1084
  hasConfirmedMfaMethods(mfa) {
1048
1085
  return mfa.methods.some((m) => m.confirmed);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aooth/user",
3
- "version": "0.2.17",
3
+ "version": "0.2.19",
4
4
  "description": "User credential primitives for aoothjs",
5
5
  "keywords": [
6
6
  "aoothjs",
@@ -64,18 +64,18 @@
64
64
  "access": "public"
65
65
  },
66
66
  "devDependencies": {
67
- "@atscript/core": "^0.2.14",
68
- "@atscript/db": "^0.1.186",
69
- "@atscript/db-sql-tools": "^0.1.186",
70
- "@atscript/db-sqlite": "^0.1.186",
71
- "@atscript/typescript": "^0.2.14",
67
+ "@atscript/core": "^0.2.16",
68
+ "@atscript/db": "^0.1.188",
69
+ "@atscript/db-sql-tools": "^0.1.188",
70
+ "@atscript/db-sqlite": "^0.1.188",
71
+ "@atscript/typescript": "^0.2.16",
72
72
  "@types/better-sqlite3": "^7.6.13",
73
73
  "@uniqu/core": "^0.2.2",
74
74
  "better-sqlite3": "^12.6.2",
75
- "unplugin-atscript": "^0.2.14"
75
+ "unplugin-atscript": "^0.2.16"
76
76
  },
77
77
  "peerDependencies": {
78
- "@atscript/db": "^0.1.186"
78
+ "@atscript/db": "^0.1.188"
79
79
  },
80
80
  "peerDependenciesMeta": {
81
81
  "@atscript/db": {