2016-08-06 12:54:50 -04:00
|
|
|
require "abstract_unit"
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
class HttpBasicAuthenticationTest < ActionController::TestCase
|
|
|
|
class DummyController < ActionController::Base
|
2012-12-07 14:46:06 -05:00
|
|
|
before_action :authenticate, only: :index
|
|
|
|
before_action :authenticate_with_request, only: :display
|
|
|
|
before_action :authenticate_long_credentials, only: :show
|
2015-08-09 16:52:22 -04:00
|
|
|
before_action :auth_with_special_chars, only: :special_creds
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2016-08-06 13:35:13 -04:00
|
|
|
http_basic_authenticate_with name: "David", password: "Goliath", only: :search
|
2011-03-28 21:09:50 -04:00
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
def index
|
2015-07-17 21:48:00 -04:00
|
|
|
render plain: "Hello Secret"
|
2009-01-28 20:59:55 -05:00
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
def display
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "Definitely Maybe" if @logged_in
|
2009-01-28 20:59:55 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-08-08 20:24:26 -04:00
|
|
|
def show
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "Only for loooooong credentials"
|
2009-08-08 20:24:26 -04:00
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2015-08-09 16:52:22 -04:00
|
|
|
def special_creds
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "Only for special credentials"
|
2015-08-09 16:52:22 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 21:09:50 -04:00
|
|
|
def search
|
2016-08-06 12:54:50 -04:00
|
|
|
render plain: "All inline"
|
2011-03-28 21:09:50 -04:00
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
private
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
def authenticate
|
|
|
|
authenticate_or_request_with_http_basic do |username, password|
|
2016-08-06 12:54:50 -04:00
|
|
|
username == "lifo" && password == "world"
|
2009-01-28 20:59:55 -05:00
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
2009-01-28 20:59:55 -05:00
|
|
|
def authenticate_with_request
|
2016-08-06 12:54:50 -04:00
|
|
|
if authenticate_with_http_basic { |username, password| username == "pretty" && password == "please" }
|
2009-01-28 20:59:55 -05:00
|
|
|
@logged_in = true
|
|
|
|
else
|
2015-05-01 14:28:00 -04:00
|
|
|
request_http_basic_authentication("SuperSecret", "Authentication Failed\n")
|
2009-01-28 20:59:55 -05:00
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2015-08-09 16:52:22 -04:00
|
|
|
def auth_with_special_chars
|
|
|
|
authenticate_or_request_with_http_basic do |username, password|
|
|
|
|
username == 'login!@#$%^&*()_+{}[];"\',./<>?`~ \n\r\t' && password == 'pwd:!@#$%^&*()_+{}[];"\',./<>?`~ \n\r\t'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-08-08 20:24:26 -04:00
|
|
|
def authenticate_long_credentials
|
|
|
|
authenticate_or_request_with_http_basic do |username, password|
|
2016-08-06 12:54:50 -04:00
|
|
|
username == "1234567890123456789012345678901234567890" && password == "1234567890123456789012345678901234567890"
|
2009-08-08 20:24:26 -04:00
|
|
|
end
|
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
AUTH_HEADERS = ["HTTP_AUTHORIZATION", "X-HTTP_AUTHORIZATION", "X_HTTP_AUTHORIZATION", "REDIRECT_X_HTTP_AUTHORIZATION"]
|
2009-01-18 14:21:34 -05:00
|
|
|
|
|
|
|
tests DummyController
|
|
|
|
|
|
|
|
AUTH_HEADERS.each do |header|
|
|
|
|
test "successful authentication with #{header.downcase}" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env[header] = encode_credentials("lifo", "world")
|
2009-01-18 14:21:34 -05:00
|
|
|
get :index
|
|
|
|
|
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Hello Secret", @response.body, "Authentication failed for request header #{header}"
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
2009-08-08 20:24:26 -04:00
|
|
|
test "successful authentication with #{header.downcase} and long credentials" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env[header] = encode_credentials("1234567890123456789012345678901234567890", "1234567890123456789012345678901234567890")
|
2009-08-08 20:24:26 -04:00
|
|
|
get :show
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-08-08 20:24:26 -04:00
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Only for loooooong credentials", @response.body, "Authentication failed for request header #{header} and long credentials"
|
2009-08-08 20:24:26 -04:00
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
AUTH_HEADERS.each do |header|
|
|
|
|
test "unsuccessful authentication with #{header.downcase}" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env[header] = encode_credentials("h4x0r", "world")
|
2009-01-18 14:21:34 -05:00
|
|
|
get :index
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
|
|
|
assert_equal "HTTP Basic: Access denied.\n", @response.body, "Authentication didn't fail for request header #{header}"
|
|
|
|
end
|
2009-08-08 20:24:26 -04:00
|
|
|
test "unsuccessful authentication with #{header.downcase} and long credentials" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env[header] = encode_credentials("h4x0rh4x0rh4x0rh4x0rh4x0rh4x0rh4x0rh4x0r", "worldworldworldworldworldworldworldworld")
|
2009-08-08 20:24:26 -04:00
|
|
|
get :show
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
|
|
|
assert_equal "HTTP Basic: Access denied.\n", @response.body, "Authentication didn't fail for request header #{header} and long credentials"
|
|
|
|
end
|
2015-01-22 22:28:53 -05:00
|
|
|
|
|
|
|
test "unsuccessful authentication with #{header.downcase} and no credentials" do
|
|
|
|
get :show
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
|
|
|
assert_equal "HTTP Basic: Access denied.\n", @response.body, "Authentication didn't fail for request header #{header} and no credentials"
|
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
2011-09-06 20:25:20 -04:00
|
|
|
def test_encode_credentials_has_no_newline
|
2016-08-06 12:54:50 -04:00
|
|
|
username = "laskjdfhalksdjfhalkjdsfhalksdjfhklsdjhalksdjfhalksdjfhlakdsjfh"
|
|
|
|
password = "kjfhueyt9485osdfasdkljfh4lkjhakldjfhalkdsjf"
|
2011-09-06 20:25:20 -04:00
|
|
|
result = ActionController::HttpAuthentication::Basic.encode_credentials(
|
|
|
|
username, password)
|
|
|
|
assert_no_match(/\n/, result)
|
|
|
|
end
|
|
|
|
|
2016-01-03 16:11:03 -05:00
|
|
|
test "successful authentication with uppercase authorization scheme" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = "BASIC #{::Base64.encode64("lifo:world")}"
|
2015-08-11 18:25:38 -04:00
|
|
|
get :index
|
|
|
|
|
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Hello Secret", @response.body, "Authentication failed when authorization scheme BASIC"
|
2015-08-11 18:25:38 -04:00
|
|
|
end
|
|
|
|
|
2009-01-18 14:21:34 -05:00
|
|
|
test "authentication request without credential" do
|
|
|
|
get :display
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
2015-05-01 14:28:00 -04:00
|
|
|
assert_equal "Authentication Failed\n", @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal 'Basic realm="SuperSecret"', @response.headers["WWW-Authenticate"]
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "authentication request with invalid credential" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = encode_credentials("pretty", "foo")
|
2009-01-18 14:21:34 -05:00
|
|
|
get :display
|
|
|
|
|
|
|
|
assert_response :unauthorized
|
2015-05-01 14:28:00 -04:00
|
|
|
assert_equal "Authentication Failed\n", @response.body
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal 'Basic realm="SuperSecret"', @response.headers["WWW-Authenticate"]
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "authentication request with valid credential" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = encode_credentials("pretty", "please")
|
2009-01-18 14:21:34 -05:00
|
|
|
get :display
|
|
|
|
|
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Definitely Maybe", @response.body
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
2012-10-27 10:03:18 -04:00
|
|
|
|
2015-08-09 16:52:22 -04:00
|
|
|
test "authentication request with valid credential special chars" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = encode_credentials('login!@#$%^&*()_+{}[];"\',./<>?`~ \n\r\t', 'pwd:!@#$%^&*()_+{}[];"\',./<>?`~ \n\r\t')
|
2015-08-09 16:52:22 -04:00
|
|
|
get :special_creds
|
|
|
|
|
|
|
|
assert_response :success
|
2016-08-06 12:54:50 -04:00
|
|
|
assert_equal "Only for special credentials", @response.body
|
2015-08-09 16:52:22 -04:00
|
|
|
end
|
|
|
|
|
2011-03-28 21:09:50 -04:00
|
|
|
test "authenticate with class method" do
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = encode_credentials("David", "Goliath")
|
2011-03-28 21:09:50 -04:00
|
|
|
get :search
|
|
|
|
assert_response :success
|
|
|
|
|
2016-08-06 12:54:50 -04:00
|
|
|
@request.env["HTTP_AUTHORIZATION"] = encode_credentials("David", "WRONG!")
|
2011-03-28 21:09:50 -04:00
|
|
|
get :search
|
|
|
|
assert_response :unauthorized
|
|
|
|
end
|
2009-01-18 14:21:34 -05:00
|
|
|
|
2013-07-07 09:39:16 -04:00
|
|
|
test "authentication request with wrong scheme" do
|
2016-08-06 12:54:50 -04:00
|
|
|
header = "Bearer " + encode_credentials("David", "Goliath").split(" ", 2)[1]
|
|
|
|
@request.env["HTTP_AUTHORIZATION"] = header
|
2013-07-07 09:39:16 -04:00
|
|
|
get :search
|
|
|
|
assert_response :unauthorized
|
|
|
|
end
|
|
|
|
|
2009-01-18 14:21:34 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def encode_credentials(username, password)
|
2012-01-01 12:57:55 -05:00
|
|
|
"Basic #{::Base64.encode64("#{username}:#{password}")}"
|
2009-01-18 14:21:34 -05:00
|
|
|
end
|
|
|
|
end
|