Sign-in goes through Authentik over OIDC with PKCE. Verified against the live provider: the discovery issuer matches the configured one exactly, and the authorize redirect carries code_challenge_method=S256. Sessions are JWTs with no database adapter, which keeps the module usable from edge middleware and makes a sign-in cost no query. The trade-off is stated in the code: the role travels in the token, so removing someone from the admin group takes effect at the next sign-in or when the eight-hour session expires, not instantly. Immediate revocation would mean asking Authentik on every request, which is what api_llm_loxi does and what this application deliberately does not — it drives a shop window, not a fleet. Group matching is trimmed and case-insensitive. Authentik group names are case-sensitive, but a capitalisation mismatch between the group and the environment variable locks the shop owner out silently, and that is the worse of the two failures. An empty variable never promotes anyone. Authorisation is enforced twice. The middleware covers every /admin page and /api/admin route at the edge; a guard inside the handlers repeats the check, because a matcher is a string, strings get edited, and a route falling outside one should not be the same thing as a route with no access control. The rule itself lives in its own framework-free module so it can be tested directly. Unknown HTTP verbs count as writes: new methods arrive locked. Pages get a redirect to the sign-in screen, API routes get a status code — a fetch that receives an HTML login page is a confusing way to learn you are signed out. The device API stays outside the matcher, as the panel cannot sign in and carries its own bearer token; this is covered by a check that /api/display still answers 401 rather than redirecting. The audit diff compares values by their JSON form, so slot arrays and dates compare by value rather than identity, and a save that changed nothing writes no entry. Recording never throws: losing the trail is bad, refusing the user's change because the trail could not be written is worse. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012cSY9pVhZmJUKNN7wf1Myd
39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
/**
|
|
* Mapping an Authentik group membership to what someone may do here.
|
|
*
|
|
* Kept pure and separate from the Auth.js wiring so the rule can be tested
|
|
* exhaustively: this is the only thing standing between a read-only visitor
|
|
* and the shop's opening hours.
|
|
*/
|
|
|
|
export type Role = 'admin' | 'viewer';
|
|
|
|
/**
|
|
* Anyone the identity provider vouches for may look; only members of the
|
|
* configured group may change anything.
|
|
*
|
|
* Comparison is trimmed and case-insensitive. Authentik group names are
|
|
* case-sensitive, but a capitalisation mismatch between the group and the
|
|
* environment variable is a silent lockout of the shop owner, which is the
|
|
* worse failure of the two.
|
|
*/
|
|
export function roleFromGroups(groups: readonly string[], adminGroup: string): Role {
|
|
const target = adminGroup.trim().toLowerCase();
|
|
if (!target) {
|
|
return 'viewer';
|
|
}
|
|
return groups.some((group) => group.trim().toLowerCase() === target) ? 'admin' : 'viewer';
|
|
}
|
|
|
|
/** Reads the `groups` claim defensively: it arrives from an external system. */
|
|
export function groupsFromClaim(claim: unknown): string[] {
|
|
if (!Array.isArray(claim)) {
|
|
return [];
|
|
}
|
|
return claim.filter((value): value is string => typeof value === 'string');
|
|
}
|
|
|
|
export function canWrite(role: Role | undefined): boolean {
|
|
return role === 'admin';
|
|
}
|