Skip to content

Commit

Permalink
Whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
sandro committed Jan 20, 2011
1 parent bb2818f commit c4e78df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
22 changes: 11 additions & 11 deletions lib/httparty/net_digest_auth.rb
Expand Up @@ -7,17 +7,17 @@ def digest_auth(username, password, response)
@header['Authorization'] = DigestAuthenticator.new(username, password, @header['Authorization'] = DigestAuthenticator.new(username, password,
@method, @path, response).authorization_header @method, @path, response).authorization_header
end end


class DigestAuthenticator class DigestAuthenticator
def initialize(username, password, method, path, response_header) def initialize(username, password, method, path, response_header)
@username = username @username = username
@password = password @password = password
@method = method @method = method
@path = path @path = path
@response = parse(response_header) @response = parse(response_header)
end end

def authorization_header def authorization_header
@cnonce = md5(random) @cnonce = md5(random)
header = [%Q(Digest username="#{@username}"), header = [%Q(Digest username="#{@username}"),
Expand All @@ -31,34 +31,34 @@ def authorization_header
%Q(nc="0")].each { |field| header << field } if qop_present? %Q(nc="0")].each { |field| header << field } if qop_present?
header header
end end

private private

def parse(response_header) def parse(response_header)
response_header['www-authenticate'] =~ /^(\w+) (.*)/ response_header['www-authenticate'] =~ /^(\w+) (.*)/
params = {} params = {}
$2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 } $2.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
params params
end end

def qop_present? def qop_present?
@response.has_key?('qop') and not @response['qop'].empty? @response.has_key?('qop') and not @response['qop'].empty?
end end

def random def random
"%x" % (Time.now.to_i + rand(65535)) "%x" % (Time.now.to_i + rand(65535))
end end

def request_digest def request_digest
a = [md5(a1), @response['nonce'], md5(a2)] a = [md5(a1), @response['nonce'], md5(a2)]
a.insert(2, "0", @cnonce, @response['qop']) if qop_present? a.insert(2, "0", @cnonce, @response['qop']) if qop_present?
md5(a.join(":")) md5(a.join(":"))
end end

def md5(str) def md5(str)
Digest::MD5.hexdigest(str) Digest::MD5.hexdigest(str)
end end

def a1 def a1
[@username, @response['realm'], @password].join(":") [@username, @response['realm'], @password].join(":")
end end
Expand Down
40 changes: 20 additions & 20 deletions spec/httparty/net_digest_auth_spec.rb
Expand Up @@ -3,47 +3,47 @@
describe Net::HTTPHeader::DigestAuthenticator do describe Net::HTTPHeader::DigestAuthenticator do
def setup_digest(response) def setup_digest(response)
digest = Net::HTTPHeader::DigestAuthenticator.new("Mufasa", digest = Net::HTTPHeader::DigestAuthenticator.new("Mufasa",
"Circle Of Life", "GET", "/dir/index.html", response) "Circle Of Life", "GET", "/dir/index.html", response)
digest.stub(:random).and_return("deadbeef") digest.stub(:random).and_return("deadbeef")
Digest::MD5.stub(:hexdigest) { |str| "md5(#{str})" } Digest::MD5.stub(:hexdigest) { |str| "md5(#{str})" }
digest digest
end end

def authorization_header def authorization_header
@digest.authorization_header.join(", ") @digest.authorization_header.join(", ")
end end




context "with specified quality of protection (qop)" do context "with specified quality of protection (qop)" do
before do before do
@digest = setup_digest({'www-authenticate' => @digest = setup_digest({'www-authenticate' =>
'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"'}) 'Digest realm="myhost@testrealm.com", nonce="NONCE", qop="auth"'})
end end

it "should set prefix" do it "should set prefix" do
authorization_header.should =~ /^Digest / authorization_header.should =~ /^Digest /
end end

it "should set username" do it "should set username" do
authorization_header.should include(%Q(username="Mufasa")) authorization_header.should include(%Q(username="Mufasa"))
end end


it "should set digest-uri" do it "should set digest-uri" do
authorization_header.should include(%Q(uri="/dir/index.html")) authorization_header.should include(%Q(uri="/dir/index.html"))
end end

it "should set qop" do it "should set qop" do
authorization_header.should include(%Q(qop="auth")) authorization_header.should include(%Q(qop="auth"))
end end


it "should set cnonce" do it "should set cnonce" do
authorization_header.should include(%Q(cnonce="md5(deadbeef)")) authorization_header.should include(%Q(cnonce="md5(deadbeef)"))
end end

it "should set nonce-count" do it "should set nonce-count" do
authorization_header.should include(%Q(nc="0")) authorization_header.should include(%Q(nc="0"))
end end

it "should set response" do it "should set response" do
request_digest = request_digest =
"md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" + "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" +
Expand All @@ -55,39 +55,39 @@ def authorization_header


context "with unspecified quality of protection (qop)" do context "with unspecified quality of protection (qop)" do
before do before do
@digest = setup_digest({'www-authenticate' => @digest = setup_digest({'www-authenticate' =>
'Digest realm="myhost@testrealm.com", nonce="NONCE"'}) 'Digest realm="myhost@testrealm.com", nonce="NONCE"'})
end end

it "should set prefix" do it "should set prefix" do
authorization_header.should =~ /^Digest / authorization_header.should =~ /^Digest /
end end

it "should set username" do it "should set username" do
authorization_header.should include(%Q(username="Mufasa")) authorization_header.should include(%Q(username="Mufasa"))
end end


it "should set digest-uri" do it "should set digest-uri" do
authorization_header.should include(%Q(uri="/dir/index.html")) authorization_header.should include(%Q(uri="/dir/index.html"))
end end

it "should not set qop" do it "should not set qop" do
authorization_header.should_not include(%Q(qop=)) authorization_header.should_not include(%Q(qop=))
end end

it "should not set cnonce" do it "should not set cnonce" do
authorization_header.should_not include(%Q(cnonce=)) authorization_header.should_not include(%Q(cnonce=))
end end

it "should not set nonce-count" do it "should not set nonce-count" do
authorization_header.should_not include(%Q(nc=)) authorization_header.should_not include(%Q(nc=))
end end

it "should set response" do it "should set response" do
request_digest = request_digest =
"md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" + "md5(md5(Mufasa:myhost@testrealm.com:Circle Of Life)" +
":NONCE:md5(GET:/dir/index.html))" ":NONCE:md5(GET:/dir/index.html))"
authorization_header.should include(%Q(response="#{request_digest}")) authorization_header.should include(%Q(response="#{request_digest}"))
end end
end end
end end

0 comments on commit c4e78df

Please sign in to comment.