Skip to content

Commit

Permalink
Add support for HMAC-SHA256 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrissler authored Sep 19, 2021
1 parent 3815889 commit 46fb99b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/oauther.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ defmodule OAuther do
consumer_secret: String.t(),
token: nil | String.t(),
token_secret: nil | String.t(),
method: :hmac_sha1 | :rsa_sha1 | :plaintext
method: :hmac_sha1 | :hmac_sha256 | :rsa_sha1 | :plaintext
}
end

Expand Down Expand Up @@ -62,7 +62,14 @@ defmodule OAuther do
def signature(verb, url, params, %Credentials{method: :hmac_sha1} = creds) do
creds
|> compose_key()
|> hmac_sha(base_string(verb, url, params))
|> hmac_sha(base_string(verb, url, params), :sha)
|> Base.encode64()
end

def signature(verb, url, params, %Credentials{method: :hmac_sha256} = creds) do
creds
|> compose_key()
|> hmac_sha(base_string(verb, url, params), :sha256)
|> Base.encode64()
end

Expand All @@ -74,12 +81,12 @@ defmodule OAuther do

# TODO: Remove this once we require at minimum OTP 22.
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :mac, 4) do
defp hmac_sha(key, data) do
:crypto.mac(:hmac, :sha, key, data)
defp hmac_sha(key, data, hash_function) do
:crypto.mac(:hmac, hash_function, key, data)
end
else
defp hmac_sha(key, data) do
:crypto.hmac(:sha, key, data)
defp hmac_sha(key, data, hash_function) do
:crypto.hmac(hash_function, key, data)
end
end

Expand Down Expand Up @@ -179,6 +186,7 @@ defmodule OAuther do

defp signature_method(:plaintext), do: "PLAINTEXT"
defp signature_method(:hmac_sha1), do: "HMAC-SHA1"
defp signature_method(:hmac_sha256), do: "HMAC-SHA256"
defp signature_method(:rsa_sha1), do: "RSA-SHA1"

defp percent_encode({key, value}) do
Expand Down
14 changes: 14 additions & 0 deletions test/oauther_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ defmodule OAutherTest do
assert signature(params, creds, "/photos") == "tR3+Ty81lMeYAr/Fid0kMTYa/WM="
end

test "HMAC-SHA256 signature" do
creds =
OAuther.credentials(
method: :hmac_sha256,
consumer_secret: "kd94hf93k423kf44",
token_secret: "pfkkdhi9sl3r4s00",
consumer_key: "dpf43f3p2l4k3l03",
token: "nnch734d00sl2jdk"
)

params = protocol_params(creds)
assert signature(params, creds, "/photos") == "WVPzl1j6ZsnkIjWr7e3OZ3jkenL57KwaLFhYsroX1hg="
end

test "RSA-SHA1 signature" do
creds =
OAuther.credentials(
Expand Down

0 comments on commit 46fb99b

Please sign in to comment.