Create Authentik groups from Apps → Authentik
A New group form in the Groups section makes a group with a name and nothing else: it starts empty and is never one that makes administrators of Authentik. A name Authentik already has is refused in its own words, and a name that decides a role in this app (Settings → Sign-in) is said to. Admin only; audited with who made which group. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
6c5a5f0bd6
commit
c9385387bc
9 files changed
+152
-12
No files matched your search
@@ -5,9 +5,10 @@
|
||||
* Making one takes three calls: the groups to offer (`GET /core/groups/`), the user (`POST /core/users/`) and a one-time recovery link for that user (`POST /core/users/{pk}/recovery/`), which is how the
|
||||
* person gets to choose their own password: this app never sets, sees or keeps one. The link is handed back once and not stored.
|
||||
*
|
||||
* Looking after groups: the groups with who is in them (`GET /core/groups/`, one `GET /core/groups/{id}/`), renaming one (`PATCH` with only `name`) and putting a user in or taking one out
|
||||
* Looking after groups: the groups with who is in them (`GET /core/groups/`, one `GET /core/groups/{id}/`), making one (`POST /core/groups/` with a name and never as a group that makes administrators),
|
||||
* renaming one (`PATCH` with only `name`) and putting a user in or taking one out
|
||||
* (`POST /core/groups/{id}/add_user/` and `remove_user/`, one user each — never a whole list of members, so someone who joined meanwhile is not dropped). Whether a group makes its members
|
||||
* administrators of Authentik (`is_superuser`) is shown and not changed from here.
|
||||
* administrators of Authentik (`is_superuser`) is shown, and neither changed nor set when one is made, from here.
|
||||
*
|
||||
* Looking after them: the users (`GET /core/users/`, one `GET /core/users/{pk}/`), who the token is (`GET /core/users/me/`), switching one off or on (`PATCH /core/users/{pk}/` with only `is_active`),
|
||||
* changing their name, email and groups (the same `PATCH`, with only what changed — never the user name, which is how a person is known elsewhere) and deleting one (`DELETE /core/users/{pk}/`). Service accounts — what an outpost or an integration signs in with — are not listed and are refused, and so are the account the token belongs to and the last
|
||||
@@ -209,6 +210,7 @@ export interface AuthentikUsersAdapter {
|
||||
listGroups(): Promise<AuthentikGroup[]>;
|
||||
listGroupDetails(): Promise<AuthentikGroupDetail[]>;
|
||||
getGroup(id: string): Promise<AuthentikGroupDetail>;
|
||||
createGroup(name: string): Promise<AuthentikGroupDetail>;
|
||||
renameGroup(id: string, name: string): Promise<AuthentikGroupDetail>;
|
||||
addMember(id: string, userPk: number): Promise<void>;
|
||||
removeMember(id: string, userPk: number): Promise<void>;
|
||||
@@ -298,6 +300,13 @@ export function createAuthentikUsersAdapter(config: AuthentikUsersConfig): Authe
|
||||
return group;
|
||||
}
|
||||
|
||||
async function createGroup(name: string): Promise<AuthentikGroupDetail> {
|
||||
// Only a name, and never a group that makes administrators: that is decided in Authentik.
|
||||
const group = parseGroupDetail(await call("POST", "/core/groups/", { name, is_superuser: false }));
|
||||
if (!group) throw new AuthentikUserError("Authentik answered, but not with the group it made", "failed");
|
||||
return group;
|
||||
}
|
||||
|
||||
async function renameGroup(id: string, name: string): Promise<AuthentikGroupDetail> {
|
||||
// Only the name: a PATCH leaves out what it does not mention, so whether the group makes administrators, its members and its parent are as they were.
|
||||
const group = parseGroupDetail(await call("PATCH", `/core/groups/${groupId(id)}/`, { name }));
|
||||
@@ -333,5 +342,5 @@ export function createAuthentikUsersAdapter(config: AuthentikUsersConfig): Authe
|
||||
return link;
|
||||
}
|
||||
|
||||
return withDiagLogging("authentik", { listUsers, getUser, whoAmI, activeAdmins, setActive, updateUser, deleteUser, listGroupDetails, getGroup, renameGroup, addMember, removeMember, listGroups, createUser, recoveryLink });
|
||||
return withDiagLogging("authentik", { listUsers, getUser, whoAmI, activeAdmins, setActive, updateUser, deleteUser, listGroupDetails, getGroup, createGroup, renameGroup, addMember, removeMember, listGroups, createUser, recoveryLink });
|
||||
}
|
||||
@@ -423,3 +423,24 @@ authentikRouter.patch("/:id/groups/:gid", requireRole("admin"), asyncHandler(asy
|
||||
sendFailure(res, err);
|
||||
}
|
||||
}));
|
||||
|
||||
const newGroupSchema = z.object({ name: z.string().trim().min(1, "A group needs a name.").max(200) });
|
||||
|
||||
/**
|
||||
* Makes a group: a name, nothing else — it has no members (they are put in with the editor, which asks first where a group decides roles here) and it is never one that makes administrators of
|
||||
* Authentik. A name Authentik already has is its own refusal. The answer says whether the new name decides a role in this app, so the page can say what putting someone in it would mean.
|
||||
*/
|
||||
authentikRouter.post("/:id/groups", requireRole("admin"), asyncHandler(async (req, res) => {
|
||||
const parsed = newGroupSchema.safeParse(req.body);
|
||||
if (!parsed.success) return res.status(400).json({ error: "invalid_body", message: parsed.error.issues[0]?.message ?? "That is not a group.", details: parsed.error.flatten() });
|
||||
const target = await usersAdapterFor(req.params.id, res);
|
||||
if (!target) return;
|
||||
try {
|
||||
const group = await target.adapter.createGroup(parsed.data.name);
|
||||
await recordAudit({ actor: req.currentUser!, category: "integration", action: "create_authentik_group", targetType: "authentik_group", targetId: group.id, detail: { integrationId: target.id, integration: target.name, group: group.name } });
|
||||
const settings = await getSettings();
|
||||
res.status(201).json({ group: { ...group, decidesRole: roleForGroupName(settings.signIn, group.name) } });
|
||||
} catch (err) {
|
||||
sendFailure(res, err);
|
||||
}
|
||||
}));
|
||||
Reference in new issue
Block a user