Skip to content

Commit ebb03fa

Browse files
candlerbrtomayko
authored andcommitted
Allow realm to be passed to auth handler's initialize method
Signed-off-by: Ryan Tomayko <[email protected]>
1 parent db1c378 commit ebb03fa

File tree

5 files changed

+14
-5
lines changed

5 files changed

+14
-5
lines changed

example/protectedlobster.ru

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
require 'rack/lobster'
22

33
use Rack::ShowExceptions
4-
use Rack::Auth::Basic do |username, password|
4+
use Rack::Auth::Basic, "Lobster 2.0" do |username, password|
55
'secret' == password
66
end
77

lib/rack/auth/abstract/handler.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class AbstractHandler
88

99
attr_accessor :realm
1010

11-
def initialize(app, &authenticator)
12-
@app, @authenticator = app, authenticator
11+
def initialize(app, realm=nil, &authenticator)
12+
@app, @realm, @authenticator = app, realm, authenticator
1313
end
1414

1515

lib/rack/auth/digest/md5.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class MD5 < AbstractHandler
2121

2222
attr_writer :passwords_hashed
2323

24-
def initialize(app)
24+
def initialize(*args)
2525
super
2626
@passwords_hashed = nil
2727
end

test/spec_rack_auth_basic.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def assert_basic_auth_challenge(response)
3535
response.should.be.a.client_error
3636
response.status.should.equal 401
3737
response.should.include 'WWW-Authenticate'
38-
response.headers['WWW-Authenticate'].should =~ /Basic realm="/
38+
response.headers['WWW-Authenticate'].should =~ /Basic realm="#{Regexp.escape(realm)}"/
3939
response.body.should.be.empty
4040
end
4141

@@ -66,4 +66,8 @@ def assert_basic_auth_challenge(response)
6666
end
6767
end
6868

69+
specify 'realm as optional constructor arg' do
70+
app = Rack::Auth::Basic.new(unprotected_app, realm) { true }
71+
assert_equal realm, app.realm
72+
end
6973
end

test/spec_rack_auth_digest.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,9 @@ def assert_bad_request(response)
218218
response.body.to_s.should.equal 'Hi Alice'
219219
end
220220
end
221+
222+
specify 'realm as optional constructor arg' do
223+
app = Rack::Auth::Digest::MD5.new(unprotected_app, realm) { true }
224+
assert_equal realm, app.realm
225+
end
221226
end

0 commit comments

Comments
 (0)