> ## Documentation Index
> Fetch the complete documentation index at: https://kaneo.app/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Custom OAuth/OIDC

> Configure Kaneo with your custom OpenID Connect (OIDC) provider.

## Overview

Kaneo supports custom OAuth 2.0 and OpenID Connect (OIDC) providers, allowing you to connect an identity provider such as Keycloak, Auth0, Okta, Azure AD, or self-hosted solutions like Pocket ID.

Use the public callback URL shown below. If `KANEO_API_URL` already ends in `/api`, do not add another `/api` segment.

## Configuration

To configure a custom OAuth/OIDC provider, you need to set the following environment variables in your `.env` file:

### Provider settings

| Variable | Description | Example |
| - | - | - |
| `CUSTOM_OAUTH_CLIENT_ID` | OAuth client ID from your provider | `2b1ae9df-8d25-4dbc-8cc8-f8f2c1ef6bd0` |
| `CUSTOM_OAUTH_CLIENT_SECRET` | OAuth client secret from your provider | `your-client-secret` |
| `CUSTOM_OAUTH_AUTHORIZATION_URL` | Authorization endpoint URL | `https://id.example.com/authorize` |
| `CUSTOM_OAUTH_TOKEN_URL` | Token exchange endpoint URL | `https://id.example.com/api/oidc/token` |
| `CUSTOM_OAUTH_USER_INFO_URL` | User info endpoint URL | `https://id.example.com/api/oidc/userinfo` |

If you use a discovery URL, the provider can supply the authorization, token, and user-info endpoints. You still need the client ID and secret.

### Optional settings

| Variable | Description | Default |
| - | - | - |
| `CUSTOM_OAUTH_DISCOVERY_URL` | OpenID Connect discovery document URL | - |
| `CUSTOM_OAUTH_SCOPES` | Comma-separated list of OAuth scopes | `profile,email` |
| `CUSTOM_OAUTH_RESPONSE_TYPE` | OAuth response type | `code` |
| `CUSTOM_AUTH_PKCE` | Enable/disable PKCE (Proof Key for Code Exchange) | `true` |
| `CUSTOM_OAUTH_NAME` | Provider name on the sign-in button, for example `Authentik` | `SSO` |
| `CUSTOM_OAUTH_LOGOUT_URL` | Provider logout endpoint, if needed | Unset |
| `CUSTOM_OAUTH_BACKCHANNEL_LOGOUT` | Sign out Kaneo sessions when the provider sends a back-channel logout. Requires `CUSTOM_OAUTH_DISCOVERY_URL`. See [back-channel logout](#back-channel-logout) | `false` |
| `CUSTOM_OAUTH_AUTO_LOGIN` | Auto-redirect to the custom OAuth provider, bypassing the login page | `false` |
| `CUSTOM_OAUTH_MANAGED_PROFILE_FIELDS` | Comma-separated profile fields the provider manages: `name`, `image`, `locale`. See [managed profile fields](#managed-profile-fields) | Unset |
| `CUSTOM_OAUTH_ACCOUNT_URL` | Provider account page, linked next to managed profile fields | Unset |

## Setup Steps

### 1. Configure Your OAuth Provider

First, create an OAuth 2.0 or OIDC application in your identity provider:

1. Log in to your identity provider's admin console
2. Create a new OAuth 2.0 or OIDC application
3. Set the redirect URI to: `https://kaneo.example.com/api/auth/oauth2/callback/custom`
   * Example: `https://api.kaneo.example.com/api/auth/oauth2/callback/custom`
4. Copy the client ID and client secret
5. Note the authorization, token, and userinfo endpoint URLs

### 2. Set Environment Variables

Add the following to your `.env` file:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
# Custom OAuth/OIDC Configuration
CUSTOM_OAUTH_CLIENT_ID=your-client-id
CUSTOM_OAUTH_CLIENT_SECRET=your-client-secret
CUSTOM_OAUTH_AUTHORIZATION_URL=https://your-idp.com/authorize
CUSTOM_OAUTH_TOKEN_URL=https://your-idp.com/oauth/token
CUSTOM_OAUTH_USER_INFO_URL=https://your-idp.com/oauth/userinfo
CUSTOM_OAUTH_LOGOUT_URL=https://your-idp.com/oauth/logout

# Optional: Use discovery URL for automatic configuration
CUSTOM_OAUTH_DISCOVERY_URL=https://your-idp.com/.well-known/openid-configuration

# Optional: Customize scopes
CUSTOM_OAUTH_SCOPES=profile,email,openid

# Keep PKCE enabled when your provider supports it
CUSTOM_AUTH_PKCE=true
```

### 3. Apply the configuration

After updating the environment variables, recreate the bundled application container:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
docker compose up -d --force-recreate kaneo
```

## Example Configurations

### Pocket ID

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_CLIENT_ID=2b1ae9df-8d25-4dbc-8cc8-f8f2c1ef6bd0
CUSTOM_OAUTH_CLIENT_SECRET=your-client-secret
CUSTOM_OAUTH_AUTHORIZATION_URL=https://id.example.com/authorize
CUSTOM_OAUTH_TOKEN_URL=https://id.example.com/api/oidc/token
CUSTOM_OAUTH_USER_INFO_URL=https://id.example.com/api/oidc/userinfo
CUSTOM_OAUTH_DISCOVERY_URL=https://id.example.com/.well-known/openid-configuration
CUSTOM_OAUTH_SCOPES=openid,profile,email
```

### Keycloak

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_CLIENT_ID=kaneo-client
CUSTOM_OAUTH_CLIENT_SECRET=your-secret-here
CUSTOM_OAUTH_DISCOVERY_URL=https://keycloak.example.com/realms/your-realm/.well-known/openid-configuration
CUSTOM_OAUTH_LOGOUT_URL=https://keycloak.example.com/realms/your-realm/protocol/openid-connect/logout
CUSTOM_OAUTH_SCOPES=openid,profile,email
```

### Auth0

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_CLIENT_ID=your-client-id
CUSTOM_OAUTH_CLIENT_SECRET=your-client-secret
CUSTOM_OAUTH_AUTHORIZATION_URL=https://your-tenant.auth0.com/authorize
CUSTOM_OAUTH_TOKEN_URL=https://your-tenant.auth0.com/oauth/token
CUSTOM_OAUTH_USER_INFO_URL=https://your-tenant.auth0.com/userinfo
CUSTOM_OAUTH_SCOPES=openid,profile,email
```

## Usage

Once configured, users will see a "Continue with OIDC" button on the sign-in page. The system will automatically remember the last used login method for each user.

### Auto-Login (Skip the Login Page)

If you want to bypass the login page entirely and automatically redirect users to your identity provider, set:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_AUTO_LOGIN=true
```

When enabled, Kaneo redirects visitors to the custom provider. If CAPTCHA is configured, it first shows the sign-in page so the visitor can complete the challenge. Verify existing local accounts before enabling this option; see account linking below.

## Troubleshooting

### "Invalid code verifier" Error

Check that the provider and Kaneo both use PKCE and that the callback returns to the same public instance URL. Keep PKCE enabled for providers that support it:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_AUTH_PKCE=true
```

Only set `CUSTOM_AUTH_PKCE=false` when your provider's documentation requires a flow without PKCE.

### Missing User Information

Ensure your OAuth scopes include at least `profile` and `email`:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_SCOPES=openid,profile,email
```

Some providers require `openid` scope as well:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_SCOPES=openid,profile,email
```

### Redirect URI Mismatch

Verify that your redirect URI in the OAuth provider matches exactly:

```
https://kaneo.example.com/api/auth/oauth2/callback/custom
```

For a separately hosted API at `https://api.kaneo.example.com`, use:

```
https://api.kaneo.example.com/api/auth/oauth2/callback/custom
```

## Discovery URL

If your provider supports OpenID Connect, you can use the discovery URL to automatically configure most settings:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_DISCOVERY_URL=https://your-idp.com/.well-known/openid-configuration
```

This will automatically discover the authorization, token, and userinfo endpoints. However, you still need to provide the client ID and secret.

## Back-channel logout

With OpenID Connect back-channel logout, your identity provider tells Kaneo when a session ends there, for example when someone signs out in another app or an administrator ends their session. Kaneo then signs out the matching Kaneo sessions, even when no Kaneo tab is open.

Kaneo reads the issuer and the signing keys from the discovery document, and it needs the `openid` scope to remember which provider session each sign-in belongs to:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_DISCOVERY_URL=https://keycloak.example.com/realms/your-realm/.well-known/openid-configuration
CUSTOM_OAUTH_SCOPES=openid,profile,email
CUSTOM_OAUTH_BACKCHANNEL_LOGOUT=true
```

Recreate the Kaneo container, then register this back-channel logout URL with your provider:

```
https://kaneo.example.com/api/auth/oauth2/backchannel-logout/custom
```

For a separately hosted API at `https://api.kaneo.example.com`, use `https://api.kaneo.example.com/api/auth/oauth2/backchannel-logout/custom`. If `KANEO_API_URL` already ends in `/api`, do not add another `/api` segment. The provider calls this URL directly, so it must be able to reach Kaneo.

What a logout signs out:

* A logout token with a session ID (`sid`) ends the Kaneo sessions that started from that provider session. Kaneo stores the `sid` of the ID token at every sign-in through the provider, also while `CUSTOM_OAUTH_BACKCHANNEL_LOGOUT` is off, so turning it on later also covers sessions that already exist.
* A logout token without a session ID ends all Kaneo sessions of that person.
* Only sign-ins through the provider store a session ID. Sessions of the Kaneo mobile app, the Kaneo CLI and MCP clients have none, even when someone approves them while signed in through the provider. Sessions that started before you upgraded to a Kaneo version with back-channel logout, or without the `openid` scope, have none either. Only a logout token without a session ID ends them.
* API keys are not sessions. They keep working until you revoke them.

Open Kaneo tabs of an ended session return to the sign-in page right away, while tabs of the person's other sessions stay signed in. A tab without an open connection at that moment notices on its next request. With several API instances, this needs [Redis](https://kaneo.app/docs/core/installation/environment-variables.md#redis).

Kaneo answers `200` when it accepts a logout token, also when no session matched, and `400` for an invalid or reused token. It answers `503` when it cannot load the discovery document or the signing keys, and `404` while `CUSTOM_OAUTH_BACKCHANNEL_LOGOUT` is not `true`. Once Kaneo has loaded the signing keys, it keeps using them while they cannot be refreshed, so a token signed with a key the provider added since then answers `400` until the keys load again.

In Keycloak, open your client and go to **Settings** > **Logout settings**:

1. Turn off **Front channel logout**.
2. Set **Backchannel logout URL** to the URL above.
3. Keep **Backchannel logout session required** on, so that logout tokens carry the session ID.

With **Backchannel logout session required** on, Keycloak sends a session ID in every logout token, so mobile app, CLI and MCP sessions stay signed in after a Keycloak logout. When someone leaves, also [deactivate their account](https://kaneo.app/docs/core/operations/administration.md#manage-users) in Kaneo. That ends all of their sessions and stops their API keys from working.

If Kaneo reads the discovery document through an internal address, configure Keycloak's hostname so the issuer in the document matches the `iss` claim of Keycloak's tokens. Otherwise, Kaneo rejects every logout token.

## Account linking

When a user signs in with this provider using an email that already belongs to a Kaneo account, Kaneo can link the OIDC identity to that account. The provider must return a verified email claim; being configured does not make an unverified email trusted. Configure your identity provider to return `email_verified: true` only after establishing ownership of the email address. An unverified claim cannot satisfy email-based invitation matching either.

For safety, Kaneo only links to an existing local account if that account's email has already been verified. This prevents someone from pre-registering another person's email with a password account and retaining access after the real owner signs in through OIDC. If your instance allows password registration alongside OIDC, consider setting `DISABLE_PASSWORD_REGISTRATION=true` (which also blocks new OTP, magic-link and guest accounts) or requiring email verification for password users to avoid account-linking surprises.

On existing installations, local accounts created before this requirement may still have an unverified email. Those users will receive `error=account_not_linked` until they verify the local account. Before enforcing SSO-only access, keep the login form available so affected users can sign in with an email OTP or magic link, which verifies the email. If users are already locked out, temporarily set `DISABLE_LOGIN_FORM=false` and `CUSTOM_OAUTH_AUTO_LOGIN=false`, recreate the Kaneo container, and have them complete an email sign-in before enabling SSO-only access again.

## Managed profile fields

When your identity provider is the source of truth for names, profile pictures, or languages, let it manage them in Kaneo:

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
CUSTOM_OAUTH_MANAGED_PROFILE_FIELDS=name,image,locale
# Optional: link people to the page where they change these fields
CUSTOM_OAUTH_ACCOUNT_URL=https://keycloak.example.com/realms/your-realm/account
```

At every sign-in through this provider, Kaneo copies the listed fields from the provider's claims. It reads the claims from the ID token when the token contains `sub` and `email`, and from the user info endpoint otherwise.

| Field | Claim | What Kaneo stores |
| - | - | - |
| `name` | `name` | The same name Kaneo uses when it creates the account: `name`, then `given_name` and `family_name`, then `preferred_username`, then the part of the email before the `@`. |
| `image` | `picture` | An absolute `http` or `https` URL of up to 2,048 characters. Without one, Kaneo removes the profile picture, but keeps a picture uploaded in Kaneo until the provider sends one. A picture from the provider replaces the upload and deletes it. |
| `locale` | `locale` | The matching Kaneo language: first by tag, then by language and script, then by language, so `de` becomes `de-DE` and `zh-Hant` or `zh-HK` becomes `zh-TW`. A missing or unsupported value keeps the current language. |

Fields you do not list stay as they are. Accounts that Kaneo creates through the provider start with the same values. Signing in never changes the email address, but it marks the email of an account already linked to the provider as verified when the provider reports the same address as verified.

People with a linked provider account see managed fields as read-only on their profile and preferences pages, with a link to `CUSTOM_OAUTH_ACCOUNT_URL` when you set it. They can still remove a picture they uploaded in Kaneo earlier, but they cannot upload a new one. People without a linked provider account keep full editing.

The API enforces this too, including administrator edits. Profile edits through `POST /api/auth/update-user` and `POST /api/auth/admin/update-user` require a user session; API keys receive `403` with the code `SESSION_REQUIRED`. With a user session, these endpoints answer `403` with the code `PROFILE_FIELD_MANAGED_BY_PROVIDER` when they would change a managed field, except removing a picture uploaded in Kaneo. A request that sends the current value again succeeds. While the provider manages the picture, `PUT /api/user/avatar` answers the same way for sessions and API keys, and so does `DELETE /api/user/avatar` if a picture other than an upload made in Kaneo is stored.

In Keycloak, the default `profile` client scope maps the `picture` and `locale` user attributes to these claims. Keycloak only sets `locale` when internationalization is enabled for the realm.

If you remove the provider settings later, remove `CUSTOM_OAUTH_MANAGED_PROFILE_FIELDS` as well, so people can edit their profiles in Kaneo again.

## Security Notes

* Always use HTTPS in production
* Keep your client secret secure and never commit it to version control
* Enable PKCE when possible for enhanced security
* Use the most restrictive scopes necessary for your use case


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.