Edit Authentik users: name, email and groups
Each person in Apps → Authentik → Users gets an Edit form. The user name is shown but not changeable. Authentik is sent only what differs, and groups as what goes in and what comes out on top of the user's current groups, so a membership added meanwhile is kept; a user whose groups Authentik doesn't report can't have them changed. The server refuses service accounts, the token's own account, and an edit that would leave no active administrator. Audited by field name, never by value. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0cd11268ee
commit
3618cb3ae8
9 files changed
+370
-30
No files matched your search
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Looking after Authentik users: reading the list, switching a user off and on, deleting one — and what is refused (service accounts, the account the token belongs to, the last active
|
||||
* Looking after Authentik users: reading the list, switching a user off and on, editing one, deleting one — and what is refused (service accounts, the account the token belongs to, the last active
|
||||
* administrator, a user name that does not match), who may, and what is written in the audit log.
|
||||
*
|
||||
* Run with: npm test --workspace server
|
||||
@@ -36,12 +36,16 @@ interface FakeUser {
|
||||
is_superuser: boolean;
|
||||
last_login: string | null;
|
||||
type: string;
|
||||
groups?: string[];
|
||||
}
|
||||
const person = (pk: number, username: string, extra: Partial<FakeUser> = {}): FakeUser => ({ pk, username, name: username.toUpperCase(), email: `${username}@example.com`, is_active: true, is_superuser: false, last_login: null, type: "internal", ...extra });
|
||||
const G_ADMIN = "22222222-2222-4222-8222-222222222222";
|
||||
const G_FAMILY = "11111111-1111-4111-8111-111111111111";
|
||||
const G_OTHER = "44444444-4444-4444-8444-444444444444";
|
||||
const person = (pk: number, username: string, extra: Partial<FakeUser> = {}): FakeUser => ({ pk, username, name: username.toUpperCase(), email: `${username}@example.com`, is_active: true, is_superuser: false, last_login: null, type: "internal", groups: [], ...extra });
|
||||
const fresh = (): FakeUser[] => [
|
||||
person(1, "akadmin", { is_superuser: true, last_login: "2026-10-30T08:00:00Z" }),
|
||||
person(2, "bob", { is_superuser: true }),
|
||||
person(3, "carol"),
|
||||
person(1, "akadmin", { is_superuser: true, last_login: "2026-10-30T08:00:00Z", groups: [G_ADMIN] }),
|
||||
person(2, "bob", { is_superuser: true, groups: [G_ADMIN] }),
|
||||
person(3, "carol", { groups: [G_FAMILY] }),
|
||||
person(4, "dave", { is_active: false }),
|
||||
person(5, "ak-outpost-front", { type: "internal_service_account", email: "" }),
|
||||
];
|
||||
@@ -66,6 +70,18 @@ describe("what Authentik's answers are read as", () => {
|
||||
assert.equal(usersApi.isServiceAccount({ type: "external" }), false);
|
||||
});
|
||||
|
||||
test("a user's groups are the ids Authentik gives; none given is not the same as none", () => {
|
||||
assert.deepEqual(usersApi.parseUser({ pk: 3, username: "x", groups: [G_FAMILY, "nope", 7] })!.groups, [G_FAMILY]);
|
||||
assert.deepEqual(usersApi.parseUser({ pk: 3, username: "x", groups: [] })!.groups, []);
|
||||
assert.equal(usersApi.parseUser({ pk: 3, username: "x" })!.groups, null);
|
||||
});
|
||||
|
||||
test("groups are changed from the ones there are now: what goes out goes, what comes in comes, once, and what neither mentions stays", () => {
|
||||
assert.deepEqual(usersApi.applyGroupChange([G_FAMILY, G_OTHER], [G_ADMIN], [G_FAMILY]), [G_OTHER, G_ADMIN]);
|
||||
assert.deepEqual(usersApi.applyGroupChange([G_FAMILY], [G_FAMILY], []), [G_FAMILY]);
|
||||
assert.deepEqual(usersApi.applyGroupChange([G_FAMILY], [], [G_OTHER]), [G_FAMILY]);
|
||||
});
|
||||
|
||||
test("who the token is comes out of /users/me/", () => {
|
||||
assert.equal(usersApi.parseMe({ user: { pk: 9 } }), 9);
|
||||
assert.equal(usersApi.parseMe({ user: { pk: "9" } }), null);
|
||||
@@ -84,6 +100,12 @@ describe("what Authentik's answers are read as", () => {
|
||||
assert.equal(usersApi.refusalFor({ ...bob, active: false }, "delete", 1, [1]), null, "one that is already off is not the one holding the door");
|
||||
assert.equal(usersApi.refusalFor(carol, "delete", 1, [1]), null);
|
||||
assert.equal(usersApi.refusalFor(dave, "disable", null, []), null);
|
||||
// Editing: the token's own account and a service account never; the last administrator only when the edit would take them out of being one.
|
||||
assert.match(usersApi.refusalFor(akadmin, "edit", 1, [1, 2]) ?? "", /token/);
|
||||
assert.match(usersApi.refusalFor(outpost, "edit", 1, [1, 2]) ?? "", /service account/);
|
||||
assert.match(usersApi.refusalFor(bob, "edit", 1, [2], true) ?? "", /only active administrator/);
|
||||
assert.equal(usersApi.refusalFor(bob, "edit", 1, [2], false), null);
|
||||
assert.equal(usersApi.refusalFor(bob, "edit", 1, [1, 2], true), null);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -118,6 +140,7 @@ before(async () => {
|
||||
res.end(JSON.stringify(json));
|
||||
};
|
||||
const url = new URL(path, "http://x");
|
||||
if (req.method === "GET" && url.pathname === "/api/v3/core/groups/") return send(200, { results: [{ pk: G_ADMIN, name: "admins", is_superuser: true }, { pk: G_FAMILY, name: "Family", is_superuser: false }, { pk: G_OTHER, name: "Other", is_superuser: false }] });
|
||||
if (req.method === "GET" && url.pathname === "/api/v3/core/users/me/") return send(200, { user: table.find((u) => u.pk === meId) ?? person(meId, "me") });
|
||||
if (req.method === "GET" && url.pathname === "/api/v3/core/users/") {
|
||||
let list = table;
|
||||
@@ -134,7 +157,9 @@ before(async () => {
|
||||
if (req.method === "GET") return send(200, user);
|
||||
if (req.method === "PATCH") {
|
||||
if (patchStatus !== 200) return send(patchStatus, { detail: "You do not have permission to perform this action." });
|
||||
user.is_active = JSON.parse(body).is_active;
|
||||
const b = JSON.parse(body) as Partial<FakeUser>;
|
||||
Object.assign(user, b);
|
||||
if (b.groups) user.is_superuser = b.groups.includes(G_ADMIN);
|
||||
return send(200, user);
|
||||
}
|
||||
if (req.method === "DELETE") {
|
||||
@@ -214,6 +239,7 @@ describe("the routes", () => {
|
||||
const list = (sub = "admin", q = "") => fetch(`${base}/${id}/users${q}`, { headers: as(sub) });
|
||||
const active = (pk: number, on: boolean, sub = "admin", integration = id) => fetch(`${base}/${integration}/users/${pk}/active`, { method: "POST", headers: as(sub), body: JSON.stringify({ active: on }) });
|
||||
const del = (pk: number, username: unknown, sub = "admin") => fetch(`${base}/${id}/users/${pk}`, { method: "DELETE", headers: as(sub), body: JSON.stringify({ username }) });
|
||||
const change = (pk: number, body: unknown, sub = "admin") => fetch(`${base}/${id}/users/${pk}`, { method: "PATCH", headers: as(sub), body: JSON.stringify(body) });
|
||||
const writes = () => seen.filter((s) => s.method !== "GET");
|
||||
const reset = () => {
|
||||
table = fresh();
|
||||
@@ -347,6 +373,104 @@ describe("the routes", () => {
|
||||
assert.equal((await audit("disable_authentik_user")).length, before, "nothing was done, so nothing more is written down");
|
||||
});
|
||||
|
||||
test("editing: only what differs is sent, never the user name; the audit log names the fields, not what they were changed to", async () => {
|
||||
reset();
|
||||
const res = await change(3, { name: "Carol C", email: "carol@new.example" });
|
||||
assert.equal(res.status, 200);
|
||||
assert.deepEqual(((await res.json()) as { changed: string[] }).changed, ["name", "email"]);
|
||||
assert.deepEqual(writes().map((s) => [s.method, s.path, s.body]), [["PATCH", "/api/v3/core/users/3/", JSON.stringify({ name: "Carol C", email: "carol@new.example" })]]);
|
||||
assert.deepEqual([table.find((u) => u.pk === 3)!.name, table.find((u) => u.pk === 3)!.username], ["Carol C", "carol"]);
|
||||
const entry = (await audit("update_authentik_user")).at(-1)!;
|
||||
assert.deepEqual([entry.category, entry.targetType, entry.targetId], ["integration", "authentik_user", "3"]);
|
||||
assert.match(entry.detail ?? "", /carol/);
|
||||
assert.match(entry.detail ?? "", /"changed":\["name","email"\]/);
|
||||
assert.equal(/new\.example|Carol C/.test(entry.detail ?? ""), false);
|
||||
// The same again changes nothing, so nothing is sent and nothing is written down.
|
||||
const before = (await audit("update_authentik_user")).length;
|
||||
seen.length = 0;
|
||||
const again = await change(3, { name: "Carol C", email: "carol@new.example", groupsAdd: [], groupsRemove: [] });
|
||||
assert.deepEqual(((await again.json()) as { changed: string[] }).changed, []);
|
||||
assert.deepEqual(writes(), []);
|
||||
assert.equal((await audit("update_authentik_user")).length, before);
|
||||
// An empty email clears it.
|
||||
assert.equal((await change(3, { email: "" })).status, 200);
|
||||
assert.equal(writes().at(-1)!.body, JSON.stringify({ email: "" }));
|
||||
});
|
||||
|
||||
test("groups are changed by what goes in and out, on top of the groups the user has now", async () => {
|
||||
reset();
|
||||
// Somebody put carol in another group after the page was read: that stays.
|
||||
table.find((u) => u.pk === 3)!.groups = [G_FAMILY, G_OTHER];
|
||||
const res = await change(3, { groupsAdd: [G_ADMIN], groupsRemove: [G_FAMILY] });
|
||||
assert.equal(res.status, 200);
|
||||
assert.deepEqual(JSON.parse(writes()[0].body), { groups: [G_OTHER, G_ADMIN] });
|
||||
seen.length = 0;
|
||||
const same = await change(3, { groupsAdd: [G_OTHER], groupsRemove: [G_FAMILY] });
|
||||
assert.deepEqual(((await same.json()) as { changed: string[] }).changed, [], "putting in what is there and taking out what is not changes nothing");
|
||||
assert.deepEqual(writes(), []);
|
||||
});
|
||||
|
||||
test("what is wrong with the change is said before Authentik is asked", async () => {
|
||||
reset();
|
||||
for (const bad of [{ name: "" }, { email: "not-an-email" }, { groupsAdd: ["not-a-uuid"] }, { groupsAdd: [G_FAMILY], groupsRemove: [G_FAMILY] }, { groupsRemove: "x" }]) {
|
||||
const res = await change(3, bad);
|
||||
assert.equal(res.status, 400, JSON.stringify(bad));
|
||||
assert.equal(typeof ((await res.json()) as { message: string }).message, "string");
|
||||
}
|
||||
assert.equal((await fetch(`${base}/${id}/users/1e3`, { method: "PATCH", headers: as("admin"), body: JSON.stringify({ name: "x" }) })).status, 400);
|
||||
assert.deepEqual(seen, []);
|
||||
});
|
||||
|
||||
test("editing is refused for a service account, for the token's own account, and for the last administrator when it would take them out of being one", async () => {
|
||||
reset();
|
||||
const refused = async (res: Response, pattern: RegExp) => {
|
||||
assert.equal(res.status, 409);
|
||||
assert.match(((await res.json()) as { message: string }).message, pattern);
|
||||
};
|
||||
await refused(await change(5, { name: "x" }), /service account/);
|
||||
await refused(await change(1, { name: "x" }), /token/);
|
||||
assert.deepEqual(writes(), []);
|
||||
// The token is carol's account now; bob is off, so akadmin is the only active administrator.
|
||||
meId = 3;
|
||||
table.find((u) => u.pk === 2)!.is_active = false;
|
||||
await refused(await change(1, { groupsRemove: [G_ADMIN] }), /only active administrator/);
|
||||
assert.deepEqual(writes(), []);
|
||||
assert.equal((await change(1, { name: "Root" })).status, 200, "a name is no danger");
|
||||
assert.equal((await change(1, { groupsAdd: [G_FAMILY] })).status, 200, "nor is another group");
|
||||
assert.equal((await change(1, { groupsRemove: [G_FAMILY] })).status, 200);
|
||||
// With another administrator on, they may be taken out of the group.
|
||||
table.find((u) => u.pk === 2)!.is_active = true;
|
||||
assert.equal((await change(1, { groupsRemove: [G_ADMIN] })).status, 200);
|
||||
assert.deepEqual(table.find((u) => u.pk === 1)!.groups, []);
|
||||
});
|
||||
|
||||
test("a user whose groups Authentik did not say can be renamed but not moved between groups", async () => {
|
||||
reset();
|
||||
delete table.find((u) => u.pk === 3)!.groups;
|
||||
const res = await change(3, { groupsAdd: [G_FAMILY] });
|
||||
assert.equal(res.status, 409);
|
||||
assert.match(((await res.json()) as { message: string }).message, /did not say which groups/);
|
||||
assert.deepEqual(writes(), []);
|
||||
assert.equal((await change(3, { name: "Only Name" })).status, 200);
|
||||
});
|
||||
|
||||
test("only an administrator edits; a user that is gone is not found; a token that may not is a bad gateway", async () => {
|
||||
reset();
|
||||
for (const sub of ["viewer", "operator"]) assert.equal((await change(3, { name: "x" }, sub)).status, 403, sub);
|
||||
assert.deepEqual(seen, []);
|
||||
assert.equal((await change(99, { name: "x" })).status, 404);
|
||||
patchStatus = 403;
|
||||
const res = await change(3, { name: "Someone Else" });
|
||||
assert.equal(res.status, 502);
|
||||
assert.match(((await res.json()) as { message: string }).message, /did not accept the token/);
|
||||
});
|
||||
|
||||
test("the list carries each person's groups", async () => {
|
||||
reset();
|
||||
const body = (await (await list()).json()) as { users: { username: string; groups: string[] | null }[] };
|
||||
assert.deepEqual(body.users.map((u) => [u.username, u.groups]), [["akadmin", [G_ADMIN]], ["bob", [G_ADMIN]], ["carol", [G_FAMILY]], ["dave", []]]);
|
||||
});
|
||||
|
||||
test("an Authentik with no token for managing users is not asked anything", async () => {
|
||||
reset();
|
||||
const res = await fetch(`${base}/${readOnly}/users`, { headers: as("admin") });
|
||||
|
||||
Reference in new issue
Block a user