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 access_token in config instead of public/private keys #56

Merged
merged 2 commits into from
Mar 25, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### Enhancements

* [Braintree.HTTP] Support both access_token and public/private keys usage in configuration

## v0.9.0 2018-06-18

### Enhancements
Expand Down
11 changes: 8 additions & 3 deletions lib/http.ex
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,23 @@ defmodule Braintree.HTTP do
end

@doc false
@spec basic_auth(binary, binary) :: binary
def basic_auth(user, pass) do
@spec basic_auth(binary, binary, binary) :: binary
def basic_auth(nil, user, pass) do
"Basic " <> :base64.encode("#{user}:#{pass}")
end

def basic_auth(access_token, _user, _pass) do
"Bearer " <> access_token
end

@doc false
@spec build_headers(Keyword.t()) :: [tuple]
def build_headers(opts) do
public = Keyword.get_lazy(opts, :public_key, fn -> Braintree.get_env(:public_key) end)
private = Keyword.get_lazy(opts, :private_key, fn -> Braintree.get_env(:private_key) end)
access_token = Keyword.get_lazy(opts, :access_token, fn -> Braintree.get_env(:access_token) end)

[{"Authorization", basic_auth(public, private)} | @headers]
[{"Authorization", basic_auth(access_token, public, private)} | @headers]
end

@doc false
Expand Down
22 changes: 18 additions & 4 deletions test/http_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ defmodule Braintree.HTTPTest do
end) =~ "unprocessable response"
end

test "basic_auth/2 encodes credentials" do
test "basic_auth/3 encodes keys" do
auth =
HTTP.basic_auth(
nil,
"432a04a551424c2b4177d76e252e991efd12ce4e",
"e1d7d9be3817565444c8b9b90ad3ef2f3eb28c0c"
)
Expand All @@ -65,6 +66,17 @@ defmodule Braintree.HTTPTest do
"MTc1NjU0NDRjOGI5YjkwYWQzZWYyZjNlYjI4YzBj"
end

test "basic_auth/3 builds bearer header for access_token" do
auth =
HTTP.basic_auth(
"some_access_token",
"432a04a551424c2b4177d76e252e991efd12ce4e",
"e1d7d9be3817565444c8b9b90ad3ef2f3eb28c0c"
)

assert auth == "Bearer some_access_token"
end

test "build_options/0 considers the application environment" do
with_applicaton_config(:http_options, [timeout: 9000], fn ->
options = HTTP.build_options()
Expand All @@ -85,14 +97,16 @@ defmodule Braintree.HTTPTest do
test "build_headers/1 builds an auth header from application config without options" do
with_applicaton_config(:private_key, "the_private_key", fn ->
with_applicaton_config(:public_key, "the_public_key", fn ->
{_, auth_header} = List.keyfind(HTTP.build_headers([]), "Authorization", 0)
assert auth_header == "Basic dGhlX3B1YmxpY19rZXk6dGhlX3ByaXZhdGVfa2V5"
with_applicaton_config(:access_token, nil, fn ->
{_, auth_header} = List.keyfind(HTTP.build_headers([]), "Authorization", 0)
assert auth_header == "Basic dGhlX3B1YmxpY19rZXk6dGhlX3ByaXZhdGVfa2V5"
end)
end)
end)
end

test "build_headers/1 builds a url from provided options" do
headers = HTTP.build_headers(private_key: "dynamic_key", public_key: "dyn_pub_key")
headers = HTTP.build_headers(access_token: nil, private_key: "dynamic_key", public_key: "dyn_pub_key")
{_, auth_header} = List.keyfind(headers, "Authorization", 0)
assert auth_header == "Basic ZHluX3B1Yl9rZXk6ZHluYW1pY19rZXk="
end
Expand Down
5 changes: 5 additions & 0 deletions test/integration/client_token_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ defmodule Braintree.Integration.ClientToken do
end

test "with a bogus customer" do
original = Braintree.get_env(:access_token)
Braintree.put_env(:access_token, nil)

assert {:error, _} = ClientToken.generate(%{customer_id: "asdfghjkl"})

Braintree.put_env(:access_token, original)
end
end
end