Module: ActionController::HttpAuthentication::Digest
- Extended by:
- Digest
- Included in:
- Digest
- Defined in:
- actionpack/lib/action_controller/metal/http_authentication.rb
Defined Under Namespace
Modules: ControllerMethods
Instance Method Summary collapse
-
#authenticate(request, realm, &password_procedure) ⇒ Object
Returns false on a valid response, true otherwise.
- #authentication_header(controller, realm) ⇒ Object
- #authentication_request(controller, realm, message = nil) ⇒ Object
- #decode_credentials(header) ⇒ Object
- #decode_credentials_header(request) ⇒ Object
- #encode_credentials(http_method, credentials, password, password_is_ha1) ⇒ Object
-
#expected_response(http_method, uri, credentials, password, password_is_ha1 = true) ⇒ Object
Returns the expected response for a request of
http_method
touri
with the decodedcredentials
and the expectedpassword
Optional parameterpassword_is_ha1
is set totrue
by default, since best practice is to store ha1 digest instead of a plain-text password. - #ha1(credentials, password) ⇒ Object
-
#nonce(secret_key, time = Time.now) ⇒ Object
Uses an MD5 digest based on time to generate a value to be used only once.
-
#opaque(secret_key) ⇒ Object
Opaque based on random generation - but changing each request?.
- #secret_token(request) ⇒ Object
-
#validate_digest_response(request, realm, &password_procedure) ⇒ Object
Returns false unless the request credentials response value matches the expected value.
-
#validate_nonce(secret_key, request, value, seconds_to_timeout = 5*60) ⇒ Object
Might want a shorter timeout depending on whether the request is a PUT or POST, and if client is browser or web service.
Instance Method Details
#authenticate(request, realm, &password_procedure) ⇒ Object
Returns false on a valid response, true otherwise
178 179 180 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 178 def authenticate(request, realm, &password_procedure) request. && validate_digest_response(request, realm, &password_procedure) end |
#authentication_header(controller, realm) ⇒ Object
233 234 235 236 237 238 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 233 def authentication_header(controller, realm) secret_key = secret_token(controller.request) nonce = self.nonce(secret_key) opaque = opaque(secret_key) controller.headers["WWW-Authenticate"] = %(Digest realm="#{realm}", qop="auth", algorithm=MD5, nonce="#{nonce}", opaque="#{opaque}") end |
#authentication_request(controller, realm, message = nil) ⇒ Object
240 241 242 243 244 245 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 240 def authentication_request(controller, realm, = nil) ||= "HTTP Digest: Access denied.\n" authentication_header(controller, realm) controller.response_body = controller.status = 401 end |
#decode_credentials(header) ⇒ Object
226 227 228 229 230 231 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 226 def decode_credentials(header) Hash[header.to_s.gsub(/^Digest\s+/,'').split(',').map do |pair| key, value = pair.split('=', 2) [key.strip.to_sym, value.to_s.gsub(/^"|"$/,'').gsub(/'/, '')] end] end |
#decode_credentials_header(request) ⇒ Object
222 223 224 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 222 def decode_credentials_header(request) decode_credentials(request.) end |
#encode_credentials(http_method, credentials, password, password_is_ha1) ⇒ Object
217 218 219 220 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 217 def encode_credentials(http_method, credentials, password, password_is_ha1) credentials[:response] = expected_response(http_method, credentials[:uri], credentials, password, password_is_ha1) "Digest " + credentials.sort_by {|x| x[0].to_s }.map {|v| "#{v[0]}='#{v[1]}'" }.join(', ') end |
#expected_response(http_method, uri, credentials, password, password_is_ha1 = true) ⇒ Object
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.
207 208 209 210 211 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 207 def expected_response(http_method, uri, credentials, password, password_is_ha1=true) ha1 = password_is_ha1 ? password : ha1(credentials, password) ha2 = ::Digest::MD5.hexdigest([http_method.to_s.upcase, uri].join(':')) ::Digest::MD5.hexdigest([ha1, credentials[:nonce], credentials[:nc], credentials[:cnonce], credentials[:qop], ha2].join(':')) end |
#ha1(credentials, password) ⇒ Object
213 214 215 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 213 def ha1(credentials, password) ::Digest::MD5.hexdigest([credentials[:username], credentials[:realm], password].join(':')) end |
#nonce(secret_key, time = Time.now) ⇒ Object
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 modified by client.
285 286 287 288 289 290 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 285 def nonce(secret_key, time = Time.now) t = time.to_i hashed = [t, secret_key] digest = ::Digest::MD5.hexdigest(hashed.join(":")) ActiveSupport::Base64.encode64("#{t}:#{digest}").gsub("\n", '') end |
#opaque(secret_key) ⇒ Object
Opaque based on random generation - but changing each request?
303 304 305 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 303 def opaque(secret_key) ::Digest::MD5.hexdigest(secret_key) end |
#secret_token(request) ⇒ Object
247 248 249 250 251 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 247 def secret_token(request) secret = request.env["action_dispatch.secret_token"] raise "You must set config.secret_token in your app's config" if secret.blank? secret end |
#validate_digest_response(request, realm, &password_procedure) ⇒ Object
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.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 185 def validate_digest_response(request, realm, &password_procedure) secret_key = secret_token(request) credentials = decode_credentials_header(request) valid_nonce = validate_nonce(secret_key, request, credentials[:nonce]) if valid_nonce && realm == credentials[:realm] && opaque(secret_key) == credentials[:opaque] password = password_procedure.call(credentials[:username]) return false unless password method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD'] uri = credentials[:uri][0,1] == '/' ? request.fullpath : request.url [true, false].any? do |password_is_ha1| expected = expected_response(method, uri, credentials, password, password_is_ha1) expected == credentials[:response] end end end |
#validate_nonce(secret_key, request, value, seconds_to_timeout = 5*60) ⇒ Object
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.
297 298 299 300 |
# File 'actionpack/lib/action_controller/metal/http_authentication.rb', line 297 def validate_nonce(secret_key, request, value, seconds_to_timeout=5*60) t = ActiveSupport::Base64.decode64(value).split(":").first.to_i nonce(secret_key, t) == value && (t - Time.now.to_i).abs <= seconds_to_timeout end |