Skip to content

Commit

Permalink
Fix PlugSignature to work with Plug 1.14 (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomekowal authored Mar 7, 2023
1 parent 343918c commit 2b91d4d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
14 changes: 1 addition & 13 deletions lib/plug_signature/conn_test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ defmodule PlugSignature.ConnTest do

conn =
conn
|> maybe_add_host_header()
|> put_req_header("date", date)

request_target =
Expand All @@ -107,6 +106,7 @@ defmodule PlugSignature.ConnTest do
"(created)" -> "(created): #{created}"
"(expires)" -> "(expires): #{expires}"
"date" -> "date: #{date}"
"host" -> "host: #{conn.host}"
header -> "#{header}: #{get_req_header(conn, header) |> Enum.join(",")}"
end)
end)
Expand Down Expand Up @@ -222,18 +222,6 @@ defmodule PlugSignature.ConnTest do
to_string(now + validity)
end

defp maybe_add_host_header(%Plug.Conn{host: host} = conn) when is_binary(host) do
case get_req_header(conn, "host") do
[] ->
put_req_header(conn, "host", host)

_ ->
conn
end
end

defp maybe_add_host_header(conn), do: conn

defp raise_on_missing_phoenix_conntest! do
Code.ensure_loaded?(Phoenix.ConnTest) ||
raise "endpoint testing requirs Phoenix.ConnTest"
Expand Down
11 changes: 7 additions & 4 deletions lib/plug_signature/signature_string.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ defmodule PlugSignature.SignatureString do

def build(conn, signature_opts, algorithm, header_list) do
signature_string =
Enum.map_join(header_list, "\n", &header_part(conn, signature_opts, algorithm, &1))
header_list
|> Enum.map(&String.downcase/1)
|> Enum.map_join("\n", &header_part(conn, signature_opts, algorithm, &1))

{:ok, signature_string}
rescue
Expand All @@ -30,10 +32,11 @@ defmodule PlugSignature.SignatureString do
"(expires): #{Keyword.fetch!(signature_opts, :expires)}"
end

defp header_part(conn, _signature_opts, _algorithm, header)
when header not in ["(created)", "(expires)"] do
header_name = String.downcase(header)
defp header_part(conn, _signature_opts, _algorithm, "host") do
"host: #{conn.host}"
end

defp header_part(conn, _signature_opts, _algorithm, header_name) do
values =
conn.req_headers
|> Enum.filter(&match?({^header_name, _}, &1))
Expand Down
7 changes: 7 additions & 0 deletions test/plug_signature/signature_string_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,14 @@ defmodule PlugSignature.SignatureStringTest do
[path, query] -> {path, query}
end

host =
case List.keyfind(headers, "host", 0) do
{"host", host} -> host
nil -> nil
end

%Plug.Conn{
host: host || "www.example.com",
method: method,
request_path: request_path,
query_string: query_string,
Expand Down
2 changes: 1 addition & 1 deletion test/plug_signature_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ defmodule PlugSignatureTest do
conn =
conn()
|> with_signature(key, key_id, config)
|> put_req_header("host", "example.org")
|> Map.put(:host, "example.org")
|> PlugSignature.call(PlugSignature.init(config))

assert conn.halted
Expand Down

0 comments on commit 2b91d4d

Please sign in to comment.