2019-07-18 09:38:45 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-07-18 09:38:45 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Plugs.MappedSignatureToIdentityPlug do
|
2020-12-06 03:59:10 -07:00
|
|
|
alias Pleroma.Helpers.AuthHelper
|
2019-07-18 09:38:45 -06:00
|
|
|
alias Pleroma.Signature
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
|
|
|
|
|
|
|
import Plug.Conn
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
def init(options), do: options
|
|
|
|
|
2020-12-06 03:59:10 -07:00
|
|
|
def call(%{assigns: %{user: %User{}}} = conn, _opts), do: conn
|
2019-07-18 09:38:45 -06:00
|
|
|
|
|
|
|
# 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
|
|
|
|
with actor_id <- Utils.get_ap_id(actor),
|
2019-07-18 10:01:21 -06:00
|
|
|
{:user, %User{} = user} <- {:user, user_from_key_id(conn)},
|
2022-10-14 04:48:32 -06:00
|
|
|
{:federate, true} <- {:federate, should_federate?(user)},
|
2019-07-18 10:01:21 -06:00
|
|
|
{:user_match, true} <- {:user_match, user.ap_id == actor_id} do
|
2020-12-06 03:59:10 -07:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> AuthHelper.skip_oauth()
|
2019-07-18 09:38:45 -06:00
|
|
|
else
|
2019-07-18 10:01:21 -06:00
|
|
|
{:user_match, false} ->
|
|
|
|
Logger.debug("Failed to map identity from signature (payload actor mismatch)")
|
2020-04-13 23:43:47 -06:00
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{inspect(actor)}")
|
2022-10-14 04:48:32 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
2019-07-18 10:01:21 -06:00
|
|
|
|
|
|
|
# remove me once testsuite uses mapped capabilities instead of what we do now
|
|
|
|
{:user, nil} ->
|
|
|
|
Logger.debug("Failed to map identity from signature (lookup failure)")
|
2020-04-13 23:43:47 -06:00
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{actor}")
|
2022-10-14 04:48:32 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
|
|
|
|
|
|
|
{:federate, false} ->
|
|
|
|
Logger.debug("Identity from signature is instance blocked")
|
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}, actor=#{actor}")
|
|
|
|
|
2019-07-18 09:38:45 -06:00
|
|
|
conn
|
2022-10-14 04:48:32 -06:00
|
|
|
|> assign(:valid_signature, false)
|
2019-07-18 09:38:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# no payload, probably a signed fetch
|
|
|
|
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
|
2022-10-14 04:48:32 -06:00
|
|
|
with %User{} = user <- user_from_key_id(conn),
|
|
|
|
{:federate, true} <- {:federate, should_federate?(user)} do
|
2020-12-06 03:59:10 -07:00
|
|
|
conn
|
|
|
|
|> assign(:user, user)
|
|
|
|
|> AuthHelper.skip_oauth()
|
2019-07-18 09:38:45 -06:00
|
|
|
else
|
2022-10-14 04:48:32 -06:00
|
|
|
{:federate, false} ->
|
|
|
|
Logger.debug("Identity from signature is instance blocked")
|
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
|
|
|
|
|
|
|
nil ->
|
|
|
|
Logger.debug("Failed to map identity from signature (lookup failure)")
|
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
|
|
|
|
|
|
|
|
only_permit_user_routes(conn)
|
|
|
|
|
2019-07-18 09:38:45 -06:00
|
|
|
_ ->
|
|
|
|
Logger.debug("Failed to map identity from signature (no payload actor mismatch)")
|
2020-04-13 23:43:47 -06:00
|
|
|
Logger.debug("key_id=#{inspect(key_id_from_conn(conn))}")
|
2022-10-14 04:48:32 -06:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
2019-07-18 09:38:45 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# no signature at all
|
|
|
|
def call(conn, _opts), do: conn
|
2020-12-06 03:59:10 -07:00
|
|
|
|
2022-10-14 04:48:32 -06:00
|
|
|
defp only_permit_user_routes(%{path_info: ["users", _]} = conn) do
|
|
|
|
conn
|
|
|
|
|> assign(:limited_ap, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp only_permit_user_routes(conn) do
|
|
|
|
conn
|
|
|
|
|> assign(:valid_signature, false)
|
|
|
|
end
|
|
|
|
|
2020-12-06 03:59:10 -07:00
|
|
|
defp key_id_from_conn(conn) do
|
|
|
|
with %{"keyId" => key_id} <- HTTPSignatures.signature_for_conn(conn),
|
|
|
|
{:ok, ap_id} <- Signature.key_id_to_actor_id(key_id) do
|
|
|
|
ap_id
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp user_from_key_id(conn) do
|
|
|
|
with key_actor_id when is_binary(key_actor_id) <- key_id_from_conn(conn),
|
|
|
|
{:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(key_actor_id) do
|
|
|
|
user
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
2022-10-14 04:48:32 -06:00
|
|
|
|
|
|
|
defp should_federate?(%User{ap_id: ap_id}), do: should_federate?(ap_id)
|
|
|
|
|
|
|
|
defp should_federate?(ap_id) do
|
|
|
|
if Pleroma.Config.get([:activitypub, :authorized_fetch_mode], false) do
|
|
|
|
Pleroma.Web.ActivityPub.Publisher.should_federate?(ap_id)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-07-18 09:38:45 -06:00
|
|
|
end
|