Skip to content

Commit e69aa7f

Browse files
author
Michael Bleigh
committed
Use proper Rack spec header recognition for the Authentication header.
1 parent 93732a3 commit e69aa7f

File tree

4 files changed

+70
-13
lines changed

4 files changed

+70
-13
lines changed

Gemfile.lock

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
PATH
2+
remote: .
3+
specs:
4+
grape (0.1.4)
5+
multi_json
6+
multi_xml
7+
rack
8+
rack-jsonp
9+
rack-mount
10+
11+
GEM
12+
remote: http://rubygems.org/
13+
specs:
14+
ZenTest (4.5.0)
15+
diff-lcs (1.1.2)
16+
json_pure (1.5.2)
17+
maruku (0.6.0)
18+
syntax (>= 1.0.0)
19+
multi_json (1.0.3)
20+
multi_xml (0.2.2)
21+
rack (1.3.0)
22+
rack-jsonp (1.2.0)
23+
rack
24+
rack-mount (0.8.1)
25+
rack (>= 1.0.0)
26+
rack-test (0.6.0)
27+
rack (>= 1.0)
28+
rake (0.9.2)
29+
rspec (2.6.0)
30+
rspec-core (~> 2.6.0)
31+
rspec-expectations (~> 2.6.0)
32+
rspec-mocks (~> 2.6.0)
33+
rspec-core (2.6.4)
34+
rspec-expectations (2.6.0)
35+
diff-lcs (~> 1.1.2)
36+
rspec-mocks (2.6.0)
37+
syntax (1.0.0)
38+
yard (0.7.1)
39+
40+
PLATFORMS
41+
ruby
42+
43+
DEPENDENCIES
44+
ZenTest
45+
bundler
46+
grape!
47+
json_pure
48+
maruku
49+
rack-test
50+
rake
51+
rspec (~> 2.6.0)
52+
yard

lib/grape/api.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def helpers(&block)
138138
end
139139

140140
# Add an authentication type to the API. Currently
141-
# only `:http_basic` is supported.
141+
# only `:http_basic` and `:oauth2` are supported.
142142
def auth(type = nil, options = {}, &block)
143143
if type
144144
set(:auth, {:type => type.to_sym, :proc => block}.merge(options))

lib/grape/middleware/auth/oauth2.rb

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
module Grape::Middleware::Auth
2+
# OAuth 2.0 authorization for Grape APIs.
23
class OAuth2 < Grape::Middleware::Base
34
def default_options
45
{
56
:token_class => 'AccessToken',
67
:realm => 'OAuth API',
78
:parameter => %w(bearer_token oauth_token),
9+
:accepted_headers => %w(HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION REDIRECT_X_HTTP_AUTHORIZATION),
810
:header => [/Bearer (.*)/i, /OAuth (.*)/i]
911
}
1012
end
@@ -21,14 +23,21 @@ def token_parameter
2123
end
2224

2325
def token_header
24-
return false unless env['Authorization']
26+
return false unless authorization_header
2527
Array(options[:header]).each do |regexp|
26-
if env['Authorization'] =~ regexp
28+
if authorization_header =~ regexp
2729
return $1
2830
end
2931
end
3032
nil
3133
end
34+
35+
def authorization_header
36+
options[:accepted_headers].each do |head|
37+
return env[head] if env[head]
38+
end
39+
nil
40+
end
3241

3342
def token_class
3443
@klass ||= eval(options[:token_class])
@@ -39,7 +48,7 @@ def verify_token(token)
3948
if token.respond_to?(:expired?) && token.expired?
4049
error_out(401, 'expired_token')
4150
else
42-
if token.permission_for?(env)
51+
if !token.respond_to?(:permission_for?) || token.permission_for?(env)
4352
env['api.token'] = token
4453
else
4554
error_out(403, 'insufficient_scope')
@@ -50,12 +59,6 @@ def verify_token(token)
5059
end
5160
end
5261

53-
def parse_authorization_header
54-
if env['Authorization'] =~ /oauth (.*)/i
55-
$1
56-
end
57-
end
58-
5962
def error_out(status, error)
6063
throw :error, {
6164
:message => error,

spec/grape/middleware/auth/oauth2_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,11 @@ def app
6565
it { @err[:headers]['WWW-Authenticate'].should == "OAuth realm='OAuth API', error='expired_token'" }
6666
end
6767

68-
context 'with the token in the header' do
69-
before { get '/awesome', {}, 'Authorization' => 'OAuth g123' }
70-
it { last_response.body.should == 'g123' }
68+
%w(HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION REDIRECT_X_HTTP_AUTHORIZATION).each do |head|
69+
context "with the token in the #{head} header" do
70+
before { get '/awesome', {}, head => 'OAuth g123' }
71+
it { last_response.body.should == 'g123' }
72+
end
7173
end
7274

7375
context 'with the token in the POST body' do

0 commit comments

Comments
 (0)