fix: address the admin-password branch review

Create shows a link back to Users when the user was saved but the password
was not stored. The spec's secure-generator rule now names admin temporary
passwords only, and the reset requirement no longer implies the emailed reset
refuses the caller's own account. The design and handover note that mobile-api
must stay the only gateway client allowed to reach /user/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-11-30 11:27:40 +08:00
1 parent 4412d455bd
commit d2560c6b8a
6 files changed
+57 -6

No files matched your search

+1 -1
View File
@@ -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
[
@@ -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`.
@@ -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
@@ -126,6 +126,8 @@ export const DEFAULT_MESSAGES: Record<string, string> = {
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.",
@@ -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<typeof import("@/app/lib/clientApi")>("@/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(<NewUserPage />);
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(<NewUserPage />);
+30 -3
View File
@@ -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 (
<>
<PageHeader title={t("UsrNewUser")} />
<ErrorState message={t("UsrCreatedNoPassword")} className="mb-4" />
<Link href="/users" className="text-primary underline-offset-4 hover:underline">
{t("UsrBackToUsers")}
</Link>
</>
);
}
return (
<>
<PageHeader title={t("UsrNewUser")} />