Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,39 +1,23 @@
/**
* Phase 09 Task #82 — OIDC ID-token claim extraction + group→role mapping.
*
* The "role" returned here is the *current* AdminUser identity layer in the
* HiveGrid boilerplate. Today the only durable AdminUser concept is "exists
* or not"; the spec adds two granular roles (`super_admin`, `staff`) that
* we persist as `ssoGroups` snapshots while we wait for the permissions
* matrix to grow first-class role columns. Callers should treat the role
* as advisory + future-proof.
* OIDC ID-token claim extraction and the narrow Authentik access gate.
* Authentik groups never map to HiveGrid roles. They only decide whether an
* already prepared AdminUser may enter the subject-binding flow.
*/
export type AuthentikGroup = "hivegrid-admin" | "hivegrid-staff";
export type AdminSsoRole = "super_admin" | "staff";
export const ADMIN_GROUP: AuthentikGroup = "hivegrid-admin";
export const STAFF_GROUP: AuthentikGroup = "hivegrid-staff";
export const ALLOWED_ADMIN_GROUPS: readonly AuthentikGroup[] = [ADMIN_GROUP, STAFF_GROUP];
/**
* Highest-priority-wins mapping. If a user is in BOTH groups they get
* super_admin (admins are a strict superset of staff for our purposes).
*/
export function mapGroupsToRole(groups: readonly string[] | undefined | null): AdminSsoRole | null {
if (!groups || groups.length === 0) return null;
if (groups.includes(ADMIN_GROUP)) return "super_admin";
if (groups.includes(STAFF_GROUP)) return "staff";
return null;
}
export type AdminSsoClaims = {
readonly subject: string;
readonly email: string;
readonly emailVerified: boolean;
readonly name: string;
readonly groups: readonly string[];
readonly authenticationMethods: readonly string[];
};
export type ClaimExtractionResult =
@@ -58,18 +42,18 @@ export function extractAdminSsoClaims(idTokenClaims: Record<string, unknown> | n
if (!email) {
return { ok: false, reason: "missing-email" };
}
// Spec security note: "email_verified=true prüfen". If the claim is
// absent we treat it as `true` only when the IdP is the trusted
// Authentik provider (which mints `email_verified` for SSO accounts);
// if it is present and false, reject.
const emailVerifiedClaim = idTokenClaims.email_verified;
const emailVerified = emailVerifiedClaim === undefined ? true : emailVerifiedClaim === true;
// The IdP must assert verification explicitly. Missing, malformed, and
// false claims all fail closed so a claim-mapping regression cannot bind
// an unverified operator identity.
const emailVerified = idTokenClaims.email_verified === true;
if (!emailVerified) {
return { ok: false, reason: "email-not-verified" };
}
const rawGroups = idTokenClaims.groups;
const groups: string[] = Array.isArray(rawGroups)
? rawGroups.filter((g): g is string => typeof g === "string" && g.length > 0)
const groups: string[] = Array.isArray(rawGroups) ? [...new Set(rawGroups.filter((g): g is string => typeof g === "string" && g.length > 0))] : [];
const rawAuthenticationMethods = idTokenClaims.amr;
const authenticationMethods: string[] = Array.isArray(rawAuthenticationMethods)
? rawAuthenticationMethods.filter((method): method is string => typeof method === "string" && method.length > 0)
: [];
const nameFromClaim = typeof idTokenClaims.name === "string" ? idTokenClaims.name.trim() : "";
const preferredName = typeof idTokenClaims.preferred_username === "string" ? idTokenClaims.preferred_username.trim() : "";
@@ -82,14 +66,13 @@ export function extractAdminSsoClaims(idTokenClaims: Record<string, unknown> | n
emailVerified,
name,
groups,
authenticationMethods,
},
};
}
/**
* Convenience guard: pass-through that throws (or returns a Response) when
* the user has no allowed group. Kept here so the JIT-provisioning logic
* stays focused on the DB upsert.
* Convenience guard for Authentik's coarse admin-login allowlist.
*/
export function isAllowedAdminGroup(group: string): group is AuthentikGroup {
return ALLOWED_ADMIN_GROUPS.includes(group as AuthentikGroup);