Oidcc.Plug.Authorize (Oidcc Plug v0.5.1)

Copy Markdown View Source

Initiate Code Flow Authorization Redirect

defmodule SampleAppWeb.Router do
  use Phoenix.Router

  # ...

  forward "/oidcc/authorize", to: Oidcc.Plug.Authorize,
    init_opts: [
      provider: SampleApp.GoogleOpenIdConfigurationProvider,
      client_id: Application.compile_env!(:sample_app, [Oidcc.Plug.Authorize, :client_id]),
      client_secret: Application.compile_env!(:sample_app, [Oidcc.Plug.Authorize, :client_secret]),
      redirect_uri: "https://localhost:4000/oidcc/callback"
    ]
end

Query Params

  • state - State to relay to OpenID Provider. Commonly used for target redirect URL after authorization. Accessible through Plug.Conn.private[Elixir.Oidcc.Plug.Authorize.State] after Oidcc.Plug.AuthorizationCallback

Redirect Modes

The plug has two redirect modes.

In :inline mode, the plug automatically sends a redirect response.

In :manual mode, the plug assigns the redirect URI as a private value to the connection. You will need to perform the redirect in your controller function.

plug Oidcc.Plug.Authorize,
     [
       # ...
       redirect_mode: :manual
     ]
     when action == :request

def request(conn, params) do
  redirect_uri = Map.fetch!(conn.private, Oidcc.Plug.Authorize)

  conn
  |> put_resp_header("location", redirect_uri)
  |> send_resp(302, "")
end

This mode is useful if you need to put values into the session before the redirect, or if you need to create a continuity cookie for an OIDC provider that uses the form_post response type.

Summary

Types

Plug Configuration Options

Types

opts()

(since 0.1.0)
@type opts() :: [
  scopes: :oidcc_scope.scopes(),
  redirect_uri: String.t() | (-> String.t()) | (Plug.Conn.t() -> String.t()),
  redirect_mode: :inline | :manual,
  url_extension: :oidcc_http_util.query_params(),
  provider: GenServer.name() | nil,
  client_store: module() | nil,
  client_id: String.t() | (-> String.t()) | (Plug.Conn.t() -> String.t()) | nil,
  client_secret:
    String.t() | (-> String.t()) | (Plug.Conn.t() -> String.t()) | nil,
  client_context_opts:
    :oidcc_client_context.opts() | (-> :oidcc_client_context.opts()) | nil,
  client_profile_opts: :oidcc_profile.opts(),
  purpose: String.t(),
  require_purpose: boolean(),
  require_pkce: boolean(),
  response_mode: String.t(),
  request_opts: :oidcc_http_util.request_opts()
]

Plug Configuration Options

Options

  • scopes - scopes to request
  • redirect_uri - Where to redirect for callback
  • redirect_mode - Selects how the redirect happens. Either :inline or :manual.
  • url_extension - Custom query parameters to add to the redirect URI
  • provider - name of the Oidcc.ProviderConfiguration.Worker
  • client_id - OAuth Client ID to use for the introspection
  • client_secret - OAuth Client Secret to use for the introspection
  • client_context_opts - Options for Client Context Initialization
  • client_profile_opts - Options for Client Context Profiles
  • client_store - A module name that implements the Oidcc.Plug.ClientStore behaviour to fetch the client context from a store instead of using the provider, client_id and client_secret directly. This is useful for storing the client context in a database or other persistent storage.
  • purpose - purpose of the authorization request, see [https://cdn.connectid.com.au/specifications/oauth2-purpose-01.html]
  • require_purpose - whether to require a purpose value
  • require_pkce - whether to require PKCE when getting the token
  • response_mode - response mode to use (defaults to "query")
  • request_opts - config for the pushed authorization HTTP request