Skip to content

Commit

Permalink
Fix the documentation for the sign parameter (#5)
Browse files Browse the repository at this point in the history
* Fix the documentation for the sign parameter

Even thought the documentation says that you should include the leading "/" in practice you need to exclude it

* add missing test

* inspect
  • Loading branch information
MathieuLegault1 authored Sep 5, 2024
1 parent 5f3589c commit 4bb9a7f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ Then a request path like:

will fail because the `sign` parameter is not present.

**The HMAC-SHA256 hash is created by taking the URL path (including the leading /), the request parameters (alphabetically-sorted and concatenated with & into a string). The hash is then base64url-encoded.**
**The HMAC-SHA256 hash is created by taking the URL path (excluding the leading /), the request parameters (alphabetically-sorted and concatenated with & into a string). The hash is then base64url-encoded.**

```elixir
Base.url_encode64(:crypto.mac(:hmac, :sha256, "1234", "/resize" <> "quality=60&url=https://s3.ca-central-1.amazonaws.com/my_image.jpg&width=300"))
# => "O8Xo9xrP0fM67PIWMIRL2hjkD_c5HzzBtRLfpo43ENY="
Base.url_encode64(:crypto.mac(:hmac, :sha256, "1234", "resize" <> "quality=60&url=https://s3.ca-central-1.amazonaws.com/my_image.jpg&width=300"))
# => "ku5SCH56vrsqEr-_VRDOFJHqa6AXslh3fpAelPAPoeI="
```

Now this request will succeed!

```sh
/imageproxy/resize?url=https://s3.ca-central-1.amazonaws.com/my_image.jpg&width=300&quality=60&sign=O8Xo9xrP0fM67PIWMIRL2hjkD_c5HzzBtRLfpo43ENY=
/imageproxy/resize?url=https://s3.ca-central-1.amazonaws.com/my_image.jpg&width=300&quality=60&sign=ku5SCH56vrsqEr-_VRDOFJHqa6AXslh3fpAelPAPoeI=
```

## License
Expand Down
14 changes: 14 additions & 0 deletions test/plug_image_processing/plug_image_processing_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,19 @@ defmodule PlugImageProcessingTest do
assert {"width", "10"} in query_params
assert {"url", "http://bucket.com/test.jpg"} in query_params
end

test "valid with signature", %{config: config} do
url_signature_key = "12345"
config = Keyword.put(config, :url_signature_key, url_signature_key)

url = PlugImageProcessing.generate_url("http://example.com", config, :resize, %{url: "http://bucket.com/test.jpg", width: 10})

assert url ===
"http://example.com/imageproxy/resize?url=http%3A%2F%2Fbucket.com%2Ftest.jpg&width=10&sign=#{generate_signature_from_url(url_signature_key, "resizeurl=http%3A%2F%2Fbucket.com%2Ftest.jpg&width=10")}"
end
end

defp generate_signature_from_url(url_signature_key, url) do
Base.url_encode64(:crypto.mac(:hmac, :sha256, url_signature_key, url))
end
end

0 comments on commit 4bb9a7f

Please sign in to comment.