Module: Net::HTTPHeader

Defined in:
lib/switchvox/net_http_digest_auth.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#digest_auth(user, password, response) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/switchvox/net_http_digest_auth.rb', line 8

def digest_auth(user, password, response)
  # based on http://segment7.net/projects/ruby/snippets/digest_auth.rb
  @@nonce_count += 1

  response['www-authenticate'] =~ /^(\w+) (.*)/

  params = {}
  $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }

  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']
  request_digest << ':' << Digest::MD5.new.update(a_2).hexdigest

  header = []
  header << "Digest username=\"#{user}\""
  header << "realm=\"#{params['realm']}\""
  header << "uri=\"#{@path}\""
  header << "nonce=\"#{params['nonce']}\""
  header << "response=\"#{Digest::MD5.new.update(request_digest).hexdigest}\""

  @header['Authorization'] = header
end