Skip to content

Commit 87b3d15

Browse files
author
Michael Bleigh
committed
Add Realm to HTTP Basic
1 parent d996563 commit 87b3d15

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/grape/api.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'rack/mount'
2+
require 'rack/auth/basic'
23

34
module Grape
45
class API
@@ -48,16 +49,21 @@ def default_format(new_format = nil)
4849
new_format ? set(:default_format, new_format.to_sym) : settings[:default_format]
4950
end
5051

51-
def auth(type = nil, &block)
52+
def auth(type = nil, options = {}, &block)
5253
if type
53-
set(:auth, {:type => type.to_sym, :proc => block})
54+
set(:auth, {:type => type.to_sym, :proc => block}.merge(options))
5455
else
5556
settings[:auth]
5657
end
5758
end
5859

59-
def http_basic(&block)
60-
auth :http_basic, &block
60+
# Add HTTP Basic authorization to the API.
61+
#
62+
# @param [Hash] options A hash of options.
63+
# @option options [String] :realm "API Authorization" The HTTP Basic realm.
64+
def http_basic(options = {}, &block)
65+
options[:realm] ||= "API Authorization"
66+
auth :http_basic, options, &block
6167
end
6268

6369
def route_set
@@ -83,7 +89,7 @@ def route(method, path_info, &block)
8389
def build_endpoint(&block)
8490
b = Rack::Builder.new
8591
b.use Grape::Middleware::Error
86-
b.use Grape::Middleware::Auth::Basic, &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
92+
b.use Rack::Auth::Basic, settings[:auth][:realm], &settings[:auth][:proc] if settings[:auth] && settings[:auth][:type] == :http_basic
8793
b.use Grape::Middleware::Prefixer, :prefix => prefix if prefix
8894
b.use Grape::Middleware::Versioner if version
8995
b.use Grape::Middleware::Formatter, :default_format => default_format || :json

0 commit comments

Comments
 (0)