Class: HTTPClient::BasicAuth

Inherits:
AuthBase show all
Includes:
Mutex_m
Defined in:
lib/httpclient/auth.rb

Overview

Authentication filter for handling BasicAuth negotiation. Used in WWWAuth and ProxyAuth.

Direct Known Subclasses

ProxyBasicAuth

Instance Attribute Summary collapse

Attributes inherited from AuthBase

#scheme

Instance Method Summary collapse

Methods inherited from AuthBase

#reset_challenge

Methods included from Util

#argument_to_hash, hash_find_value, #http?, #https?, #keyword_argument, try_require, uri_dirname, uri_part_of, urify, #warning

Constructor Details

#initializeBasicAuth

Creates new BasicAuth filter.



255
256
257
258
259
260
# File 'lib/httpclient/auth.rb', line 255

def initialize
  super('Basic')
  @cred = nil
  @auth = {}
  @force_auth = false
end

Instance Attribute Details

#force_authObject

Send Authorization Header without receiving 401



252
253
254
# File 'lib/httpclient/auth.rb', line 252

def force_auth
  @force_auth
end

Instance Method Details

#challenge(uri, param_str = nil) ⇒ Object

Challenge handler: remember URL for response.



298
299
300
301
302
303
# File 'lib/httpclient/auth.rb', line 298

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



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/httpclient/auth.rb', line 284

def get(req)
  target_uri = req.header.request_uri
  synchronize {
    return nil if !@force_auth and !@challenge.any? { |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

#set(uri, user, passwd) ⇒ Object

Set authentication credential. uri == nil for generic purpose (allow to use user/password for any URL).



264
265
266
267
268
269
270
271
272
273
# File 'lib/httpclient/auth.rb', line 264

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
  end
end

#set?Boolean

have we marked this as set - ie that it’s valid to use in this context?

Returns:

  • (Boolean)


276
277
278
# File 'lib/httpclient/auth.rb', line 276

def set?
  @cred || @auth.any?
end