Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
1.7 kB · 79 lines
Elixir
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980defmodule GitSyncWeb.Auth do @moduledoc """ Session handling and the gate every browser route passes through.
The operator identity itself comes from Forgejo OIDC; this module only knows how to put it in the session, read it back, and refuse requests that lack it. """
import Plug.Conn import Phoenix.Controller
use GitSyncWeb, :verified_routes
@session_key "operator"
@doc """ Stores the operator in a renewed session and sends them to the dashboard. """ def log_in(conn, operator) do conn |> renew_session() |> put_session(@session_key, operator) |> redirect(to: ~p"/") end
def log_out(conn) do conn |> renew_session() |> redirect(to: ~p"/login") end
def fetch_operator(conn, _opts) do assign(conn, :current_operator, get_session(conn, @session_key)) end
def require_operator(conn, _opts) do if conn.assigns.current_operator do conn else conn |> redirect(to: ~p"/login") |> halt() end end
@doc """ Sends every request to the wizard until the Forgejo instance is configured. """ def require_setup(conn, _opts) do if GitSync.Connections.configured?() do conn else conn |> redirect(to: ~p"/setup") |> halt() end end
@doc """ Closes the wizard once it has been completed, so the gate cannot be repointed at an attacker's instance. """ def require_unconfigured(conn, _opts) do if GitSync.Connections.configured?() do conn |> redirect(to: ~p"/") |> halt() else conn end end
defp renew_session(conn) do conn |> configure_session(renew: true) |> clear_session() endend