Skip to content

Commit

Permalink
Fixed #4503: assume token_type is Bearer in OAuth2 when not specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jun 3, 2017
1 parent 3e6bd33 commit a3b77d3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 10 additions & 0 deletions spec/std/oauth2/access_token_spec.cr
Expand Up @@ -67,6 +67,16 @@ class OAuth2::AccessToken
}))
token.extra.not_nil!["unknown"].should eq("[1,2,3]")
end

it "builds from json without token_type, assumes Bearer (#4503)" do
token = AccessToken.from_json(%({
"access_token" : "foo",
"refresh_token" : "bar",
"scope" : "baz"
}))
token.should be_a(AccessToken::Bearer)
token.access_token.should eq("foo")
end
end

describe Mac do
Expand Down
18 changes: 8 additions & 10 deletions src/oauth2/access_token/access_token.cr
Expand Up @@ -29,17 +29,15 @@ abstract class OAuth2::AccessToken

access_token = access_token.not_nil!

if token_type
case token_type.downcase
when "bearer"
Bearer.new(access_token, expires_in, refresh_token, scope, extra)
when "mac"
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope, Time.now.epoch, extra)
else
raise "Uknown token_type in access token json: #{token_type}"
end
token_type ||= "bearer"

case token_type.downcase
when "bearer"
Bearer.new(access_token, expires_in, refresh_token, scope, extra)
when "mac"
Mac.new(access_token, expires_in, mac_algorithm.not_nil!, mac_key.not_nil!, refresh_token, scope, Time.now.epoch, extra)
else
raise "Missing token_type in access token json"
raise "Uknown token_type in access token json: #{token_type}"

This comment has been minimized.

Copy link
@Sija

Sija Jun 3, 2017

Contributor

Uknown -> Unknown

end
end

Expand Down

0 comments on commit a3b77d3

Please sign in to comment.