---
title: "Authentication"
description: "Add Google or any OIDC identity provider to your Pug instance, turn on org SSO connections, and verify domains without DNS."
---

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

# Authentication

Password and magic-link sign-in need only a working [email provider](/self-hosting/configuration#email). This page covers **external sign-in providers** (Google, Okta, Keycloak, Entra ID, Auth0, or anything else that speaks OpenID Connect), org SSO connections, verified domains, and [Turnstile](#turnstile).

A provider reaches your instance in one of two ways:

| | Server-wide provider | Org SSO connection |
|---|---|---|
| Set up by | You, the operator, in `PUG_CONFIG_FILE` | An org admin, under **Settings > SSO & domains** |
| Turned on by | Adding it to the file and restarting | Setting [`PUG_SSO_SECRET_KEY`](#org-sso-connections) once |
| On the sign-in page | A button for everyone | Shown after someone enters an email on one of its domains |
| Who it signs in | Anyone the provider authenticates | Only emails on the verified domains it lists |
| Redirect URI | `/oauth/callback` | `/oauth/callback/<connection id>` |

Org admins also verify domains and choose auto-join, org creation and Require SSO in that tab. [SSO & domains](/get-started/sso) is their guide.

## The config file

Server-wide providers live in a versioned JSON file that the server reads at startup. Point `PUG_CONFIG_FILE` at it:

```yaml
services:
  server:
    environment:
      PUG_CONFIG_FILE: /etc/pug/config.json
    volumes:
      - ./config.json:/etc/pug/config.json:ro
```

```json title="config.json"
{
  "version": 1,
  "auth": {
    "providers": [
      {
        "id": "google",
        "type": "oidc",
        "displayName": "Google",
        "issuerUrl": "https://accounts.google.com",
        "clientId": "YOUR_GOOGLE_CLIENT_ID",
        "clientSecret": "YOUR_GOOGLE_CLIENT_SECRET",
        "scopes": ["openid", "profile", "email"]
      },
      {
        "id": "company_sso",
        "type": "oidc",
        "displayName": "Company SSO",
        "issuerUrl": "https://login.example.com/realms/main",
        "clientId": "pug",
        "scopes": ["openid", "profile", "email"],
        "emailDomains": ["example.com"]
      }
    ]
  }
}
```

| Field | Required? | Description |
|-------|-----------|-------------|
| `id` | **Yes** | Unique lowercase identifier (`^[a-z][a-z0-9_-]*$`, max 63 chars). **Permanent** - see [below](#the-id-is-permanent). |
| `type` | **Yes** | `oidc` is the only supported value. |
| `displayName` | **Yes** | 1-64 characters, shown on the sign-in button. |
| `clientId` | **Yes** | OAuth client ID from the provider. |
| `clientSecret` | No | Only for confidential clients. See [Secrets never reach the browser](#secrets-never-reach-the-browser). |
| `issuerUrl` | **Yes** | The OIDC issuer, exactly as its discovery document's `issuer` names it, trailing slash included. HTTPS with no credentials, query or fragment; HTTP only on `localhost`, `127.0.0.1` or `::1`. |
| `scopes` | No | Defaults to `["openid", "profile", "email"]`. Must include `openid` and `email`. |
| `emailDomains` | No | Email domains this provider speaks for. Only for a provider your company runs, never on Google. See [Domains a provider speaks for](#domains-a-provider-speaks-for). |

The server validates the file at startup, and **any problem stops it**: an unknown field, an unsupported `version`, a duplicate `id`, a missing required value, or a non-HTTPS remote issuer.

## Set up a provider

1. **Create an OIDC client at the identity provider**

   Enable the **Authorization Code flow with PKCE** (`S256`). Register exactly this redirect URI, using your dashboard's origin:

   ```text
   https://YOUR_PUG_DASHBOARD/oauth/callback
   ```
2. **Check the ID token claims**

   The provider must serve OIDC discovery metadata (`/.well-known/openid-configuration`) and put these claims in the ID token:

   - `sub`
   - `email`
   - `email_verified: true`

   A token without `email_verified` is still accepted if it carries Entra ID's `xms_edov: true`, or carries neither claim and has an email on one of the provider's [`emailDomains`](#domains-a-provider-speaks-for). Entra ID never sends `email_verified`, so an Entra ID provider needs one of the two. An explicit `email_verified: false` is always refused.

   `name` and `picture` are optional. The server checks the ID token's signature, issuer, audience, expiry and `nonce`.

   The dashboard reads the discovery document from the browser, so the provider must allow cross-origin requests to it. Okta, Entra ID, Auth0 and Google do.
3. **Add it to config.json and restart**

   Add an entry to `auth.providers` and restart the server. It logs the configured provider IDs at startup, or a warning when there are none. The dashboard fetches the provider list at runtime and renders a button per provider, so it needs no rebuild.

Discovery runs on the **first sign-in**, not at startup, so an unreachable issuer doesn't stop the server. Sign-ins through that provider fail with `OAUTH_PROVIDER_UNAVAILABLE` until the issuer recovers, with no restart needed. The same code covers an `issuerUrl` that differs from the discovery document's `issuer`, a client secret the provider refuses, and an ID token whose signature, issuer or audience doesn't check out. The server logs the cause.

### Redirect URI rules

The `redirect_uri` must use the path `/oauth/callback`, with no query or fragment, over HTTPS except on `localhost`, `127.0.0.1` or `::1`. When the browser sends an `Origin` header, it must be same-origin with that URI. Pug doesn't check the **host**: the redirect URI you register at the identity provider pins it.

An [org SSO connection](#org-sso-connections) uses its own path, `/oauth/callback/<connection id>`, under the same rules. Pug refuses a connection's sign-in on any other path, and a server-wide provider's sign-in on a connection's path. A refused redirect URI fails with `invalid_argument`.

### Secrets never reach the browser

The dashboard gets only each provider's id, type, display name, client ID, issuer and scopes. The server exchanges the authorization code itself, and never returns `clientSecret`.

Set `clientSecret` only when your provider requires a confidential client. Public clients, such as a typical Keycloak SPA client, omit it and rely on PKCE.

> **The id is permanent**
>
> A provider's `id` is stored on every account linked through it, so **renaming it orphans those links**. Affected users still sign in, matched on their verified email, but each picks up a second, stale identity row. Pick the id once (`google`, `okta`, `company_sso`) and change `displayName` instead.

## Google

Configure Google like any other OIDC provider: issuer `https://accounts.google.com`, plus a client ID and secret from the [Google Cloud console](https://console.cloud.google.com/apis/credentials). Create the client as a "Web application" and register the same `/oauth/callback` redirect URI on it. Name the entry `"id": "google"`: existing Google accounts are stored under that id, so they resolve with no migration.

That one client covers Google Workspace SSO for every org on your instance, with no Google setup per org: an admin verifies the domain and turns on the settings. A Workspace account's ID token carries an `hd` claim naming its domain, and Pug reads it as proof of that domain, but only from a provider whose configured issuer is `https://accounts.google.com`. A personal Google account has no `hd` and proves nothing.

Google brings three constraints:

- The redirect URI must be on a public domain name, such as `pug.example.com`, though the name may resolve only inside your network.
- The server needs outbound HTTPS to Google to exchange the code and fetch Google's signing keys, so an air-gapped server can't use Google. Use your own identity provider instead.
- Set the OAuth consent screen to **Internal** if only your own Workspace's accounts should sign in with Google.

> **Breaking change: PUG\_OAUTH\_GOOGLE\_CLIENT\_ID was removed**
>
> Google sign-in used to be configured with `PUG_OAUTH_GOOGLE_CLIENT_ID` (backend) and `VITE_GOOGLE_CLIENT_ID` (dashboard). Both are gone. The server ignores the old variable rather than rejecting it, so an instance upgraded without `PUG_CONFIG_FILE` starts with no external providers and no Google button, and logs a startup warning naming the variable. Move the client ID into `config.json`, add the client secret, which the code exchange needs, and restart.

## Domains a provider speaks for

A provider your company runs can list the email domains it speaks for in `emailDomains`. A sign-in through it with an email on a listed domain **proves** that domain. Entries are exact domains, so list each subdomain you need.

Orgs that verified the domain under **Settings > SSO & domains** act on that proof. With **Auto-join** on, people who prove the domain join the org when they sign in. With **Require SSO** on, accounts on the domain can sign in only through a provider that proves it, and passwords and email links stop working for them. See [SSO & domains](/get-started/sso), and [`pug domains unenforce`](#verified-domains-and-pug-domains) for when that provider breaks.

Pug also trusts the provider for every address on a listed domain, even without `email_verified`. **Never list a domain on a provider anyone can sign up to**: a stranger could register someone else's address there and sign in to that person's Pug account.

`emailDomains` on Google's issuer stops the server at startup. Google Workspace accounts prove their domain through `hd` instead.

> **emailDomains needs Pug v0.0.25 or later**
>
> An older server rejects the unknown field and refuses to start. Add it only after every server is upgraded, and remove it before you roll back.

## Org SSO connections

Org admins can connect their own OIDC provider under **Settings > SSO & domains**, with no change to `PUG_CONFIG_FILE` and no restart. [SSO connections](/get-started/sso#sso-connections) covers the admin's side.

Connections are off until you set a key:

```bash
openssl rand -base64 32    # paste the output into PUG_SSO_SECRET_KEY
```

- The key encrypts the connections' client secrets in Postgres. A key that isn't 32 bytes, base64-encoded, stops the server at startup.
- With it empty, the tab tells admins that connections are off and names this variable. Existing connections can't sign anyone in, and the server logs an error at startup when there are any. A domain that requires SSO through a connection is then locked out until you set the key again or run [`pug domains unenforce`](#verified-domains-and-pug-domains).
- Keep the key stable. A new key can't read the stored secrets, so connection sign-ins fail until each admin enters their client secret again.
- Only the server reads it (`pug server`, or `pug dev`).

Each connection has its own redirect URI, `https://YOUR_PUG_DASHBOARD/oauth/callback/<connection id>`, which the tab shows once the connection is saved; the admin registers it with their provider. Connections live in Postgres and add to the providers in `PUG_CONFIG_FILE`, never replacing them.

### Issuers on a private network

The server fetches each connection's discovery document, signing keys and tokens itself. By default it refuses private, loopback, link-local and cloud metadata addresses, and ignores proxy settings. Providers in `PUG_CONFIG_FILE` skip this check.

Set `PUG_SSO_ALLOW_PRIVATE_ISSUERS=true` when your identity provider is on an internal network, or the server reaches the internet only through a proxy; connections then use the environment's proxy settings. Leave it off if people outside your company can sign up to your instance: any of them can create an org and point a connection at a service inside your network.

> **Org SSO connections need Pug v0.0.26 or later**
>
> Run `pug postgres migrate` when you upgrade. Its migration 022 needs PostgreSQL 15 or later. Rebuild the dashboard from a current `pug-sh/app` too.

## Verified domains and `pug domains`

Org admins verify a domain with a DNS TXT record, `_pug-verification.<domain>`, which the server looks up itself. On a server that can't see your public DNS, such as an air-gapped one or one with its own view of the domain, the check can't succeed. Verify the domain for them instead:

```bash
pug domains verify <org-id> example.com     # mark it verified without DNS
pug domains show example.com                # every org that added it
pug domains release <org-id> example.com    # drop one org's claim, e.g. in a dispute
pug domains unenforce example.com           # turn Require SSO off in every org
```

The commands talk only to Postgres, and leave every org setting to the org's admins except `unenforce`. If SSO breaks while a domain requires it, for example when the provider's client secret expires, nobody on the domain can sign in to turn Require SSO off, admins included. `unenforce` is the way back.

An operator-verified domain shows as **Verified by your administrator**, and setting changes don't re-check its record. Admins find their org's ID under **Settings > Organization**; they can't skip DNS themselves. See the [CLI reference](/self-hosting/cli#domains) for each command's output.

## Accounts and linking

A verified email from an identity provider links to the existing Pug account with the same address, including one with a password or magic-link sign-in. Linking keeps the password, so the user can sign in either way, and marks the account's email verified.

Pug refuses to create or link an account from an email with non-ASCII characters. An account already linked to the provider keeps signing in.

An address with no Pug account gets one, with an organization that has the user as its **admin**, and a default project. It gets no organization or project when:

- the sign-in added the person to an org, through an invite or [auto-join](/get-started/sso#auto-join), or
- an org that verified the email's domain turned off **Let members create their own organizations**.

> **There is no allowlist or invite-only mode**
>
> **Everyone your identity provider authenticates can create an account on your instance**, and usually an organization of their own, as anyone can through a magic link. Scope it at the provider: assign only the intended users or groups to the Pug OIDC client. A verified domain can stop the people on it from creating orgs, but not accounts.

## Turnstile

Pug can put Cloudflare Turnstile in front of password sign-in and magic-link requests. It's off by default. To turn it on, create a Turnstile widget in Cloudflare for the dashboard's hostname, then set both of its keys on the server and restart it:

```bash
PUG_TURNSTILE_SITE_KEY=...
PUG_TURNSTILE_SECRET_KEY=...
```

- Setting only one stops the server at startup.
- The dashboard reads the site key from the server and shows the widget. If a current dashboard gets `TURNSTILE_FAILED` on every sign-in, check that the widget lists the dashboard's hostname and that both keys come from that widget.
- The browser loads the widget from `challenges.cloudflare.com`, and the server calls Cloudflare to check each token, so leave Turnstile off on a server without internet access. While the server can't reach Cloudflare, or Cloudflare refuses its secret key, those requests fail with `TURNSTILE_UNAVAILABLE`.

Unset both keys and restart the server to turn it off.

> **Turnstile needs Pug v0.0.29 or later**
>
> Rebuild the dashboard from a current `pug-sh/app` before you set the keys. An older dashboard sends no token, so every password sign-in and magic-link request is refused with `TURNSTILE_FAILED`.

## Not in this release

Pug keeps no refresh token from the provider and doesn't map provider groups to Pug roles. Signing out of Pug doesn't sign anyone out of the identity provider. SAML and SCIM aren't supported; see [Not supported yet](/get-started/sso#not-supported-yet).

## Further reading

- [SSO & domains](/get-started/sso): the org admin's side - domains, auto-join, Require SSO and connections
- [Configuration](/self-hosting/configuration): `PUG_CONFIG_FILE`, `PUG_SSO_SECRET_KEY` and every other backend variable
- [CLI reference](/self-hosting/cli#domains): `pug domains`
- [Dashboard](/self-hosting/dashboard): build and serve the UI these buttons appear on

Source: https://docs.pug.sh/self-hosting/authentication/index.mdx
