Files
romm/frontend/src/utils/currentPassword.test.ts
Georges-Antoine Assi ff9627ae70 fix(auth): require the current password to change your own username, password or email (#5351)
* feat(auth): require the current password to change your own password

PUT /api/users/{id} set a new password or email for the caller with
nothing more than a me.write credential, so a leaked client token or a
hijacked session could take the account over for good.

A self-edit that changes the password or the email now needs
current_password, checked with bcrypt off the event loop (400 when
missing, 403 when wrong). Admins editing another user and the
forgot/reset-password flow are unchanged. Accounts with no local
password (OIDC-provisioned placeholders) can set a first one only from a
sign-in session, not from a bearer or client token.

UserSchema gains has_password so the UI asks for the current password
only when the backend will: the change-password dialog, the profile
email field, and the edit-user dialog when editing yourself.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* docs(user): shorten _verify_account_holder docstring

Addresses Greptile review on #5351.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(user): treat a case-only email edit as no change (code-review)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* refactor(user): simplify current-password checks and tests (simplify)

Hash the new password off the event loop, reuse errorMessage and the
viewer token fixture, and table-drive the needsCurrentPassword tests.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* fix(user): require the current password to change your own username

Login is by username and a rename revokes every session, so a stolen
me.write token could lock the owner out without touching the password.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(user): lock the email of OIDC accounts to the identity provider

The provider overwrites a linked account's email at every sign-in, and
finds an unlinked password-less account by it, so a self-edit is either
lost or breaks SSO sign-in. Admins can still change it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

* feat(RTextField): give read-only fields a muted style

Dashed chrome, no fill and muted text, with no hover or pointer-focus
halo, so a locked value no longer looks editable. Keyboard and gamepad
focus keep their outline.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-26 11:12:27 -04:00

43 lines
1.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { needsCurrentPassword } from "./currentPassword";
const saved = {
username: "me",
email: "me@example.com",
has_password: true,
};
describe("needsCurrentPassword", () => {
it.each([
["a new password", saved, { password: "a_new_password" }, true],
["a new username", saved, { username: "someone_else" }, true],
["a username that only differs by case", saved, { username: "Me" }, false],
["a changed email", saved, { email: "new@example.com" }, true],
["a cleared email", saved, { email: "" }, true],
[
"an email that only differs by case",
saved,
{ email: "Me@Example.com" },
false,
],
[
"no password or email change",
saved,
{ password: "", email: "me@example.com" },
false,
],
[
"an account without a password",
{ username: "me", email: null, has_password: false },
{
username: "someone_else",
password: "a_first_password",
email: "new@example.com",
},
false,
],
])("for %s", (_label, account, edit, expected) => {
expect(needsCurrentPassword(account, edit)).toBe(expected);
});
});