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
48 lines
1.6 KiB
TypeScript
48 lines
1.6 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
|
|
import { auth } from '@/lib/auth';
|
|
|
|
/**
|
|
* The single choke point for administrative access.
|
|
*
|
|
* Every /admin page and every /api/admin route passes through here, so no
|
|
* individual page can forget to check. Pages get a redirect to the sign-in
|
|
* screen; API routes get a status code, because a fetch that receives an HTML
|
|
* login page is a confusing way to learn you are signed out.
|
|
*
|
|
* Write access is enforced here too: a `viewer` may read anything and change
|
|
* nothing. Doing it at the edge means a read-only account cannot reach a
|
|
* handler that mutates, whatever that handler remembers to check.
|
|
*
|
|
* The device API (/api/setup, /api/display, /api/log) is deliberately outside
|
|
* this matcher: the panel cannot sign in, and carries its own bearer token.
|
|
*/
|
|
export default auth((request) => {
|
|
const { pathname } = request.nextUrl;
|
|
const isApi = pathname.startsWith('/api/admin');
|
|
const session = request.auth;
|
|
|
|
if (!session?.user) {
|
|
if (isApi) {
|
|
return NextResponse.json({ error: 'Non authentifié' }, { status: 401 });
|
|
}
|
|
const target = new URL('/login', request.nextUrl.origin);
|
|
target.searchParams.set('from', pathname);
|
|
return NextResponse.redirect(target);
|
|
}
|
|
|
|
const isRead = request.method === 'GET' || request.method === 'HEAD';
|
|
if (!isRead && session.user.role !== 'admin') {
|
|
return NextResponse.json(
|
|
{ error: 'Compte en lecture seule' },
|
|
{ status: 403 },
|
|
);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
});
|
|
|
|
export const config = {
|
|
matcher: ['/admin/:path*', '/api/admin/:path*'],
|
|
};
|