forked from jnunemaker/httparty
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Digest Access Authentication support to HTTParty.
New File: lib/net_http_digest.rb From: http://codesnippets.joyent.com/posts/show/1075 This extends Net::HTTP with a new digest_authentication method. Extended HTTParty to use this new functionality in a backwards compatible fashion. To use the new digest_auth method, merely use :digest_auth as your HTTParty credentials option instead of :basic_auth. HTTParty will take care of the rest.
- Loading branch information
Brian Artiaco
authored and
Brian Artiaco
committed
Feb 10, 2010
1 parent
03852d2
commit a760f14
Showing
4 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
require 'digest/md5' | ||
require 'net/http' | ||
|
||
module Net | ||
module HTTPHeader | ||
@@nonce_count = -1 | ||
CNONCE = Digest::MD5.new.update("%x" % (Time.now.to_i + rand(65535))).hexdigest | ||
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 << ':' << ('%08x' % @@nonce_count) | ||
request_digest << ':' << CNONCE | ||
request_digest << ':' << params['qop'] | ||
request_digest << ':' << Digest::MD5.new.update(a_2).hexdigest | ||
|
||
header = [] | ||
header << "Digest username=\"#{user}\"" | ||
header << "realm=\"#{params['realm']}\"" | ||
|
||
header << "qop=#{params['qop']}" | ||
|
||
header << "algorithm=MD5" | ||
header << "uri=\"#{@path}\"" | ||
header << "nonce=\"#{params['nonce']}\"" | ||
header << "nc=#{'%08x' % @@nonce_count}" | ||
header << "cnonce=\"#{CNONCE}\"" | ||
header << "response=\"#{Digest::MD5.new.update(request_digest).hexdigest}\"" | ||
|
||
@header['Authorization'] = header | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters