feat: switch to a central singleton handler

This also adds support for Headscale TLS installations
This commit is contained in:
Aarnav Tale 2025-03-17 22:21:16 -04:00
parent 43e06987ad
commit 6108de52e7
No known key found for this signature in database
35 changed files with 339 additions and 399 deletions

View file

@ -10,15 +10,10 @@ import Code from '~/components/Code';
import Input from '~/components/Input';
import type { Key } from '~/types';
import { pull } from '~/utils/headscale';
import { noContext } from '~/utils/log';
import { oidcEnabled } from '~/utils/oidc';
import { commitSession, getSession } from '~/utils/sessions.server';
import type { AppContext } from '~server/context/app';
import { hp_getConfig, hp_getSingleton } from '~server/context/global';
export async function loader({
request,
context,
}: LoaderFunctionArgs<AppContext>) {
export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request.headers.get('Cookie'));
if (session.has('hsApiKey')) {
return redirect('/machines', {
@ -28,37 +23,34 @@ export async function loader({
});
}
if (!context) {
throw noContext();
}
const context = hp_getConfig();
const disableApiKeyLogin = context.oidc?.disable_api_key_login;
let oidc = false;
// Only set if OIDC is properly enabled anyways
const ctx = context.context;
if (oidcEnabled() && ctx.oidc?.disable_api_key_login) {
return redirect('/oidc/start');
}
try {
// Only set if OIDC is properly enabled anyways
hp_getSingleton('oidc_client');
oidc = true;
if (disableApiKeyLogin) {
return redirect('/oidc/start');
}
} catch {}
return {
oidc: oidcEnabled(),
apiKey: !ctx.oidc?.disable_api_key_login,
oidc,
apiKey: !disableApiKeyLogin,
};
}
export async function action({
request,
context,
}: ActionFunctionArgs<AppContext>) {
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData();
const oidcStart = formData.get('oidc-start');
const session = await getSession(request.headers.get('Cookie'));
if (oidcStart) {
if (!context) {
throw noContext();
}
const ctx = context.context;
if (!ctx.oidc) {
const context = hp_getConfig();
if (!context.oidc) {
throw new Error('An invalid OIDC configuration was provided');
}

View file

@ -1,14 +1,21 @@
import { type LoaderFunctionArgs, redirect } from 'react-router';
import { noContext } from '~/utils/log';
import { finishAuthFlow, formatError } from '~/utils/oidc';
import { send } from '~/utils/res';
import { commitSession, getSession } from '~/utils/sessions.server';
import type { AppContext } from '~server/context/app';
import { hp_getConfig, hp_getSingleton } from '~server/context/global';
export async function loader({ request }: LoaderFunctionArgs) {
const { oidc } = hp_getConfig();
try {
if (!oidc) {
throw new Error('OIDC is not enabled');
}
hp_getSingleton('oidc_client');
} catch {
return send({ error: 'OIDC is not enabled' }, { status: 400 });
}
export async function loader({
request,
context,
}: LoaderFunctionArgs<AppContext>) {
// Check if we have 0 query parameters
const url = new URL(request.url);
if (url.searchParams.toString().length === 0) {
@ -20,15 +27,6 @@ export async function loader({
return redirect('/machines');
}
if (!context) {
throw noContext();
}
const { oidc } = context.context;
if (!oidc) {
throw new Error('An invalid OIDC configuration was provided');
}
const codeVerifier = session.get('oidc_code_verif');
const state = session.get('oidc_state');
const nonce = session.get('oidc_nonce');

View file

@ -1,25 +1,24 @@
import { type LoaderFunctionArgs, redirect } from 'react-router';
import { noContext } from '~/utils/log';
import { beginAuthFlow, getRedirectUri } from '~/utils/oidc';
import { send } from '~/utils/res';
import { commitSession, getSession } from '~/utils/sessions.server';
import type { AppContext } from '~server/context/app';
import { hp_getConfig, hp_getSingleton } from '~server/context/global';
export async function loader({
request,
context,
}: LoaderFunctionArgs<AppContext>) {
export async function loader({ request }: LoaderFunctionArgs) {
const session = await getSession(request.headers.get('Cookie'));
if (session.has('hsApiKey')) {
return redirect('/machines');
}
if (!context) {
throw noContext();
}
const { oidc } = hp_getConfig();
try {
if (!oidc) {
throw new Error('OIDC is not enabled');
}
const { oidc } = context.context;
if (!oidc) {
throw new Error('An invalid OIDC configuration was provided');
hp_getSingleton('oidc_client');
} catch {
return send({ error: 'OIDC is not enabled' }, { status: 400 });
}
const redirectUri = oidc.redirect_uri ?? getRedirectUri(request);