class Net::HTTPHeader::DigestAuthenticator

Public Class Methods

new(username, password, method, path, response_header) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 13
def initialize(username, password, method, path, response_header)
  @username = username
  @password = password
  @method   = method
  @path     = path
  @response = parse(response_header)
end

Public Instance Methods

authorization_header() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 21
def authorization_header
  @cnonce = md5(random)
  header = [
    %Q(Digest username="#{@username}"),
    %Q(realm="#{@response['realm']}"),
    %Q(nonce="#{@response['nonce']}"),
    %Q(uri="#{@path}"),
    %Q(response="#{request_digest}"),
  ]

  if qop_present?
    fields = [
      %Q(cnonce="#{@cnonce}"),
      %Q(qop="#{@response['qop']}"),
      %Q(nc=00000001)
    ]
    fields.each { |field| header << field }
  end

  header << %Q(opaque="#{@response['opaque']}") if opaque_present?
  header
end

Private Instance Methods

a1() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 75
def a1
  [@username, @response['realm'], @password].join(":")
end
a2() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 79
def a2
  [@method, @path].join(":")
end
md5(str) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 71
def md5(str)
  Digest::MD5.hexdigest(str)
end
opaque_present?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 53
def opaque_present?
  @response.has_key?('opaque') and not @response['opaque'].empty?
end
parse(response_header) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 46
def parse(response_header)
  response_header['www-authenticate'] =~ /Digest (.*)/
  params = {}
  $1.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
  params
end
qop_present?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 57
def qop_present?
  @response.has_key?('qop') and not @response['qop'].empty?
end
random() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 61
def random
  "%x" % (Time.now.to_i + rand(65535))
end
request_digest() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 65
def request_digest
  a = [md5(a1), @response['nonce'], md5(a2)]
  a.insert(2, "00000001", @cnonce, @response['qop']) if qop_present?
  md5(a.join(":"))
end