Skip to content

Commit 3109923

Browse files
author
Ary Borenszweig
committedMay 20, 2017
Fixed #4437: Uknown key in access token json: id_token
1 parent be945b5 commit 3109923

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed
 

‎spec/std/oauth2/access_token_spec.cr

+11
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ class OAuth2::AccessToken
5656
}))
5757
access_token.expires_in.should be_nil
5858
end
59+
60+
it "builds from json with unknown key (#4437)" do
61+
token = AccessToken.from_json(%({
62+
"access_token" : "foo",
63+
"token_type" : "Bearer",
64+
"refresh_token" : "bar",
65+
"scope" : "baz",
66+
"unknown": [1, 2, 3]
67+
}))
68+
token.extra.not_nil!["unknown"].should eq("[1,2,3]")
69+
end
5970
end
6071

6172
describe Mac do

‎src/oauth2/access_token/access_token.cr

+14-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract class OAuth2::AccessToken
1010
scope = nil
1111
mac_algorithm = nil
1212
mac_key = nil
13+
extra = nil
1314

1415
pull.read_object do |key|
1516
case key
@@ -21,7 +22,8 @@ abstract class OAuth2::AccessToken
2122
when "mac_algorithm" then mac_algorithm = pull.read_string
2223
when "mac_key" then mac_key = pull.read_string
2324
else
24-
raise "Uknown key in access token json: #{key}"
25+
extra ||= {} of String => String
26+
extra[key] = pull.read_raw
2527
end
2628
end
2729

@@ -30,9 +32,9 @@ abstract class OAuth2::AccessToken
3032
if token_type
3133
case token_type.downcase
3234
when "bearer"
33-
Bearer.new(access_token, expires_in, refresh_token, scope)
35+
Bearer.new(access_token, expires_in, refresh_token, scope, extra)
3436
when "mac"
35-
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope)
37+
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope, Time.now.epoch, extra)
3638
else
3739
raise "Uknown token_type in access token json: #{token_type}"
3840
end
@@ -46,7 +48,15 @@ abstract class OAuth2::AccessToken
4648
property refresh_token : String?
4749
property scope : String?
4850

49-
def initialize(@access_token : String, expires_in : Int?, @refresh_token : String? = nil, @scope : String? = nil)
51+
# JSON key-value pairs that are outside of the OAuth2 spec are
52+
# stored in this property in case they are needed. Their value
53+
# is the raw JSON string found in the JSON value (with possible
54+
# changes in the string format, but preserving JSON semantic).
55+
# For example if the value was `[1, 2, 3]` then the value in this hash
56+
# will be the string "[1,2,3]".
57+
property extra : Hash(String, String)?
58+
59+
def initialize(@access_token : String, expires_in : Int?, @refresh_token : String? = nil, @scope : String? = nil, @extra = nil)
5060
@expires_in = expires_in.try &.to_i64
5161
end
5262

‎src/oauth2/access_token/mac.cr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class OAuth2::AccessToken::Mac < OAuth2::AccessToken
1212
property mac_key : String
1313
property issued_at : Int64
1414

15-
def initialize(access_token, expires_in, @mac_algorithm, @mac_key, refresh_token = nil, scope = nil, @issued_at = Time.now.epoch)
16-
super(access_token, expires_in, refresh_token, scope)
15+
def initialize(access_token, expires_in, @mac_algorithm, @mac_key, refresh_token = nil, scope = nil, @issued_at = Time.now.epoch, extra = nil)
16+
super(access_token, expires_in, refresh_token, scope, extra)
1717
end
1818

1919
def token_type

0 commit comments

Comments
 (0)
Please sign in to comment.