headplane/app/routes/login.tsx

162 lines
4.4 KiB
TypeScript
Raw Normal View History

2024-03-26 11:11:12 -04:00
import { type ActionFunctionArgs, json, type LoaderFunctionArgs, redirect } from '@remix-run/node'
2024-03-30 02:44:06 -04:00
import { Form, useActionData, useLoaderData } from '@remix-run/react'
2024-03-25 17:51:11 -04:00
import { useMemo } from 'react'
2024-03-30 02:46:21 -04:00
import Button from '~/components/Button'
import Card from '~/components/Card'
import Code from '~/components/Code'
2024-03-30 02:46:21 -04:00
import Input from '~/components/Input'
2024-03-30 02:44:06 -04:00
import { type Key } from '~/types'
2024-03-26 11:11:12 -04:00
import { pull } from '~/utils/headscale'
2024-03-25 17:51:11 -04:00
import { startOidc } from '~/utils/oidc'
2024-03-26 11:11:12 -04:00
import { commitSession, getSession } from '~/utils/sessions'
2024-03-25 17:51:11 -04:00
export async function loader({ request }: LoaderFunctionArgs) {
2024-03-26 11:11:12 -04:00
const session = await getSession(request.headers.get('Cookie'))
if (session.has('hsApiKey')) {
return redirect('/machines', {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Set-Cookie': await commitSession(session)
}
})
}
2024-03-25 17:51:11 -04:00
const issuer = process.env.OIDC_ISSUER
const id = process.env.OIDC_CLIENT_ID
const secret = process.env.OIDC_CLIENT_SECRET
const normal = process.env.DISABLE_API_KEY_LOGIN
if (issuer && (!id || !secret)) {
throw new Error('An invalid OIDC configuration was provided')
}
const data = {
oidc: issuer,
apiKey: normal === undefined
}
if (!data.oidc && !data.apiKey) {
throw new Error('No authentication method is enabled')
}
if (data.oidc && !data.apiKey) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return startOidc(data.oidc, id!, request)
}
return data
}
2024-03-26 11:11:12 -04:00
export async function action({ request }: ActionFunctionArgs) {
const formData = await request.formData()
2024-03-30 02:44:06 -04:00
const oidcStart = String(formData.get('oidc-start'))
if (oidcStart) {
const issuer = process.env.OIDC_ISSUER
const id = process.env.OIDC_CLIENT_ID
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return startOidc(issuer!, id!, request)
}
2024-03-26 11:11:12 -04:00
const apiKey = String(formData.get('api-key'))
const session = await getSession(request.headers.get('Cookie'))
// Test the API key
try {
2024-03-30 02:44:06 -04:00
const apiKeys = await pull<{ apiKeys: Key[] }>('v1/apikey', apiKey)
const key = apiKeys.apiKeys.find(k => apiKey.startsWith(k.prefix))
if (!key) {
throw new Error('Invalid API key')
}
const expiry = new Date(key.expiration)
const expiresIn = expiry.getTime() - Date.now()
const expiresDays = Math.round(expiresIn / 1000 / 60 / 60 / 24)
session.set('hsApiKey', apiKey)
session.set('user', {
name: key.prefix,
email: `${expiresDays} days`
})
return redirect('/machines', {
headers: {
// eslint-disable-next-line @typescript-eslint/naming-convention
'Set-Cookie': await commitSession(session)
}
})
2024-03-26 11:11:12 -04:00
} catch (error) {
console.error(error)
return json({
error: 'Invalid API key'
})
}
}
2024-03-25 17:51:11 -04:00
export default function Page() {
const data = useLoaderData<typeof loader>()
2024-03-26 11:11:12 -04:00
const actionData = useActionData<typeof action>()
2024-03-25 17:51:11 -04:00
const showOr = useMemo(() => data.oidc && data.apiKey, [data])
return (
<div className='flex min-h-screen items-center justify-center'>
2024-03-30 02:46:21 -04:00
<Card className='w-96'>
2024-03-25 17:51:11 -04:00
<h1 className='text-2xl mb-8'>Login</h1>
{data.apiKey ? (
2024-03-26 11:11:12 -04:00
<Form method='post'>
2024-03-25 17:51:11 -04:00
<p className='text-sm text-gray-500 mb-4'>
Enter an API key to authenticate with Headplane. You can generate
one by running
{' '}
<Code>
2024-03-25 17:51:11 -04:00
headscale apikeys create
</Code>
2024-03-25 17:51:11 -04:00
{' '}
in your terminal.
</p>
2024-03-26 11:11:12 -04:00
{actionData?.error ? (
<p className='text-red-500 text-sm mb-2'>{actionData.error}</p>
) : undefined}
2024-03-30 02:46:21 -04:00
<Input
2024-03-26 11:11:12 -04:00
required
2024-03-25 17:51:11 -04:00
type='text'
2024-03-26 11:11:12 -04:00
name='api-key'
2024-03-25 17:51:11 -04:00
id='api-key'
className='border rounded-md p-2 w-full'
placeholder='API Key'
/>
2024-03-26 11:11:12 -04:00
2024-03-30 02:46:21 -04:00
<Button
variant='emphasized'
2024-03-26 11:11:12 -04:00
type='submit'
className='bg-gray-800 text-white rounded-md p-2 w-full mt-4'
>
Login
2024-03-30 02:46:21 -04:00
</Button>
2024-03-26 11:11:12 -04:00
</Form>
2024-03-25 17:51:11 -04:00
) : undefined}
{showOr ? (
<div className='flex items-center gap-x-2 py-2'>
2024-03-30 02:46:21 -04:00
<hr className='flex-1 dark:border-zinc-700'/>
2024-03-25 17:51:11 -04:00
<span className='text-gray-500'>or</span>
2024-03-30 02:46:21 -04:00
<hr className='flex-1 dark:border-zinc-700'/>
2024-03-25 17:51:11 -04:00
</div>
) : undefined}
{data.oidc ? (
2024-03-30 02:44:06 -04:00
<Form method='POST'>
<input type='hidden' name='oidc-start' value='true'/>
2024-03-30 02:46:21 -04:00
<Button
variant='emphasized'
type='submit'
className='bg-gray-800 text-white rounded-md p-2 w-full'
>
2024-03-25 17:51:11 -04:00
Login with SSO
2024-03-30 02:46:21 -04:00
</Button>
2024-03-30 02:44:06 -04:00
</Form>
2024-03-25 17:51:11 -04:00
) : undefined}
2024-03-30 02:46:21 -04:00
</Card>
2024-03-25 17:51:11 -04:00
</div>
)
}