2019-12-03 11:29:51 +01:00
|
|
|
# Portions of this file are derived from Pleroma:
|
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Plugs.MappedSignatureToIdentity do
|
2019-12-03 11:29:51 +01:00
|
|
|
@moduledoc """
|
|
|
|
Get actor identity from Signature when handing fetches
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Plug.Conn
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2021-11-19 17:40:42 +01:00
|
|
|
alias Mobilizon.Actors
|
2020-01-22 02:14:42 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Utils
|
|
|
|
alias Mobilizon.Federation.HTTPSignatures.Signature
|
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
require Logger
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec init(any()) :: any()
|
2019-12-03 11:29:51 +01:00
|
|
|
def init(options), do: options
|
|
|
|
|
|
|
|
@spec key_id_from_conn(Plug.Conn.t()) :: String.t() | nil
|
|
|
|
defp key_id_from_conn(conn) do
|
|
|
|
case HTTPSignatures.signature_for_conn(conn) do
|
|
|
|
%{"keyId" => key_id} ->
|
|
|
|
Signature.key_id_to_actor_url(key_id)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
@spec actor_from_key_id(Plug.Conn.t()) ::
|
|
|
|
{:ok, Actor.t()} | {:error, :actor_not_found | :no_key_in_conn}
|
2019-12-03 11:29:51 +01:00
|
|
|
defp actor_from_key_id(conn) do
|
2021-11-16 15:43:53 +01:00
|
|
|
Logger.debug("Determining actor from connection signature")
|
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
case key_id_from_conn(conn) do
|
|
|
|
key_actor_id when is_binary(key_actor_id) ->
|
|
|
|
# We don't need to call refreshment here since
|
|
|
|
# the Mobilizon.Federation.HTTPSignatures.Signature plug
|
|
|
|
# should already have refreshed the actor if needed
|
2021-11-19 17:40:42 +01:00
|
|
|
Actors.get_actor_by_url(key_actor_id)
|
2021-11-16 15:47:53 +01:00
|
|
|
|
|
|
|
nil ->
|
|
|
|
{:error, :no_key_in_conn}
|
2019-12-03 11:29:51 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 19:40:37 +02:00
|
|
|
@spec call(Plug.Conn.t(), any()) :: Plug.Conn.t()
|
2019-12-03 11:29:51 +01:00
|
|
|
def call(%{assigns: %{actor: _}} = conn, _opts), do: conn
|
|
|
|
|
|
|
|
# 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
|
2021-11-13 18:45:01 +01:00
|
|
|
with actor_id when actor_id != nil <- Utils.get_url(actor),
|
2021-11-16 15:47:53 +01:00
|
|
|
{:ok, %Actor{} = actor} <- actor_from_key_id(conn),
|
2019-12-03 11:29:51 +01:00
|
|
|
{:actor_match, true} <- {:actor_match, actor.url == actor_id} do
|
2021-11-13 18:45:01 +01:00
|
|
|
Logger.debug("Mapped identity to #{actor.url} from actor param")
|
2019-12-03 11:29:51 +01:00
|
|
|
assign(conn, :actor, actor)
|
|
|
|
else
|
|
|
|
{:actor_match, false} ->
|
|
|
|
Logger.debug("Failed to map identity from signature (payload actor mismatch)")
|
|
|
|
Logger.debug("key_id=#{key_id_from_conn(conn)}, actor=#{actor}")
|
|
|
|
assign(conn, :valid_signature, false)
|
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
{:error, :no_key_in_conn} ->
|
|
|
|
Logger.debug("There was no key in conn")
|
|
|
|
conn
|
|
|
|
|
2021-11-13 18:45:01 +01:00
|
|
|
# TODO: remove me once testsuite uses mapped capabilities instead of what we do now
|
2021-11-16 15:47:53 +01:00
|
|
|
{:error, :actor_not_found} ->
|
2019-12-03 11:29:51 +01:00
|
|
|
Logger.debug("Failed to map identity from signature (lookup failure)")
|
|
|
|
Logger.debug("key_id=#{key_id_from_conn(conn)}, actor=#{actor}")
|
|
|
|
conn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# no payload, probably a signed fetch
|
|
|
|
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
|
|
|
|
case actor_from_key_id(conn) do
|
2021-11-16 15:47:53 +01:00
|
|
|
{:ok, %Actor{} = actor} ->
|
2021-11-13 18:45:01 +01:00
|
|
|
Logger.debug("Mapped identity to #{actor.url} from signed fetch")
|
2019-12-03 11:29:51 +01:00
|
|
|
assign(conn, :actor, actor)
|
|
|
|
|
2021-11-16 15:47:53 +01:00
|
|
|
{:error, :no_key_in_conn} ->
|
|
|
|
Logger.debug("There was no key in conn")
|
|
|
|
assign(conn, :valid_signature, false)
|
|
|
|
|
|
|
|
{:error, :actor_not_found} ->
|
2019-12-03 11:29:51 +01:00
|
|
|
Logger.debug("Failed to map identity from signature (no payload actor mismatch)")
|
|
|
|
Logger.debug("key_id=#{key_id_from_conn(conn)}")
|
|
|
|
assign(conn, :valid_signature, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# no signature at all
|
|
|
|
def call(conn, _opts), do: conn
|
|
|
|
end
|