Skip to content

Commit

Permalink
Ensure HTTP Digest auth uses appropriate HTTP method [#2490 state:res…
Browse files Browse the repository at this point in the history
…olved] [Steve Madsen]
  • Loading branch information
lifo committed May 18, 2009
1 parent 28f5cfe commit 195fadb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion actionpack/lib/action_controller/base/http_authentication.rb
Expand Up @@ -194,9 +194,10 @@ def validate_digest_response(request, realm, &password_procedure)

if valid_nonce && realm == credentials[:realm] && opaque == credentials[:opaque]
password = password_procedure.call(credentials[:username])
method = request.env['rack.methodoverride.original_method'] || request.env['REQUEST_METHOD']

[true, false].any? do |password_is_ha1|
expected = expected_response(request.env['REQUEST_METHOD'], request.env['REQUEST_URI'], credentials, password, password_is_ha1)
expected = expected_response(method, request.env['REQUEST_URI'], credentials, password, password_is_ha1)
expected == credentials[:response]
end
end
Expand Down
23 changes: 20 additions & 3 deletions actionpack/test/controller/http_digest_authentication_test.rb
Expand Up @@ -149,6 +149,16 @@ def authenticate_with_request
assert_equal 'Definitely Maybe', @response.body
end

test "authentication request with _method" do
@request.env['HTTP_AUTHORIZATION'] = encode_credentials(:username => 'pretty', :password => 'please', :method => :post)
@request.env['rack.methodoverride.original_method'] = 'POST'
put :display

assert_response :success
assert assigns(:logged_in)
assert_equal 'Definitely Maybe', @response.body
end

private

def encode_credentials(options)
Expand All @@ -159,15 +169,22 @@ def encode_credentials(options)
# to prevent tampering of timestamp
ActionController::Base.session_options[:secret] = "session_options_secret"

# Perform unauthenticated GET to retrieve digest parameters to use on subsequent request
get :index
# Perform unauthenticated request to retrieve digest parameters to use on subsequent request
method = options.delete(:method) || 'GET'

case method.to_s.upcase
when 'GET'
get :index
when 'POST'
post :index
end

assert_response :unauthorized

credentials = decode_credentials(@response.headers['WWW-Authenticate'])
credentials.merge!(options)
credentials.reverse_merge!(:uri => "#{@request.env['REQUEST_URI']}")
ActionController::HttpAuthentication::Digest.encode_credentials("GET", credentials, password, options[:password_is_ha1])
ActionController::HttpAuthentication::Digest.encode_credentials(method, credentials, password, options[:password_is_ha1])
end

def decode_credentials(header)
Expand Down

0 comments on commit 195fadb

Please sign in to comment.