Skip to content

Commit 271f94c

Browse files
committed
better authorization interface
1 parent 4fa54c1 commit 271f94c

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

lib/exhal/authorizer.ex

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,27 @@ defprotocol ExHal.Authorizer do
99
"""
1010
@type url :: String.t()
1111

12+
@typedoc """
13+
An object that implements the ExHal.Authorizer protocol.
14+
"""
15+
@type authorizer :: any()
16+
17+
@typedoc """
18+
Name of a HTTP header field.
19+
"""
20+
@type header_field_name :: String.t
21+
1222
@doc """
13-
Returns `{:ok, credentials}` if the authorizer
14-
knows the resource and has credentials for it. Otherwise, returns
15-
`:no_auth`.
23+
24+
Called before each request to calculate any header fields needed to
25+
authorize the request. A common return would be
26+
27+
%{"Authorization" => "Bearer <sometoken>"}
28+
29+
If the URL is unrecognized or no header fields are appropriate or
30+
needed this function should return and empty map.
31+
1632
"""
17-
@spec authorization(any, url()) :: {:ok, credentials()} | :no_auth
33+
@spec authorization(authorizer, url()) :: %{optional(header_field_name()) => String.t()}
1834
def authorization(authorizer, url)
1935
end

lib/exhal/client.ex

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ defmodule ExHal.Client do
146146
headers =
147147
client.headers
148148
|> merge_headers(normalize_headers(local_headers))
149-
|> merge_headers(auth_headers(client, url))
149+
|> merge_headers(Authorizer.authorization(client.authorizer, url))
150150

151151
poison_opts = merge_poison_opts(client.opts, local_opts)
152152

@@ -191,11 +191,4 @@ defmodule ExHal.Client do
191191
defp normalize_headers(headers) do
192192
Enum.into(headers, %{}, fn {k, v} -> {to_string(k), v} end)
193193
end
194-
195-
defp auth_headers(client, url) do
196-
case Authorizer.authorization(client.authorizer, url) do
197-
:no_auth -> %{}
198-
{:ok, auth} -> %{"Authorization" => auth}
199-
end
200-
end
201194
end

lib/exhal/null_authorizer.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ defmodule ExHal.NullAuthorizer do
1010
end
1111

1212
defimpl ExHal.Authorizer, for: ExHal.NullAuthorizer do
13-
def authorization(_authorizer, _url), do: :no_auth
13+
def authorization(_authorizer, _url), do: %{}
1414
end

lib/exhal/simple_authorizer.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ defimpl ExHal.Authorizer, for: ExHal.SimpleAuthorizer do
1818
url
1919
|> String.starts_with?(authorizer.url_prefix)
2020
|> if do
21-
{:ok, authorizer.authorization}
21+
%{"Authorization" => authorizer.authorization}
2222
else
23-
:no_auth
23+
%{}
2424
end
2525
end
2626
end

test/exhal/null_authorizer_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule ExHal.NullAuthorizerTest do
77
end
88

99
test ".authorization/2" do
10-
assert :no_auth = Authorizer.authorization(null_authorizer_factory(), "http://example.com")
10+
assert %{} == Authorizer.authorization(null_authorizer_factory(), "http://example.com")
1111
end
1212

1313
defp null_authorizer_factory() do

test/exhal/simple_authorizer_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ defmodule ExHal.SimpleAuthorizerTest do
88

99
describe ".authorization/2" do
1010
test "alien resource" do
11-
assert :no_auth =
12-
Authorizer.authorization(simple_authorizer_factory(), "http://malware.com")
11+
assert %{} ==
12+
Authorizer.authorization(simple_authorizer_factory(), "http://malware.com")
1313
end
1414

1515
test "subtly alien resource" do
16-
assert :no_auth =
17-
Authorizer.authorization(simple_authorizer_factory(), "http://mallory.example.com")
16+
assert %{} ==
17+
Authorizer.authorization(simple_authorizer_factory(), "http://mallory.example.com")
1818
end
1919

2020
test "recognized resource" do
21-
assert {:ok, "Bearer hello-beautiful"} =
21+
assert %{"Authorization" => "Bearer hello-beautiful"} ==
2222
Authorizer.authorization(
2323
simple_authorizer_factory("Bearer hello-beautiful"),
2424
"http://example.com/foo"

0 commit comments

Comments
 (0)