Skip to content

Commit 2114dc0

Browse files
author
Ary Borenszweig
committedFeb 16, 2017
OAuth2: expires_in field of AccessToken is optional. Fixes #4041
1 parent dbe0a29 commit 2114dc0

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed
 

Diff for: ‎spec/std/oauth2/access_token_spec.cr

+10
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,16 @@ class OAuth2::AccessToken
4646
token.authenticate request, false
4747
request.headers["Authorization"].should eq("Bearer access token")
4848
end
49+
50+
it "builds from json without expires_in (#4041)" do
51+
access_token = AccessToken.from_json(%({
52+
"access_token" : "foo",
53+
"token_type" : "Bearer",
54+
"refresh_token" : "bar",
55+
"scope" : "baz"
56+
}))
57+
access_token.expires_in.should be_nil
58+
end
4959
end
5060

5161
describe Mac do

Diff for: ‎src/oauth2/access_token/access_token.cr

+3-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ abstract class OAuth2::AccessToken
2626
end
2727

2828
access_token = access_token.not_nil!
29-
expires_in = expires_in.not_nil!
3029

3130
if token_type
3231
case token_type.downcase
@@ -43,12 +42,12 @@ abstract class OAuth2::AccessToken
4342
end
4443

4544
property access_token : String
46-
property expires_in : Int64
45+
property expires_in : Int64?
4746
property refresh_token : String?
4847
property scope : String?
4948

50-
def initialize(@access_token : String, expires_in : Int, @refresh_token : String? = nil, @scope : String? = nil)
51-
@expires_in = expires_in.to_i64
49+
def initialize(@access_token : String, expires_in : Int?, @refresh_token : String? = nil, @scope : String? = nil)
50+
@expires_in = expires_in.try &.to_i64
5251
end
5352

5453
abstract def authenticate(request : HTTP::Request, tls)

Diff for: ‎src/oauth2/session.cr

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
class OAuth2::Session
44
getter oauth2_client : Client
55
getter access_token : AccessToken
6-
getter expires_at : Time
6+
getter expires_at : Time?
77

88
# Creates an `OAuth2::Session`.
99
#
@@ -32,13 +32,25 @@ class OAuth2::Session
3232
end
3333

3434
private def access_token_expired?
35-
Time.utc_now >= @expires_at
35+
if expires_at = @expires_at
36+
Time.utc_now >= expires_at
37+
else
38+
false
39+
end
3640
end
3741

3842
private def refresh_access_token
3943
old_access_token = @access_token
4044
@access_token = @oauth2_client.get_access_token_using_refresh_token(@access_token.refresh_token)
41-
@expires_at = Time.utc_now + @access_token.expires_in.seconds
45+
46+
expires_in = @access_token.expires_in
47+
if expires_in
48+
@expires_at = Time.utc_now + expires_in.seconds
49+
else
50+
# If there's no expires_in in the access token, we assume it never expires
51+
@expires_at = nil
52+
end
53+
4254
@access_token.refresh_token ||= old_access_token.refresh_token
4355
end
4456
end

0 commit comments

Comments
 (0)
Please sign in to comment.