defmodule SyntropyWeb.Plugs.ApiAuth do @moduledoc """ Enforces Syntropy external API auth when token auth is enabled. """ import Plug.Conn import Phoenix.Controller, only: [json: 2] alias SyntropyWeb.ApiAuth @spec init(term()) :: term() def init(opts), do: opts @spec call(Plug.Conn.t(), term()) :: Plug.Conn.t() def call(conn, _opts) do case ApiAuth.validate_conn(conn) do :ok -> conn {:error, reason} -> conn |> put_status(:unauthorized) |> json(%{ error: %{ code: "unauthorized", message: ApiAuth.error_message(reason) } }) |> halt() end end end