feat: authenticate against Authentik with admin and viewer roles
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
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
import { diffOf } from './audit';
|
||||
|
||||
describe('diffOf', () => {
|
||||
it('reports nothing when nothing moved', () => {
|
||||
// A log full of empty changes is a log nobody reads.
|
||||
expect(diffOf({ a: 1 }, { a: 1 })).toBeNull();
|
||||
expect(diffOf(null, null)).toBeNull();
|
||||
expect(diffOf({}, {})).toBeNull();
|
||||
});
|
||||
|
||||
it('reports a changed field', () => {
|
||||
expect(diffOf({ isClosed: false }, { isClosed: true })).toEqual({
|
||||
isClosed: { before: false, after: true },
|
||||
});
|
||||
});
|
||||
|
||||
it('reports only the fields that moved', () => {
|
||||
expect(diffOf({ a: 1, b: 2 }, { a: 1, b: 3 })).toEqual({ b: { before: 2, after: 3 } });
|
||||
});
|
||||
|
||||
it('reports an added and a removed field', () => {
|
||||
expect(diffOf({ a: 1 }, { a: 1, b: 2 })).toEqual({ b: { before: null, after: 2 } });
|
||||
expect(diffOf({ a: 1, b: 2 }, { a: 1 })).toEqual({ b: { before: 2, after: null } });
|
||||
});
|
||||
|
||||
it('treats a missing field and an explicit null as the same absence', () => {
|
||||
expect(diffOf({ note: null }, {})).toBeNull();
|
||||
expect(diffOf({ note: undefined }, { note: null })).toBeNull();
|
||||
});
|
||||
|
||||
it('compares slot arrays by value, not by identity', () => {
|
||||
const before = { slots: [{ open: '10:00', close: '18:30' }] };
|
||||
const after = { slots: [{ open: '10:00', close: '18:30' }] };
|
||||
expect(diffOf(before, after)).toBeNull();
|
||||
|
||||
expect(diffOf(before, { slots: [{ open: '14:00', close: '18:30' }] })).toEqual({
|
||||
slots: {
|
||||
before: [{ open: '10:00', close: '18:30' }],
|
||||
after: [{ open: '14:00', close: '18:30' }],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('compares dates by their instant, not their object', () => {
|
||||
const before = { startsAt: new Date('2026-09-20T00:00:00Z') };
|
||||
expect(diffOf(before, { startsAt: new Date('2026-09-20T00:00:00Z') })).toBeNull();
|
||||
|
||||
const changed = diffOf(before, { startsAt: new Date('2026-09-21T00:00:00Z') });
|
||||
expect(changed?.startsAt).toEqual({
|
||||
before: '2026-09-20T00:00:00.000Z',
|
||||
after: '2026-09-21T00:00:00.000Z',
|
||||
});
|
||||
});
|
||||
|
||||
it('records a creation and a deletion', () => {
|
||||
expect(diffOf(null, { textFr: 'Bonjour' })).toEqual({
|
||||
textFr: { before: null, after: 'Bonjour' },
|
||||
});
|
||||
expect(diffOf({ textFr: 'Bonjour' }, null)).toEqual({
|
||||
textFr: { before: 'Bonjour', after: null },
|
||||
});
|
||||
});
|
||||
|
||||
it('distinguishes values that only look alike', () => {
|
||||
expect(diffOf({ priority: 1 }, { priority: '1' })).not.toBeNull();
|
||||
expect(diffOf({ isActive: false }, { isActive: null })).not.toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user