You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.7 KiB
66 lines
1.7 KiB
6 years ago
|
# Pleroma: A lightweight social networking server
|
||
5 years ago
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
||
6 years ago
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
||
7 years ago
|
defmodule Pleroma.Web.Plugs.HTTPSignaturePlug do
|
||
|
import Plug.Conn
|
||
5 years ago
|
import Phoenix.Controller, only: [get_format: 1, text: 2]
|
||
7 years ago
|
require Logger
|
||
7 years ago
|
|
||
|
def init(options) do
|
||
|
options
|
||
|
end
|
||
|
|
||
6 years ago
|
def call(%{assigns: %{valid_signature: true}} = conn, _opts) do
|
||
7 years ago
|
conn
|
||
|
end
|
||
|
|
||
6 years ago
|
def call(conn, _opts) do
|
||
5 years ago
|
if get_format(conn) == "activity+json" do
|
||
|
conn
|
||
|
|> maybe_assign_valid_signature()
|
||
|
|> maybe_require_signature()
|
||
|
else
|
||
|
conn
|
||
|
end
|
||
5 years ago
|
end
|
||
7 years ago
|
|
||
5 years ago
|
defp maybe_assign_valid_signature(conn) do
|
||
|
if has_signature_header?(conn) do
|
||
5 years ago
|
# set (request-target) header to the appropriate value
|
||
|
# we also replace the digest header with the one we computed
|
||
5 years ago
|
request_target = String.downcase("#{conn.method}") <> " #{conn.request_path}"
|
||
7 years ago
|
|
||
5 years ago
|
conn =
|
||
5 years ago
|
conn
|
||
|
|> put_req_header("(request-target)", request_target)
|
||
|
|> case do
|
||
|
%{assigns: %{digest: digest}} = conn -> put_req_header(conn, "digest", digest)
|
||
|
conn -> conn
|
||
5 years ago
|
end
|
||
7 years ago
|
|
||
5 years ago
|
assign(conn, :valid_signature, HTTPSignatures.validate_conn(conn))
|
||
|
else
|
||
|
Logger.debug("No signature header!")
|
||
|
conn
|
||
7 years ago
|
end
|
||
|
end
|
||
5 years ago
|
|
||
|
defp has_signature_header?(conn) do
|
||
|
conn |> get_req_header("signature") |> Enum.at(0, false)
|
||
|
end
|
||
|
|
||
|
defp maybe_require_signature(%{assigns: %{valid_signature: true}} = conn), do: conn
|
||
|
|
||
|
defp maybe_require_signature(conn) do
|
||
|
if Pleroma.Config.get([:activitypub, :authorized_fetch_mode], false) do
|
||
|
conn
|
||
|
|> put_status(:unauthorized)
|
||
5 years ago
|
|> text("Request not signed")
|
||
5 years ago
|
|> halt()
|
||
|
else
|
||
|
conn
|
||
|
end
|
||
|
end
|
||
7 years ago
|
end
|