View Source Oidcc.Plug.AuthorizationCallback (Oidcc Plug v0.1.2)
Retrieve Token for Code Flow Authorization Callback
This plug does not send a response. Instead it will load and validate all token data and leave the rest to a controller action that will be executed after.
Via Phoenix.Router
defmodule SampleAppWeb.Router do
use Phoenix.Router
# ...
pipeline :oidcc_callback do
plug Oidcc.Plug.AuthorizationCallback,
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
forward "/oidcc/authorize", to: Oidcc.Plug.Authorize,
init_opts: [...]
scope "/oidcc/callback", SampleAppWeb do
pipe_through :oidcc_callback
get "/", AuthController, :handle_callback
post "/", AuthController, :handle_callback
end
end
Via Controller
defmodule SampleAppWeb.AuthController do
# ...
plug Oidcc.Plug.AuthorizationCallback,
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"
when action in [:handle_callback]
def handle_callback(
%Plug.Conn{private: %{
Oidcc.Plug.AuthorizationCallback => {:ok, {token, userinfo}}
}},
_params
) do
# Handle Success
conn
|> put_session("auth_token", token)
|> put_session("auth_userinfo", userinfo)
|> redirect(to: "/")
end
def handle_callback(
%Plug.Conn{private: %{
Oidcc.Plug.AuthorizationCallback => {:error, reason}}
},
_params
) do
# Handle Error
conn
|> put_status(400)
|> render("error.html", reason: reason)
end
end
Summary
Types
@type error() :: :oidcc_client_context.error() | :oidcc_token.error() | :oidcc_userinfo.error() | :useragent_mismatch | :peer_ip_mismatch | {:missing_request_param, param :: String.t()}
@type opts() :: [ provider: GenServer.name(), client_id: String.t() | (-> String.t()), client_secret: String.t() | (-> String.t()), redirect_uri: String.t() | (-> String.t()), check_useragent: boolean(), check_peer_ip: boolean(), retrieve_userinfo: boolean(), request_opts: :oidcc_http_util.request_opts() ]
Plug Configuration Options
Options
provider
- name of theOidcc.ProviderConfiguration.Worker
client_id
- OAuth Client ID to use for the introspectionclient_secret
- OAuth Client Secret to use for the introspectionredirect_uri
- Where to redirect for callbackcheck_useragent
- check if useragent is the same as before the authorization requestcheck_peer_ip
- check if the client IP is the same as before the authorization requestretrieve_userinfo
- whether to load userinfo from the providerrequest_opts
- request opts for http calls to provider