Parent

Included Modules

Class Index [+]

Quicksearch

Net::HTTP::DigestAuth

An implementation of RFC 2617 Digest Access Authentication.

www.rfc-editor.org/rfc/rfc2617.txt

Here is a sample usage of DigestAuth on Net::HTTP:

  require 'uri'
  require 'net/http'
  require 'net/http/digest_auth'

  digest_auth = Net::HTTP::DigestAuth.new

  uri = URI.parse 'http://localhost:8000/'
  uri.user = 'username'
  uri.password = 'password'

  h = Net::HTTP.new uri.host, uri.port

  req = Net::HTTP::Get.new uri.request_uri

  res = h.request req
  # res is a 401 response with a WWW-Authenticate header

  auth = digest_auth.auth_header uri, res['www-authenticate'], 'GET'

  # create a new request with the Authorization header
  req = Net::HTTP::Get.new uri.request_uri
  req.add_field 'Authorization', auth

  # re-issue request with Authorization
  res = h.request req

Constants

VERSION

Version of Net::HTTP::DigestAuth you are using

Public Class Methods

new(cnonce = make_cnonce) click to toggle source

Creates a new DigestAuth header creator.

cnonce is the client nonce value. This should be an MD5 hexdigest of a secret value.

    # File lib/net/http/digest_auth.rb, line 59
59:   def initialize cnonce = make_cnonce
60:     mon_initialize
61:     @nonce_count = 1
62:     @cnonce = cnonce
63:   end

Public Instance Methods

auth_header(uri, www_authenticate, method, iis = false) click to toggle source

Creates a digest auth header for uri from the www_authenticate header for HTTP method method.

The result of this method should be sent along with the HTTP request as the “Authorization” header. In Net::HTTP this will look like:

  request.add_field 'Authorization', digest_auth.auth_header # ...

See Net::HTTP::DigestAuth for a complete example.

IIS servers handle the “qop” parameter of digest authentication differently so you may need to set iis to true for such servers.

     # File lib/net/http/digest_auth.rb, line 79
 79:   def auth_header uri, www_authenticate, method, iis = false
 80:     nonce_count = next_nonce
 81: 
 82:     user     = CGI.unescape uri.user
 83:     password = CGI.unescape uri.password
 84: 
 85:     www_authenticate =~ /^(\w+) (.*)/
 86: 
 87:     challenge = $2
 88: 
 89:     params = {}
 90:     challenge.gsub(/(\w+)="(.*?)"/) { params[$1] = $2 }
 91: 
 92:     challenge =~ /algorithm=(.*?)([, ]|$)/
 93: 
 94:     params['algorithm'] = $1 || 'MD5'
 95: 
 96:     if params['algorithm'] =~ /(.*?)(-sess)?$/
 97:       algorithm = case $1
 98:                   when 'MD5'    then Digest::MD5
 99:                   when 'SHA1'   then Digest::SHA1
100:                   when 'SHA2'   then Digest::SHA2
101:                   when 'SHA256' then Digest::SHA256
102:                   when 'SHA384' then Digest::SHA384
103:                   when 'SHA512' then Digest::SHA512
104:                   when 'RMD160' then Digest::RMD160
105:                   else raise Error, "unknown algorithm \"#{$1}\""
106:                   end
107:       sess = $2
108:     end
109: 
110:     a1 = if sess then
111:            [ algorithm.hexdigest("#{user}:#{params['realm']}:#{password}"),
112:              params['nonce'],
113:              @cnonce,
114:            ].join ':'
115:          else
116:            "#{user}:#{params['realm']}:#{password}"
117:          end
118: 
119:     qop = params['qop']
120: 
121:     ha1 = algorithm.hexdigest a1
122:     ha2 = algorithm.hexdigest "#{method}:#{uri.request_uri}"
123: 
124:     request_digest = [ha1, params['nonce']]
125:     request_digest.push(('%08x' % nonce_count), @cnonce, qop) if qop
126:     request_digest << ha2
127:     request_digest = request_digest.join ':'
128: 
129:     header = [
130:       "Digest username=\"#{user}\"",
131:       "realm=\"#{params['realm']}\"",
132:       "algorithm=#{params['algorithm']}",
133:       if qop.nil? then
134:       elsif iis then
135:         "qop=\"#{qop}\""
136:       else
137:         "qop=#{qop}"
138:       end,
139:       "uri=\"#{uri.request_uri}\"",
140:       "nonce=\"#{params['nonce']}\"",
141:       "nc=#{'%08x' % @nonce_count}",
142:       "cnonce=\"#{@cnonce}\"",
143:       "response=\"#{algorithm.hexdigest(request_digest)[0, 32]}\"",
144:       if params.key? 'opaque' then
145:         "opaque=\"#{params['opaque']}\""
146:       end
147:     ].compact
148: 
149:     header.join ', '
150:   end
make_cnonce() click to toggle source

Creates a client nonce value that is used across all requests based on the current time.

     # File lib/net/http/digest_auth.rb, line 156
156:   def make_cnonce
157:     Digest::MD5.hexdigest "%x" % (Time.now.to_i + rand(65535))
158:   end
next_nonce() click to toggle source
     # File lib/net/http/digest_auth.rb, line 160
160:   def next_nonce
161:     synchronize do
162:       @nonce_count += 1
163:     end
164:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.