Implement Plugins API authorization (#3373)
* Implement Plugins API Token schema * Work with domain change grace period * Do not cast internal data, extend schema with hints * Implement Plugins API authorization * Test no authorization header passed * Preload authorized site * Fixup typespecspull/3608/head^2 r2023.09.26-195
parent
311d66b341
commit
755345f4ba
@ -0,0 +1,31 @@
|
||||
defmodule PlausibleWeb.Plugins.API.Errors do
|
||||
@moduledoc """
|
||||
Common responses for Plugins API
|
||||
"""
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
@spec unauthorized(Plug.Conn.t()) :: Plug.Conn.t()
|
||||
def unauthorized(conn) do
|
||||
conn
|
||||
|> put_resp_header("www-authenticate", ~s[Basic realm="Plugins API Access"])
|
||||
|> error(:unauthorized, "Plugins API: unauthorized")
|
||||
end
|
||||
|
||||
@spec error(Plug.Conn.t(), Plug.Conn.status(), String.t() | [String.t()]) :: Plug.Conn.t()
|
||||
def error(conn, status, message) when is_binary(message) do
|
||||
error(conn, status, [message])
|
||||
end
|
||||
|
||||
def error(conn, status, messages) when is_list(messages) do
|
||||
response =
|
||||
Jason.encode!(%{
|
||||
errors: Enum.map(messages, &%{detail: &1})
|
||||
})
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("application/json")
|
||||
|> send_resp(status, response)
|
||||
|> halt()
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,38 @@
|
||||
defmodule PlausibleWeb.Plugs.AuthorizePluginsAPI do
|
||||
@moduledoc """
|
||||
Plug for Basic HTTP Authentication using
|
||||
Plugins API Tokens lookup.
|
||||
"""
|
||||
|
||||
alias PlausibleWeb.Plugins.API.Errors
|
||||
alias Plausible.Plugins.API.Tokens
|
||||
|
||||
def init(opts), do: opts
|
||||
|
||||
def call(conn, _opts \\ []) do
|
||||
with {:ok, domain, token} <- extract_token(conn),
|
||||
{:ok, conn} <- authorize(conn, domain, token) do
|
||||
conn
|
||||
end
|
||||
end
|
||||
|
||||
defp authorize(conn, domain, token_value) do
|
||||
case Tokens.find(domain, token_value) do
|
||||
{:ok, token} ->
|
||||
{:ok, Plug.Conn.assign(conn, :authorized_site, token.site)}
|
||||
|
||||
{:error, :not_found} ->
|
||||
Errors.unauthorized(conn)
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_token(conn) do
|
||||
case Plug.BasicAuth.parse_basic_auth(conn) do
|
||||
{token_identifier, token_value} ->
|
||||
{:ok, token_identifier, token_value}
|
||||
|
||||
:error ->
|
||||
Errors.unauthorized(conn)
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,56 @@
|
||||
defmodule PlausibleWeb.Plugs.AuthorizePluginsAPITest do
|
||||
use PlausibleWeb.ConnCase, async: true
|
||||
|
||||
alias Plausible.Plugins.API.Tokens
|
||||
alias PlausibleWeb.Plugs.AuthorizePluginsAPI
|
||||
|
||||
import Plug.Conn
|
||||
|
||||
test "plug passes when a token is found" do
|
||||
%{id: site_id} = site = insert(:site, domain: "pass.example.com")
|
||||
{:ok, _, raw} = Tokens.create(site, "Some token")
|
||||
|
||||
credentials = "Basic " <> Base.encode64("#{site.domain}:#{raw}")
|
||||
|
||||
conn =
|
||||
build_conn()
|
||||
|> put_req_header("authorization", credentials)
|
||||
|> AuthorizePluginsAPI.call()
|
||||
|
||||
refute conn.halted
|
||||
assert %Plausible.Site{id: ^site_id} = conn.assigns.authorized_site
|
||||
end
|
||||
|
||||
test "plug halts when a token is not found" do
|
||||
site = insert(:site, domain: "pass.example.com")
|
||||
|
||||
credentials = "Basic " <> Base.encode64("#{site.domain}:invalid-token")
|
||||
|
||||
conn =
|
||||
build_conn()
|
||||
|> put_req_header("authorization", credentials)
|
||||
|> AuthorizePluginsAPI.call()
|
||||
|
||||
assert conn.halted
|
||||
|
||||
assert json_response(conn, 401) == %{
|
||||
"errors" => [
|
||||
%{"detail" => "Plugins API: unauthorized"}
|
||||
]
|
||||
}
|
||||
end
|
||||
|
||||
test "plug halts when no authorization header is passed" do
|
||||
conn =
|
||||
build_conn()
|
||||
|> AuthorizePluginsAPI.call()
|
||||
|
||||
assert conn.halted
|
||||
|
||||
assert json_response(conn, 401) == %{
|
||||
"errors" => [
|
||||
%{"detail" => "Plugins API: unauthorized"}
|
||||
]
|
||||
}
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue