X-Git-Url: http://git.squeep.com/?a=blobdiff_plain;f=lib%2Fpleroma%2Fweb%2Fhttp_signatures%2Fhttp_signatures.ex;h=5e42a871b682dc6945e863830d0b9dfc5e9e2721;hb=745072b2cc0ce3be6e1896bcacffb5e48782f6da;hp=cdc5e1f3f505c78637d1967da6c86613cbc3e2cc;hpb=b331cb449a46bbead19fea4fba59762c1a2e3a10;p=akkoma diff --git a/lib/pleroma/web/http_signatures/http_signatures.ex b/lib/pleroma/web/http_signatures/http_signatures.ex index cdc5e1f3f..5e42a871b 100644 --- a/lib/pleroma/web/http_signatures/http_signatures.ex +++ b/lib/pleroma/web/http_signatures/http_signatures.ex @@ -1,25 +1,30 @@ # https://tools.ietf.org/html/draft-cavage-http-signatures-08 defmodule Pleroma.Web.HTTPSignatures do alias Pleroma.User + alias Pleroma.Web.ActivityPub.Utils alias Pleroma.Web.ActivityPub.ActivityPub + require Logger def split_signature(sig) do default = %{"headers" => "date"} - sig = sig - |> String.trim() - |> String.split(",") - |> Enum.reduce(default, fn(part, acc) -> - [key | rest] = String.split(part, "=") - value = Enum.join(rest, "=") - Map.put(acc, key, String.trim(value, "\"")) - end) + sig = + sig + |> String.trim() + |> String.split(",") + |> Enum.reduce(default, fn part, acc -> + [key | rest] = String.split(part, "=") + value = Enum.join(rest, "=") + Map.put(acc, key, String.trim(value, "\"")) + end) Map.put(sig, "headers", String.split(sig["headers"], ~r/\s/)) end def validate(headers, signature, public_key) do sigstring = build_signing_string(headers, signature["headers"]) + Logger.debug("Signature: #{signature["signature"]}") + Logger.debug("Sigstring: #{sigstring}") {:ok, sig} = Base.decode64(signature["signature"]) :public_key.verify(sigstring, :sha256, sig, public_key) end @@ -27,20 +32,23 @@ defmodule Pleroma.Web.HTTPSignatures do def validate_conn(conn) do # TODO: How to get the right key and see if it is actually valid for that request. # For now, fetch the key for the actor. - with actor_id <- conn.params["actor"], + with actor_id <- Utils.get_ap_id(conn.params["actor"]), {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do if validate_conn(conn, public_key) do true else + Logger.debug("Could not validate, re-fetching user and trying one more time") # Fetch user anew and try one more time - with actor_id <- conn.params["actor"], + with actor_id <- Utils.get_ap_id(conn.params["actor"]), {:ok, _user} <- ActivityPub.make_user_from_ap_id(actor_id), {:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do validate_conn(conn, public_key) end end else - _ -> false + _e -> + Logger.debug("Could not public key!") + false end end @@ -52,7 +60,7 @@ defmodule Pleroma.Web.HTTPSignatures do def build_signing_string(headers, used_headers) do used_headers - |> Enum.map(fn (header) -> "#{header}: #{headers[header]}" end) + |> Enum.map(fn header -> "#{header}: #{headers[header]}" end) |> Enum.join("\n") end @@ -60,8 +68,10 @@ defmodule Pleroma.Web.HTTPSignatures do with {:ok, %{info: %{"keys" => keys}}} <- Pleroma.Web.WebFinger.ensure_keys_present(user), {:ok, private_key, _} = Pleroma.Web.Salmon.keys_from_pem(keys) do sigstring = build_signing_string(headers, Map.keys(headers)) - signature = :public_key.sign(sigstring, :sha256, private_key) - |> Base.encode64() + + signature = + :public_key.sign(sigstring, :sha256, private_key) + |> Base.encode64() [ keyId: user.ap_id <> "#main-key", @@ -69,7 +79,7 @@ defmodule Pleroma.Web.HTTPSignatures do headers: Map.keys(headers) |> Enum.join(" "), signature: signature ] - |> Enum.map(fn({k, v}) -> "#{k}=\"#{v}\"" end) + |> Enum.map(fn {k, v} -> "#{k}=\"#{v}\"" end) |> Enum.join(",") end end