From 9b6447cca7d40ab137aa9daa517d85999b7b1763 Mon Sep 17 00:00:00 2001 From: James Rissler Date: Sun, 26 Jan 2020 07:29:01 -0600 Subject: [PATCH 1/4] Add support for HMAC-SHA256 --- lib/oauther.ex | 7 +++++++ test/oauther_test.exs | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/oauther.ex b/lib/oauther.ex index f977387..accbcfd 100644 --- a/lib/oauther.ex +++ b/lib/oauther.ex @@ -65,6 +65,12 @@ defmodule OAuther do |> Base.encode64() end + def signature(verb, url, params, %Credentials{method: :hmac_sha256} = creds) do + :sha256 + |> :crypto.hmac(compose_key(creds), base_string(verb, url, params)) + |> Base.encode64() + end + def signature(verb, url, params, %Credentials{method: :rsa_sha1} = creds) do base_string(verb, url, params) |> :public_key.sign(:sha, decode_private_key(creds.consumer_secret)) @@ -167,6 +173,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 diff --git a/test/oauther_test.exs b/test/oauther_test.exs index 5a54972..ddf21ba 100644 --- a/test/oauther_test.exs +++ b/test/oauther_test.exs @@ -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( From be8ace3ac5657fa6c32454d22abd72f4ccff1d1b Mon Sep 17 00:00:00 2001 From: James Rissler Date: Thu, 3 Jun 2021 10:33:23 -0500 Subject: [PATCH 2/4] Switch to crypto:mac/4 since :crypto.hmac/3 has been replaced. --- lib/oauther.ex | 4 ++-- mix.exs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/oauther.ex b/lib/oauther.ex index accbcfd..8e912be 100644 --- a/lib/oauther.ex +++ b/lib/oauther.ex @@ -61,13 +61,13 @@ defmodule OAuther do def signature(verb, url, params, %Credentials{method: :hmac_sha1} = creds) do :sha - |> :crypto.hmac(compose_key(creds), base_string(verb, url, params)) + |> :crypto.mac(:hmac, compose_key(creds), base_string(verb, url, params)) |> Base.encode64() end def signature(verb, url, params, %Credentials{method: :hmac_sha256} = creds) do :sha256 - |> :crypto.hmac(compose_key(creds), base_string(verb, url, params)) + |> :crypto.mac(:hmac, compose_key(creds), base_string(verb, url, params)) |> Base.encode64() end diff --git a/mix.exs b/mix.exs index b6bed9b..6969765 100644 --- a/mix.exs +++ b/mix.exs @@ -12,7 +12,7 @@ defmodule OAuther.Mixfile do end def application() do - [applications: []] + [applications: [:crypto, :public_key]] end defp description() do From 8a1671396674e09186ce94ef807c5791ed41de75 Mon Sep 17 00:00:00 2001 From: James Rissler Date: Thu, 3 Jun 2021 10:44:51 -0500 Subject: [PATCH 3/4] Tweak order --- lib/oauther.ex | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/oauther.ex b/lib/oauther.ex index 8e912be..15a4450 100644 --- a/lib/oauther.ex +++ b/lib/oauther.ex @@ -60,14 +60,14 @@ defmodule OAuther do end def signature(verb, url, params, %Credentials{method: :hmac_sha1} = creds) do - :sha - |> :crypto.mac(:hmac, compose_key(creds), base_string(verb, url, params)) + :hmac + |> :crypto.mac(:sha, compose_key(creds), base_string(verb, url, params)) |> Base.encode64() end def signature(verb, url, params, %Credentials{method: :hmac_sha256} = creds) do - :sha256 - |> :crypto.mac(:hmac, compose_key(creds), base_string(verb, url, params)) + :hmac + |> :crypto.mac(:sha256, compose_key(creds), base_string(verb, url, params)) |> Base.encode64() end From dd343282848d6985bbbc331d6f9b594ffd1a4d4f Mon Sep 17 00:00:00 2001 From: James Rissler Date: Wed, 15 Sep 2021 08:42:07 -0500 Subject: [PATCH 4/4] Update typespec --- lib/oauther.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/oauther.ex b/lib/oauther.ex index 419acae..56cb181 100644 --- a/lib/oauther.ex +++ b/lib/oauther.ex @@ -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