85 lines
3.3 KiB
TypeScript
85 lines
3.3 KiB
TypeScript
/**
|
|
* 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 const ADMIN_GROUP: AuthentikGroup = "hivegrid-admin";
|
|
export const STAFF_GROUP: AuthentikGroup = "hivegrid-staff";
|
|
|
|
export const ALLOWED_ADMIN_GROUPS: readonly AuthentikGroup[] = [ADMIN_GROUP, STAFF_GROUP];
|
|
|
|
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 =
|
|
| { readonly ok: true; readonly claims: AdminSsoClaims }
|
|
| { readonly ok: false; readonly reason: "missing-sub" | "missing-email" | "email-not-verified" | "no-groups" };
|
|
|
|
/**
|
|
* Validate + extract the claim shape we trust. Authentik emits standard
|
|
* OIDC claims plus `groups` from the configured scope. We are deliberately
|
|
* conservative: missing/invalid claims short-circuit the login flow rather
|
|
* than silently falling back.
|
|
*/
|
|
export function extractAdminSsoClaims(idTokenClaims: Record<string, unknown> | null | undefined): ClaimExtractionResult {
|
|
if (!idTokenClaims) {
|
|
return { ok: false, reason: "missing-sub" };
|
|
}
|
|
const subject = typeof idTokenClaims.sub === "string" ? idTokenClaims.sub.trim() : "";
|
|
if (!subject) {
|
|
return { ok: false, reason: "missing-sub" };
|
|
}
|
|
const email = typeof idTokenClaims.email === "string" ? idTokenClaims.email.trim().toLowerCase() : "";
|
|
if (!email) {
|
|
return { ok: false, reason: "missing-email" };
|
|
}
|
|
// 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) ? [...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() : "";
|
|
const name = nameFromClaim || preferredName || email;
|
|
return {
|
|
ok: true,
|
|
claims: {
|
|
subject,
|
|
email,
|
|
emailVerified,
|
|
name,
|
|
groups,
|
|
authenticationMethods,
|
|
},
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 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);
|
|
}
|
|
|
|
export function hasAllowedAdminGroup(groups: readonly string[] | undefined | null): boolean {
|
|
if (!groups) return false;
|
|
return groups.some(isAllowedAdminGroup);
|
|
}
|