SaltDns.OIDC (Salt v1.0.0)

Signing in through an OpenID Connect provider (Authentik in production), with the oidcc library: the authorization code flow with PKCE (S256), state and nonce. The ID token's signature, issuer, audience, expiry and nonce are checked by oidcc, and the provider's userinfo is added to its claims. SaltDns.Accounts.oidc_sign_in/2 then decides who the user is.

Ported from Karta.OIDC (MIT, Shiloh B.V.), without Karta's group and role mapping.

Configuration (config :salt_dns, SaltDns.OIDC; in production from the SALT_DNS_OIDC_* variables, see config/runtime.exs):

  • issuer - the provider's issuer URL (its discovery document is <issuer>/.well-known/openid-configuration). Without it, sign-in through a provider is off.
  • client_id, client_secret - this application's client at the provider (a confidential client).
  • name - shown on the sign-in button (default "single sign-on").
  • scopes - default ["openid", "email", "profile"].
  • auto_create - create a user at first sign-in (default true).
  • cacertfile - a CA bundle for the provider's certificate, when it is not signed by a CA the system trusts. The certificate is always verified, with its host name.
  • allow_http - allow a provider without TLS. For tests only.

The provider's configuration and keys are loaded at start and refreshed by a worker (child_spec/1); while the provider cannot be reached the worker retries with a growing pause, and sign-in fails with :provider_unavailable until it succeeds. The application starts either way.

Summary

Functions

Starts a sign-in: the provider's authorization URL for redirect_uri, and the flow to keep in the session until the provider sends the user back (state, nonce, the PKCE verifier, the time it started).

Finishes a sign-in with the provider's answer (params of the redirect back) and the flow from authorize/1: the claims of the ID token and userinfo, or {:error, reason}

The provider worker (started by the application when a provider is configured).

The configuration, with defaults.

True when a provider is configured.

Checks a logout token of OpenID Connect Back-Channel Logout 1.0 and gives the subject and session it names: {:ok, %{sub: sub, sid: sid_or_nil}}, {:error, reason} for a token that is refused (the reason is a short atom, safe to log), or {:error, :provider_unavailable} when the provider's keys are not loaded (the caller answers 503 so the provider tries again).

Functions

authorize(redirect_uri)

@spec authorize(String.t()) ::
  {:ok, String.t(), map()} | {:error, :provider_unavailable}

Starts a sign-in: the provider's authorization URL for redirect_uri, and the flow to keep in the session until the provider sends the user back (state, nonce, the PKCE verifier, the time it started).

callback(arg1, flow, redirect_uri)

@spec callback(map(), map() | nil, String.t()) :: {:ok, map()} | {:error, term()}

Finishes a sign-in with the provider's answer (params of the redirect back) and the flow from authorize/1: the claims of the ID token and userinfo, or {:error, reason}:

  • {:provider_error, error} - the provider refused (for example the user cancelled: access_denied);
  • :invalid_state - no flow, another flow's state, or a flow older than ten minutes;
  • :invalid_token - the code could not be exchanged or the ID token did not verify (signature, issuer, audience, expiry, nonce, PKCE);
  • :provider_unavailable.

child_spec(arg)

The provider worker (started by the application when a provider is configured).

config()

@spec config() :: keyword()

The configuration, with defaults.

enabled?()

@spec enabled?() :: boolean()

True when a provider is configured.

verify_logout_token(token)

@spec verify_logout_token(term()) ::
  {:ok, %{sub: String.t(), sid: String.t() | nil}} | {:error, atom()}

Checks a logout token of OpenID Connect Back-Channel Logout 1.0 and gives the subject and session it names: {:ok, %{sub: sub, sid: sid_or_nil}}, {:error, reason} for a token that is refused (the reason is a short atom, safe to log), or {:error, :provider_unavailable} when the provider's keys are not loaded (the caller answers 503 so the provider tries again).

The signature is checked with the same oidcc code and the same algorithms as for an ID token (the provider's id_token_signing_alg_values_supported, never none), against the provider's key set. Then: iss is the configured issuer, aud has this client's id, iat is present, not in the future (a minute of skew) and not older than an hour (a retry after an outage still counts), exp (when present) has not passed, events has the logout member, there is no nonce, sub and jti are present. A token with only a sid is refused: Salt keeps no provider session id.