mirror of
https://github.com/tale/headplane.git
synced 2026-08-10 22:06:52 +00:00
d2c4f5eb2b
* Cookies are now encrypted JWTs (GHSA-wrqq-v7qw-r5w7) * Authentication is stored in the SQLite database (auto-migrated) * Session logic is much cleaner
43 lines
1003 B
TypeScript
43 lines
1003 B
TypeScript
import { createCookie, type LoaderFunctionArgs, redirect } from 'react-router';
|
|
import type { LoadContext } from '~/server';
|
|
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
|
|
|
|
export async function loader({
|
|
request,
|
|
context,
|
|
}: LoaderFunctionArgs<LoadContext>) {
|
|
try {
|
|
await context.sessions.auth(request);
|
|
return redirect('/machines');
|
|
} catch {}
|
|
|
|
if (!context.oidc || !context.config.oidc) {
|
|
throw new Error('OIDC is not enabled');
|
|
}
|
|
|
|
const cookie = createCookie('__oidc_auth_flow', {
|
|
httpOnly: true,
|
|
maxAge: 300, // 5 minutes
|
|
});
|
|
|
|
const redirectUri =
|
|
context.config.oidc?.redirect_uri ?? getRedirectUri(request);
|
|
const data = await beginAuthFlow(
|
|
context.oidc,
|
|
redirectUri,
|
|
context.config.oidc.token_endpoint_auth_method,
|
|
);
|
|
|
|
return redirect(data.url, {
|
|
status: 302,
|
|
headers: {
|
|
'Set-Cookie': await cookie.serialize({
|
|
state: data.state,
|
|
nonce: data.nonce,
|
|
code_verifier: data.codeVerifier,
|
|
redirect_uri: redirectUri,
|
|
}),
|
|
},
|
|
});
|
|
}
|