feat(oauth): login confirmation page

This commit is contained in:
Ivan R. 2024-09-10 01:40:05 +05:00
parent c1a4b839bd
commit 1eb567cd6d
Signed by: lumin
GPG key ID: E0937DC7CD6D3817
3 changed files with 63 additions and 1 deletions

View file

@ -0,0 +1,44 @@
defmodule ComfycampWeb.OauthController do
use ComfycampWeb, :controller
alias Comfycamp.SSO
alias Comfycamp.SSO.OIDCApp
def confirm(conn, %{"client_id" => client_id, "response_type" => "code"} = params) do
app = %OIDCApp{enabled: true} = SSO.get_oidc_app!(client_id)
render(conn, :confirm,
page_title: "Подтвердите вход",
app_name: app.name,
params: URI.encode_query(params)
)
end
def generate_code(conn, %{"client_id" => client_id, "redirect_uri" => redirect_uri} = params) do
%OIDCApp{enabled: true} = SSO.get_oidc_app!(client_id)
uri = build_redirect_uri(redirect_uri, "test_code", params["state"])
redirect(conn, external: uri)
end
defp build_redirect_uri(redirect_uri, code, state) do
parsed_uri = URI.parse(redirect_uri)
query =
build_query_params(code, state)
|> URI.encode_query()
%{parsed_uri | query: query}
|> URI.to_string()
end
defp build_query_params(code, state) do
params = %{"code" => code}
if state do
Map.put(params, "state", state)
else
params
end
end
end

View file

@ -0,0 +1,16 @@
defmodule ComfycampWeb.OauthHTML do
use ComfycampWeb, :html
def confirm(assigns) do
~H"""
<h1>Подтвердите вход</h1>
<p>Приложению "<%= @app_name %>" будут доступны:</p>
<ul>
<li>Логин</li>
<li>Email</li>
</ul>
<.link href={"/oauth/generate_code?#{@params}"} method="POST">Разрешить доступ</.link>
"""
end
end

View file

@ -28,7 +28,6 @@ defmodule ComfycampWeb.Router do
get "/cinema", CinemaController, :index
end
# Other scopes may use custom stacks.
# scope "/api", ComfycampWeb do
# pipe_through :api
# end
@ -69,6 +68,9 @@ defmodule ComfycampWeb.Router do
scope "/", ComfycampWeb do
pipe_through [:browser, :require_authenticated_user]
get "/oauth", OauthController, :confirm
post "/oauth/generate_code", OauthController, :generate_code
live_session :require_authenticated_user,
on_mount: [{ComfycampWeb.UserAuth, :ensure_authenticated}] do
live "/users/settings", UserSettingsLive, :edit