mapped signature plug: use user assign like authentication plug

This commit is contained in:
Ariadne Conill 2019-07-18 20:31:25 +00:00
parent 621cacf667
commit c947cfec5a
2 changed files with 5 additions and 5 deletions

View File

@ -31,14 +31,14 @@ defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlug do
end end
end end
def call(%{assigns: %{mapped_identity: _}} = conn, _opts), do: conn def call(%{assigns: %{user: _}} = conn, _opts), do: conn
# if this has payload make sure it is signed by the same actor that made it # if this has payload make sure it is signed by the same actor that made it
def call(%{assigns: %{valid_signature: true}, params: %{"actor" => actor}} = conn, _opts) do def call(%{assigns: %{valid_signature: true}, params: %{"actor" => actor}} = conn, _opts) do
with actor_id <- Utils.get_ap_id(actor), with actor_id <- Utils.get_ap_id(actor),
{:user, %User{} = user} <- {:user, user_from_key_id(conn)}, {:user, %User{} = user} <- {:user, user_from_key_id(conn)},
{:user_match, true} <- {:user_match, user.ap_id == actor_id} do {:user_match, true} <- {:user_match, user.ap_id == actor_id} do
assign(conn, :mapped_identity, user) assign(conn, :user, user)
else else
{:user_match, false} -> {:user_match, false} ->
Logger.debug("Failed to map identity from signature (payload actor mismatch)") Logger.debug("Failed to map identity from signature (payload actor mismatch)")
@ -56,7 +56,7 @@ defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlug do
# no payload, probably a signed fetch # no payload, probably a signed fetch
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
with %User{} = user <- user_from_key_id(conn) do with %User{} = user <- user_from_key_id(conn) do
assign(conn, :mapped_identity, user) assign(conn, :user, user)
else else
_ -> _ ->
Logger.debug("Failed to map identity from signature (no payload actor mismatch)") Logger.debug("Failed to map identity from signature (no payload actor mismatch)")

View File

@ -26,7 +26,7 @@ defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlugTest do
|> set_signature("http://mastodon.example.org/users/admin") |> set_signature("http://mastodon.example.org/users/admin")
|> MappedSignatureToIdentityPlug.call(%{}) |> MappedSignatureToIdentityPlug.call(%{})
refute is_nil(conn.assigns.mapped_identity) refute is_nil(conn.assigns.user)
end end
test "it successfully maps a valid identity with a valid signature with payload" do test "it successfully maps a valid identity with a valid signature with payload" do
@ -35,7 +35,7 @@ defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlugTest do
|> set_signature("http://mastodon.example.org/users/admin") |> set_signature("http://mastodon.example.org/users/admin")
|> MappedSignatureToIdentityPlug.call(%{}) |> MappedSignatureToIdentityPlug.call(%{})
refute is_nil(conn.assigns.mapped_identity) refute is_nil(conn.assigns.user)
end end
test "it considers a mapped identity to be invalid when it mismatches a payload" do test "it considers a mapped identity to be invalid when it mismatches a payload" do