Class: HTTPClient::BasicAuth
- Inherits:
-
Object
- Object
- HTTPClient::BasicAuth
- Includes:
- Util, Mutex_m
- Defined in:
- lib/httpclient/auth.rb
Overview
Authentication filter for handling BasicAuth negotiation. Used in WWWAuth and ProxyAuth.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#scheme ⇒ Object
readonly
Authentication scheme.
Instance Method Summary collapse
-
#challenge(uri, param_str = nil) ⇒ Object
Challenge handler: remember URL for response.
-
#get(req) ⇒ Object
Response handler: returns credential.
-
#initialize ⇒ BasicAuth
constructor
Creates new BasicAuth filter.
-
#reset_challenge ⇒ Object
Resets challenge state.
-
#set(uri, user, passwd) ⇒ Object
Set authentication credential.
-
#set? ⇒ Boolean
have we marked this as set - ie that it’s valid to use in this context?.
Methods included from Util
#argument_to_hash, get_buf, hash_find_value, #http?, #https?, #keyword_argument, uri_dirname, uri_part_of, urify
Constructor Details
#initialize ⇒ BasicAuth
Creates new BasicAuth filter.
239 240 241 242 243 244 245 246 |
# File 'lib/httpclient/auth.rb', line 239 def initialize super @cred = nil @set = false @auth = {} @challenge = {} @scheme = "Basic" end |
Instance Attribute Details
#scheme ⇒ Object (readonly)
Authentication scheme.
236 237 238 |
# File 'lib/httpclient/auth.rb', line 236 def scheme @scheme end |
Instance Method Details
#challenge(uri, param_str = nil) ⇒ Object
Challenge handler: remember URL for response.
295 296 297 298 299 300 |
# File 'lib/httpclient/auth.rb', line 295 def challenge(uri, param_str = nil) synchronize { @challenge[urify(uri)] = true true } end |
#get(req) ⇒ Object
Response handler: returns credential. It sends cred only when a given uri is;
-
child page of challengeable(got *Authenticate before) uri and,
-
child page of defined credential
281 282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/httpclient/auth.rb', line 281 def get(req) target_uri = req.header.request_uri synchronize { return nil unless @challenge.find { |uri, ok| Util.uri_part_of(target_uri, uri) and ok } return @cred if @cred Util.hash_find_value(@auth) { |uri, cred| Util.uri_part_of(target_uri, uri) } } end |
#reset_challenge ⇒ Object
Resets challenge state. Do not send ‘*Authorization’ header until the server sends ‘*Authentication’ again.
250 251 252 253 254 |
# File 'lib/httpclient/auth.rb', line 250 def reset_challenge synchronize { @challenge.clear } end |
#set(uri, user, passwd) ⇒ Object
Set authentication credential. uri == nil for generic purpose (allow to use user/password for any URL).
258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/httpclient/auth.rb', line 258 def set(uri, user, passwd) synchronize do if uri.nil? @cred = ["#{user}:#{passwd}"].pack('m').tr("\n", '') else uri = Util.uri_dirname(uri) @auth[uri] = ["#{user}:#{passwd}"].pack('m').tr("\n", '') end @set = true end end |
#set? ⇒ Boolean
have we marked this as set - ie that it’s valid to use in this context?
271 272 273 274 275 |
# File 'lib/httpclient/auth.rb', line 271 def set? synchronize { @set == true } end |