51 lines
1.3 KiB
Elixir
51 lines
1.3 KiB
Elixir
defmodule SsoBsnWeb.Oauth.TokenController do
|
|
@behaviour Boruta.Oauth.TokenApplication
|
|
|
|
use SsoBsnWeb, :controller
|
|
|
|
alias Boruta.Oauth.Error
|
|
alias Boruta.Oauth.TokenResponse
|
|
|
|
def oauth_module, do: Application.get_env(:sso_bsn, :oauth_module, Boruta.Oauth)
|
|
|
|
def token(%Plug.Conn{} = conn, _params) do
|
|
conn |> oauth_module().token(__MODULE__)
|
|
end
|
|
|
|
@impl Boruta.Oauth.TokenApplication
|
|
def token_success(conn, %TokenResponse{
|
|
token_type: token_type,
|
|
access_token: access_token,
|
|
expires_in: expires_in,
|
|
refresh_token: refresh_token,
|
|
id_token: id_token
|
|
}) do
|
|
conn
|
|
|> put_resp_header("pragma", "no-cache")
|
|
|> put_resp_header("cache-control", "no-store")
|
|
|> json(
|
|
%{
|
|
token_type: token_type,
|
|
access_token: access_token,
|
|
expires_in: expires_in,
|
|
refresh_token: refresh_token,
|
|
id_token: id_token
|
|
}
|
|
|> Enum.filter(
|
|
fn
|
|
{_key, nil} -> false
|
|
_ -> true
|
|
end
|
|
)
|
|
|> Enum.into(%{})
|
|
)
|
|
end
|
|
|
|
@impl Boruta.Oauth.TokenApplication
|
|
def token_error(conn, %Error{status: status, error: error, error_description: error_description}) do
|
|
conn
|
|
|> put_status(status)
|
|
|> json(%{error: error, error_description: error_description})
|
|
end
|
|
end
|