diff --git a/docs/deployment.md b/docs/deployment.md index bdb5044..173522d 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -172,7 +172,7 @@ Generate each random value separately, for example with `openssl rand -base64 48 | `appkeys.json` | `api-gateway` | `/deployments/appkeys/appkeys.json` | The clients allowed to call the gateway (example below) | | `key.json` | every Java image except `api-gateway` | `/var/secrets/gcp/key.json` | The Pub/Sub service-account key (section 5) | -`appkeys.json` holds one entry, for mobile-api: +`appkeys.json` holds one entry, for mobile-api. Keep it that way: mobile-api is what checks who may change whose password, so no other client may get a key that reaches the gateway's `/user/` routes. ```json [ diff --git a/openspec/changes/add-admin-temporary-password/design.md b/openspec/changes/add-admin-temporary-password/design.md index 9277b1c..29af03e 100644 --- a/openspec/changes/add-admin-temporary-password/design.md +++ b/openspec/changes/add-admin-temporary-password/design.md @@ -32,6 +32,7 @@ - stores the bcrypt hash with `login = N` through the existing `userService.resetPassword`; - returns rows updated: 0 means refused. - Both endpoints are reachable only inside the cluster, through the gateway `/user/**` route, like the existing ones. + - mobile-api is the only authority for who may set whose password: services-system does not check the caller or a super-admin target. So `appkeys.json` must never give a second key access to `/user/`. - Alternative: let `/user/save` accept a password. Rejected: save copies the stored hash on purpose, and it serves every user update. 3. **mobile-api create** (`AdminUserRequest` gains `password` and `confirmPassword`): - Missing password: 400 `REQUIRED_FIELD`. diff --git a/openspec/changes/add-admin-temporary-password/specs/admin-users/spec.md b/openspec/changes/add-admin-temporary-password/specs/admin-users/spec.md index 68d0cb9..4cb82ce 100644 --- a/openspec/changes/add-admin-temporary-password/specs/admin-users/spec.md +++ b/openspec/changes/add-admin-temporary-password/specs/admin-users/spec.md @@ -26,7 +26,7 @@ The system SHALL let an admin with `BO_USERS` create an admin user with name, us ### Requirement: Admin-triggered password reset -The system SHALL let an admin with `BO_USERS` give another active user a new password in one of two ways: an emailed reset, where the system emails a generated temporary password, or a temporary password the admin types with its confirmation. Either way the user's old password SHALL stop working, and the user SHALL be required to change the password at the next login. A typed password SHALL pass the admin password rules. No password SHALL be logged or returned in a response. +The system SHALL let an admin with `BO_USERS` give an active admin user a new password in one of two ways: an emailed reset, where the system emails a generated temporary password, or a temporary password the admin types with its confirmation. Either way the user's old password SHALL stop working, and the user SHALL be required to change the password at the next login. A typed password SHALL pass the admin password rules. No password SHALL be logged or returned in a response. #### Scenario: Reset @@ -77,7 +77,7 @@ The system SHALL refuse to set a temporary password for a disabled user, for the ### Requirement: Generated temporary passwords are unpredictable -Every temporary password the system generates SHALL come from a cryptographically secure random source. This covers the emailed admin reset and forgot-password. +Every temporary password the system generates for an admin user SHALL come from a cryptographically secure random source. This covers the emailed admin reset and admin forgot-password. #### Scenario: Generated passwords do not repeat diff --git a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts index f371b68..8ce9c72 100644 --- a/solvway-admin-portal/app/lib/i18n/defaultMessages.ts +++ b/solvway-admin-portal/app/lib/i18n/defaultMessages.ts @@ -126,6 +126,8 @@ export const DEFAULT_MESSAGES: Record = { UsrOwnAccountHint: "You can't deactivate your own account", UsrNameRequired: "Name is required.", UsrCreatedBanner: "User created. Give them the password you set; they will be asked to choose their own at first sign-in.", + UsrCreatedNoPassword: "User created, but the password was not stored. Open the user from the users list and use Set temporary password.", + UsrBackToUsers: "Back to users", UsrConfirmPassword: "Confirm password", UsrPasswordRequired: "Password is required.", UsrPasswordMismatch: "The passwords do not match.", diff --git a/solvway-admin-portal/app/users/new/page.test.tsx b/solvway-admin-portal/app/users/new/page.test.tsx index 69afde2..ffa6db5 100644 --- a/solvway-admin-portal/app/users/new/page.test.tsx +++ b/solvway-admin-portal/app/users/new/page.test.tsx @@ -107,6 +107,27 @@ describe("NewUserPage", () => { expect(mocks.push).not.toHaveBeenCalled(); }); + it("when the user was created but the password was not stored, links back to the users list", async () => { + const { ApiError } = await vi.importActual("@/app/lib/clientApi"); + mocks.saveAdminUser.mockRejectedValue( + new ApiError("User created but the password was not stored; set a temporary password", 500, { code: "SAVE_FAILED" }), + ); + render(); + await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument()); + await userEvent.type(screen.getByLabelText("Name"), "Jane"); + await userEvent.type(screen.getByLabelText("Username"), "jane.doe"); + await userEvent.type(screen.getByLabelText("Email"), "jane@example.com"); + await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1"); + await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1"); + await userEvent.click(screen.getByRole("button", { name: /create user/i })); + + expect( + await screen.findByText("User created, but the password was not stored. Open the user from the users list and use Set temporary password."), + ).toBeInTheDocument(); + expect(screen.getByRole("link", { name: "Back to users" })).toHaveAttribute("href", "/users"); + expect(screen.queryByRole("button", { name: /create user/i })).not.toBeInTheDocument(); + }); + it("renders NotAuthorized when the Users module is absent from aclMenu", () => { mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false }); render(); diff --git a/solvway-admin-portal/app/users/new/page.tsx b/solvway-admin-portal/app/users/new/page.tsx index c23d4df..4382803 100644 --- a/solvway-admin-portal/app/users/new/page.tsx +++ b/solvway-admin-portal/app/users/new/page.tsx @@ -1,6 +1,10 @@ "use client"; +import { useState } from "react"; +import Link from "next/link"; import { useRouter } from "next/navigation"; +import { ApiError } from "@/app/lib/clientApi"; +import { ErrorState } from "@/app/components/ui/states"; import { RequireModule } from "@/app/lib/RequireModule"; import { PageHeader } from "@/app/components/ui/page-header"; import { UserForm, type UserFormPayload } from "@/app/components/users/UserForm"; @@ -10,18 +14,41 @@ import { useT } from "@/app/lib/i18n/MessagesProvider"; // The admin types the first password (legacy backoffice flow); the user is saved with a pending // password change and must choose their own at first sign-in. No email is sent. Once the // mobile-api returns the created user's id, we redirect to its edit page with a `created=1` banner. +// SAVE_FAILED after the save means the user exists without a password; the error carries no id, so link +// back to the list, where the admin opens the user and sets a temporary password. function NewUserPageContent() { const t = useT(); const router = useRouter(); + const [passwordNotStored, setPasswordNotStored] = useState(false); async function handleSubmit(payload: UserFormPayload) { - const created = await saveAdminUser(payload); - if (created.id != null) { - router.push(`/users/${created.id}?created=1`); + try { + const created = await saveAdminUser(payload); + if (created.id != null) { + router.push(`/users/${created.id}?created=1`); + } + } catch (err) { + if (err instanceof ApiError && (err.data as { code?: string } | undefined)?.code === "SAVE_FAILED") { + setPasswordNotStored(true); + return; + } + throw err; } } + if (passwordNotStored) { + return ( + <> + + + + {t("UsrBackToUsers")} + + + ); + } + return ( <>