Skip to content

Commit 9276509

Browse files
committed
Do not allow the user arg to Provider.access_token() to be nil. If you want implicit user lookup, pass :implicit as the user. This prevents accidental authorization in cases where the application developer does not check the result of their User.find() calls.
1 parent 7775e63 commit 9276509

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

README.rdoc

+2-2
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,10 @@ determine whether to serve the request or not.
362362

363363
It is also common to provide a dynamic resource for getting some basic data
364364
about a user by supplying their access token. This can be done by passing
365-
<tt>nil</tt> as the resource owner:
365+
<tt>:implicit</tt> as the resource owner:
366366

367367
get '/me' do
368-
token = Songkick::OAuth2::Provider.access_token(nil, [], env)
368+
token = Songkick::OAuth2::Provider.access_token(:implicit, [], env)
369369
if token.valid?
370370
JSON.unparse('username' => token.owner.username)
371371
else

example/application.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@
105105
# Domain API
106106

107107
get '/me' do
108-
authorization = Songkick::OAuth2::Provider.access_token(nil, [], env)
108+
authorization = Songkick::OAuth2::Provider.access_token(:implicit, [], env)
109109
headers authorization.response_headers
110110
status authorization.response_status
111111

lib/songkick/oauth2/provider/access_token.rb

+7-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ def validate!
5656
return @error = EXPIRED_TOKEN if @authorization.expired?
5757
return @error = INSUFFICIENT_SCOPE unless @authorization.in_scope?(@scopes)
5858

59-
if @resource_owner and @authorization.owner != @resource_owner
60-
@error = INSUFFICIENT_SCOPE
59+
case @resource_owner
60+
when :implicit
61+
# no error
62+
when nil
63+
@error = INVALID_TOKEN
64+
else
65+
@error = INSUFFICIENT_SCOPE if @authorization.owner != @resource_owner
6166
end
6267
end
6368
end

spec/songkick/oauth2/provider/access_token_spec.rb

+13-1
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,23 @@
5252
it_should_behave_like "valid token"
5353
end
5454

55+
describe "with an implicit user" do
56+
let :token do
57+
Songkick::OAuth2::Provider::AccessToken.new(:implicit, ['profile'], 'magic-key')
58+
end
59+
it_should_behave_like "valid token"
60+
end
61+
5562
describe "with no user" do
5663
let :token do
5764
Songkick::OAuth2::Provider::AccessToken.new(nil, ['profile'], 'magic-key')
5865
end
59-
it_should_behave_like "valid token"
66+
it_should_behave_like "invalid token"
67+
68+
it "returns an error response" do
69+
token.response_headers['WWW-Authenticate'].should == "OAuth realm='Demo App', error='invalid_token'"
70+
token.response_status.should == 401
71+
end
6072
end
6173

6274
describe "with less scope than was granted" do

0 commit comments

Comments
 (0)