Skip to content

Commit 1ed38f8

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

1 file changed

Lines changed: 30 additions & 2 deletions

File tree

lib/shopify/oauth.ex

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,38 @@ 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(query, nil) do
89+
query
90+
end
91+
92+
defp query_string(query, ids) do
93+
# Convert the ids to a string representing and array of numeric strings:
94+
# ["1", "2", "3"]
95+
ids = ids
96+
|> Enum.map(fn x -> "\"#{x}\"" end)
97+
|> Enum.join(", ")
98+
99+
# Concatenate the ids back to the query - they must not be URI encoded!
100+
# https://community.shopify.com/c/Shopify-APIs-SDKs/HMAC-calculation-vs-ids-arrays/m-p/261154
101+
"ids=[#{ids}]&#{query}"
102+
end
103+
104+
defp query_string(params) when is_map(params) do
105+
# Extract the ids
106+
ids = params["ids"]
107+
108+
# Remove the ids & hmac parameters and make a query string
109+
query = params
110+
|> Map.delete("ids")
111+
|> Map.delete("hmac")
112+
|> URI.encode_query
113+
114+
query_string(query, ids)
115+
end
88116
end

0 commit comments

Comments
 (0)