Karta.OIDC (Karta v1.0.0)

Signing in through an OpenID Connect provider (tested with Authentik), 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. Karta.Accounts.oidc_sign_in/2 then decides who the user is.

Configuration (config :karta, Karta.OIDC; in production from the KARTA_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 - Karta's client at the provider (a confidential client; the secret is sent with HTTP Basic or in the form, as the provider supports).
  • name - shown on the sign-in button (default "single sign-on").
  • scopes - default ["openid", "email", "profile"].
  • groups_claim, admin_groups, group_roles, auto_create, link_by_email (default false) - see Karta.Accounts.oidc_sign_in/2. parse_group_roles/1 reads group_roles from text.
  • 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. Karta itself 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.

Reads group_roles from text: group:organisation-slug:role entries separated by commas (KARTA_OIDC_GROUP_ROLES). The group is everything before the last two colons, so it may contain colons; not commas.

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 (e.g. 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.

parse_group_roles(text)

@spec parse_group_roles(String.t()) :: [{String.t(), String.t(), atom()}]

Reads group_roles from text: group:organisation-slug:role entries separated by commas (KARTA_OIDC_GROUP_ROLES). The group is everything before the last two colons, so it may contain colons; not commas.