2017-09-18 03:39:57 -06:00
|
|
|
# https://tools.ietf.org/html/draft-cavage-http-signatures-08
|
|
|
|
defmodule Pleroma.Web.HTTPSignatures do
|
|
|
|
def split_signature(sig) do
|
2017-09-18 10:10:21 -06:00
|
|
|
default = %{"headers" => "date"}
|
2017-09-18 03:39:57 -06:00
|
|
|
|
2017-09-18 10:10:21 -06:00
|
|
|
sig = sig
|
2017-09-18 03:39:57 -06:00
|
|
|
|> 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)
|
2017-09-18 10:10:21 -06:00
|
|
|
|
|
|
|
Map.put(sig, "headers", String.split(sig["headers"], ~r/\s/))
|
2017-09-18 03:39:57 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
|
2017-12-11 02:37:40 -07:00
|
|
|
def validate_conn(conn, public_key) do
|
|
|
|
headers = Enum.into(conn.req_headers, %{})
|
|
|
|
signature = split_signature(headers["signature"])
|
|
|
|
validate(headers, signature, public_key)
|
|
|
|
end
|
|
|
|
|
2017-09-18 03:39:57 -06:00
|
|
|
def build_signing_string(headers, used_headers) do
|
|
|
|
used_headers
|
|
|
|
|> Enum.map(fn (header) -> "#{header}: #{headers[header]}" end)
|
|
|
|
|> Enum.join("\n")
|
|
|
|
end
|
|
|
|
end
|