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 25
def initialize(username, password, method, path, response_header)
  @username = username
  @password = password
  @method   = method
  @path     = path
  @response = parse(response_header)
  @cookies  = parse_cookies(response_header)
end

Public Instance Methods

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

  header << %(algorithm="#{@response['algorithm']}") if algorithm_present?

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

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

Private Instance Methods

a1() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 121
def a1
  a1_user_realm_pwd =  [@username, @response['realm'], @password].join(':')
  if use_md5_sess?
    [ md5(a1_user_realm_pwd), @response['nonce'], @cnonce ].join(':')
  else
    a1_user_realm_pwd
  end
end
a2() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 130
def a2
  [@method, @path].join(":")
end
algorithm_present?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 113
def algorithm_present?
  @response.key?('algorithm') && !@response['algorithm'].empty?
end
md5(str) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 109
def md5(str)
  Digest::MD5.hexdigest(str)
end
opaque_present?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 91
def opaque_present?
  @response.key?('opaque') && !@response['opaque'].empty?
end
parse(response_header) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 65
def parse(response_header)
  header = response_header['www-authenticate']
           .gsub(/qop=(auth(?:-int)?)/, 'qop="\\1"')

  header =~ /Digest (.*)/
  params = {}
  if $1
    non_quoted = $1.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
    non_quoted.gsub(/(\w+)=([^,]*)/) { params[$1] = $2 }
  end
  params
end
parse_cookies(response_header) click to toggle source
# File lib/httparty/net_digest_auth.rb, line 78
def parse_cookies(response_header)
  return [] unless response_header['Set-Cookie']

  cookies = response_header['Set-Cookie'].split('; ')

  cookies.reduce([]) do |ret, cookie|
    ret << cookie
    ret
  end

  cookies
end
qop_present?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 95
def qop_present?
  @response.key?('qop') && !@response['qop'].empty?
end
random() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 99
def random
  format "%x", (Time.now.to_i + rand(65535))
end
request_digest() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 103
def request_digest
  a = [md5(a1), @response['nonce'], md5(a2)]
  a.insert(2, "00000001", @cnonce, @response['qop']) if qop_present?
  md5(a.join(":"))
end
use_md5_sess?() click to toggle source
# File lib/httparty/net_digest_auth.rb, line 117
def use_md5_sess?
  algorithm_present? && @response['algorithm'] == 'MD5-sess'
end