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