headplane/app/routes/auth/login.tsx

141 lines
3.8 KiB
TypeScript
Raw Normal View History

2025-01-28 16:02:47 -05:00
import { useMemo } from 'react';
import {
type ActionFunctionArgs,
type LoaderFunctionArgs,
redirect,
} from 'react-router';
2024-12-31 10:30:14 +05:30
import { Form, useActionData, useLoaderData } from 'react-router';
import Button from '~/components/Button';
import Card from '~/components/Card';
import Code from '~/components/Code';
2025-01-28 16:02:47 -05:00
import Input from '~/components/Input';
2024-12-31 10:31:50 +05:30
import type { Key } from '~/types';
2024-12-31 10:30:14 +05:30
import { loadContext } from '~/utils/config/headplane';
import { pull } from '~/utils/headscale';
import { commitSession, getSession } from '~/utils/sessions.server';
2024-03-25 17:51:11 -04:00
export async function loader({ request }: LoaderFunctionArgs) {
2024-12-31 10:30:14 +05:30
const session = await getSession(request.headers.get('Cookie'));
2024-03-26 11:11:12 -04:00
if (session.has('hsApiKey')) {
return redirect('/machines', {
headers: {
2024-05-15 21:54:02 -04:00
'Set-Cookie': await commitSession(session),
},
2024-12-31 10:30:14 +05:30
});
2024-03-26 11:11:12 -04:00
}
2024-12-31 10:30:14 +05:30
const context = await loadContext();
2024-03-25 17:51:11 -04:00
// Only set if OIDC is properly enabled anyways
if (context.oidc?.disableKeyLogin) {
return redirect('/oidc/start');
2024-03-25 17:51:11 -04:00
}
return {
oidc: context.oidc?.issuer,
apiKey: !context.oidc?.disableKeyLogin,
2024-12-31 10:30:14 +05:30
};
2024-03-25 17:51:11 -04:00
}
2024-03-26 11:11:12 -04:00
export async function action({ request }: ActionFunctionArgs) {
2024-12-31 10:30:14 +05:30
const formData = await request.formData();
const oidcStart = formData.get('oidc-start');
const session = await getSession(request.headers.get('Cookie'));
2024-03-30 02:44:06 -04:00
if (oidcStart) {
2024-12-31 10:30:14 +05:30
const context = await loadContext();
2024-05-21 23:57:03 -04:00
if (!context.oidc) {
2024-12-31 10:30:14 +05:30
throw new Error('An invalid OIDC configuration was provided');
}
return redirect('/oidc/start');
2024-03-30 02:44:06 -04:00
}
2024-12-31 10:30:14 +05:30
const apiKey = String(formData.get('api-key'));
2024-03-26 11:11:12 -04:00
// Test the API key
try {
2024-12-31 10:30:14 +05:30
const apiKeys = await pull<{ apiKeys: Key[] }>('v1/apikey', apiKey);
const key = apiKeys.apiKeys.find((k) => apiKey.startsWith(k.prefix));
2024-03-30 02:44:06 -04:00
if (!key) {
2024-12-31 10:30:14 +05:30
throw new Error('Invalid API key');
2024-03-30 02:44:06 -04:00
}
2024-12-31 10:30:14 +05:30
const expiry = new Date(key.expiration);
const expiresIn = expiry.getTime() - Date.now();
const expiresDays = Math.round(expiresIn / 1000 / 60 / 60 / 24);
2024-03-30 02:44:06 -04:00
2024-12-31 10:30:14 +05:30
session.set('hsApiKey', apiKey);
2024-03-30 02:44:06 -04:00
session.set('user', {
subject: 'unknown-non-oauth',
2024-03-30 02:44:06 -04:00
name: key.prefix,
2024-05-15 21:54:02 -04:00
email: `${expiresDays.toString()} days`,
2024-12-31 10:30:14 +05:30
});
2024-03-30 02:44:06 -04:00
return redirect('/machines', {
headers: {
'Set-Cookie': await commitSession(session, {
2024-05-15 21:54:02 -04:00
maxAge: expiresIn,
}),
},
2024-12-31 10:30:14 +05:30
});
2024-07-10 19:36:13 -04:00
} catch {
2024-12-06 11:58:17 -05:00
return {
2024-05-15 21:54:02 -04:00
error: 'Invalid API key',
2024-12-31 10:30:14 +05:30
};
2024-03-26 11:11:12 -04:00
}
}
2024-03-25 17:51:11 -04:00
export default function Page() {
2024-12-31 10:30:14 +05:30
const data = useLoaderData<typeof loader>();
const actionData = useActionData<typeof action>();
const showOr = useMemo(() => data.oidc && data.apiKey, [data]);
2024-03-25 17:51:11 -04:00
return (
2024-05-15 21:54:02 -04:00
<div className="flex min-h-screen items-center justify-center">
<Card className="max-w-sm m-4 sm:m-0" variant="raised">
2024-12-31 10:30:14 +05:30
<Card.Title>Welcome to Headplane</Card.Title>
{data.apiKey ? (
<Form method="post">
<Card.Text className="mb-8 text-sm">
Enter an API key to authenticate with Headplane. You can generate
one by running <Code>headscale apikeys create</Code> in your
terminal.
</Card.Text>
{actionData?.error ? (
<p className="text-red-500 text-sm mb-2">{actionData.error}</p>
) : undefined}
2025-01-28 16:02:47 -05:00
<Input
2024-12-31 10:30:14 +05:30
isRequired
label="API Key"
name="api-key"
placeholder="API Key"
type="password"
/>
<Button className="w-full mt-2.5" variant="heavy" type="submit">
Sign In
2024-12-31 10:30:14 +05:30
</Button>
</Form>
) : undefined}
{showOr ? (
<div className="flex items-center gap-x-1.5 py-1">
<hr className="flex-1 border-ui-300 dark:border-ui-800" />
<span className="text-gray-500 text-sm">or</span>
<hr className="flex-1 border-ui-300 dark:border-ui-800" />
</div>
) : undefined}
{data.oidc ? (
<Form method="POST">
<input type="hidden" name="oidc-start" value="true" />
<Button className="w-full" type="submit">
Single Sign-On
2024-12-31 10:30:14 +05:30
</Button>
</Form>
) : undefined}
2024-03-30 02:46:21 -04:00
</Card>
2024-03-25 17:51:11 -04:00
</div>
2024-12-31 10:30:14 +05:30
);
2024-03-25 17:51:11 -04:00
}