Skip to content

Commit

Permalink
Merge pull request #59 from jeffutter/string_expires_in
Browse files Browse the repository at this point in the history
Parse expires_in to future timestamp
  • Loading branch information
scrogson committed Jul 28, 2016
2 parents c034793 + eb09c5f commit 3277a29
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/oauth2/access_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ defmodule OAuth2.AccessToken do
struct(AccessToken, [
access_token: std["access_token"],
refresh_token: std["refresh_token"],
expires_at: (std["expires_in"] || other["expires"]) |> expires_at(),
expires_at: (std["expires_in"] |> expires_at) || (other["expires"] |> expires),
token_type: std["token_type"] |> normalize_token_type(),
other_params: other
])
Expand All @@ -135,11 +135,24 @@ defmodule OAuth2.AccessToken do
"""
def expires_at(nil), do: nil
def expires_at(val) when is_binary(val) do
{int, _} = Integer.parse(val)
int
val
|> Integer.parse
|> elem(0)
|> expires_at
end
def expires_at(int), do: unix_now + int

@doc """
Returns the expires value as an integer
"""
def expires(nil), do: nil
def expires(val) when is_binary(val) do
val
|> Integer.parse
|> elem(0)
end
def expires(int), do: int

defp normalize_token_type(nil), do: "Bearer"
defp normalize_token_type("bearer"), do: "Bearer"
defp normalize_token_type(string), do: string
Expand Down
7 changes: 7 additions & 0 deletions test/oauth2/access_token_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,12 @@ defmodule OAuth2.AccessTokenTest do
test "expires_in" do
assert AccessToken.expires_at(nil) == nil
assert AccessToken.expires_at(3600) == OAuth2.Util.unix_now + 3600
assert AccessToken.expires_at("3600") == OAuth2.Util.unix_now + 3600
end

test "expires" do
assert AccessToken.expires(nil) == nil
assert AccessToken.expires(1469725602) == 1469725602
assert AccessToken.expires("1469725602") == 1469725602
end
end

0 comments on commit 3277a29

Please sign in to comment.