@aooth/user 0.2.5 → 0.2.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.
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_federated_identity_store = require("./federated-identity-store-DL0He6X_.cjs");
2
+ const require_federated_identity_store = require("./federated-identity-store-8G0e79Zc.cjs");
3
3
  let node_crypto = require("node:crypto");
4
4
  // src/atscript-db/federated-identity-store.ts
5
5
  function isConflict$1(err) {
@@ -1,4 +1,4 @@
1
- import { c as setAtPath, d as UserAuthError, l as UserStore, n as pickDefinedProfile, t as FederatedIdentityStore } from "./federated-identity-store-B1J4lMeN.mjs";
1
+ import { c as setAtPath, f as UserAuthError, l as UserStore, n as pickDefinedProfile, t as FederatedIdentityStore } from "./federated-identity-store-CUjGv4O7.mjs";
2
2
  import { randomUUID } from "node:crypto";
3
3
  // src/atscript-db/federated-identity-store.ts
4
4
  function isConflict$1(err) {
@@ -1,35 +1,62 @@
1
1
  require("node:crypto");
2
2
  // src/errors.ts
3
3
  /**
4
- * The default English message of each `UserAuthError` type — the error's
5
- * `type` is its message code.
4
+ * The message code of each `UserAuthError` type — the id its text is
5
+ * registered and translated by, and the `code` of the HTTP error made from it.
6
+ * Message codes share one process-wide id space with every other library, so
7
+ * types with a generic name get an `AOOTH_USER_` code; `NOT_FOUND` and
8
+ * `INVALID_CREDENTIALS` are shared on purpose and keep neutral texts.
9
+ */
10
+ const USER_AUTH_ERROR_CODES = Object.freeze({
11
+ NOT_FOUND: "NOT_FOUND",
12
+ ALREADY_EXISTS: "AOOTH_USER_ALREADY_EXISTS",
13
+ LOCKED: "AOOTH_USER_LOCKED",
14
+ INACTIVE: "AOOTH_USER_INACTIVE",
15
+ INVALID_CREDENTIALS: "INVALID_CREDENTIALS",
16
+ POLICY_VIOLATION: "AOOTH_USER_POLICY_VIOLATION",
17
+ PASSWORDS_MISMATCH: "PASSWORDS_MISMATCH",
18
+ PASSWORD_IN_HISTORY: "PASSWORD_IN_HISTORY",
19
+ MFA_REQUIRED: "MFA_REQUIRED",
20
+ MFA_INVALID: "MFA_INVALID",
21
+ MFA_NOT_CONFIGURED: "MFA_NOT_CONFIGURED",
22
+ CAS_EXHAUSTED: "AOOTH_USER_CAS_EXHAUSTED"
23
+ });
24
+ /**
25
+ * The default English message of each `UserAuthError`, by its message code
26
+ * ({@link USER_AUTH_ERROR_CODES}).
6
27
  *
7
28
  * `@aooth/auth-moost` registers it with moost's `registerMessages`, so a moost
8
- * translator (`setTranslator`) translates a `UserAuthError` by its type and the
29
+ * translator (`setTranslator`) translates a `UserAuthError` by its code and the
9
30
  * texts reach the `$i18n` the server sends with form schemas and `/meta`.
10
31
  */
11
32
  const USER_AUTH_ERROR_MESSAGES = Object.freeze({
12
- NOT_FOUND: "User not found",
13
- ALREADY_EXISTS: "User already exists",
14
- LOCKED: "Account is locked",
15
- INACTIVE: "Account is not active",
33
+ /** Shared on purpose — the same neutral text as other libraries' `NOT_FOUND`. */
34
+ NOT_FOUND: "Not found",
35
+ AOOTH_USER_ALREADY_EXISTS: "User already exists",
36
+ AOOTH_USER_LOCKED: "Account is locked",
37
+ AOOTH_USER_INACTIVE: "Account is not active",
38
+ /** Shared on purpose — also the sign-in workflow's code, with the same text. */
16
39
  INVALID_CREDENTIALS: "Invalid credentials",
17
- POLICY_VIOLATION: "Password does not meet policy requirements",
40
+ AOOTH_USER_POLICY_VIOLATION: "Password does not meet policy requirements",
18
41
  PASSWORDS_MISMATCH: "Passwords do not match",
19
42
  PASSWORD_IN_HISTORY: "Password was recently used",
20
43
  MFA_REQUIRED: "Multi-factor authentication is required",
21
44
  MFA_INVALID: "Invalid MFA code",
22
45
  MFA_NOT_CONFIGURED: "MFA method is not configured",
23
- CAS_EXHAUSTED: "Update conflict — please retry"
46
+ AOOTH_USER_CAS_EXHAUSTED: "Update conflict — please retry"
24
47
  });
25
48
  var UserAuthError = class extends Error {
26
49
  type;
27
50
  details;
28
51
  name = "UserAuthError";
52
+ /** The message code — the id the error's text is translated by ({@link USER_AUTH_ERROR_CODES}). */
53
+ code;
29
54
  constructor(type, message, details) {
30
- super(message ?? USER_AUTH_ERROR_MESSAGES[type]);
55
+ const code = USER_AUTH_ERROR_CODES[type];
56
+ super(message ?? USER_AUTH_ERROR_MESSAGES[code]);
31
57
  this.type = type;
32
58
  this.details = details;
59
+ this.code = code;
33
60
  }
34
61
  };
35
62
  // src/store/user-store.ts
@@ -137,6 +164,12 @@ Object.defineProperty(exports, "FederatedIdentityStore", {
137
164
  return FederatedIdentityStore;
138
165
  }
139
166
  });
167
+ Object.defineProperty(exports, "USER_AUTH_ERROR_CODES", {
168
+ enumerable: true,
169
+ get: function() {
170
+ return USER_AUTH_ERROR_CODES;
171
+ }
172
+ });
140
173
  Object.defineProperty(exports, "USER_AUTH_ERROR_MESSAGES", {
141
174
  enumerable: true,
142
175
  get: function() {
@@ -1,35 +1,62 @@
1
1
  import "node:crypto";
2
2
  // src/errors.ts
3
3
  /**
4
- * The default English message of each `UserAuthError` type — the error's
5
- * `type` is its message code.
4
+ * The message code of each `UserAuthError` type — the id its text is
5
+ * registered and translated by, and the `code` of the HTTP error made from it.
6
+ * Message codes share one process-wide id space with every other library, so
7
+ * types with a generic name get an `AOOTH_USER_` code; `NOT_FOUND` and
8
+ * `INVALID_CREDENTIALS` are shared on purpose and keep neutral texts.
9
+ */
10
+ const USER_AUTH_ERROR_CODES = Object.freeze({
11
+ NOT_FOUND: "NOT_FOUND",
12
+ ALREADY_EXISTS: "AOOTH_USER_ALREADY_EXISTS",
13
+ LOCKED: "AOOTH_USER_LOCKED",
14
+ INACTIVE: "AOOTH_USER_INACTIVE",
15
+ INVALID_CREDENTIALS: "INVALID_CREDENTIALS",
16
+ POLICY_VIOLATION: "AOOTH_USER_POLICY_VIOLATION",
17
+ PASSWORDS_MISMATCH: "PASSWORDS_MISMATCH",
18
+ PASSWORD_IN_HISTORY: "PASSWORD_IN_HISTORY",
19
+ MFA_REQUIRED: "MFA_REQUIRED",
20
+ MFA_INVALID: "MFA_INVALID",
21
+ MFA_NOT_CONFIGURED: "MFA_NOT_CONFIGURED",
22
+ CAS_EXHAUSTED: "AOOTH_USER_CAS_EXHAUSTED"
23
+ });
24
+ /**
25
+ * The default English message of each `UserAuthError`, by its message code
26
+ * ({@link USER_AUTH_ERROR_CODES}).
6
27
  *
7
28
  * `@aooth/auth-moost` registers it with moost's `registerMessages`, so a moost
8
- * translator (`setTranslator`) translates a `UserAuthError` by its type and the
29
+ * translator (`setTranslator`) translates a `UserAuthError` by its code and the
9
30
  * texts reach the `$i18n` the server sends with form schemas and `/meta`.
10
31
  */
11
32
  const USER_AUTH_ERROR_MESSAGES = Object.freeze({
12
- NOT_FOUND: "User not found",
13
- ALREADY_EXISTS: "User already exists",
14
- LOCKED: "Account is locked",
15
- INACTIVE: "Account is not active",
33
+ /** Shared on purpose — the same neutral text as other libraries' `NOT_FOUND`. */
34
+ NOT_FOUND: "Not found",
35
+ AOOTH_USER_ALREADY_EXISTS: "User already exists",
36
+ AOOTH_USER_LOCKED: "Account is locked",
37
+ AOOTH_USER_INACTIVE: "Account is not active",
38
+ /** Shared on purpose — also the sign-in workflow's code, with the same text. */
16
39
  INVALID_CREDENTIALS: "Invalid credentials",
17
- POLICY_VIOLATION: "Password does not meet policy requirements",
40
+ AOOTH_USER_POLICY_VIOLATION: "Password does not meet policy requirements",
18
41
  PASSWORDS_MISMATCH: "Passwords do not match",
19
42
  PASSWORD_IN_HISTORY: "Password was recently used",
20
43
  MFA_REQUIRED: "Multi-factor authentication is required",
21
44
  MFA_INVALID: "Invalid MFA code",
22
45
  MFA_NOT_CONFIGURED: "MFA method is not configured",
23
- CAS_EXHAUSTED: "Update conflict — please retry"
46
+ AOOTH_USER_CAS_EXHAUSTED: "Update conflict — please retry"
24
47
  });
25
48
  var UserAuthError = class extends Error {
26
49
  type;
27
50
  details;
28
51
  name = "UserAuthError";
52
+ /** The message code — the id the error's text is translated by ({@link USER_AUTH_ERROR_CODES}). */
53
+ code;
29
54
  constructor(type, message, details) {
30
- super(message ?? USER_AUTH_ERROR_MESSAGES[type]);
55
+ const code = USER_AUTH_ERROR_CODES[type];
56
+ super(message ?? USER_AUTH_ERROR_MESSAGES[code]);
31
57
  this.type = type;
32
58
  this.details = details;
59
+ this.code = code;
33
60
  }
34
61
  };
35
62
  // src/store/user-store.ts
@@ -131,4 +158,4 @@ function pickDefinedProfile(src) {
131
158
  * keeps the federated-login core (`@aooth/idp`, phase 2) storage-agnostic.
132
159
  */
133
160
  var FederatedIdentityStore = class {};
134
- export { maskEmail as a, setAtPath as c, UserAuthError as d, incrementAtPath as i, UserStore as l, pickDefinedProfile as n, maskMfaValue as o, deepMerge as r, maskPhone as s, FederatedIdentityStore as t, USER_AUTH_ERROR_MESSAGES as u };
161
+ export { maskEmail as a, setAtPath as c, USER_AUTH_ERROR_MESSAGES as d, UserAuthError as f, incrementAtPath as i, UserStore as l, pickDefinedProfile as n, maskMfaValue as o, deepMerge as r, maskPhone as s, FederatedIdentityStore as t, USER_AUTH_ERROR_CODES as u };
package/dist/index.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_federated_identity_store = require("./federated-identity-store-DL0He6X_.cjs");
2
+ const require_federated_identity_store = require("./federated-identity-store-8G0e79Zc.cjs");
3
3
  let node_crypto = require("node:crypto");
4
4
  // src/base-x/base32.ts
5
5
  /**
@@ -509,6 +509,18 @@ var UserService = class {
509
509
  return this.incrementAndMaybeLock(user.id, user.account, "INVALID_CREDENTIALS", lockoutOverride);
510
510
  }
511
511
  /**
512
+ * The account checks `login` runs before the password, without one — for a
513
+ * sign-in that proves the first factor another way (a magic link): throws
514
+ * `NOT_FOUND`, `INACTIVE` (incl. a pending invitation) or `LOCKED`, and
515
+ * lifts an expired lock like `login` does. Returns the fresh row.
516
+ */
517
+ async assertCanSignIn(id) {
518
+ const user = await this.getUser(id);
519
+ if (!user.account.active) throw new require_federated_identity_store.UserAuthError("INACTIVE");
520
+ await this.ensureNotLockedOrThrow(user.id, user.account);
521
+ return user;
522
+ }
523
+ /**
512
524
  * Stamp a successful login on the user record: `account.lastLogin = now` and
513
525
  * reset `account.failedLoginAttempts = 0`. The single writer of `lastLogin`.
514
526
  *
@@ -756,9 +768,7 @@ var UserService = class {
756
768
  * total tries across BOTH factors, not `2 * threshold`.
757
769
  */
758
770
  async verifyMfa(id, code, config, lockoutOverride) {
759
- const user = await this.getUser(id);
760
- if (!user.account.active) throw new require_federated_identity_store.UserAuthError("INACTIVE");
761
- await this.ensureNotLockedOrThrow(id, user.account);
771
+ const user = await this.assertCanSignIn(id);
762
772
  const totp = user.mfa.methods.find((m) => m.name === "totp" && m.confirmed);
763
773
  if (!totp) throw new require_federated_identity_store.UserAuthError("MFA_NOT_CONFIGURED");
764
774
  const matchedCounter = verifyTotpCode(totp.value, code, config);
@@ -1429,6 +1439,7 @@ exports.PASSWORD_POLICY_MESSAGES = PASSWORD_POLICY_MESSAGES;
1429
1439
  exports.PasswordHasher = PasswordHasher;
1430
1440
  exports.PasswordPolicy = PasswordPolicy;
1431
1441
  exports.SEEN_DEVICES_DEFAULT_CAP = SEEN_DEVICES_DEFAULT_CAP;
1442
+ exports.USER_AUTH_ERROR_CODES = require_federated_identity_store.USER_AUTH_ERROR_CODES;
1432
1443
  exports.USER_AUTH_ERROR_MESSAGES = require_federated_identity_store.USER_AUTH_ERROR_MESSAGES;
1433
1444
  exports.UserAuthError = require_federated_identity_store.UserAuthError;
1434
1445
  exports.UserService = UserService;
package/dist/index.d.cts CHANGED
@@ -1,17 +1,42 @@
1
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";
2
2
  /**
3
- * The default English message of each `UserAuthError` type — the error's
4
- * `type` is its message code.
3
+ * The message code of each `UserAuthError` type — the id its text is
4
+ * registered and translated by, and the `code` of the HTTP error made from it.
5
+ * Message codes share one process-wide id space with every other library, so
6
+ * types with a generic name get an `AOOTH_USER_` code; `NOT_FOUND` and
7
+ * `INVALID_CREDENTIALS` are shared on purpose and keep neutral texts.
8
+ */
9
+ export declare const USER_AUTH_ERROR_CODES: Readonly<{
10
+ readonly NOT_FOUND: "NOT_FOUND";
11
+ readonly ALREADY_EXISTS: "AOOTH_USER_ALREADY_EXISTS";
12
+ readonly LOCKED: "AOOTH_USER_LOCKED";
13
+ readonly INACTIVE: "AOOTH_USER_INACTIVE";
14
+ readonly INVALID_CREDENTIALS: "INVALID_CREDENTIALS";
15
+ readonly POLICY_VIOLATION: "AOOTH_USER_POLICY_VIOLATION";
16
+ readonly PASSWORDS_MISMATCH: "PASSWORDS_MISMATCH";
17
+ readonly PASSWORD_IN_HISTORY: "PASSWORD_IN_HISTORY";
18
+ readonly MFA_REQUIRED: "MFA_REQUIRED";
19
+ readonly MFA_INVALID: "MFA_INVALID";
20
+ readonly MFA_NOT_CONFIGURED: "MFA_NOT_CONFIGURED";
21
+ readonly CAS_EXHAUSTED: "AOOTH_USER_CAS_EXHAUSTED";
22
+ }>;
23
+ /** A message code of a `UserAuthError` — see {@link USER_AUTH_ERROR_CODES}. */
24
+ type UserAuthErrorCode = (typeof USER_AUTH_ERROR_CODES)[UserAuthErrorType];
25
+ /**
26
+ * The default English message of each `UserAuthError`, by its message code
27
+ * ({@link USER_AUTH_ERROR_CODES}).
5
28
  *
6
29
  * `@aooth/auth-moost` registers it with moost's `registerMessages`, so a moost
7
- * translator (`setTranslator`) translates a `UserAuthError` by its type and the
30
+ * translator (`setTranslator`) translates a `UserAuthError` by its code and the
8
31
  * texts reach the `$i18n` the server sends with form schemas and `/meta`.
9
32
  */
10
- export declare const USER_AUTH_ERROR_MESSAGES: Readonly<Record<UserAuthErrorType, string>>;
33
+ export declare const USER_AUTH_ERROR_MESSAGES: Readonly<Record<UserAuthErrorCode, string>>;
11
34
  export declare class UserAuthError extends Error {
12
35
  readonly type: UserAuthErrorType;
13
36
  readonly details?: Record<string, unknown> | undefined;
14
37
  readonly name = "UserAuthError";
38
+ /** The message code — the id the error's text is translated by ({@link USER_AUTH_ERROR_CODES}). */
39
+ readonly code: UserAuthErrorCode;
15
40
  constructor(type: UserAuthErrorType, message?: string, details?: Record<string, unknown> | undefined);
16
41
  }
17
42
  export declare class PasswordHasher {
@@ -156,6 +181,13 @@ export declare class UserService<T extends object = object> {
156
181
  */
157
182
  findByIdentifier(value: string): Promise<(UserCredentials & T) | null>;
158
183
  login(handle: string, password: string, lockoutOverride?: Partial<LockoutConfig>): Promise<LoginResult<T>>;
184
+ /**
185
+ * The account checks `login` runs before the password, without one — for a
186
+ * sign-in that proves the first factor another way (a magic link): throws
187
+ * `NOT_FOUND`, `INACTIVE` (incl. a pending invitation) or `LOCKED`, and
188
+ * lifts an expired lock like `login` does. Returns the fresh row.
189
+ */
190
+ assertCanSignIn(id: string): Promise<UserCredentials & T>;
159
191
  /**
160
192
  * Stamp a successful login on the user record: `account.lastLogin = now` and
161
193
  * reset `account.failedLoginAttempts = 0`. The single writer of `lastLogin`.
@@ -536,4 +568,4 @@ export declare function maskEmail(email: string): string;
536
568
  export declare function maskPhone(phone: string): string;
537
569
  export declare function maskMfaValue(method: MfaMethod): string;
538
570
  export declare function setAtPath(obj: object, path: string, value: unknown): void;
539
- export { type AccountData, type DeepPartial, type FederatedIdentity, FederatedIdentityStore, type FederatedIdentityStoreMemoryOptions, type FederatedProfileSnapshot, type LockStatus, type LockoutConfig, type LoginResult, type MfaData, type MfaMethod, type MfaMethodInfo, type NewFederatedIdentity, type PasswordConfig, type PasswordData, type PasswordPolicyContext, type PasswordPolicyDef, type PasswordPolicyEvalFn, type PasswordPolicyInstance, type PasswordPolicyMessageCode, type PasswordPolicyParams, type PolicyCheckResult, type TotpConfig, type TransferablePolicy, type TrustedDeviceRecord, type UserAuthErrorType, type UserCredentials, type UserServiceConfig, UserStore, type UserStoreUpdate, pickDefinedProfile };
571
+ export { type AccountData, type DeepPartial, type FederatedIdentity, FederatedIdentityStore, type FederatedIdentityStoreMemoryOptions, type FederatedProfileSnapshot, type LockStatus, type LockoutConfig, type LoginResult, type MfaData, type MfaMethod, type MfaMethodInfo, type NewFederatedIdentity, type PasswordConfig, type PasswordData, type PasswordPolicyContext, type PasswordPolicyDef, type PasswordPolicyEvalFn, type PasswordPolicyInstance, type PasswordPolicyMessageCode, type PasswordPolicyParams, type PolicyCheckResult, type TotpConfig, type TransferablePolicy, type TrustedDeviceRecord, type UserAuthErrorCode, type UserAuthErrorType, type UserCredentials, type UserServiceConfig, UserStore, type UserStoreUpdate, pickDefinedProfile };
package/dist/index.d.mts CHANGED
@@ -1,17 +1,42 @@
1
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";
2
2
  /**
3
- * The default English message of each `UserAuthError` type — the error's
4
- * `type` is its message code.
3
+ * The message code of each `UserAuthError` type — the id its text is
4
+ * registered and translated by, and the `code` of the HTTP error made from it.
5
+ * Message codes share one process-wide id space with every other library, so
6
+ * types with a generic name get an `AOOTH_USER_` code; `NOT_FOUND` and
7
+ * `INVALID_CREDENTIALS` are shared on purpose and keep neutral texts.
8
+ */
9
+ export declare const USER_AUTH_ERROR_CODES: Readonly<{
10
+ readonly NOT_FOUND: "NOT_FOUND";
11
+ readonly ALREADY_EXISTS: "AOOTH_USER_ALREADY_EXISTS";
12
+ readonly LOCKED: "AOOTH_USER_LOCKED";
13
+ readonly INACTIVE: "AOOTH_USER_INACTIVE";
14
+ readonly INVALID_CREDENTIALS: "INVALID_CREDENTIALS";
15
+ readonly POLICY_VIOLATION: "AOOTH_USER_POLICY_VIOLATION";
16
+ readonly PASSWORDS_MISMATCH: "PASSWORDS_MISMATCH";
17
+ readonly PASSWORD_IN_HISTORY: "PASSWORD_IN_HISTORY";
18
+ readonly MFA_REQUIRED: "MFA_REQUIRED";
19
+ readonly MFA_INVALID: "MFA_INVALID";
20
+ readonly MFA_NOT_CONFIGURED: "MFA_NOT_CONFIGURED";
21
+ readonly CAS_EXHAUSTED: "AOOTH_USER_CAS_EXHAUSTED";
22
+ }>;
23
+ /** A message code of a `UserAuthError` — see {@link USER_AUTH_ERROR_CODES}. */
24
+ type UserAuthErrorCode = (typeof USER_AUTH_ERROR_CODES)[UserAuthErrorType];
25
+ /**
26
+ * The default English message of each `UserAuthError`, by its message code
27
+ * ({@link USER_AUTH_ERROR_CODES}).
5
28
  *
6
29
  * `@aooth/auth-moost` registers it with moost's `registerMessages`, so a moost
7
- * translator (`setTranslator`) translates a `UserAuthError` by its type and the
30
+ * translator (`setTranslator`) translates a `UserAuthError` by its code and the
8
31
  * texts reach the `$i18n` the server sends with form schemas and `/meta`.
9
32
  */
10
- export declare const USER_AUTH_ERROR_MESSAGES: Readonly<Record<UserAuthErrorType, string>>;
33
+ export declare const USER_AUTH_ERROR_MESSAGES: Readonly<Record<UserAuthErrorCode, string>>;
11
34
  export declare class UserAuthError extends Error {
12
35
  readonly type: UserAuthErrorType;
13
36
  readonly details?: Record<string, unknown> | undefined;
14
37
  readonly name = "UserAuthError";
38
+ /** The message code — the id the error's text is translated by ({@link USER_AUTH_ERROR_CODES}). */
39
+ readonly code: UserAuthErrorCode;
15
40
  constructor(type: UserAuthErrorType, message?: string, details?: Record<string, unknown> | undefined);
16
41
  }
17
42
  export declare class PasswordHasher {
@@ -156,6 +181,13 @@ export declare class UserService<T extends object = object> {
156
181
  */
157
182
  findByIdentifier(value: string): Promise<(UserCredentials & T) | null>;
158
183
  login(handle: string, password: string, lockoutOverride?: Partial<LockoutConfig>): Promise<LoginResult<T>>;
184
+ /**
185
+ * The account checks `login` runs before the password, without one — for a
186
+ * sign-in that proves the first factor another way (a magic link): throws
187
+ * `NOT_FOUND`, `INACTIVE` (incl. a pending invitation) or `LOCKED`, and
188
+ * lifts an expired lock like `login` does. Returns the fresh row.
189
+ */
190
+ assertCanSignIn(id: string): Promise<UserCredentials & T>;
159
191
  /**
160
192
  * Stamp a successful login on the user record: `account.lastLogin = now` and
161
193
  * reset `account.failedLoginAttempts = 0`. The single writer of `lastLogin`.
@@ -536,4 +568,4 @@ export declare function maskEmail(email: string): string;
536
568
  export declare function maskPhone(phone: string): string;
537
569
  export declare function maskMfaValue(method: MfaMethod): string;
538
570
  export declare function setAtPath(obj: object, path: string, value: unknown): void;
539
- export { type AccountData, type DeepPartial, type FederatedIdentity, FederatedIdentityStore, type FederatedIdentityStoreMemoryOptions, type FederatedProfileSnapshot, type LockStatus, type LockoutConfig, type LoginResult, type MfaData, type MfaMethod, type MfaMethodInfo, type NewFederatedIdentity, type PasswordConfig, type PasswordData, type PasswordPolicyContext, type PasswordPolicyDef, type PasswordPolicyEvalFn, type PasswordPolicyInstance, type PasswordPolicyMessageCode, type PasswordPolicyParams, type PolicyCheckResult, type TotpConfig, type TransferablePolicy, type TrustedDeviceRecord, type UserAuthErrorType, type UserCredentials, type UserServiceConfig, UserStore, type UserStoreUpdate, pickDefinedProfile };
571
+ export { type AccountData, type DeepPartial, type FederatedIdentity, FederatedIdentityStore, type FederatedIdentityStoreMemoryOptions, type FederatedProfileSnapshot, type LockStatus, type LockoutConfig, type LoginResult, type MfaData, type MfaMethod, type MfaMethodInfo, type NewFederatedIdentity, type PasswordConfig, type PasswordData, type PasswordPolicyContext, type PasswordPolicyDef, type PasswordPolicyEvalFn, type PasswordPolicyInstance, type PasswordPolicyMessageCode, type PasswordPolicyParams, type PolicyCheckResult, type TotpConfig, type TransferablePolicy, type TrustedDeviceRecord, type UserAuthErrorCode, type UserAuthErrorType, type UserCredentials, type UserServiceConfig, UserStore, type UserStoreUpdate, pickDefinedProfile };
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { a as maskEmail, c as setAtPath, d as UserAuthError, i as incrementAtPath, l as UserStore, n as pickDefinedProfile, o as maskMfaValue, r as deepMerge, s as maskPhone, t as FederatedIdentityStore, u as USER_AUTH_ERROR_MESSAGES } from "./federated-identity-store-B1J4lMeN.mjs";
1
+ import { a as maskEmail, c as setAtPath, d as USER_AUTH_ERROR_MESSAGES, f as UserAuthError, i as incrementAtPath, l as UserStore, n as pickDefinedProfile, o as maskMfaValue, r as deepMerge, s as maskPhone, t as FederatedIdentityStore, u as USER_AUTH_ERROR_CODES } from "./federated-identity-store-CUjGv4O7.mjs";
2
2
  import { createHash, createHmac, randomBytes, randomInt, randomUUID, scrypt, timingSafeEqual } from "node:crypto";
3
3
  // src/base-x/base32.ts
4
4
  /**
@@ -508,6 +508,18 @@ var UserService = class {
508
508
  return this.incrementAndMaybeLock(user.id, user.account, "INVALID_CREDENTIALS", lockoutOverride);
509
509
  }
510
510
  /**
511
+ * The account checks `login` runs before the password, without one — for a
512
+ * sign-in that proves the first factor another way (a magic link): throws
513
+ * `NOT_FOUND`, `INACTIVE` (incl. a pending invitation) or `LOCKED`, and
514
+ * lifts an expired lock like `login` does. Returns the fresh row.
515
+ */
516
+ async assertCanSignIn(id) {
517
+ const user = await this.getUser(id);
518
+ if (!user.account.active) throw new UserAuthError("INACTIVE");
519
+ await this.ensureNotLockedOrThrow(user.id, user.account);
520
+ return user;
521
+ }
522
+ /**
511
523
  * Stamp a successful login on the user record: `account.lastLogin = now` and
512
524
  * reset `account.failedLoginAttempts = 0`. The single writer of `lastLogin`.
513
525
  *
@@ -755,9 +767,7 @@ var UserService = class {
755
767
  * total tries across BOTH factors, not `2 * threshold`.
756
768
  */
757
769
  async verifyMfa(id, code, config, lockoutOverride) {
758
- const user = await this.getUser(id);
759
- if (!user.account.active) throw new UserAuthError("INACTIVE");
760
- await this.ensureNotLockedOrThrow(id, user.account);
770
+ const user = await this.assertCanSignIn(id);
761
771
  const totp = user.mfa.methods.find((m) => m.name === "totp" && m.confirmed);
762
772
  if (!totp) throw new UserAuthError("MFA_NOT_CONFIGURED");
763
773
  const matchedCounter = verifyTotpCode(totp.value, code, config);
@@ -1422,4 +1432,4 @@ function verifyMfaCode(submitted, expectedHash) {
1422
1432
  if (a.length !== b.length) return false;
1423
1433
  return timingSafeEqual(a, b);
1424
1434
  }
1425
- export { FederatedIdentityStore, FederatedIdentityStoreMemory, PASSWORD_POLICY_MESSAGES, PasswordHasher, PasswordPolicy, SEEN_DEVICES_DEFAULT_CAP, USER_AUTH_ERROR_MESSAGES, UserAuthError, UserService, UserStore, UserStoreMemory, definePasswordPolicy, generateMfaCode, generateTotpCode, generateTotpSecret, generateTotpUri, hashMfaCode, maskEmail, maskMfaValue, maskPhone, normalizePolicies, pickDefinedProfile, ppHasLowerCase, ppHasMinLength, ppHasNumber, ppHasSpecialChar, ppHasUpperCase, ppMaxRepeatedChars, setAtPath, verifyMfaCode, verifyTotpCode };
1435
+ export { FederatedIdentityStore, FederatedIdentityStoreMemory, PASSWORD_POLICY_MESSAGES, PasswordHasher, PasswordPolicy, SEEN_DEVICES_DEFAULT_CAP, USER_AUTH_ERROR_CODES, USER_AUTH_ERROR_MESSAGES, UserAuthError, UserService, UserStore, UserStoreMemory, definePasswordPolicy, generateMfaCode, generateTotpCode, generateTotpSecret, generateTotpUri, hashMfaCode, maskEmail, maskMfaValue, maskPhone, normalizePolicies, pickDefinedProfile, ppHasLowerCase, ppHasMinLength, ppHasNumber, ppHasSpecialChar, ppHasUpperCase, ppMaxRepeatedChars, setAtPath, verifyMfaCode, verifyTotpCode };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aooth/user",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "User credential primitives for aoothjs",
5
5
  "keywords": [
6
6
  "aoothjs",
@@ -60,18 +60,18 @@
60
60
  "access": "public"
61
61
  },
62
62
  "devDependencies": {
63
- "@atscript/core": "^0.2.8",
64
- "@atscript/db": "^0.1.171",
65
- "@atscript/db-sql-tools": "^0.1.171",
66
- "@atscript/db-sqlite": "^0.1.171",
67
- "@atscript/typescript": "^0.2.8",
63
+ "@atscript/core": "^0.2.9",
64
+ "@atscript/db": "^0.1.174",
65
+ "@atscript/db-sql-tools": "^0.1.174",
66
+ "@atscript/db-sqlite": "^0.1.174",
67
+ "@atscript/typescript": "^0.2.9",
68
68
  "@types/better-sqlite3": "^7.6.13",
69
69
  "@uniqu/core": "^0.2.1",
70
70
  "better-sqlite3": "^12.6.2",
71
- "unplugin-atscript": "^0.2.8"
71
+ "unplugin-atscript": "^0.2.9"
72
72
  },
73
73
  "peerDependencies": {
74
- "@atscript/db": "^0.1.171"
74
+ "@atscript/db": "^0.1.174"
75
75
  },
76
76
  "peerDependenciesMeta": {
77
77
  "@atscript/db": {