From fd7c4b6d3af7a179cf77b112c89ae24d8da23578 Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Tue, 11 Jun 2024 20:23:15 +0200 Subject: [PATCH] Formatting --- lib/http_signatures/http_signatures.ex | 27 +++++++++++++++++++------- test/http_signatures_test.exs | 25 ++++++++++++++++++++---- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/http_signatures/http_signatures.ex b/lib/http_signatures/http_signatures.ex index 6063ff5..bea941b 100644 --- a/lib/http_signatures/http_signatures.ex +++ b/lib/http_signatures/http_signatures.ex @@ -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"]) @@ -74,14 +81,20 @@ defmodule HTTPSignatures do def build_signing_string(headers, used_headers, created, expires) do used_headers |> Enum.map_join("\n", fn - "(created)" -> "(created): #{created}" - "(expires)" -> "(expires): #{expires}" - header -> "#{header}: #{headers[header]}" - end) + "(created)" -> "(created): #{created}" + "(expires)" -> "(expires): #{expires}" + header -> "#{header}: #{headers[header]}" + end) 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) diff --git a/test/http_signatures_test.exs b/test/http_signatures_test.exs index 0153e0e..a4fbee3 100644 --- a/test/http_signatures_test.exs +++ b/test/http_signatures_test.exs @@ -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