Skip to content

Commit

Permalink
Resolves beam-community#462. Add authorization header api key to deau…
Browse files Browse the repository at this point in the history
…thorization oauth request.
  • Loading branch information
zgohr committed Feb 27, 2019
1 parent c2ef776 commit 9d60113
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/stripe/api.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Stripe.API do
alias Stripe.{Config, Error}

@callback oauth_request(method, String.t(), map) :: {:ok, map}
@callback http_module() :: module

@type method :: :get | :post | :put | :delete | :patch
@type headers :: %{String.t() => String.t()} | %{}
Expand Down Expand Up @@ -94,6 +95,10 @@ defmodule Stripe.API do
|> Map.put("Content-Type", "multipart/form-data")
end

@spec maybe_add_auth_header_oauth(headers, String.t(), String.t() | nil) :: headers
defp maybe_add_auth_header_oauth(headers, "deauthorize", api_key), do: add_auth_header(headers, api_key)
defp maybe_add_auth_header_oauth(headers, _endpoint, _api_key), do: headers

@spec add_auth_header(headers, String.t() | nil) :: headers
defp add_auth_header(existing_headers, api_key) do
api_key = fetch_api_key(api_key)
Expand Down Expand Up @@ -208,15 +213,16 @@ defmodule Stripe.API do
@doc """
A low level utility function to make an OAuth request to the Stripe API
"""
@spec oauth_request(method, String.t(), map) :: {:ok, map} | {:error, Stripe.Error.t()}
def oauth_request(method, endpoint, body) do
@spec oauth_request(method, String.t(), map, String.t()) :: {:ok, map} | {:error, Stripe.Error.t()}
def oauth_request(method, endpoint, body, api_key \\ nil) do
base_url = "https://connect.stripe.com/oauth/"
req_url = base_url <> endpoint
req_body = Stripe.URI.encode_query(body)

req_headers =
%{}
|> add_default_headers()
|> maybe_add_auth_header_oauth(endpoint, api_key)
|> Map.to_list()

req_opts =
Expand All @@ -228,6 +234,7 @@ defmodule Stripe.API do
|> handle_response()
end


@spec perform_request(String.t(), method, body, headers, list) ::
{:ok, map} | {:error, Stripe.Error.t()}
defp perform_request(req_url, method, body, headers, opts) do
Expand Down
30 changes: 30 additions & 0 deletions test/stripe/api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,34 @@ defmodule Stripe.APITest do

assert Stripe.APIMock.oauth_request(:post, "www", %{body: "body"}) == :post
end

test "oauth_request sets authorization header for deauthorize request" do
verify_on_exit!()

defmodule TestHackneyBehavior do
@typep http_success :: {:ok, integer, [{String.t(), String.t()}], String.t()}
@typep http_failure :: {:error, term}

@callback request(method :: atom, req_url :: String.t(), headers :: list, body :: map, opts :: list) :: http_success | http_failure
end

Mox.defmock(HackneyMock, for: TestHackneyBehavior)
Application.put_env(:stripity_stripe, :http_module, HackneyMock)

HackneyMock
|> expect(:request, 3, fn _, _, headers, _, _ ->
kv_headers = headers
|> Enum.reduce(%{}, fn {k, v}, acc -> Map.put(acc, k, v) end)
{:ok, 200, headers, Poison.encode!(kv_headers)}
end)

{:ok, body} = Stripe.API.oauth_request(:post, "deauthorize", %{})
assert body["Authorization"] == "Bearer sk_test_123"

{:ok, body} = Stripe.API.oauth_request(:post, "deauthorize", %{}, "1234")
assert body["Authorization"] == "Bearer 1234"

{:ok, body} = Stripe.API.oauth_request(:post, "token", %{})
assert Map.keys(body) |> Enum.member?("Authorization") == false
end
end

0 comments on commit 9d60113

Please sign in to comment.