Class: HTTPClient::BasicAuth

Inherits:
Object
  • Object
show all
Includes:
Util
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

Instance Method Summary collapse

Methods included from Util

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

Constructor Details

#initializeBasicAuth

Creates new BasicAuth filter.



237
238
239
240
241
242
243
# File 'lib/httpclient/auth.rb', line 237

def initialize
  @cred = nil
  @set = false
  @auth = {}
  @challengeable = {}
  @scheme = "Basic"
end

Instance Attribute Details

#schemeObject (readonly)

Authentication scheme.



234
235
236
# File 'lib/httpclient/auth.rb', line 234

def scheme
  @scheme
end

Instance Method Details

#challenge(uri, param_str = nil) ⇒ Object

Challenge handler: remember URL for response.



284
285
286
287
# File 'lib/httpclient/auth.rb', line 284

def challenge(uri, param_str = nil)
  @challengeable[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



272
273
274
275
276
277
278
279
280
281
# File 'lib/httpclient/auth.rb', line 272

def get(req)
  target_uri = req.header.request_uri
  return nil unless @challengeable.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_challengeObject

Resets challenge state. Do not send ‘*Authorization’ header until the server sends ‘*Authentication’ again.



247
248
249
# File 'lib/httpclient/auth.rb', line 247

def reset_challenge
  @challengeable.clear
end

#set(uri, user, passwd) ⇒ Object

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



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

def set(uri, user, passwd)
  @set = true
  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

#set?Boolean

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

Returns:

  • (Boolean)


264
265
266
# File 'lib/httpclient/auth.rb', line 264

def set?
  @set == true
end