defmodule TarakanWeb.ClientAuthorizationLive do use TarakanWeb, :live_view alias Tarakan.Accounts.ClientAuthorizations @impl true def mount(%{"user_code" => user_code}, _session, socket) do actor_key = socket.assigns.current_scope.account.id case ClientAuthorizations.get_for_browser(user_code, actor_key) do {:ok, authorization} -> {:ok, socket |> assign(:authorization, authorization) |> assign( :display_code, ClientAuthorizations.display_user_code(authorization.user_code) ) |> assign(:page_title, "Authorize Tarakan Client")} # Throttled and missing look the same on purpose: telling a grinder which # of its guesses were merely rate limited would hand back the oracle. {:error, _reason} -> {:ok, socket |> assign(:authorization, nil) |> assign(:display_code, user_code) |> assign(:page_title, "Login request expired")} end end @impl true def handle_event(action, _params, %{assigns: %{authorization: nil}} = socket) when action in ["approve", "deny"] do {:noreply, put_flash(socket, :error, "This login request has expired or was already used.")} end def handle_event("approve", _params, socket) do account = socket.assigns.current_scope.account case ClientAuthorizations.approve(socket.assigns.authorization, account) do {:ok, authorization} -> {:noreply, socket |> assign(:authorization, authorization) |> put_flash(:info, "Connected. You can go back to the terminal.")} {:error, _reason} -> {:noreply, socket |> assign(:authorization, nil) |> put_flash(:error, "This login request expired or was already used.")} end end def handle_event("deny", _params, socket) do account = socket.assigns.current_scope.account case ClientAuthorizations.deny(socket.assigns.authorization, account) do {:ok, authorization} -> {:noreply, socket |> assign(:authorization, authorization) |> put_flash(:info, "Denied.")} {:error, _reason} -> {:noreply, assign(socket, :authorization, nil)} end end @impl true def render(assigns) do ~H"""
<.icon name="hero-command-line" class={["size-6"]} />

Connect your terminal

Approving lets the client claim jobs as @{@current_scope.account.handle}.

<%= cond do %> <% is_nil(@authorization) -> %>

That code is invalid or expired. Run tarakan login again.

<% @authorization.status == "approved" -> %>

Connected.

Back to your terminal.

<% @authorization.status == "denied" -> %>

Denied. Nothing was granted.

<% true -> %>

Code

<%!-- Deliberately outside the two-token tracking scale (0.12em labels / 0.02em display): this is a code the user transcribes by hand, and the wide gaps keep characters from running together. --%>
{@display_code}
<%!-- The name comes from whoever started the login, not from Tarakan, so it is attributed rather than asserted: a request that calls itself something official must not read as though this page vouches for it. --%>

Calls itself

{@authorization.client_name}

Approving lets it claim jobs and submit reviews as @{@current_scope.account.handle} for about 7 days. Revoke anytime in settings.

Only approve this if you just ran tarakan login yourself and the code above matches your terminal. Nobody at Tarakan will ever ask you for it.

<% end %>
""" end end