Skip to content

Commit 0d492cb

Browse files
author
humancopy
committed
The ids parameter should be prepared differently for the HMAC
1 parent d04c075 commit 0d492cb

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

lib/shopify/oauth.ex

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,27 @@ defmodule Shopify.OAuth do
7979

8080
defp valid_hmac?(secret, params) do
8181
hmac = params["hmac"]
82-
query = params |> Map.delete("hmac") |> URI.encode_query
8382

84-
:crypto.hmac(:sha256, secret, query)
83+
:crypto.hmac(:sha256, secret, query_string(params))
8584
|> Base.encode16(case: :lower)
8685
|> String.equivalent?(hmac)
8786
end
87+
88+
defp query_string(params) do
89+
# Extract the ids and convert them to an array of strings
90+
# ["1", "2", "3"]
91+
ids = params["ids"]
92+
|> Enum.map(fn x -> "\"#{x}\"" end)
93+
|> Enum.join(", ")
94+
95+
# Remove the ids & hmac parameters and make a query string
96+
query = params
97+
|> Map.delete("ids")
98+
|> Map.delete("hmac")
99+
|> URI.encode_query
100+
101+
# Concatenate the ids back to the query - they must not be URI encoded!
102+
# https://community.shopify.com/c/Shopify-APIs-SDKs/HMAC-calculation-vs-ids-arrays/m-p/261154
103+
"ids=[#{ids}]&#{query}"
104+
end
88105
end

0 commit comments

Comments
 (0)