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 discoveredissuer: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_secretas secret:private_key_jwt- authenticate with JWT using:private_key_pathor:private_keyas secret
:client_secret- the client secret, required if:client_authentication_methodis:client_secret_basic,:client_secret_post, or:client_secret_jwt:openid_configuration- the OpenID configuration, optional, the configuration will be fetched from:openid_configuration_uriif this is not defined:id_token_signed_response_alg- theid_token_signed_response_algparameter sent by the Client during Registration, defaults toRS256:userinfo_signed_response_alg- theuserinfo_signed_response_algparameter sent by the Client during Registration, required for signed UserInfo responses:id_token_ttl_seconds- the number of seconds fromiatthat 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 requiresissto 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
endNonce
: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.
Summary
Types
Return value of authorize_url/1.
Return value of callback/3.
The session parameters returned from authorize_url/1, to be stored and
passed back into callback/3.
Functions
Generates an authorization URL for the request phase.
Callback phase for generating an access token and fetching user data.
Fetches user params from ID token.
Fetches claims from userinfo endpoint.
Validates the ID token.
Types
@type on_authorize_url() :: Assent.Strategy.OAuth2.on_authorize_url()
Return value of authorize_url/1.
@type on_callback() :: Assent.Strategy.OAuth2.on_callback()
Return value of callback/3.
@type session_params() :: %{ optional(:state) => binary(), optional(:code_verifier) => binary(), optional(:code_challenge) => binary(), optional(:code_challenge_method) => binary(), optional(:nonce) => binary() }
The session parameters returned from authorize_url/1, to be stored and
passed back into callback/3.
Functions
@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.
@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.
See Assent.Strategy.OAuth2.callback/3 for more.
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.
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.
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.