# `Assent.Strategy.OIDC`
[🔗](https://github.com/pow-auth/assent/blob/v0.4.0/lib/assent/strategies/oidc.ex#L1)

OpenID Connect strategy.

This is built upon the `Assent.Strategy.OAuth2` strategy with added OpenID
Connect capabilities.

## Configuration

  * `:client_id` - the client ID, required

  * `:base_url` - the OIDC issuer, required, must exactly match the
    discovered `issuer`

  * `:openid_configuration_uri` - the URI for OpenID Provider, optional,
    defaults to `/.well-known/openid-configuration`

  * `:client_authentication_method` - the Client Authentication method to
    use, optional, defaults to `:client_secret_basic`. The value may be one
    of the following:

    * `:none` - no client authentication, used with public clients

    * `:client_secret_basic` - authenticate with basic authorization header

    * `:client_secret_post` - authenticate with post params

    * `:client_secret_jwt` - authenticate with JWT using `:client_secret` as
      secret

    * `:private_key_jwt` - authenticate with JWT using `:private_key_path` or
      `:private_key` as secret

  * `:client_secret` - the client secret, required if
    `:client_authentication_method` is `:client_secret_basic`,
    `:client_secret_post`, or `:client_secret_jwt`

  * `:openid_configuration` - the OpenID configuration, optional, the
    configuration will be fetched from `:openid_configuration_uri` if this is
    not defined

  * `:id_token_signed_response_alg` - the `id_token_signed_response_alg`
    parameter sent by the Client during Registration, defaults to `RS256`

  * `:userinfo_signed_response_alg` - the `userinfo_signed_response_alg`
    parameter sent by the Client during Registration, required for signed
    UserInfo responses

  * `:id_token_ttl_seconds` - the number of seconds from `iat` that an ID
    Token will be considered valid, optional, defaults to nil

  * `:nonce` - the nonce to use for authorization request, optional, MUST be
    session based and unguessable

  * `:trusted_audiences` - a list of audiences to trust in addition to the
    client_id, optional

  * `:trusted_issuers` - a list of issuers to trust in addition to the
    provider's issuer, optional. The OIDC spec requires `iss` to match
    exactly, so only set this for providers that issue under more than one
    issuer.

See `Assent.Strategy.OAuth2` for more.

## Usage

    defmodule OIDCAuth do
      import Plug.Conn

      alias Assent.Strategy.OIDC

      defp config do
        [
          client_id: Application.fetch_env!(:my_app, :oidc, :client_id),
          client_secret: Application.fetch_env!(:my_app, :oidc, :client_secret),
          base_url: "https://server.example.com",
          authorization_params: [scope: "user:read user:write"],
          redirect_uri: "http://localhost:4000/auth/callback"
        ]
      end

      def request(conn) do
        config()
        |> OIDC.authorize_url()
        |> case do
          {:ok, %{url: url, session_params: session_params}} ->
            conn
            |> put_session(:session_params, session_params)
            |> put_resp_header("location", url)
            |> send_resp(302, "")

          {:error, _error} ->
            send_resp(conn, 500, "Failed authorization")
        end
      end

      def callback(conn) do
        %{params: params} = fetch_query_params(conn)
        session_params = get_session(conn, :session_params)
        conn = delete_session(conn, :session_params)

        config()
        |> Keyword.put(:session_params, session_params)
        |> OIDC.callback(params)
        |> case do
          {:ok, %{user: user, token: token}} ->
            conn
            |> put_session(:user, user)
            |> put_session(:token, token)
            |> put_resp_header("location", "/")
            |> send_resp(302, "")

          {:error, _error} ->
            send_resp(conn, 500, "Failed authorization")
        end
      end
    end

## Nonce

`:nonce` can be set in the provider config. The `:nonce` will be returned in
the `:session_params` along with `:state`. You can use this to store the value
in the current session e.g. an `HttpOnly` session cookie.

A random value generator can look like this:

    16
    |> :crypto.strong_rand_bytes()
    |> Base.encode64(padding: false)

Assent will dynamically generate one for the session if `:nonce` is set to
`true`.

See `Assent.Strategy.OIDC.authorize_url/1` for more.

# `on_authorize_url`

```elixir
@type on_authorize_url() :: Assent.Strategy.OAuth2.on_authorize_url()
```

Return value of `authorize_url/1`.

# `on_callback`

```elixir
@type on_callback() :: Assent.Strategy.OAuth2.on_callback()
```

Return value of `callback/3`.

# `session_params`

```elixir
@type session_params() :: %{
  optional(:state) =&gt; binary(),
  optional(:code_verifier) =&gt; binary(),
  optional(:code_challenge) =&gt; binary(),
  optional(:code_challenge_method) =&gt; binary(),
  optional(:nonce) =&gt; binary()
}
```

The session parameters returned from `authorize_url/1`, to be stored and
passed back into `callback/3`.

# `authorize_url`

```elixir
@spec authorize_url(Keyword.t()) :: on_authorize_url()
```

Generates an authorization URL for the request phase.

The authorization URL will be fetched from the OpenID configuration URI.

`openid` will automatically be added to the `:scope` in
`:authorization_params`, unless `:openid_default_scope` has been set.

Add `:nonce` to the config to pass it with the authorization request. The
nonce will be returned in `:session_params`. The nonce MUST be session based
and unguessable. A cryptographic hash of a cryptographically random value
could be stored in an `HttpOnly` session cookie.

See `Assent.Strategy.OAuth2.authorize_url/1` for more.

# `callback`

```elixir
@spec callback(Keyword.t(), map(), atom()) :: on_callback()
```

Callback phase for generating an access token and fetching user data.

The token URL will be fetched from the OpenID configuration URI.

If the returned ID Token is signed with a symmetric key, `:client_secret`
will be required and used to verify the ID Token. If it was signed with a
private key, the appropriate public key will be fetched from the `jwks_uri`
setting in the OpenID configuration to verify the ID Token.

The ID Token will be validated per
[OpenID Connect Core 1.0 rules](https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation).

See `Assent.Strategy.OAuth2.callback/3` for more.

# `fetch_user`

```elixir
@spec fetch_user(Keyword.t(), map()) :: {:ok, map()} | {:error, term()}
```

Fetches user params from ID token.

The ID Token is validated, and the claims are returned as the user params.
Use `fetch_userinfo/2` to fetch the claims from the `userinfo` endpoint.

# `fetch_userinfo`

```elixir
@spec fetch_userinfo(Keyword.t(), map()) :: {:ok, map()} | {:error, term()}
```

Fetches claims from userinfo endpoint.

The userinfo will be fetched from the `userinfo_endpoint` OpenID
configuration.

The returned claims will be validated against the `id_token` verifying that
`sub` is equal.

# `validate_id_token`

```elixir
@spec validate_id_token(Keyword.t(), binary()) :: {:ok, map()} | {:error, term()}
```

Validates the ID token.

The OpenID configuration will be dynamically fetched if not set in the
config.

The ID Token will be validated per
[OpenID Connect Core 1.0 rules](https://openid.net/specs/openid-connect-core-1_0.html#IDTokenValidation).

---

*Consult [api-reference.md](api-reference.md) for complete listing*
