Files
bewcloud/routes/oidc/callback.tsx
Bruno Bernardino aa18dcdb4e 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
2025-06-05 18:10:40 +01:00

63 lines
1.6 KiB
TypeScript

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>
);
}