Classes and Modules
Module ActionController::HttpAuthentication::Digest::ControllerMethods
Public Instance methods
authenticate(controller, realm, &password_procedure)

Returns false on a valid response, true otherwise

     # File actionpack/lib/action_controller/http_authentication.rb, line 175
175:       def authenticate(controller, realm, &password_procedure)
176:         authorization(controller.request) && validate_digest_response(controller.request, realm, &password_procedure)
177:       end
authentication_header(controller, realm)
     # File actionpack/lib/action_controller/http_authentication.rb, line 237
237:       def authentication_header(controller, realm)
238:         controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}")
239:       end
authentication_request(controller, realm, message = nil)
     # File actionpack/lib/action_controller/http_authentication.rb, line 241
241:       def authentication_request(controller, realm, message = nil)
242:         message ||= "HTTP Digest: Access denied.\n"
243:         authentication_header(controller, realm)
244:         controller.__send__ :render, :text => message, :status => :unauthorized
245:       end
     # File actionpack/lib/action_controller/http_authentication.rb, line 179
179:       def authorization(request)
180:         request.env['HTTP_AUTHORIZATION']   ||
181:         request.env['X-HTTP_AUTHORIZATION'] ||
182:         request.env['X_HTTP_AUTHORIZATION'] ||
183:         request.env['REDIRECT_X_HTTP_AUTHORIZATION']
184:       end
     # File actionpack/lib/action_controller/http_authentication.rb, line 229
229:       def decode_credentials(header)
230:         header.to_s.gsub(/^Digest\s+/,'').split(',').inject({}.with_indifferent_access) do |hash, pair|
231:           key, value = pair.split('=', 2)
232:           hash[key.strip] = value.to_s.gsub(/^"|"$/,'').gsub(/'/, '')
233:           hash
234:         end
235:       end
     # File actionpack/lib/action_controller/http_authentication.rb, line 225
225:       def decode_credentials_header(request)
226:         decode_credentials(authorization(request))
227:       end
encode_credentials(http_method, credentials, password, password_is_ha1)
     # File actionpack/lib/action_controller/http_authentication.rb, line 220
220:       def encode_credentials(http_method, credentials, password, password_is_ha1)
221:         credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password, password_is_ha1)
222:         "Digest " + credentials.sort_by {|x| x[0].to_s }.inject([]) {|a, v| a << "#{v[0]}='#{v[1]}'" }.join(', ')
223:       end
expected_response(http_method, uri, credentials, password, password_is_ha1=true)

Returns the expected response for a request of http_method to uri with the decoded credentials and the expected password Optional parameter +password_is_ha1+ is set to true by default, since best practice is to store ha1 digest instead of a plain-text password.

     # File actionpack/lib/action_controller/http_authentication.rb, line 210
210:       def expected_response(http_method, uri, credentials, password, password_is_ha1=true)
211:         ha1 = password_is_ha1 ? password : ha1(credentials, password)
212:         ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':'))
213:         ::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':'))
214:       end
ha1(credentials, password)
     # File actionpack/lib/action_controller/http_authentication.rb, line 216
216:       def ha1(credentials, password)
217:         ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':'))
218:       end
nonce(time =

Uses an MD5 digest based on time to generate a value to be used only once.

A server-specified data string which should be uniquely generated each time a 401 response is made. It is recommended that this string be base64 or hexadecimal data. Specifically, since the string is passed in the header lines as a quoted string, the double-quote character is not allowed.

The contents of the nonce are implementation dependent. The quality of the implementation depends on a good choice. A nonce might, for example, be constructed as the base 64 encoding of

> time-stamp H(time-stamp ":" ETag ":" private-key)

where time-stamp is a server-generated time or other non-repeating value, ETag is the value of the HTTP ETag header associated with the requested entity, and private-key is data known only to the server. With a nonce of this form a server would recalculate the hash portion after receiving the client authentication header and reject the request if it did not match the nonce from that header or if the time-stamp value is not recent enough. In this way the server can limit the time of the nonce‘s validity. The inclusion of the ETag prevents a replay request for an updated version of the resource. (Note: including the IP address of the client in the nonce would appear to offer the server the ability to limit the reuse of the nonce to the same client that originally got it. However, that would break proxy farms, where requests from a single user often go through different proxies in the farm. Also, IP address spoofing is not that hard.)

An implementation might choose not to accept a previously used nonce or a previously used digest, in order to protect against a replay attack. Or, an implementation might choose to use one-time nonces or digests for POST or PUT requests and a time-stamp for GET requests. For more details on the issues involved see Section 4 of this document.

The nonce is opaque to the client. Composed of Time, and hash of Time with secret key from the Rails session secret generated upon creation of project. Ensures the time cannot be modifed by client.

     # File actionpack/lib/action_controller/http_authentication.rb, line 279
279:       def nonce(time =
280:         t = time.to_i
281:         hashed = [t, secret_key]
282:         digest = ::Digest::MD5.hexdigest(hashed.join(":"))
283:         Base64.encode64("#{t}:#{digest}").gsub("\n", '')
284:       end

Opaque based on random generation - but changing each request?

     # File actionpack/lib/action_controller/http_authentication.rb, line 298
298:       def opaque()
299:         ::Digest::MD5.hexdigest(secret_key)
300:       end

Set in /initializers/session_store.rb, and loaded even if sessions are not in use.

     # File actionpack/lib/action_controller/http_authentication.rb, line 303
303:       def secret_key
304:         ActionController::Base.session_options[:secret]
305:       end
validate_digest_response(request, realm, &password_procedure)

Returns false unless the request credentials response value matches the expected value. First try the password as a ha1 digest password. If this fails, then try it as a plain text password.

     # File actionpack/lib/action_controller/http_authentication.rb, line 189
189:       def validate_digest_response(request, realm, &password_procedure)
190:         credentials = decode_credentials_header(request)
191:         valid_nonce = validate_nonce(request, credentials[:nonce])
193:         if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
194:           password =[:username])
195:           return false unless password
197:           method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']
198:           uri    = credentials[:uri][0,1] == '/' ? request.request_uri : request.url
200:          [true, false].any? do |password_is_ha1|
201:            expected = expected_response(method, uri, credentials, password, password_is_ha1)
202:            expected == credentials[:response]
203:          end
204:         end
205:       end
validate_nonce(request, value, seconds_to_timeout=5*60)

Might want a shorter timeout depending on whether the request is a PUT or POST, and if client is browser or web service. Can be much shorter if the Stale directive is implemented. This would allow a user to use new nonce without prompting user again for their username and password.

     # File actionpack/lib/action_controller/http_authentication.rb, line 291
291:       def validate_nonce(request, value, seconds_to_timeout=5*60)
292:         return false if value.nil?
293:         t = Base64.decode64(value).split(":").first.to_i
294:         nonce(t) == value && (t - <= seconds_to_timeout
295:       end