提交 ba924a51 编写于 作者: K Keenan Brock

Give authentication methods the ability to customize response message.

Digest allowed the messages.
Add the same feature to basic and token
上级 b67197ee
......@@ -74,16 +74,16 @@ def http_basic_authenticate_with(options = {})
end
end
def authenticate_or_request_with_http_basic(realm = "Application", &login_procedure)
authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm)
def authenticate_or_request_with_http_basic(realm = "Application", message = nil, &login_procedure)
authenticate_with_http_basic(&login_procedure) || request_http_basic_authentication(realm, message)
end
def authenticate_with_http_basic(&login_procedure)
HttpAuthentication::Basic.authenticate(request, &login_procedure)
end
def request_http_basic_authentication(realm = "Application")
HttpAuthentication::Basic.authentication_request(self, realm)
def request_http_basic_authentication(realm = "Application", message = nil)
HttpAuthentication::Basic.authentication_request(self, realm, message)
end
end
......@@ -117,10 +117,11 @@ def encode_credentials(user_name, password)
"Basic #{::Base64.strict_encode64("#{user_name}:#{password}")}"
end
def authentication_request(controller, realm)
def authentication_request(controller, realm, message)
message ||= "HTTP Basic: Access denied.\n"
controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.tr('"'.freeze, "".freeze)}")
controller.status = 401
controller.response_body = "HTTP Basic: Access denied.\n"
controller.response_body = message
end
end
......@@ -170,8 +171,8 @@ module Digest
extend self
module ControllerMethods
def authenticate_or_request_with_http_digest(realm = "Application", &password_procedure)
authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm)
def authenticate_or_request_with_http_digest(realm = "Application", message = nil, &password_procedure)
authenticate_with_http_digest(realm, &password_procedure) || request_http_digest_authentication(realm, message)
end
# Authenticate with HTTP Digest, returns true or false
......@@ -401,16 +402,16 @@ module Token
extend self
module ControllerMethods
def authenticate_or_request_with_http_token(realm = "Application", &login_procedure)
authenticate_with_http_token(&login_procedure) || request_http_token_authentication(realm)
def authenticate_or_request_with_http_token(realm = "Application", message = nil, &login_procedure)
authenticate_with_http_token(&login_procedure) || request_http_token_authentication(realm, message)
end
def authenticate_with_http_token(&login_procedure)
Token.authenticate(self, &login_procedure)
end
def request_http_token_authentication(realm = "Application")
Token.authentication_request(self, realm)
def request_http_token_authentication(realm = "Application", message = nil)
Token.authentication_request(self, realm, message)
end
end
......@@ -498,9 +499,10 @@ def encode_credentials(token, options = {})
# realm - String realm to use in the header.
#
# Returns nothing.
def authentication_request(controller, realm)
def authentication_request(controller, realm, message = nil)
message ||= "HTTP Token: Access denied.\n"
controller.headers["WWW-Authenticate"] = %(Token realm="#{realm.tr('"'.freeze, "".freeze)}")
controller.__send__ :render, :text => "HTTP Token: Access denied.\n", :status => :unauthorized
controller.__send__ :render, :text => message, :status => :unauthorized
end
end
end
......
......@@ -36,7 +36,7 @@ def authenticate_with_request
if authenticate_with_http_basic { |username, password| username == 'pretty' && password == 'please' }
@logged_in = true
else
request_http_basic_authentication("SuperSecret")
request_http_basic_authentication("SuperSecret", "Authentication Failed\n")
end
end
......@@ -104,7 +104,7 @@ def test_encode_credentials_has_no_newline
get :display
assert_response :unauthorized
assert_equal "HTTP Basic: Access denied.\n", @response.body
assert_equal "Authentication Failed\n", @response.body
assert_equal 'Basic realm="SuperSecret"', @response.headers['WWW-Authenticate']
end
......@@ -113,7 +113,7 @@ def test_encode_credentials_has_no_newline
get :display
assert_response :unauthorized
assert_equal "HTTP Basic: Access denied.\n", @response.body
assert_equal "Authentication Failed\n", @response.body
assert_equal 'Basic realm="SuperSecret"', @response.headers['WWW-Authenticate']
end
......
......@@ -30,7 +30,7 @@ def authenticate_with_request
if authenticate_with_http_token { |token, options| token == '"quote" pretty' && options[:algorithm] == 'test' }
@logged_in = true
else
request_http_token_authentication("SuperSecret")
request_http_token_authentication("SuperSecret", "Authentication Failed\n")
end
end
......@@ -91,7 +91,7 @@ def authenticate_long_credentials
get :display
assert_response :unauthorized
assert_equal "HTTP Token: Access denied.\n", @response.body
assert_equal "Authentication Failed\n", @response.body
assert_equal 'Token realm="SuperSecret"', @response.headers['WWW-Authenticate']
end
......@@ -100,7 +100,7 @@ def authenticate_long_credentials
get :display
assert_response :unauthorized
assert_equal "HTTP Token: Access denied.\n", @response.body
assert_equal "Authentication Failed\n", @response.body
assert_equal 'Token realm="SuperSecret"', @response.headers['WWW-Authenticate']
end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册