Module: Net::HTTPHeader

Defined in:
lib/rbrainz/core_ext/net_http_digest.rb

Overview

:nodoc:

Constant Summary collapse

CNONCE =
Digest::MD5.new.update("%x" % (Time.now.to_i + rand(65535))).hexdigest
@@nonce_count =
0

Instance Method Summary collapse

Instance Method Details

#digest_auth(user, password, response) ⇒ Object



47
48
49
# File 'lib/rbrainz/core_ext/net_http_digest.rb', line 47

def digest_auth(user, password, response)
  @header['Authorization'] = digest_encode(response['www-authenticate'], user, password)
end

#digest_encode(authenticate, user, password) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rbrainz/core_ext/net_http_digest.rb', line 55

def digest_encode(authenticate, user, password)
  @@nonce_count += 1
  authenticate =~ /^(\w+) (.*)/
  
  params = {}
  $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }

  qop = 'auth' if 
    params['qop'] and params['qop'].split(',').include?('auth')

  a_1 = "#{user}:#{params['realm']}:#{password}"
  a_2 = "#{@method}:#{@path}"
  request_digest = ''
  request_digest << Digest::MD5.new.update(a_1).hexdigest
  request_digest << ':' << params['nonce']
  if qop
    request_digest << ':' << ('%08x' % @@nonce_count)
    request_digest << ':' << CNONCE
    request_digest << ':' << qop
  end
  request_digest << ':' << Digest::MD5.new.update(a_2).hexdigest

  header = []
  header << "Digest username=\"#{user}\""
  header << "realm=\"#{params['realm']}\""

  header << "qop=#{qop}" if qop

  header << "algorithm=MD5"
  header << "uri=\"#{@path}\""
  header << "nonce=\"#{params['nonce']}\""
  header << "nc=#{'%08x' % @@nonce_count}" if params['qop']
  header << "cnonce=\"#{CNONCE}\"" if params['qop']
  header << "response=\"#{Digest::MD5.new.update(request_digest).hexdigest}\""
  header << "opaque=\"#{params['opaque']}\"" if params['opaque']
  
  return header
end

#proxy_digest_auth(user, password, response) ⇒ Object



51
52
53
# File 'lib/rbrainz/core_ext/net_http_digest.rb', line 51

def proxy_digest_auth(user, password, response)
  @header['Proxy-Authorization'] = digest_encode(response['proxy-authenticate'], user, password)
end

#proxy_select_auth(user, password, response) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rbrainz/core_ext/net_http_digest.rb', line 33

def proxy_select_auth(user, password, response)
  response['proxy-authenticate'] =~ /^(\w+) (.*)/
  auth_type = $1
  case auth_type
    when 'Digest'
      proxy_digest_auth(user,password,response)
      return true
    when 'Basic'
      proxy_basic_auth(user,password)
      return true
  end
  return false
end

#select_auth(user, password, response) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rbrainz/core_ext/net_http_digest.rb', line 19

def select_auth(user,password,response)
  response['www-authenticate'] =~ /^(\w+) (.*)/
  auth_type = $1
  case auth_type
    when 'Digest'
      digest_auth(user,password,response)
      return true
    when 'Basic'
      basic_auth(user,password)
      return true
  end
  return false
end