Module: Net::HTTPHeader

Defined in:
lib/marklogic/connection.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#basic_auth(user, password) ⇒ Object


53
54
55
56
57
# File 'lib/marklogic/connection.rb', line 53

def basic_auth(user, password)
  encoded = Base64.encode64("#{user}:#{password}").chomp

  @header['Authorization'] = ["Basic #{encoded}"]
end

#capitalize(name) ⇒ Object

diable header capitalization for a performance gain


60
61
62
# File 'lib/marklogic/connection.rb', line 60

def capitalize(name)
  name
end

#create_digest_auth(user, password, response) ⇒ Object


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/marklogic/connection.rb', line 12

def create_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 }

  digest_auth(user, password, params)
end

#digest_auth(user, password, params) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/marklogic/connection.rb', line 24

def digest_auth(user, password, params)

  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.hexdigest(request_digest)}\""

  @header['Authorization'] = header
  params
end