1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
use strict;
use warnings FATAL => 'all';
use Apache::Test;
use Apache::TestRequest;
use Apache::TestUtil qw(t_write_file);
use File::Spec;
# test the possibility of doing authz by user id or envvar in conjunction
# with the different AuthTypes
Apache::TestRequest::user_agent(keep_alive => 1);
my @headers = qw(WWW-Authenticate Authentication-Info Location);
my %do_tests = ( basic => 11,
digest => 11,
form => 16,
);
my $tests = 2; # AuthzSendForbiddenOnFailure tests
foreach my $t (keys %do_tests) {
$tests += $do_tests{$t};
}
plan tests => $tests,
need need_lwp,
need_module('mod_authn_core'),
need_module('mod_authz_core'),
need_module('mod_authn_file'),
need_module('mod_authz_host'),
need_min_apache_version('2.3.7');
foreach my $t (sort keys %do_tests) {
if (!have_module("mod_auth_$t")) {
skip("skipping mod_auth_$t tests") for (1 .. $do_tests{$t});
delete $do_tests{$t};
}
}
write_htpasswd();
# the auth type we are currently testing
my $type;
foreach my $t (qw/basic digest/) {
next unless exists $do_tests{$t};
$type = $t;
my $url = "/authz/$type/index.html";
{
my $response = GET $url;
ok($response->code,
401,
"$type: no user to authenticate and no env to authorize");
}
{
# bad pass
my $response = GET $url,
username => "u$type", password => 'foo';
ok($response->code,
401,
"$type: u$type:foo not found");
}
{
# authenticated
my $response = GET $url,
username => "u$type", password => "p$type";
ok($response->code,
200,
"$type: u$type:p$type found");
}
{
# authorized by env
my $response = GET $url, 'X-Allowed' => 'yes';
ok($response->code,
200,
"$type: authz by envvar");
check_headers($response, 200);
}
{
# authorized by env / with error
my $response = GET "$url.foo", 'X-Allowed' => 'yes';
ok($response->code,
404,
"$type: not found");
check_headers($response, 404);
}
}
#
# Form based authentication works a bit differently
#
if (exists $do_tests{form} && !have_module("mod_session_cookie")) {
skip("skipping mod_auth_form tests (mod_session_cookie required)")
for (1 .. $do_tests{form});
}
elsif (exists $do_tests{form}) {
$type = 'form';
my $url = "/authz/$type/index.html";
my $login_form_url='/authz/login.html';
my $login_url='/authz/form/dologin.html';
my @params = ( reset => 1, cookie_jar => {}, requests_redirectable => 0 );
Apache::TestRequest::user_agent(@params);
{
my $response = GET $url;
ok($response->code,
302,
"$type: access without user/env should redirect with 302");
my $loc = $response->header("Location");
if (defined $loc && $loc =~ m{^http://[^/]+(/.*)$}) {
$loc = $1;
}
ok($loc,
"/authz/login.html",
"form: login without user/env should redirect to login form");
}
{
Apache::TestRequest::user_agent(@params);
# bad pass
my $response = POST $login_url,
content => "httpd_username=uform&httpd_password=foo";
ok($response->code,
302,
"form: login with wrong passwd should redirect with 302");
my $loc = $response->header("Location");
if (defined $loc && $loc =~ m{^http://[^/]+(/.*)$}) {
$loc = $1;
}
ok($loc,
"/authz/login.html",
"form: login with wrong passwd should redirect to login form");
$response = GET $url;
ok($response->code,
302,
"$type: wrong passwd should not allow access");
}
{
# authenticated
Apache::TestRequest::user_agent(@params);
my $response = POST $login_url,
content => "httpd_username=uform&httpd_password=pform";
ok($response->code,
302,
"form: login with correct passwd should redirect with 302");
my $loc = $response->header("Location");
if (defined $loc && $loc =~ m{^http://[^/]+(/.*)$}) {
$loc = $1;
}
ok($1,
"/authz/form/",
"form: login with correct passwd should redirect to SuccessLocation");
$response = GET $url;
ok($response->code,
200,
"$type: correct passwd did not allow access");
}
{
# authorized by env
Apache::TestRequest::user_agent(@params);
my $response = GET $url, 'X-Allowed' => 'yes';
ok($response->code,
200,
"$type: authz by envvar");
check_headers($response, 200);
}
{
# authorized by env / with error
my $response = GET "$url.foo", 'X-Allowed' => 'yes';
ok($response->code,
404,
"$type: not found");
check_headers($response, 404);
}
}
#
# Test AuthzSendForbiddenOnFailure
#
if (have_min_apache_version("2.3.11")) {
foreach my $want (401, 403) {
my $response = GET "/authz/fail/$want",
username => "ubasic",
password => "pbasic";
my $got = $response->code;
ok($got, $want, "Expected code $want, got $got");
}
}
else {
skip "skipping tests with httpd <2.3.11" foreach (1..2);
}
#
# check that none of the authentication related headers exists
#
sub check_headers
{
my $response = shift;
my $code = shift;
foreach my $h (@headers) {
ok($response->header($h),
undef,
"$type: $code response should have no $h header");
}
}
#
# write out the htpasswd files
#
sub write_htpasswd
{
my $digest_file = File::Spec->catfile(Apache::Test::vars('serverroot'), 'realm2');
t_write_file($digest_file, << 'EOF' );
# udigest/pdigest
udigest:realm2:bccffb0d42943019acfbebf2039b8a3a
EOF
my $basic_file = File::Spec->catfile(Apache::Test::vars('serverroot'), 'basic1');
t_write_file($basic_file, << 'EOF' );
# ubasic:pbasic
ubasic:$apr1$opONH1Fj$dX0sZdZ0rRWEk0Wj8y.Qv1
EOF
my $form_file = File::Spec->catfile(Apache::Test::vars('serverroot'), 'form1');
t_write_file($form_file, << 'EOF' );
# uform:pform
uform:$apr1$BzhDZ03D$U598kbSXGy/R7OhYXu.JJ0
EOF
}
|