diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 94f435948..94c63a6be 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -229,9 +229,14 @@ class ApplicationController < ActionController::Base format.any(:atom, :pdf, :csv) { redirect_to signin_path(:back_url => url) } - format.xml { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } + format.api { + if Setting.rest_api_enabled? && accept_api_auth? + head(:unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"') + else + head(:forbidden) + end + } format.js { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } - format.json { head :unauthorized, 'WWW-Authenticate' => 'Basic realm="Redmine API"' } format.any { head :unauthorized } end return false diff --git a/test/integration/api_test/disabled_rest_api_test.rb b/test/integration/api_test/disabled_rest_api_test.rb index f0283ba34..cefa82499 100644 --- a/test/integration/api_test/disabled_rest_api_test.rb +++ b/test/integration/api_test/disabled_rest_api_test.rb @@ -41,11 +41,11 @@ class Redmine::ApiTest::DisabledRestApiTest < Redmine::ApiTest::Base @token = Token.create!(:user => @user, :action => 'api') get "/news.xml?key=#{@token.value}" - assert_response :unauthorized + assert_response :forbidden assert_equal User.anonymous, User.current get "/news.json?key=#{@token.value}" - assert_response :unauthorized + assert_response :forbidden assert_equal User.anonymous, User.current end @@ -55,11 +55,25 @@ class Redmine::ApiTest::DisabledRestApiTest < Redmine::ApiTest::Base end get "/news.xml", :headers => credentials(@user.login, 'my_password') - assert_response :unauthorized + assert_response :forbidden assert_equal User.anonymous, User.current get "/news.json", :headers => credentials(@user.login, 'my_password') - assert_response :unauthorized + assert_response :forbidden + assert_equal User.anonymous, User.current + end + + def test_with_valid_username_and_wrong_password_http_authentication + @user = User.generate! do |user| + user.password = 'my_password' + end + + get '/news.xml', :headers => credentials(@user.login, 'wrong_password') + assert_response :forbidden + assert_equal User.anonymous, User.current + + get "/news.json", :headers => credentials(@user.login, 'wrong_password') + assert_response :forbidden assert_equal User.anonymous, User.current end @@ -68,11 +82,11 @@ class Redmine::ApiTest::DisabledRestApiTest < Redmine::ApiTest::Base @token = Token.create!(:user => @user, :action => 'api') get "/news.xml", :headers => credentials(@token.value, 'X') - assert_response :unauthorized + assert_response :forbidden assert_equal User.anonymous, User.current get "/news.json", :headers => credentials(@token.value, 'X') - assert_response :unauthorized + assert_response :forbidden assert_equal User.anonymous, User.current end end