Formatting

This commit is contained in:
Natsu Kagami 2024-06-11 20:23:15 +02:00 committed by Floatingghost
parent 87c8622d62
commit fd7c4b6d3a
2 changed files with 41 additions and 11 deletions

View file

@ -27,7 +27,14 @@ defmodule HTTPSignatures do
end
def validate(headers, signature, public_key) do
sigstring = build_signing_string(headers, signature["headers"], signature["created"], signature["expires"])
sigstring =
build_signing_string(
headers,
signature["headers"],
signature["created"],
signature["expires"]
)
Logger.debug("Signature: #{signature["signature"]}")
Logger.debug("Sigstring: #{sigstring}")
{:ok, sig} = Base.decode64(signature["signature"])
@ -81,7 +88,13 @@ defmodule HTTPSignatures do
end
def sign(private_key, key_id, headers) do
sigstring = build_signing_string(headers, Enum.sort(Map.keys(headers)), headers["(created)"], headers["(expires)"])
sigstring =
build_signing_string(
headers,
Enum.sort(Map.keys(headers)),
headers["(created)"],
headers["(expires)"]
)
signature =
:public_key.sign(sigstring, :sha256, private_key)
@ -93,7 +106,7 @@ defmodule HTTPSignatures do
headers: Map.keys(headers) |> Enum.sort() |> Enum.join(" "),
signature: signature,
created: headers["(created)"],
expires: headers["(expires)"],
expires: headers["(expires)"]
]
|> Enum.filter(fn {_, v} -> v != nil end)
|> Enum.map_join(",", fn {k, v} -> "#{k}=\"#{v}\"" end)

View file

@ -16,8 +16,10 @@ defmodule HttpSignaturesTest do
"(request-target)" => "post /foo?param=value&pet=dog",
"host" => "example.com",
"date" => "Thu, 05 Jan 2014 21:31:40 GMT",
"(created)" => "1388957500", # the same date, in unix timestamp
"(expires)" => "1388967500", # 10000 seconds later
# the same date, in unix timestamp
"(created)" => "1388957500",
# 10000 seconds later
"(expires)" => "1388967500",
"content-type" => "application/json",
"digest" => "SHA-256=X48E9qOokqqrvdts8nOJRJN3OWDUoyWxBf7kbu9DBPE=",
"content-length" => "18"
@ -79,9 +81,24 @@ defmodule HttpSignaturesTest do
test "it contructs a signing string" do
expected = "date: Thu, 05 Jan 2014 21:31:40 GMT\ncontent-length: 18"
assert expected == HTTPSignatures.build_signing_string(@headers, ["date", "content-length"], @headers["(created)"], @headers["(expires)"])
assert expected ==
HTTPSignatures.build_signing_string(
@headers,
["date", "content-length"],
@headers["(created)"],
@headers["(expires)"]
)
expected = "(created): 1388957500\n(expires): 1388967500\ncontent-length: 18"
assert expected == HTTPSignatures.build_signing_string(@headers, ["(created)", "(expires)", "content-length"], @headers["(created)"], @headers["(expires)"])
assert expected ==
HTTPSignatures.build_signing_string(
@headers,
["(created)", "(expires)", "content-length"],
@headers["(created)"],
@headers["(expires)"]
)
end
test "it parses the http signature for a conn" do