From 96fd09ff0f0e92d2575b821d7367b0adb62cf3cb Mon Sep 17 00:00:00 2001 From: Hernan Alvarado Date: Thu, 25 Jun 2026 19:40:19 +0000 Subject: [PATCH] feat(oidc): add Authentik OpenID Connect Provider --- .../content/docs/(core)/oauth/authentik.mdx | 202 ++++++++++++++++++ packages/core/src/oauth/authentik.ts | 45 ++++ 2 files changed, 247 insertions(+) create mode 100644 docs/src/content/docs/(core)/oauth/authentik.mdx create mode 100644 packages/core/src/oauth/authentik.ts diff --git a/docs/src/content/docs/(core)/oauth/authentik.mdx b/docs/src/content/docs/(core)/oauth/authentik.mdx new file mode 100644 index 00000000..930dd439 --- /dev/null +++ b/docs/src/content/docs/(core)/oauth/authentik.mdx @@ -0,0 +1,202 @@ +--- +title: Authentik +description: Add Authentik authorization provider to Aura Auth for authentication and authorization +--- + + + + + +## Authentik OAuth App + +### Register the Application + +The first step is to create and register an OAuth App on the Authentik Applications to obtain access to the user's resources. + +1. Navigate to your Authentik profile and go to **Applications > Applications > New Provider**. +2. Click **New Application**. +3. Select **OAuth2/OIDC** as Provider Type +3. Fill in the "Application name" and "Homepage URL". +4. Set the **Authorization callback URL** to `http://localhost:3000/auth/callback/authentik`. + - _(Make sure to replace `localhost:3000` with your production domain when deploying)_ +5. Click **Register application**. +6. Ensure you copy the **Client ID** and click **Generate a new client secret**. + + + + + +## Installation + +Install the package using a package manager like `npm`, `pnpm`, or `yarn`: + +```npm +npm install @aura-stack/auth +``` + + + + + +## Environment setup + +Now, you must configure the environment variables required by Aura Auth, including the Authentik credentials and the encryption secrets. + +```bash title=".env" lineNumbers +# Aura Secrets +AURA_AUTH_SECRET="your-32-byte-secret" +AURA_AUTH_SALT="your-32-byte-salt" + +# Authentik Credentials +AURA_AUTH_AUTHENTIK_CLIENT_ID="your_authentik_client_id" +AURA_AUTH_AUTHENTIK_CLIENT_SECRET="your_authentik_client_secret" +``` + + + **CRITICAL SECURITY WARNING:** The `AURA_AUTH_SECRET` and `AURA_AUTH_SALT` variables are used to encrypt and sign user sessions. + These MUST be securely generated, highly randomized strings consisting of at least 32 bytes to ensure adequate entropy. Never + hardcode these values in your repository. Use a secure generator (like `openssl rand -base64 32`) to create them, and store them + exclusively in your secure environment variables manager. + + + + + + +## Configure the Auth Instance + +Configure the `createAuth` instance inside an `auth.ts` file located at the root of your project. Ensure you explicitly export the `handlers`, `api`, and `jose` objects. + +```ts title="auth.ts" lineNumbers +import { createAuth } from "@aura-stack/auth" + +export const auth = createAuth({ + oauth: ["authentik"], +}) + +// Extract the required utilities +export const { handlers, api, jose } = auth +``` + + + The `handlers` object contains mapping utilities for standard HTTP methods (`GET`, `POST`, `PATCH`) as well as a unified `ALL` + handler. This allows you to easily mount the authentication routes across any framework (Next.js, Elysia, Express, etc.). + + + + + + +## Customizing the OAuth Provider + +If you need to define custom scopes, change the response type, or map profile data differently, you can use the provider's factory function instead of a simple string identifier. + +```ts title="auth.ts" lineNumbers +import { createAuth } from "@aura-stack/auth" +import { authentik } from "@aura-stack/auth/oauth/authentik" + +export const auth = createAuth({ + oauth: [ + authentik({ + authorize: { + params: { + // Override default scopes + scope: "read:user user:email", + }, + }, + }), + ], +}) + +export const { handlers, api, jose } = auth +``` + + + + + +## Sign In to Authentik (Client & Server) + +There are multiple ways to trigger the sign-in flow depending on your ecosystem. + +### Sign-in Path (Direct Navigation) + +The common route to trigger the auth flow natively without needing a client library is simply navigating the browser to: +`http://localhost:3000/auth/signIn/authentik` + +--- + +### Client-Side (React, Vue, etc.) + +You can utilize the `createAuthClient` utility to programmatically trigger sign-ins. You can also define a `redirectTo` destination. + + + **Constraint Rule**: The `baseURL` passed into `createAuthClient` MUST exactly match the root domain and path where the HTTP + `handlers` expose their endpoints on the server. + + +```ts title="components/Login.tsx" lineNumbers +import { createAuthClient } from "@aura-stack/auth/client" + +export const authClient = createAuthClient({ + baseURL: "http://localhost:3000/auth", +}) + +const triggerSignIn = async () => { + await authClient.signIn("authentik", { + redirectTo: "/dashboard", + }) +} +``` + +--- + +### Server-Side (Next.js Actions, Remix Loaders, etc.) + +For environments supporting server-side actions, use the programmatic `api.signIn` method securely. + +```ts title="actions.ts" lineNumbers +import { api } from "./auth" + +export const serverSignIn = async () => { + const response = await api.signIn("authentik", { + redirectTo: "http://localhost:3000/dashboard", + }) + + // Example returning redirect location + return response.headers.get("Location") +} +``` + +--- + +### Session Retrieval + +After a user successfully signs in, you can retrieve their session data securely. + +**Client-Side:** + +```ts +const session = await authClient.getSession() +console.log(session?.user) // The authenticated Authentik user profile +``` + +**Server-Side:** + +```ts +// Note: You must pass the native Web Request object or Headers! +const session = await api.getSession(request) +console.log(session?.user) // Safely retrieved backend session +``` + + + + + +--- + +## Resources + +- [RFC - The OAuth 2.0 Authorization Framework](https://datatracker.ietf.org/doc/html/rfc6749) +- [Authentik - OAuth 2.0 Provider](https://docs.goauthentik.io/add-secure-apps/providers/oauth2/) +- [Authentik - Create an OAuth2 Provider](https://docs.goauthentik.io/add-secure-apps/providers/oauth2/create-oauth2-provider/) \ No newline at end of file diff --git a/packages/core/src/oauth/authentik.ts b/packages/core/src/oauth/authentik.ts new file mode 100644 index 00000000..65c58cb6 --- /dev/null +++ b/packages/core/src/oauth/authentik.ts @@ -0,0 +1,45 @@ +import type { OpenIDProvider, User } from "@/@types/index.ts" + +export interface AuthentikProfile { + iss: string + sub: string + aud: string + exp: number + iat: number + auth_time: number + acr: string + c_hash: string + nonce: string + at_hash: string + email: string + email_verified: boolean + name: string + given_name: string + family_name: string + preferred_username: string + nickname: string +} + +/** + * Authentik OpenID Connect Provider + * + * @see [Authentik - OAuth 2.0 Provider](https://docs.goauthentik.io/add-secure-apps/providers/oauth2/) + * @see [Authentik - Create an OAuth2 Provider](https://docs.goauthentik.io/add-secure-apps/providers/oauth2/create-oauth2-provider/) + */ +export const authentik = ( + options?: Partial> +): OpenIDProvider => { + return { + id: "authentik", + name: "Authentik", + issuer: "https://authentik.company/application/o/:application_slug/.well-known/openid-configuration,", + profile: (profile) => + ({ + sub: profile.sub, + name: profile.name, + email: profile.email, + image: null, + }) as DefaultUser, + ...options, + } +} \ No newline at end of file