Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HMAC-SHA256 #15

Merged
merged 7 commits into from
Sep 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
jrissler marked this conversation as resolved.
Show resolved Hide resolved
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