Class: Harbor::Auth::Basic
- Inherits:
-
Object
- Object
- Harbor::Auth::Basic
- Defined in:
- lib/harbor/auth/basic.rb
Overview
Simple mechanism for implementing HTTP basic authentication.
get("/secret-page") do |request, response|
Harbor::Auth::Basic.authenticate(request, response) { |username, password| username == "wieck" }
response.puts "Secret Stuff"
end
Constant Summary collapse
- AUTHORIZATION_KEYS =
['HTTP_AUTHORIZATION', 'X-HTTP_AUTHORIZATION', 'X_HTTP_AUTHORIZATION']
Instance Attribute Summary collapse
-
#realm ⇒ Object
Returns the value of attribute realm.
Class Method Summary collapse
-
.authenticate(request, response) ⇒ Object
Checks the credentials provided in the request against the provided block.
Instance Method Summary collapse
- #credentials ⇒ Object
-
#initialize(request) ⇒ Basic
constructor
A new instance of Basic.
- #provided? ⇒ Boolean
Constructor Details
#initialize(request) ⇒ Basic
Returns a new instance of Basic.
31 32 33 |
# File 'lib/harbor/auth/basic.rb', line 31 def initialize(request) @request = request end |
Instance Attribute Details
#realm ⇒ Object
Returns the value of attribute realm.
16 17 18 |
# File 'lib/harbor/auth/basic.rb', line 16 def realm @realm end |
Class Method Details
.authenticate(request, response) ⇒ Object
Checks the credentials provided in the request against the provided block. If the block returns false, the request aborted.
22 23 24 25 26 27 28 29 |
# File 'lib/harbor/auth/basic.rb', line 22 def self.authenticate(request, response) #:yields: username, password auth = new(request) unless auth.provided? && yield(auth.credentials) response.headers["WWW-Authenticate"] = 'Basic realm="%s"' % auth.realm response. end end |
Instance Method Details
#credentials ⇒ Object
39 40 41 |
# File 'lib/harbor/auth/basic.rb', line 39 def credentials @request.env[].split(' ', 2).last.unpack("m*").first.split(/:/, 2) end |
#provided? ⇒ Boolean
35 36 37 |
# File 'lib/harbor/auth/basic.rb', line 35 def provided? !! end |