Implement (optional) SSO via OIDC (OpenID Connect) (#64)

This implements optional SSO via OIDC for logging in and signing up (for the first admin sign up or if sign up is allowed). The most requested feature!

Tested with Authentik and Google!

It includes a new `SimpleCache` interface (in-memory, using [`caches`](https://developer.mozilla.org/en-US/docs/Web/API/Window/caches)) for storing the state and code challenges.

Closes #13
This commit is contained in:
Bruno Bernardino
2025-06-05 18:10:40 +01:00
committed by GitHub
parent cabc18f15d
commit aa18dcdb4e
14 changed files with 490 additions and 22 deletions

62
routes/oidc/callback.tsx Normal file
View File

@@ -0,0 +1,62 @@
import { Handlers, PageProps } from 'fresh/server.ts';
import { FreshContextState } from '/lib/types.ts';
import { AppConfig } from '/lib/config.ts';
import { OidcModel } from '/lib/models/oidc.ts';
interface Data {
error?: string;
}
export const handler: Handlers<Data, FreshContextState> = {
async GET(request, context) {
const isSingleSignOnEnabled = await AppConfig.isSingleSignOnEnabled();
if (context.state.user || !isSingleSignOnEnabled) {
return new Response('Redirect', { status: 303, headers: { 'Location': `/` } });
}
let error = '';
try {
const { response } = await OidcModel.validateAndCreateSession(request);
return response;
} catch (validationError) {
console.error(validationError);
error = (validationError as Error).message;
}
return await context.render({
error,
});
},
};
export default function OidcCallback({ data }: PageProps<Data, FreshContextState>) {
return (
<main>
<section class='max-w-screen-md mx-auto flex flex-col items-center justify-center'>
<h1 class='text-4xl mb-6'>
Login with SSO
</h1>
{data?.error
? (
<section class='notification-error'>
<h3>Failed to login!</h3>
<p>{data?.error}</p>
</section>
)
: null}
<h2 class='text-2xl mb-4 text-center'>Go back?</h2>
<p class='text-center mt-2 mb-6'>
Go back to{' '}
<strong>
<a href='/login'>login</a>
</strong>.
</p>
</section>
</main>
);
}