Class: RoadForest::HTTP::Keychain
- Inherits:
-
Object
- Object
- RoadForest::HTTP::Keychain
- Defined in:
- lib/roadforest/http/keychain.rb
Overview
Manages user credentials for HTTP Basic auth
Defined Under Namespace
Classes: Credentials
Constant Summary collapse
- BASIC_SCHEME =
/basic\s+realm=(?<q>['"])(?<realm>(?:(?!['"]).)*)\k<q>/i
Instance Method Summary collapse
- #add(url, user, secret, realm = nil) ⇒ Object
- #add_credentials(url, creds, realm) ⇒ Object
- #challenge_response(url, challenge) ⇒ Object
-
#initialize ⇒ Keychain
constructor
A new instance of Keychain.
- #missing_credentials(url, realm) ⇒ Object
- #preemptive_response(url) ⇒ Object
- #response(url, realm) ⇒ Object
Constructor Details
#initialize ⇒ Keychain
Returns a new instance of Keychain.
14 15 16 17 |
# File 'lib/roadforest/http/keychain.rb', line 14 def initialize @realm_for_url = {} @with_realm = {} end |
Instance Method Details
#add(url, user, secret, realm = nil) ⇒ Object
19 20 21 22 |
# File 'lib/roadforest/http/keychain.rb', line 19 def add(url, user, secret, realm=nil) creds = Credentials.new(user, secret) add_credentials(url, creds, realm || :default) end |
#add_credentials(url, creds, realm) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/roadforest/http/keychain.rb', line 24 def add_credentials(url, creds, realm) if url.to_s[-1] != "/" url << "/" end @realm_for_url[url.to_s] = realm url = Addressable::URI.parse(url) url.path = "/" @with_realm[[url.to_s,realm]] = creds end |
#challenge_response(url, challenge) ⇒ Object
37 38 39 40 41 42 43 44 |
# File 'lib/roadforest/http/keychain.rb', line 37 def challenge_response(url, challenge) if (match = BASIC_SCHEME.match(challenge)).nil? return nil end realm = match[:realm] response(url, realm) end |
#missing_credentials(url, realm) ⇒ Object
62 63 64 |
# File 'lib/roadforest/http/keychain.rb', line 62 def missing_credentials(url, realm) nil end |
#preemptive_response(url) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/roadforest/http/keychain.rb', line 66 def preemptive_response(url) url = Addressable::URI.parse(url) while (realm = @realm_for_url[url.to_s]).nil? new_url = url.join("..") break if new_url == url url = new_url end return response(url, realm) end |
#response(url, realm) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/roadforest/http/keychain.rb', line 46 def response(url, realm) lookup_url = Addressable::URI.parse(url) lookup_url.path = "/" creds = @with_realm[[lookup_url.to_s,realm]] if creds.nil? and not realm.nil? creds = missing_credentials(url, realm) unless creds.nil? add_credentials(url, creds, realm) end end return nil if creds.nil? return creds.header_value end |