Skip to content

Commit ba0e072

Browse files
kelostradascrogson
authored andcommitted
normalized expires and expires_in to act the same if present from a request (#78)
1 parent 02e57e9 commit ba0e072

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

lib/oauth2/access_token.ex

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ defmodule OAuth2.AccessToken do
6060
struct(AccessToken, [
6161
access_token: std["access_token"],
6262
refresh_token: std["refresh_token"],
63-
expires_at: (std["expires_in"] |> expires_at) || (other["expires"] |> expires),
63+
expires_at: (std["expires_in"] || other["expires"]) |> expires_at,
6464
token_type: std["token_type"] |> normalize_token_type(),
6565
other_params: other
6666
])
@@ -94,17 +94,6 @@ defmodule OAuth2.AccessToken do
9494
end
9595
def expires_at(int), do: unix_now + int
9696

97-
@doc """
98-
Returns the expires value as an integer
99-
"""
100-
def expires(nil), do: nil
101-
def expires(val) when is_binary(val) do
102-
val
103-
|> Integer.parse
104-
|> elem(0)
105-
end
106-
def expires(int), do: int
107-
10897
defp normalize_token_type(nil), do: "Bearer"
10998
defp normalize_token_type("bearer"), do: "Bearer"
11099
defp normalize_token_type(string), do: string

test/oauth2/access_token_test.exs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,29 @@ defmodule OAuth2.AccessTokenTest do
22
use ExUnit.Case, async: true
33
doctest OAuth2.AccessToken
44

5+
import OAuth2.TestHelpers, only: [unix_now: 0]
6+
57
alias OAuth2.{AccessToken, Response}
68

79
test "new from binary token" do
810
token = AccessToken.new("abc123")
911
assert token.access_token == "abc123"
1012
end
1113

14+
test "new with 'expires_in' param" do
15+
response = Response.new(200, [{"content-type", "application/x-www-form-urlencoded"}], "access_token=abc123&expires_in=123")
16+
token = AccessToken.new(response.body)
17+
assert token.access_token == "abc123"
18+
assert token.expires_at == 123 + unix_now
19+
assert token.token_type == "Bearer"
20+
assert token.other_params == %{}
21+
end
22+
1223
test "new with 'expires' param" do
1324
response = Response.new(200, [{"content-type", "application/x-www-form-urlencoded"}], "access_token=abc123&expires=123")
1425
token = AccessToken.new(response.body)
1526
assert token.access_token == "abc123"
16-
assert token.expires_at == 123
27+
assert token.expires_at == 123 + unix_now
1728
assert token.token_type == "Bearer"
1829
assert token.other_params == %{"expires" => "123"}
1930
end
@@ -22,7 +33,7 @@ defmodule OAuth2.AccessTokenTest do
2233
response = Response.new(200, [{"content-type", "text/plain"}], "access_token=abc123&expires=123")
2334
token = AccessToken.new(response.body)
2435
assert token.access_token == "abc123"
25-
assert token.expires_at == 123
36+
assert token.expires_at == 123 + unix_now
2637
assert token.token_type == "Bearer"
2738
assert token.other_params == %{"expires" => "123"}
2839
end
@@ -39,13 +50,8 @@ defmodule OAuth2.AccessTokenTest do
3950

4051
test "expires_in" do
4152
assert AccessToken.expires_at(nil) == nil
42-
assert AccessToken.expires_at(3600) == OAuth2.Util.unix_now + 3600
43-
assert AccessToken.expires_at("3600") == OAuth2.Util.unix_now + 3600
53+
assert AccessToken.expires_at(3600) == unix_now + 3600
54+
assert AccessToken.expires_at("3600") == unix_now + 3600
4455
end
4556

46-
test "expires" do
47-
assert AccessToken.expires(nil) == nil
48-
assert AccessToken.expires(1469725602) == 1469725602
49-
assert AccessToken.expires("1469725602") == 1469725602
50-
end
5157
end

test/support/test_helpers.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ defmodule OAuth2.TestHelpers do
2626
end
2727
end
2828

29+
def unix_now do
30+
{mega, sec, _micro} = :os.timestamp
31+
(mega * 1_000_000) + sec
32+
end
33+
2934
defp parse_req_body(conn) do
3035
opts = [parsers: [:urlencoded, :json],
3136
pass: ["*/*"],

0 commit comments

Comments
 (0)