diff --git a/lib/plug_image_processing/middlewares/signature_key.ex b/lib/plug_image_processing/middlewares/signature_key.ex index 50fe139..a0af760 100644 --- a/lib/plug_image_processing/middlewares/signature_key.ex +++ b/lib/plug_image_processing/middlewares/signature_key.ex @@ -14,6 +14,8 @@ defmodule PlugImageProcessing.Middlewares.SignatureKey do |> Map.drop(["sign"]) |> URI.encode_query() + IO.inspect(url_path <> url_query) + Base.url_encode64(:crypto.mac(:hmac, :sha256, config.url_signature_key, url_path <> url_query)) end diff --git a/test/plug_image_processing/plug_image_processing_test.exs b/test/plug_image_processing/plug_image_processing_test.exs index 449be8d..8bdcb3f 100644 --- a/test/plug_image_processing/plug_image_processing_test.exs +++ b/test/plug_image_processing/plug_image_processing_test.exs @@ -15,5 +15,19 @@ defmodule PlugImageProcessingTest do assert url === "http://example.com/imageproxy/resize?url=http%3A%2F%2Fbucket.com%2Ftest.jpg&width=10" 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