@jini-ai/user-management 0.6.17 → 0.6.22

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/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.6.22
4
+
5
+ - Owner recovery: hashed single-use recovery codes, atomic reset and revoke, rate limited (`issueRecoveryCodes`, `recoverOwner`, `resetOwnerPassword`), and hidden password input for the reset command.
6
+
3
7
  ## 0.6.17
4
8
 
5
9
  ### Patch Changes
@@ -88,7 +92,7 @@
88
92
  - Canonical package replaces the retired identity surface; user tools live in ./server, primitive ports come from core, and session lifetime and tenancy are configurable.
89
93
  - Distribution includes runtime output, release documentation and required assets only. Process records and per-job neutrality checks are no longer part of the package surface.
90
94
 
91
- ## Unreleased
95
+ ## 0.6.19
92
96
 
93
97
  ### The built-in admin role holds role.manage
94
98
 
@@ -16,4 +16,6 @@ export * from "./session-tokens.js";
16
16
  export * from "./auth-challenge.js";
17
17
  export { buildIdentityRegistrations, identityDerivedRisk, type IdentityToolDeps } from "./agent-tool-registrations.js";
18
18
  export { defaultUserManagementMessages, type UserManagementMessages } from "./messages.js";
19
+ export { resolveOwnerResetTarget, issueRecoveryCodes, recoverOwner, resetOwnerPassword, type RecoveryCodeStore, type OwnerRecoveryDeps } from './recovery-codes.js';
20
+ export { readHiddenPassword, ownerResetPassword } from './owner-reset-input.js';
19
21
  //# sourceMappingURL=index.d.ts.map
@@ -18,4 +18,6 @@ export * from "./session-tokens.js";
18
18
  export * from "./auth-challenge.js";
19
19
  export { buildIdentityRegistrations, identityDerivedRisk } from "./agent-tool-registrations.js";
20
20
  export { defaultUserManagementMessages } from "./messages.js";
21
+ export { resolveOwnerResetTarget, issueRecoveryCodes, recoverOwner, resetOwnerPassword } from './recovery-codes.js';
22
+ export { readHiddenPassword, ownerResetPassword } from './owner-reset-input.js';
21
23
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,23 @@
1
+ /** Read one terminal secret through a muted readline output; never print input or accept argv.
2
+ * @throws On a nonterminal, EOF, Ctrl-C, or input error. Always closes readline to restore raw mode.
3
+ * @complexity O(p) memory/time for p entered characters; password policy bounds the accepted result.
4
+ */
5
+ export declare function readHiddenPassword({ prompt }: {
6
+ prompt: string;
7
+ }, { input, output }?: {
8
+ input?: NodeJS.ReadableStream & {
9
+ isTTY?: boolean;
10
+ };
11
+ output?: Pick<NodeJS.WritableStream, 'write'>;
12
+ }): Promise<string>;
13
+ /** Resolve the host-secret fallback or two hidden prompts; validate before opening a writable store.
14
+ * @throws On mismatch or invalid password. Passwords are preserved exactly as entered.
15
+ * @complexity O(p) for password validation; at most two terminal reads, no database I/O.
16
+ */
17
+ export declare function ownerResetPassword({ env, passwordEnvKey }: {
18
+ env: Readonly<Record<string, string | undefined>>;
19
+ passwordEnvKey: string;
20
+ }, { prompt }?: {
21
+ prompt?: (label: string) => Promise<string>;
22
+ }): Promise<string>;
23
+ //# sourceMappingURL=owner-reset-input.d.ts.map
@@ -0,0 +1,42 @@
1
+ import { createInterface } from 'node:readline';
2
+ import { Writable } from 'node:stream';
3
+ import { validatePasswordPolicy } from './password-policy.js';
4
+ /** Read one terminal secret through a muted readline output; never print input or accept argv.
5
+ * @throws On a nonterminal, EOF, Ctrl-C, or input error. Always closes readline to restore raw mode.
6
+ * @complexity O(p) memory/time for p entered characters; password policy bounds the accepted result.
7
+ */
8
+ export async function readHiddenPassword({ prompt }, { input = process.stdin, output = process.stderr } = {}) {
9
+ if (!input.isTTY)
10
+ throw new Error('Password input needs a terminal. Run interactively or set the owner reset password in the host secret environment. Details: owner-reset-terminal');
11
+ const muted = new Writable({ write: (_chunk, _encoding, done) => done() });
12
+ const rl = createInterface({ input, output: muted, terminal: true });
13
+ output.write(prompt);
14
+ try {
15
+ return await new Promise((resolve, reject) => {
16
+ const cancelled = () => reject(new Error('Password entry was cancelled. Run the reset command again. Details: owner-reset-input'));
17
+ rl.once('line', resolve);
18
+ rl.once('close', cancelled);
19
+ rl.once('SIGINT', cancelled);
20
+ rl.once('error', reject);
21
+ });
22
+ }
23
+ finally {
24
+ rl.close();
25
+ output.write('\n');
26
+ }
27
+ }
28
+ /** Resolve the host-secret fallback or two hidden prompts; validate before opening a writable store.
29
+ * @throws On mismatch or invalid password. Passwords are preserved exactly as entered.
30
+ * @complexity O(p) for password validation; at most two terminal reads, no database I/O.
31
+ */
32
+ export async function ownerResetPassword({ env, passwordEnvKey }, { prompt = (label) => readHiddenPassword({ prompt: label }) } = {}) {
33
+ const password = env[passwordEnvKey] ?? await prompt('New owner password (not shown): ');
34
+ if (env[passwordEnvKey] === undefined && password !== await prompt('Confirm owner password (not shown): ')) {
35
+ throw new Error('The passwords do not match. Run the reset command again. Details: owner-reset-confirmation');
36
+ }
37
+ const error = validatePasswordPolicy({ password });
38
+ if (error)
39
+ throw new Error(`${error} Run the reset command again. Details: owner-reset-password`);
40
+ return password;
41
+ }
42
+ //# sourceMappingURL=owner-reset-input.js.map
@@ -0,0 +1,59 @@
1
+ import type { AuthServiceDeps } from './auth-service.js';
2
+ type OwnerScope = {
3
+ workspaceId: string;
4
+ principalId: string;
5
+ };
6
+ /** Storage must join auth's identity transaction, including rollback and workspace serialization. */
7
+ export interface RecoveryCodeStore {
8
+ replace(input: OwnerScope & {
9
+ hashes: string[];
10
+ }): Promise<void>;
11
+ contains(input: OwnerScope & {
12
+ hash: string;
13
+ }): Promise<boolean>;
14
+ clear(input: OwnerScope): Promise<void>;
15
+ }
16
+ export interface OwnerRecoveryDeps {
17
+ auth: AuthServiceDeps;
18
+ codes: RecoveryCodeStore;
19
+ seededOwnerPrincipalId: string;
20
+ }
21
+ type Required<Input> = {
22
+ deps: OwnerRecoveryDeps;
23
+ input: Input;
24
+ };
25
+ type Options = Record<string, never>;
26
+ /** Rotate ten 256-bit codes after checking the current password; raw codes leave only in this result.
27
+ * @throws OwnerRequiredError or IdentityValidationError; storage errors roll back the rotation.
28
+ * @complexity O(1): ten entropy/digest operations and one bounded replacement per explicit rotation.
29
+ */
30
+ export declare function issueRecoveryCodes(required: Required<OwnerScope & {
31
+ password: string;
32
+ }>, _options?: Options): Promise<{
33
+ codes: string[];
34
+ }>;
35
+ /** Trusted host/desktop entry: use only after proving host ownership or an authenticated owner session.
36
+ * Reuses the password transition and proves its write before clearing the kit in the same transaction.
37
+ * @throws OwnerRequiredError or IdentityValidationError; any failure rolls back all writes.
38
+ * @complexity O(s) time/space for the existing reset transition's s owner sessions, plus one kit delete.
39
+ */
40
+ export declare function resetOwnerPassword(required: Required<OwnerScope & {
41
+ password: string;
42
+ }>, _options?: Options): Promise<void>;
43
+ /** Redeem only in its owner/workspace transaction; concurrent redemptions have exactly one winner.
44
+ * @throws IdentityValidationError for an unavailable code; reset failures roll back code consumption.
45
+ * @complexity O(s) for owner sessions; one indexed hash read and at most ten deleted code rows.
46
+ */
47
+ export declare function recoverOwner(required: Required<OwnerScope & {
48
+ code: string;
49
+ password: string;
50
+ }>, _options?: Options): Promise<void>;
51
+ /** Resolve host recovery by the seeded identity, never a default username or a delegated account.
52
+ * @throws OwnerRequiredError for another requested username or an unavailable owner.
53
+ * @complexity O(1), two scoped identity reads; constant memory.
54
+ */
55
+ export declare function resolveOwnerResetTarget(required: Required<OwnerScope & {
56
+ username?: string;
57
+ }>, _options?: Options): Promise<import("../core/types.js").UserRecord>;
58
+ export {};
59
+ //# sourceMappingURL=recovery-codes.d.ts.map
@@ -0,0 +1,88 @@
1
+ import { resetUserPassword } from './admin-crud-service.js';
2
+ import { inIdentityTransaction } from './identity-transaction.js';
3
+ import { IdentityValidationError, OwnerRequiredError } from '../core/types.js';
4
+ /** Refuse staff and inactive identities before any recovery write; never assigns roles.
5
+ * @complexity O(1), two scoped identity reads, constant memory.
6
+ */
7
+ async function requireOwner({ deps, input }) {
8
+ if (input.principalId !== deps.seededOwnerPrincipalId) {
9
+ throw new OwnerRequiredError({ message: 'Only the site owner can recover this account. Sign in as the owner and try again.' });
10
+ }
11
+ const principal = await deps.auth.repos.principals.findById({ workspaceId: input.workspaceId, id: input.principalId });
12
+ const user = await deps.auth.repos.users.findByPrincipalId({ workspaceId: input.workspaceId, principalId: input.principalId });
13
+ if (principal?.kind !== 'user' || principal.status !== 'active' || !user) {
14
+ throw new OwnerRequiredError({ message: 'The owner account is unavailable. Ask the host operator to check the owner account.' });
15
+ }
16
+ return user;
17
+ }
18
+ /** Domain separation prevents a stored digest being useful for another owner, workspace or session.
19
+ * @complexity O(1), bounded code and identity fields; no I/O.
20
+ */
21
+ function codeHash({ deps, input }) {
22
+ return deps.auth.tokens.hashToken({ rawToken: JSON.stringify(['owner-recovery-v1', input.workspaceId, input.principalId, input.code]) });
23
+ }
24
+ /** Rotate ten 256-bit codes after checking the current password; raw codes leave only in this result.
25
+ * @throws OwnerRequiredError or IdentityValidationError; storage errors roll back the rotation.
26
+ * @complexity O(1): ten entropy/digest operations and one bounded replacement per explicit rotation.
27
+ */
28
+ export async function issueRecoveryCodes(required, _options = {}) {
29
+ const { deps, input } = required;
30
+ return inIdentityTransaction({ transactions: deps.auth.repos.transactions, workspaceId: input.workspaceId, execute: async () => {
31
+ const user = await requireOwner(required);
32
+ if (!input.password || !await deps.auth.hasher.verify({ hash: user.passwordHash, password: input.password })) {
33
+ throw new IdentityValidationError({ message: 'The current password was not accepted. Enter your current owner password and try again.' });
34
+ }
35
+ const codes = Array.from({ length: 10 }, () => deps.auth.tokens.newToken({}));
36
+ if (new Set(codes).size !== 10 || codes.some(code => !/^[a-f0-9]{64}$/.test(code))) {
37
+ throw new Error('Recovery code generation failed. Try again after the host operator checks the entropy source. Details: recovery-entropy');
38
+ }
39
+ await deps.codes.replace({ workspaceId: input.workspaceId, principalId: input.principalId, hashes: codes.map(code => codeHash({ deps, input: { ...input, code } })) });
40
+ return { codes };
41
+ } });
42
+ }
43
+ /** Trusted host/desktop entry: use only after proving host ownership or an authenticated owner session.
44
+ * Reuses the password transition and proves its write before clearing the kit in the same transaction.
45
+ * @throws OwnerRequiredError or IdentityValidationError; any failure rolls back all writes.
46
+ * @complexity O(s) time/space for the existing reset transition's s owner sessions, plus one kit delete.
47
+ */
48
+ export async function resetOwnerPassword(required, _options = {}) {
49
+ const { deps, input } = required;
50
+ await inIdentityTransaction({ transactions: deps.auth.repos.transactions, workspaceId: input.workspaceId, execute: async () => {
51
+ await requireOwner(required);
52
+ await resetUserPassword({ deps: deps.auth, input: { ...input, callerPrincipalId: input.principalId, seededOwnerPrincipalId: deps.seededOwnerPrincipalId } });
53
+ const fresh = await deps.auth.repos.users.findByPrincipalId({ workspaceId: input.workspaceId, principalId: input.principalId });
54
+ if (!fresh || !await deps.auth.hasher.verify({ hash: fresh.passwordHash, password: input.password })) {
55
+ throw new Error('The new owner password could not be verified. No recovery changes were saved. Ask the host operator to check password hashing. Details: owner-reset-verification');
56
+ }
57
+ await deps.codes.clear({ workspaceId: input.workspaceId, principalId: input.principalId });
58
+ } });
59
+ }
60
+ /** Redeem only in its owner/workspace transaction; concurrent redemptions have exactly one winner.
61
+ * @throws IdentityValidationError for an unavailable code; reset failures roll back code consumption.
62
+ * @complexity O(s) for owner sessions; one indexed hash read and at most ten deleted code rows.
63
+ */
64
+ export async function recoverOwner(required, _options = {}) {
65
+ const { deps, input } = required;
66
+ if (!/^[a-f0-9]{64}$/.test(input.code)) {
67
+ throw new IdentityValidationError({ message: 'The recovery code was not accepted. Use an unused code from your latest download.' });
68
+ }
69
+ await inIdentityTransaction({ transactions: deps.auth.repos.transactions, workspaceId: input.workspaceId, execute: async () => {
70
+ await requireOwner(required);
71
+ if (!await deps.codes.contains({ workspaceId: input.workspaceId, principalId: input.principalId, hash: codeHash(required) })) {
72
+ throw new IdentityValidationError({ message: 'The recovery code was not accepted. Use an unused code from your latest download.' });
73
+ }
74
+ await resetOwnerPassword(required, {});
75
+ } });
76
+ }
77
+ /** Resolve host recovery by the seeded identity, never a default username or a delegated account.
78
+ * @throws OwnerRequiredError for another requested username or an unavailable owner.
79
+ * @complexity O(1), two scoped identity reads; constant memory.
80
+ */
81
+ export async function resolveOwnerResetTarget(required, _options = {}) {
82
+ const owner = await requireOwner(required);
83
+ if (required.input.username !== undefined && required.input.username !== owner.username) {
84
+ throw new OwnerRequiredError({ message: 'That username is not the site owner. Omit the username to recover the current owner account.' });
85
+ }
86
+ return owner;
87
+ }
88
+ //# sourceMappingURL=recovery-codes.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jini-ai/user-management",
3
- "version": "0.6.17",
3
+ "version": "0.6.22",
4
4
  "description": "Runtime-separated user management contracts and Node authentication, authorization, and administration services.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -96,7 +96,7 @@
96
96
  "registry": "https://registry.npmjs.org"
97
97
  },
98
98
  "dependencies": {
99
- "@jini-ai/core": "^0.6.17"
99
+ "@jini-ai/core": "^0.6.22"
100
100
  },
101
101
  "peerDependencies": {
102
102
  "argon2": "^0.44.0",
@@ -120,12 +120,12 @@
120
120
  "@testing-library/user-event": "^14.5.0",
121
121
  "@testing-library/jest-dom": "^6.6.3",
122
122
  "jsdom": "^25.0.1",
123
- "@jini-ai/ui": "0.6.17",
124
- "@jini-ai/cms": "0.6.17",
125
- "@jini-ai/agentic": "0.6.17",
126
- "@jini-ai/core": "^0.6.17",
127
- "@jini-ai/ui-kit": "^0.6.17",
128
- "@jini-ai/admin": "0.6.17"
123
+ "@jini-ai/ui": "0.6.22",
124
+ "@jini-ai/admin": "0.6.22",
125
+ "@jini-ai/cms": "0.6.22",
126
+ "@jini-ai/agentic": "0.6.19",
127
+ "@jini-ai/ui-kit": "^0.6.19",
128
+ "@jini-ai/core": "^0.6.22"
129
129
  },
130
130
  "peerDependenciesMeta": {
131
131
  "argon2": {