Skip to content

Commit 4bb9a7f

Browse files
Fix the documentation for the sign parameter (#5)
* 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
1 parent 5f3589c commit 4bb9a7f

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,17 @@ Then a request path like:
7070

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

73-
**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.**
73+
**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.**
7474

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

8080
Now this request will succeed!
8181

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

8686
## License

test/plug_image_processing/plug_image_processing_test.exs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,19 @@ defmodule PlugImageProcessingTest do
2020
assert {"width", "10"} in query_params
2121
assert {"url", "http://bucket.com/test.jpg"} in query_params
2222
end
23+
24+
test "valid with signature", %{config: config} do
25+
url_signature_key = "12345"
26+
config = Keyword.put(config, :url_signature_key, url_signature_key)
27+
28+
url = PlugImageProcessing.generate_url("http://example.com", config, :resize, %{url: "http://bucket.com/test.jpg", width: 10})
29+
30+
assert url ===
31+
"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")}"
32+
end
33+
end
34+
35+
defp generate_signature_from_url(url_signature_key, url) do
36+
Base.url_encode64(:crypto.mac(:hmac, :sha256, url_signature_key, url))
2337
end
2438
end

0 commit comments

Comments
 (0)