Skip to content

Commit dca3985

Browse files
author
Michael Bleigh
committed
Adds http_basic to API
1 parent 948746b commit dca3985

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

lib/grape/api.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def default_format(new_format = nil)
4848
new_format ? set(:default_format, new_format.to_sym) : settings[:default_format]
4949
end
5050

51+
def http_basic(&block)
52+
block_given? ? set(:basic_auth, block) : settings[:basic_auth]
53+
end
54+
5155
def route_set
5256
@route_set ||= Rack::Mount::RouteSet.new
5357
end
@@ -68,6 +72,7 @@ def route(method, path_info, &block)
6872
def build_endpoint(&block)
6973
b = Rack::Builder.new
7074
b.use Grape::Middleware::Error
75+
b.use Grape::Middleware::Auth::Basic, &http_basic if http_basic
7176
b.use Grape::Middleware::Prefixer, :prefix => prefix if prefix
7277
b.use Grape::Middleware::Versioner if version
7378
b.use Grape::Middleware::Formatter, :default_format => default_format || :json
@@ -82,9 +87,9 @@ def head(path_info, &block); route('HEAD', path_info, &block) end
8287
def delete(path_info, &block); route('DELETE', path_info, &block) end
8388

8489
def namespace(space = nil, &block)
85-
if space
90+
if space || block_given?
8691
settings_stack << {}
87-
set(:namespace, space.to_s)
92+
set(:namespace, space.to_s) if space
8893
instance_eval &block
8994
settings_stack.pop
9095
else

spec/grape/api_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def app; subject end
7878
subject.namespace.should == '/'
7979
end
8080

81+
it 'should be callable with nil just to push onto the stack' do
82+
subject.namespace do
83+
version 'v2'
84+
compile_path('hello').should == '/v2/hello'
85+
end
86+
subject.compile_path('hello').should == '/hello'
87+
end
88+
8189
%w(group resource resources).each do |als|
8290
it "`.#{als}` should be an alias" do
8391
subject.send(als, :awesome) do
@@ -86,4 +94,33 @@ def app; subject end
8694
end
8795
end
8896
end
97+
98+
describe '.basic' do
99+
it 'should protect any resources on the same scope' do
100+
subject.http_basic do |u,p|
101+
u == 'allow'
102+
end
103+
subject.get(:hello){ "Hello, world."}
104+
get '/hello'
105+
last_response.status.should == 401
106+
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic('allow','whatever')
107+
last_response.status.should == 200
108+
end
109+
110+
it 'should be scopable' do
111+
subject.get(:hello){ "Hello, world."}
112+
subject.namespace :admin do
113+
http_basic do |u,p|
114+
u == 'allow'
115+
end
116+
117+
get(:hello){ "Hello, world." }
118+
end
119+
120+
get '/hello'
121+
last_response.status.should == 200
122+
get '/admin/hello'
123+
last_response.status.should == 401
124+
end
125+
end
89126
end

spec/grape/middleware/auth/basic_spec.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,19 @@ def app
1313
end
1414
end
1515

16-
def encode(username, password)
17-
"Basic " + Base64.encode64("#{username}:#{password}")
18-
end
19-
2016
it 'should throw a 401 if no auth is given' do
2117
@proc = lambda{ false }
2218
get '/whatever'
2319
last_response.status.should == 401
2420
end
2521

2622
it 'should authenticate if given valid creds' do
27-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode('admin','admin')
23+
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic('admin','admin')
2824
last_response.status.should == 200
2925
end
3026

3127
it 'should throw a 401 is wrong auth is given' do
32-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode('admin','wrong')
28+
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic('admin','wrong')
3329
last_response.status.should == 401
3430
end
3531
end

spec/spec_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
require 'rspec'
1111
require 'rack/test'
1212

13+
require 'base64'
14+
def encode_basic(username, password)
15+
"Basic " + Base64.encode64("#{username}:#{password}")
16+
end
17+
1318
RSpec.configure do |config|
1419
config.include Rack::Test::Methods
1520
end

0 commit comments

Comments
 (0)