Add plug to validate signed http requests.

This commit is contained in:
Roger Braun 2017-12-12 10:17:21 +01:00
commit a9c23e1c32
3 changed files with 43 additions and 1 deletions

View file

@ -1,5 +1,7 @@
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
defmodule Pleroma.Web.HTTPSignatures do
alias Pleroma.User
def split_signature(sig) do
default = %{"headers" => "date"}
@ -18,7 +20,18 @@ defmodule Pleroma.Web.HTTPSignatures do
def validate(headers, signature, public_key) do
sigstring = build_signing_string(headers, signature["headers"])
{:ok, sig} = Base.decode64(signature["signature"])
verify = :public_key.verify(sigstring, :sha256, sig, public_key)
:public_key.verify(sigstring, :sha256, sig, public_key)
end
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"],
{:ok, public_key} <- User.get_public_key_for_ap_id(actor_id) do
validate_conn(conn, public_key)
else
_ -> false
end
end
def validate_conn(conn, public_key) do