| 1 | package Apache::AuthCookie; | ||
| 2 | $Apache::AuthCookie::VERSION = '3.32'; | ||
| 3 | # ABSTRACT: Perl Authentication and Authorization via cookies | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use mod_perl qw(1.07 StackedHandlers MethodHandlers Authen Authz); | ||
| 9 | use Apache::Constants qw(:common M_GET FORBIDDEN OK REDIRECT); | ||
| 10 | use Apache::AuthCookie::Params; | ||
| 11 | use Apache::AuthCookie::Util qw(is_blank is_local_destination); | ||
| 12 | use Apache::Util qw(escape_uri); | ||
| 13 | use Apache::URI; | ||
| 14 | use Encode (); | ||
| 15 | |||
| 16 | |||
| 17 | sub recognize_user ($$) { | ||
| 18 | my ($self, $r) = @_; | ||
| 19 | |||
| 20 | # only check if user is not already set | ||
| 21 | return DECLINED unless is_blank($r->connection->user); | ||
| 22 | |||
| 23 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 24 | my ($auth_type, $auth_name) = ($r->auth_type, $r->auth_name); | ||
| 25 | |||
| 26 | return DECLINED if is_blank($auth_type) or is_blank($auth_name); | ||
| 27 | |||
| 28 | return DECLINED if is_blank($r->header_in('Cookie')); | ||
| 29 | |||
| 30 | my $cookie_name = $self->cookie_name($r); | ||
| 31 | |||
| 32 | my ($cookie) = $r->header_in('Cookie') =~ /$cookie_name=([^;]+)/; | ||
| 33 | $r->log_error("cookie $cookie_name is $cookie") if $debug >= 2; | ||
| 34 | return DECLINED unless $cookie; | ||
| 35 | |||
| 36 | my ($user, @args) = $auth_type->authen_ses_key($r, $cookie); | ||
| 37 | if (!is_blank($user) and scalar @args == 0) { | ||
| 38 | $r->log_error("user is $user") if $debug >= 2; | ||
| 39 | |||
| 40 | # if SessionTimeout is on, send new cookie with new Expires. | ||
| 41 | if (my $expires = $r->dir_config("${auth_name}SessionTimeout")) { | ||
| 42 | $self->send_cookie($cookie, { expires => $expires }); | ||
| 43 | } | ||
| 44 | |||
| 45 | $r->connection->user( $self->_encode($r, $user) ); | ||
| 46 | } | ||
| 47 | elsif (scalar @args > 0 and $auth_type->can('custom_errors')) { | ||
| 48 | return $auth_type->custom_errors($r, $user, @args); | ||
| 49 | } | ||
| 50 | |||
| 51 | return is_blank($user) ? DECLINED : OK; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub cookie_name { | ||
| 55 | my ($self, $r) = @_; | ||
| 56 | |||
| 57 | my $auth_type = $r->auth_type; | ||
| 58 | my $auth_name = $r->auth_name; | ||
| 59 | |||
| 60 | my $cookie_name = $r->dir_config("${auth_name}CookieName") | ||
| 61 | || "${auth_type}_${auth_name}"; | ||
| 62 | |||
| 63 | return $cookie_name; | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub encoding { | ||
| 68 | my ($self, $r) = @_; | ||
| 69 | |||
| 70 | my $auth_name = $r->auth_name; | ||
| 71 | |||
| 72 | return $r->dir_config("${auth_name}Encoding"); | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | sub requires_encoding { | ||
| 77 | my ($self, $r) = @_; | ||
| 78 | |||
| 79 | my $auth_name = $r->auth_name; | ||
| 80 | |||
| 81 | return $r->dir_config("${auth_name}RequiresEncoding"); | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub decoded_user { | ||
| 86 | my ($self, $r) = @_; | ||
| 87 | |||
| 88 | my $user = $r->connection->user; | ||
| 89 | |||
| 90 | if (is_blank($user)) { | ||
| 91 | return $user; | ||
| 92 | } | ||
| 93 | |||
| 94 | my $encoding = $self->encoding($r); | ||
| 95 | |||
| 96 | if (!is_blank($encoding)) { | ||
| 97 | $user = Encode::decode($encoding, $user); | ||
| 98 | } | ||
| 99 | |||
| 100 | return $user; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub decoded_requires { | ||
| 105 | my ($self, $r) = @_; | ||
| 106 | |||
| 107 | my $reqs = $r->requires or return; | ||
| 108 | my $encoding = $self->requires_encoding($r); | ||
| 109 | |||
| 110 | unless (is_blank($encoding)) { | ||
| 111 | for my $req (@$reqs) { | ||
| 112 | $$req{requirement} = Encode::decode($encoding, $$req{requirement}); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | return $reqs; | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub handle_cache { | ||
| 121 | my $self = shift; | ||
| 122 | |||
| 123 | my $r = Apache->request; | ||
| 124 | |||
| 125 | my $auth_name = $r->auth_name; | ||
| 126 | return unless $auth_name; | ||
| 127 | |||
| 128 | unless ($r->dir_config("${auth_name}Cache")) { | ||
| 129 | $r->no_cache(1); | ||
| 130 | $r->err_header_out(Pragma => 'no-cache'); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | sub remove_cookie { | ||
| 136 | my $self = shift; | ||
| 137 | |||
| 138 | my $r = Apache->request; | ||
| 139 | |||
| 140 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 141 | |||
| 142 | my $cookie_name = $self->cookie_name($r); | ||
| 143 | |||
| 144 | my $str = $self->cookie_string( | ||
| 145 | request => $r, | ||
| 146 | key => $cookie_name, | ||
| 147 | value => '', | ||
| 148 | expires => 'Mon, 21-May-1971 00:00:00 GMT' | ||
| 149 | ); | ||
| 150 | |||
| 151 | $r->err_headers_out->add("Set-Cookie" => "$str"); | ||
| 152 | |||
| 153 | $r->log_error("removed cookie $cookie_name") if $debug >= 2; | ||
| 154 | } | ||
| 155 | |||
| 156 | # convert current request to GET | ||
| 157 | sub _convert_to_get { | ||
| 158 | my ($self, $r) = @_; | ||
| 159 | |||
| 160 | return unless $r->method eq 'POST'; | ||
| 161 | |||
| 162 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 163 | |||
| 164 | $r->log_error("Converting POST -> GET") if $debug >= 2; | ||
| 165 | |||
| 166 | my $args = $self->params($r); | ||
| 167 | |||
| 168 | my @pairs = (); | ||
| 169 | |||
| 170 | for my $name ($args->param) { | ||
| 171 | # we dont want to copy login data, only extra data | ||
| 172 | next if $name eq 'destination' | ||
| 173 | or $name =~ /^credential_\d+$/; | ||
| 174 | |||
| 175 | for my $v ($args->param($name)) { | ||
| 176 | push @pairs, escape_uri($name) . '=' . escape_uri($v); | ||
| 177 | } | ||
| 178 | } | ||
| 179 | |||
| 180 | $r->args(join '&', @pairs) if scalar(@pairs) > 0; | ||
| 181 | |||
| 182 | $r->method('GET'); | ||
| 183 | $r->method_number(M_GET); | ||
| 184 | $r->headers_in->unset('Content-Length'); | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | sub params { | ||
| 189 | my ($self, $r) = @_; | ||
| 190 | |||
| 191 | return Apache::AuthCookie::Params->new($r); | ||
| 192 | } | ||
| 193 | |||
| 194 | |||
| 195 | sub login ($$) { | ||
| 196 | my ($self, $r) = @_; | ||
| 197 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 198 | |||
| 199 | my ($auth_type, $auth_name) = ($r->auth_type, $r->auth_name); | ||
| 200 | |||
| 201 | my $params = $self->params($r); | ||
| 202 | |||
| 203 | $self->_convert_to_get($r) if $r->method eq 'POST'; | ||
| 204 | |||
| 205 | my $destination = $params->param('destination'); | ||
| 206 | my $default_destination = $r->dir_config("${auth_name}DefaultDestination"); | ||
| 207 | |||
| 208 | if (is_blank($destination)) { | ||
| 209 | if (!is_blank($default_destination)) { | ||
| 210 | $destination = $default_destination; | ||
| 211 | $r->log_error("destination set to $destination"); | ||
| 212 | } | ||
| 213 | else { | ||
| 214 | $r->log_error("No key 'destination' found in form data"); | ||
| 215 | $r->subprocess_env('AuthCookieReason', 'no_cookie'); | ||
| 216 | return $auth_type->login_form; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | if ($r->dir_config("${auth_name}EnforceLocalDestination")) { | ||
| 221 | my $current_url = Apache::URI->parse($r)->unparse; | ||
| 222 | unless (is_local_destination($destination, $current_url)) { | ||
| 223 | $r->log_error("non-local destination $destination detected for uri ",$r->uri); | ||
| 224 | |||
| 225 | if (is_local_destination($default_destination, $current_url)) { | ||
| 226 | $destination = $default_destination; | ||
| 227 | $r->log_error("destination changed to $destination"); | ||
| 228 | } | ||
| 229 | else { | ||
| 230 | $r->log_error("Returning login form: non local destination: $destination"); | ||
| 231 | $r->subprocess_env('AuthCookieReason', 'no_cookie'); | ||
| 232 | return $auth_type->login_form($r); | ||
| 233 | } | ||
| 234 | } | ||
| 235 | } | ||
| 236 | |||
| 237 | # Get the credentials from the data posted by the client | ||
| 238 | my @credentials; | ||
| 239 | for (my $i = 0 ; defined $params->param("credential_$i") ; $i++) { | ||
| 240 | my $key = "credential_$i"; | ||
| 241 | my $val = $params->param("credential_$i"); | ||
| 242 | $r->log_error("$key $val") if $debug >= 2; | ||
| 243 | push @credentials, $val; | ||
| 244 | } | ||
| 245 | |||
| 246 | # save creds in pnotes in case login form script wants to use them. | ||
| 247 | $r->pnotes("${auth_name}Creds", \@credentials); | ||
| 248 | |||
| 249 | # Exchange the credentials for a session key. | ||
| 250 | my $ses_key = $self->authen_cred($r, @credentials); | ||
| 251 | unless ($ses_key) { | ||
| 252 | $r->log_error("Bad credentials") if $debug >= 2; | ||
| 253 | $r->subprocess_env('AuthCookieReason', 'bad_credentials'); | ||
| 254 | $r->uri($self->untaint_destination($destination)); | ||
| 255 | return $auth_type->login_form; | ||
| 256 | } | ||
| 257 | |||
| 258 | if ($debug >= 2) { | ||
| 259 | if (defined $ses_key) { | ||
| 260 | $r->log_error("ses_key $ses_key"); | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | $r->log_error("ses_key undefined"); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | |||
| 267 | $self->send_cookie($ses_key); | ||
| 268 | |||
| 269 | $self->handle_cache; | ||
| 270 | |||
| 271 | $r->header_out(Location => $self->untaint_destination($destination)); | ||
| 272 | |||
| 273 | return REDIRECT; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | sub untaint_destination { | ||
| 278 | my ($self, $dest) = @_; | ||
| 279 | |||
| 280 | return Apache::AuthCookie::Util::escape_destination($dest); | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | sub logout($$) { | ||
| 285 | my ($self, $r) = @_; | ||
| 286 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 287 | |||
| 288 | $self->remove_cookie; | ||
| 289 | |||
| 290 | $self->handle_cache; | ||
| 291 | |||
| 292 | #my %args = $r->args; | ||
| 293 | #if (exists $args{'redirect'}) { | ||
| 294 | # $r->err_header_out("Location" => $args{'redirect'}); | ||
| 295 | # return REDIRECT; | ||
| 296 | #} else { | ||
| 297 | # $r->status(200); | ||
| 298 | # return OK; | ||
| 299 | #} | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | sub authenticate ($$) { | ||
| 304 | my ($auth_type, $r) = @_; | ||
| 305 | my $auth_user; | ||
| 306 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 307 | |||
| 308 | $r->log_error("auth_type " . $auth_type) if ($debug >= 3); | ||
| 309 | |||
| 310 | unless ($r->is_initial_req) { | ||
| 311 | if (defined $r->prev) { | ||
| 312 | # we are in a subrequest. Just copy user from previous request. | ||
| 313 | # encoding would have been handled in prev req, so do not encode here. | ||
| 314 | $r->connection->user($r->prev->connection->user); | ||
| 315 | } | ||
| 316 | return OK; | ||
| 317 | } | ||
| 318 | |||
| 319 | if ($r->auth_type ne $auth_type) { | ||
| 320 | # This location requires authentication because we are being called, | ||
| 321 | # but we don't handle this AuthType. | ||
| 322 | $r->log_error("AuthType mismatch: $auth_type =/= " . $r->auth_type) | ||
| 323 | if $debug >= 3; | ||
| 324 | return DECLINED; | ||
| 325 | } | ||
| 326 | |||
| 327 | # Ok, the AuthType is $auth_type which we handle, what's the authentication | ||
| 328 | # realm's name? | ||
| 329 | my $auth_name = $r->auth_name; | ||
| 330 | $r->log_error("auth_name " . $auth_name) if $debug >= 2; | ||
| 331 | unless ($auth_name) { | ||
| 332 | $r->log_reason("AuthName not set, AuthType=$auth_type", $r->uri); | ||
| 333 | return SERVER_ERROR; | ||
| 334 | } | ||
| 335 | |||
| 336 | # Get the Cookie header. If there is a session key for this realm, strip | ||
| 337 | # off everything but the value of the cookie. | ||
| 338 | my $cookie_name = $auth_type->cookie_name($r); | ||
| 339 | my ($ses_key_cookie) = | ||
| 340 | ($r->header_in("Cookie") || "") =~ /$cookie_name=([^;]+)/; | ||
| 341 | $ses_key_cookie = "" unless defined($ses_key_cookie); | ||
| 342 | |||
| 343 | $r->log_error("ses_key_cookie " . $ses_key_cookie) if ($debug >= 1); | ||
| 344 | $r->log_error("uri " . $r->uri) if ($debug >= 2); | ||
| 345 | |||
| 346 | if ($ses_key_cookie) { | ||
| 347 | my ($auth_user, @args) = | ||
| 348 | $auth_type->authen_ses_key($r, $ses_key_cookie); | ||
| 349 | |||
| 350 | if (!is_blank($auth_user) and scalar @args == 0) { | ||
| 351 | |||
| 352 | # We have a valid session key, so we return with an OK value. | ||
| 353 | # Tell the rest of Apache what the authentication method and | ||
| 354 | # user is. | ||
| 355 | |||
| 356 | $r->connection->auth_type($auth_type); | ||
| 357 | $r->connection->user( $auth_type->_encode($r, $auth_user) ); | ||
| 358 | $r->log_error("user authenticated as $auth_user") if $debug >= 1; | ||
| 359 | |||
| 360 | # if SessionTimeout is on, send cookie with new expires | ||
| 361 | if (my $expires = $r->dir_config("${auth_name}SessionTimeout")) { | ||
| 362 | $auth_type->send_cookie($ses_key_cookie, | ||
| 363 | { expires => $expires }); | ||
| 364 | } | ||
| 365 | |||
| 366 | return OK; | ||
| 367 | } | ||
| 368 | elsif (scalar @args > 0 and $auth_type->can('custom_errors')) { | ||
| 369 | return $auth_type->custom_errors($r, $auth_user, @args); | ||
| 370 | } | ||
| 371 | else { | ||
| 372 | |||
| 373 | # There was a session key set, but it's invalid for some reason. So, | ||
| 374 | # remove it from the client now so when the credential data is posted | ||
| 375 | # we act just like it's a new session starting. | ||
| 376 | $auth_type->remove_cookie; | ||
| 377 | $r->subprocess_env('AuthCookieReason', 'bad_cookie'); | ||
| 378 | } | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | $r->subprocess_env('AuthCookieReason', 'no_cookie'); | ||
| 382 | } | ||
| 383 | |||
| 384 | # They aren't authenticated, and they tried to get a protected | ||
| 385 | # document. Send them the authen form. | ||
| 386 | return $auth_type->login_form; | ||
| 387 | } | ||
| 388 | |||
| 389 | |||
| 390 | sub login_form { | ||
| 391 | my $self = shift; | ||
| 392 | |||
| 393 | my $r = Apache->request or die "no request"; | ||
| 394 | my $auth_name = $r->auth_name; | ||
| 395 | |||
| 396 | $self->_convert_to_get($r) if $r->method eq 'POST'; | ||
| 397 | |||
| 398 | # There should be a PerlSetVar directive that gives us the URI of | ||
| 399 | # the script to execute for the login form. | ||
| 400 | |||
| 401 | my $authen_script; | ||
| 402 | unless ($authen_script = $r->dir_config($auth_name . "LoginScript")) { | ||
| 403 | $r->log_reason("PerlSetVar '${auth_name}LoginScript' not set", $r->uri); | ||
| 404 | return SERVER_ERROR; | ||
| 405 | } | ||
| 406 | |||
| 407 | #$r->log_error("Redirecting to $authen_script"); | ||
| 408 | my $status = $self->login_form_status($r); | ||
| 409 | $status = FORBIDDEN unless defined $status; | ||
| 410 | |||
| 411 | if ($status == OK) { | ||
| 412 | # custom_response doesn't work for OK, DONE, or DECLINED in apache 1.x | ||
| 413 | $r->internal_redirect($authen_script); | ||
| 414 | } | ||
| 415 | else { | ||
| 416 | $r->custom_response($status, $authen_script); | ||
| 417 | } | ||
| 418 | |||
| 419 | return $status; | ||
| 420 | } | ||
| 421 | |||
| 422 | |||
| 423 | sub login_form_status { | ||
| 424 | my ($self, $r) = @_; | ||
| 425 | |||
| 426 | my $ua = $r->headers_in->get('User-Agent') | ||
| 427 | or return FORBIDDEN; | ||
| 428 | |||
| 429 | if (Apache::AuthCookie::Util::understands_forbidden_response($ua)) { | ||
| 430 | return FORBIDDEN; | ||
| 431 | } | ||
| 432 | else { | ||
| 433 | return OK; | ||
| 434 | } | ||
| 435 | } | ||
| 436 | |||
| 437 | sub satisfy_is_valid { | ||
| 438 | my ($auth_type, $r, $satisfy) = @_; | ||
| 439 | $satisfy = lc $satisfy; | ||
| 440 | |||
| 441 | if ($satisfy eq 'any' or $satisfy eq 'all') { | ||
| 442 | return 1; | ||
| 443 | } | ||
| 444 | else { | ||
| 445 | my $auth_name = $r->auth_name; | ||
| 446 | $r->log_reason("PerlSetVar ${auth_name}Satisfy $satisfy invalid", | ||
| 447 | $r->uri); | ||
| 448 | return 0; | ||
| 449 | } | ||
| 450 | } | ||
| 451 | |||
| 452 | |||
| 453 | sub get_satisfy { | ||
| 454 | my ($auth_type, $r) = @_; | ||
| 455 | |||
| 456 | my $auth_name = $r->auth_name; | ||
| 457 | |||
| 458 | return lc $r->dir_config("${auth_name}Satisfy") || 'all'; | ||
| 459 | } | ||
| 460 | |||
| 461 | |||
| 462 | sub authorize ($$) { | ||
| 463 | my ($auth_type, $r) = @_; | ||
| 464 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 465 | |||
| 466 | $r->log_error('authorize() for ' . $r->uri()) if ($debug >= 3); | ||
| 467 | return OK unless $r->is_initial_req; #only the first internal request | ||
| 468 | |||
| 469 | if ($r->auth_type ne $auth_type) { | ||
| 470 | $r->log_error($auth_type . " auth type is " . $r->auth_type) | ||
| 471 | if ($debug >= 3); | ||
| 472 | return DECLINED; | ||
| 473 | } | ||
| 474 | |||
| 475 | my $reqs_arr = $auth_type->decoded_requires($r) or return DECLINED; | ||
| 476 | |||
| 477 | my $user = $auth_type->decoded_user($r); | ||
| 478 | if (is_blank($user)) { | ||
| 479 | # authentication failed | ||
| 480 | $r->log_reason("No user authenticated", $r->uri); | ||
| 481 | return FORBIDDEN; | ||
| 482 | } | ||
| 483 | |||
| 484 | my $satisfy = $auth_type->get_satisfy($r); | ||
| 485 | return SERVER_ERROR unless $auth_type->satisfy_is_valid($r, $satisfy); | ||
| 486 | my $satisfy_all = $satisfy eq 'all'; | ||
| 487 | |||
| 488 | my ($forbidden); | ||
| 489 | foreach my $req (@$reqs_arr) { | ||
| 490 | my ($requirement, $args) = split /\s+/, $req->{requirement}, 2; | ||
| 491 | $args = '' unless defined $args; | ||
| 492 | $r->log_error("requirement := $requirement, $args") if $debug >= 2; | ||
| 493 | |||
| 494 | if (lc($requirement) eq 'valid-user') { | ||
| 495 | if ($satisfy_all) { | ||
| 496 | next; | ||
| 497 | } | ||
| 498 | else { | ||
| 499 | return OK; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | if ($requirement eq 'user') { | ||
| 504 | if ($args =~ m/\b$user\b/) { | ||
| 505 | next if $satisfy_all; | ||
| 506 | return OK; # satisfy any | ||
| 507 | } | ||
| 508 | |||
| 509 | $forbidden = 1; | ||
| 510 | next; | ||
| 511 | } | ||
| 512 | |||
| 513 | # Call a custom method | ||
| 514 | my $ret_val = $auth_type->$requirement($r, $args); | ||
| 515 | $r->log_error("$auth_type->$requirement returned $ret_val") | ||
| 516 | if $debug >= 3; | ||
| 517 | if ($ret_val == OK) { | ||
| 518 | next if $satisfy_all; | ||
| 519 | return OK; # satisfy any | ||
| 520 | } | ||
| 521 | |||
| 522 | # Nothing succeeded, deny access to this user. | ||
| 523 | $forbidden = 1; | ||
| 524 | } | ||
| 525 | |||
| 526 | return $forbidden ? FORBIDDEN : OK; | ||
| 527 | } | ||
| 528 | |||
| 529 | |||
| 530 | sub send_cookie { | ||
| 531 | my ($self, $ses_key, $cookie_args) = @_; | ||
| 532 | my $r = Apache->request(); | ||
| 533 | |||
| 534 | $cookie_args = {} unless defined $cookie_args; | ||
| 535 | |||
| 536 | my $cookie_name = $self->cookie_name($r); | ||
| 537 | |||
| 538 | my $cookie = $self->cookie_string( | ||
| 539 | request => $r, | ||
| 540 | key => $cookie_name, | ||
| 541 | value => $ses_key, | ||
| 542 | %$cookie_args | ||
| 543 | ); | ||
| 544 | |||
| 545 | # add P3P header if user has configured it. | ||
| 546 | $self->send_p3p($r); | ||
| 547 | |||
| 548 | $r->err_headers_out->add("Set-Cookie" => $cookie); | ||
| 549 | } | ||
| 550 | |||
| 551 | |||
| 552 | sub send_p3p { | ||
| 553 | my ($self, $r) = @_; | ||
| 554 | |||
| 555 | my $auth_name = $r->auth_name; | ||
| 556 | |||
| 557 | if (my $p3p = $r->dir_config("${auth_name}P3P")) { | ||
| 558 | $r->err_header_out(P3P => $p3p); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | |||
| 562 | |||
| 563 | sub cookie_string { | ||
| 564 | my $self = shift; | ||
| 565 | |||
| 566 | # if passed 3 args, we have old-style call. | ||
| 567 | if (scalar(@_) == 3) { | ||
| 568 | carp "deprecated old style call to " | ||
| 569 | . __PACKAGE__ | ||
| 570 | . "::cookie_string()"; | ||
| 571 | my ($r, $key, $value) = @_; | ||
| 572 | return $self->cookie_string(request => $r, key => $key, | ||
| 573 | value => $value); | ||
| 574 | } | ||
| 575 | |||
| 576 | # otherwise assume named parameters. | ||
| 577 | my %p = @_; | ||
| 578 | for (qw/request key/) { | ||
| 579 | croak "missing required parameter $_" unless defined $p{$_}; | ||
| 580 | } | ||
| 581 | |||
| 582 | # its okay if value is undef here. | ||
| 583 | |||
| 584 | my $r = $p{request}; | ||
| 585 | |||
| 586 | $p{value} = '' unless defined $p{value}; | ||
| 587 | |||
| 588 | my $string = sprintf '%s=%s', @p{ 'key', 'value' }; | ||
| 589 | |||
| 590 | my $auth_name = $r->auth_name; | ||
| 591 | |||
| 592 | if (my $expires = $p{expires} || $r->dir_config("${auth_name}Expires")) { | ||
| 593 | $expires = Apache::AuthCookie::Util::expires($expires); | ||
| 594 | $string .= "; expires=$expires"; | ||
| 595 | } | ||
| 596 | |||
| 597 | $string .= '; path=' . ($self->get_cookie_path($r) || '/'); | ||
| 598 | |||
| 599 | #$r->log_error("Attribute ${auth_name}Path not set") unless $path; | ||
| 600 | |||
| 601 | if (my $domain = $r->dir_config("${auth_name}Domain")) { | ||
| 602 | $string .= "; domain=$domain"; | ||
| 603 | } | ||
| 604 | |||
| 605 | if ($r->dir_config("${auth_name}Secure")) { | ||
| 606 | $string .= '; secure'; | ||
| 607 | } | ||
| 608 | |||
| 609 | # HttpOnly is an MS extension. See | ||
| 610 | # http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp | ||
| 611 | if ($r->dir_config("${auth_name}HttpOnly")) { | ||
| 612 | $string .= '; HttpOnly'; | ||
| 613 | } | ||
| 614 | |||
| 615 | # SameSite is an anti-CSRF cookie property. See | ||
| 616 | # https://www.owasp.org/index.php/SameSite | ||
| 617 | if (my $samesite = $r->dir_config("${auth_name}SameSite")) { | ||
| 618 | if ($samesite =~ /\A(strict|lax)\z/i) { | ||
| 619 | $samesite = lc($1); | ||
| 620 | $string .= "; SameSite=$samesite"; | ||
| 621 | } | ||
| 622 | } | ||
| 623 | |||
| 624 | return $string; | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | sub key { | ||
| 629 | my $self = shift; | ||
| 630 | my $r = Apache->request; | ||
| 631 | |||
| 632 | my $allcook = ($r->header_in("Cookie") || ""); | ||
| 633 | my $cookie_name = $self->cookie_name($r); | ||
| 634 | return ($allcook =~ /(?:^|\s)$cookie_name=([^;]*)/)[0]; | ||
| 635 | } | ||
| 636 | |||
| 637 | |||
| 638 | sub get_cookie_path { | ||
| 639 | my $self = shift; | ||
| 640 | my $r = shift || Apache->request; | ||
| 641 | |||
| 642 | my $auth_name = $r->auth_name; | ||
| 643 | |||
| 644 | return $r->dir_config("${auth_name}Path"); | ||
| 645 | } | ||
| 646 | |||
| 647 | sub _encode { | ||
| 648 | my ($self, $r, $value) = @_; | ||
| 649 | |||
| 650 | my $encoding = $self->encoding($r); | ||
| 651 | |||
| 652 | if (is_blank($encoding)) { | ||
| 653 | return $value; | ||
| 654 | } | ||
| 655 | else { | ||
| 656 | return Encode::encode($encoding, $value); | ||
| 657 | } | ||
| 658 | } | ||
| 659 | |||
| 660 | 1; | ||
| 661 | |||
| 662 | =pod | ||
| 663 | |||
| 664 | =encoding UTF-8 | ||
| 665 | |||
| 666 | =head1 NAME | ||
| 667 | |||
| 668 | Apache::AuthCookie - Perl Authentication and Authorization via cookies | ||
| 669 | |||
| 670 | =head1 VERSION | ||
| 671 | |||
| 672 | version 3.32 | ||
| 673 | |||
| 674 | =head1 SYNOPSIS | ||
| 675 | |||
| 676 | Make sure your mod_perl is at least 1.24, with StackedHandlers, | ||
| 677 | MethodHandlers, Authen, and Authz compiled in. | ||
| 678 | |||
| 679 | # In httpd.conf or .htaccess: | ||
| 680 | PerlModule Sample::Apache::AuthCookieHandler | ||
| 681 | PerlSetVar WhatEverPath / | ||
| 682 | PerlSetVar WhatEverLoginScript /login.pl | ||
| 683 | |||
| 684 | # use to alter how "require" directives are matched. Can be "Any" or "All". | ||
| 685 | # If its "Any", then you must only match Any of the "require" directives. If | ||
| 686 | # its "All", then you must match All of the require directives. | ||
| 687 | # | ||
| 688 | # Default: All | ||
| 689 | PerlSetVar WhatEverSatisfy Any | ||
| 690 | |||
| 691 | # The following line is optional - it allows you to set the domain | ||
| 692 | # scope of your cookie. Default is the current domain. | ||
| 693 | PerlSetVar WhatEverDomain .yourdomain.com | ||
| 694 | |||
| 695 | # Use this to only send over a secure connection | ||
| 696 | PerlSetVar WhatEverSecure 1 | ||
| 697 | |||
| 698 | # Use this if you want user session cookies to expire if the user | ||
| 699 | # doesn't request a auth-required or recognize_user page for some | ||
| 700 | # time period. If set, a new cookie (with updated expire time) | ||
| 701 | # is set on every request. | ||
| 702 | PerlSetVar WhatEverSessionTimeout +30m | ||
| 703 | |||
| 704 | # to enable the HttpOnly cookie property, use HttpOnly. | ||
| 705 | # this is an MS extension. See | ||
| 706 | # http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp | ||
| 707 | PerlSetVar WhatEverHttpOnly 1 | ||
| 708 | |||
| 709 | # Usually documents are uncached - turn off here | ||
| 710 | PerlSetVar WhatEverCache 1 | ||
| 711 | |||
| 712 | # Use this to make your cookies persistent (+2 hours here) | ||
| 713 | PerlSetVar WhatEverExpires +2h | ||
| 714 | |||
| 715 | # Use to make AuthCookie send a P3P header with the cookie | ||
| 716 | # see http://www.w3.org/P3P/ for details about what the value | ||
| 717 | # of this should be | ||
| 718 | PerlSetVar WhatEverP3P "CP=\"...\"" | ||
| 719 | |||
| 720 | # optional: enforce that the destination argument from the login form is | ||
| 721 | # local to the server | ||
| 722 | PerlSetVar WhatEverEnforceLocalDestination 1 | ||
| 723 | |||
| 724 | # optional: specify a default destination for when the destination argument | ||
| 725 | # of the login form is invalid or unspecified | ||
| 726 | PerlSetVar WhatEverDefaultDestination /protected/user/ | ||
| 727 | |||
| 728 | # These documents require user to be logged in. | ||
| 729 | <Location /protected> | ||
| 730 | AuthType Sample::Apache::AuthCookieHandler | ||
| 731 | AuthName WhatEver | ||
| 732 | PerlAuthenHandler Sample::Apache::AuthCookieHandler->authenticate | ||
| 733 | PerlAuthzHandler Sample::Apache::AuthCookieHandler->authorize | ||
| 734 | require valid-user | ||
| 735 | </Location> | ||
| 736 | |||
| 737 | # These documents don't require logging in, but allow it. | ||
| 738 | <FilesMatch "\.ok$"> | ||
| 739 | AuthType Sample::Apache::AuthCookieHandler | ||
| 740 | AuthName WhatEver | ||
| 741 | PerlFixupHandler Sample::Apache::AuthCookieHandler->recognize_user | ||
| 742 | </FilesMatch> | ||
| 743 | |||
| 744 | # This is the action of the login.pl script above. | ||
| 745 | <Files LOGIN> | ||
| 746 | AuthType Sample::Apache::AuthCookieHandler | ||
| 747 | AuthName WhatEver | ||
| 748 | SetHandler perl-script | ||
| 749 | PerlHandler Sample::Apache::AuthCookieHandler->login | ||
| 750 | </Files> | ||
| 751 | |||
| 752 | =head1 DESCRIPTION | ||
| 753 | |||
| 754 | B<Apache::AuthCookie> allows you to intercept a user's first | ||
| 755 | unauthenticated access to a protected document. The user will be | ||
| 756 | presented with a custom form where they can enter authentication | ||
| 757 | credentials. The credentials are posted to the server where AuthCookie | ||
| 758 | verifies them and returns a session key. | ||
| 759 | |||
| 760 | The session key is returned to the user's browser as a cookie. As a | ||
| 761 | cookie, the browser will pass the session key on every subsequent | ||
| 762 | accesses. AuthCookie will verify the session key and re-authenticate | ||
| 763 | the user. | ||
| 764 | |||
| 765 | All you have to do is write a custom module that inherits from | ||
| 766 | AuthCookie. Your module is a class which implements two methods: | ||
| 767 | |||
| 768 | =over 4 | ||
| 769 | |||
| 770 | =item C<authen_cred()> | ||
| 771 | |||
| 772 | Verify the user-supplied credentials and return a session key. The | ||
| 773 | session key can be any string - often you'll use some string | ||
| 774 | containing username, timeout info, and any other information you need | ||
| 775 | to determine access to documents, and append a one-way hash of those | ||
| 776 | values together with some secret key. | ||
| 777 | |||
| 778 | =item C<authen_ses_key()> | ||
| 779 | |||
| 780 | Verify the session key (previously generated by C<authen_cred()>, | ||
| 781 | possibly during a previous request) and return the user ID. This user | ||
| 782 | ID will be fed to C<$r-E<gt>connection-E<gt>user()> to set Apache's | ||
| 783 | idea of who's logged in. | ||
| 784 | |||
| 785 | =back | ||
| 786 | |||
| 787 | By using AuthCookie versus Apache's built-in AuthBasic you can design | ||
| 788 | your own authentication system. There are several benefits. | ||
| 789 | |||
| 790 | =over 4 | ||
| 791 | |||
| 792 | =item 1. | ||
| 793 | |||
| 794 | The client doesn't *have* to pass the user credentials on every | ||
| 795 | subsequent access. If you're using passwords, this means that the | ||
| 796 | password can be sent on the first request only, and subsequent | ||
| 797 | requests don't need to send this (potentially sensitive) information. | ||
| 798 | This is known as "ticket-based" authentication. | ||
| 799 | |||
| 800 | =item 2. | ||
| 801 | |||
| 802 | When you determine that the client should stop using the | ||
| 803 | credentials/session key, the server can tell the client to delete the | ||
| 804 | cookie. Letting users "log out" is a notoriously impossible-to-solve | ||
| 805 | problem of AuthBasic. | ||
| 806 | |||
| 807 | =item 3. | ||
| 808 | |||
| 809 | AuthBasic dialog boxes are ugly. You can design your own HTML login | ||
| 810 | forms when you use AuthCookie. | ||
| 811 | |||
| 812 | =item 4. | ||
| 813 | |||
| 814 | You can specify the domain of a cookie using PerlSetVar commands. For | ||
| 815 | instance, if your AuthName is C<WhatEver>, you can put the command | ||
| 816 | |||
| 817 | PerlSetVar WhatEverDomain .yourhost.com | ||
| 818 | |||
| 819 | into your server setup file and your access cookies will span all | ||
| 820 | hosts ending in C<.yourhost.com>. | ||
| 821 | |||
| 822 | =item 5. | ||
| 823 | |||
| 824 | You can optionally specify the name of your cookie using the C<CookieName> | ||
| 825 | directive. For instance, if your AuthName is C<WhatEver>, you can put the | ||
| 826 | command | ||
| 827 | |||
| 828 | PerlSetVar WhatEverCookieName MyCustomName | ||
| 829 | |||
| 830 | into your server setup file and your cookies for this AuthCookie realm will be | ||
| 831 | named MyCustomName. Default is AuthType_AuthName. | ||
| 832 | |||
| 833 | =item 6. | ||
| 834 | |||
| 835 | By default users must satisfy ALL of the C<require> directives. If you | ||
| 836 | want authentication to succeed if ANY C<require> directives are met, use the | ||
| 837 | C<Satisfy> directive. For instance, if your AuthName is C<WhatEver>, you can | ||
| 838 | put the command | ||
| 839 | |||
| 840 | PerlSetVar WhatEverSatisfy Any | ||
| 841 | |||
| 842 | into your server startup file and authentication for this realm will succeed if | ||
| 843 | ANY of the C<require> directives are met. | ||
| 844 | |||
| 845 | =back | ||
| 846 | |||
| 847 | This is the flow of the authentication handler, less the details of the | ||
| 848 | redirects. Two REDIRECT's are used to keep the client from displaying | ||
| 849 | the user's credentials in the Location field. They don't really change | ||
| 850 | AuthCookie's model, but they do add another round-trip request to the | ||
| 851 | client. | ||
| 852 | |||
| 853 | (-----------------------) +---------------------------------+ | ||
| 854 | ( Request a protected ) | AuthCookie sets custom error | | ||
| 855 | ( page, but user hasn't )---->| document and returns | | ||
| 856 | ( authenticated (no ) | FORBIDDEN. Apache abandons | | ||
| 857 | ( session key cookie) ) | current request and creates sub | | ||
| 858 | (-----------------------) | request for the error document. |<-+ | ||
| 859 | | Error document is a script that | | | ||
| 860 | | generates a form where the user | | | ||
| 861 | return | enters authentication | | | ||
| 862 | ^------------------->| credentials (login & password). | | | ||
| 863 | / \ False +---------------------------------+ | | ||
| 864 | / \ | | | ||
| 865 | / \ | | | ||
| 866 | / \ V | | ||
| 867 | / \ +---------------------------------+ | | ||
| 868 | / Pass \ | User's client submits this form | | | ||
| 869 | / user's \ | to the LOGIN URL, which calls | | | ||
| 870 | | credentials |<------------| AuthCookie->login(). | | | ||
| 871 | \ to / +---------------------------------+ | | ||
| 872 | \authen_cred/ | | ||
| 873 | \ function/ | | ||
| 874 | \ / | | ||
| 875 | \ / | | ||
| 876 | \ / +------------------------------------+ | | ||
| 877 | \ / return | Authen cred returns a session | +--+ | ||
| 878 | V------------->| key which is opaque to AuthCookie.*| | | ||
| 879 | True +------------------------------------+ | | ||
| 880 | | | | ||
| 881 | +--------------------+ | +---------------+ | ||
| 882 | | | | | If we had a | | ||
| 883 | V | V | cookie, add | | ||
| 884 | +----------------------------+ r | ^ | a Set-Cookie | | ||
| 885 | | If we didn't have a session| e |T / \ | header to | | ||
| 886 | | key cookie, add a | t |r / \ | override the | | ||
| 887 | | Set-Cookie header with this| u |u / \ | invalid cookie| | ||
| 888 | | session key. Client then | r |e / \ +---------------+ | ||
| 889 | | returns session key with | n | / pass \ ^ | ||
| 890 | | successive requests | | / session \ | | ||
| 891 | +----------------------------+ | / key to \ return | | ||
| 892 | | +-| authen_ses_key|------------+ | ||
| 893 | V \ / False | ||
| 894 | +-----------------------------------+ \ / | ||
| 895 | | Tell Apache to set Expires header,| \ / | ||
| 896 | | set user to user ID returned by | \ / | ||
| 897 | | authen_ses_key, set authentication| \ / | ||
| 898 | | to our type (e.g. AuthCookie). | \ / | ||
| 899 | +-----------------------------------+ \ / | ||
| 900 | V | ||
| 901 | (---------------------) ^ | ||
| 902 | ( Request a protected ) | | ||
| 903 | ( page, user has a )--------------+ | ||
| 904 | ( session key cookie ) | ||
| 905 | (---------------------) | ||
| 906 | |||
| 907 | |||
| 908 | * The session key that the client gets can be anything you want. For | ||
| 909 | example, encrypted information about the user, a hash of the | ||
| 910 | username and password (similar in function to Digest | ||
| 911 | authentication), or the user name and password in plain text | ||
| 912 | (similar in function to HTTP Basic authentication). | ||
| 913 | |||
| 914 | The only requirement is that the authen_ses_key function that you | ||
| 915 | create must be able to determine if this session_key is valid and | ||
| 916 | map it back to the originally authenticated user ID. | ||
| 917 | |||
| 918 | =head1 METHODS | ||
| 919 | |||
| 920 | =head2 authen_cred($r, @credentials) | ||
| 921 | |||
| 922 | You must define this method yourself in your subclass of | ||
| 923 | C<Apache::AuthCookie>. Its job is to create the session key that will | ||
| 924 | be preserved in the user's cookie. The arguments passed to it are: | ||
| 925 | |||
| 926 | sub authen_cred ($$\@) { | ||
| 927 | my $self = shift; # Package name (same as AuthName directive) | ||
| 928 | my $r = shift; # Apache request object | ||
| 929 | my @cred = @_; # Credentials from login form | ||
| 930 | |||
| 931 | ...blah blah blah, create a session key... | ||
| 932 | return $session_key; | ||
| 933 | } | ||
| 934 | |||
| 935 | The only limitation on the session key is that you should be able to | ||
| 936 | look at it later and determine the user's username. You are | ||
| 937 | responsible for implementing your own session key format. A typical | ||
| 938 | format is to make a string that contains the username, an expiration | ||
| 939 | time, whatever else you need, and an MD5 hash of all that data | ||
| 940 | together with a secret key. The hash will ensure that the user | ||
| 941 | doesn't tamper with the session key. More info in the Eagle book. | ||
| 942 | |||
| 943 | =head2 authen_ses_key($r, $session_key) | ||
| 944 | |||
| 945 | You must define this method yourself in your subclass of | ||
| 946 | Apache::AuthCookie. Its job is to look at a session key and determine | ||
| 947 | whether it is valid. If so, it returns the username of the | ||
| 948 | authenticated user. | ||
| 949 | |||
| 950 | sub authen_ses_key ($$$) { | ||
| 951 | my ($self, $r, $session_key) = @_; | ||
| 952 | ...blah blah blah, check whether $session_key is valid... | ||
| 953 | return $ok ? $username : undef; | ||
| 954 | } | ||
| 955 | |||
| 956 | Optionally, return an array of 2 or more items that will be passed to method | ||
| 957 | custom_errors. It is the responsibility of this method to return the correct | ||
| 958 | response to the main Apache module. | ||
| 959 | |||
| 960 | =head2 custom_errors($r,@_) | ||
| 961 | |||
| 962 | Note: this interface is experimental. | ||
| 963 | |||
| 964 | This method handles the server response when you wish to access the Apache | ||
| 965 | custom_response method. Any suitable response can be used. this is | ||
| 966 | particularly useful when implementing 'by directory' access control using | ||
| 967 | the user authentication information. i.e. | ||
| 968 | |||
| 969 | /restricted | ||
| 970 | /one user is allowed access here | ||
| 971 | /two not here | ||
| 972 | /three AND here | ||
| 973 | |||
| 974 | The authen_ses_key method would return a normal response when the user attempts | ||
| 975 | to access 'one' or 'three' but return (NOT_FOUND, 'File not found') if an | ||
| 976 | attempt was made to access subdirectory 'two'. Or, in the case of expired | ||
| 977 | credentials, (AUTH_REQUIRED,'Your session has timed out, you must login | ||
| 978 | again'). | ||
| 979 | |||
| 980 | example 'custom_errors' | ||
| 981 | |||
| 982 | sub custom_errors { | ||
| 983 | my ($self,$r,$CODE,$msg) = @_; | ||
| 984 | # return custom message else use the server's standard message | ||
| 985 | $r->custom_response($CODE, $msg) if $msg; | ||
| 986 | return($CODE); | ||
| 987 | } | ||
| 988 | |||
| 989 | where CODE is a valid code from Apache::Constants | ||
| 990 | |||
| 991 | =head2 recognize_user($r) | ||
| 992 | |||
| 993 | If the user has provided a valid session key but the document isn't | ||
| 994 | protected, this method will set C<$r-E<gt>connection-E<gt>user> | ||
| 995 | anyway. Use it as a PerlFixupHandler, unless you have a better idea. | ||
| 996 | |||
| 997 | =head2 encoding($r): string | ||
| 998 | |||
| 999 | Return the ${auth_name}Encoding setting that is in effect for this request. | ||
| 1000 | |||
| 1001 | =head2 requires_encoding($r): string | ||
| 1002 | |||
| 1003 | Return the ${auth_name}RequiresEncoding setting that is in effect for this request. | ||
| 1004 | |||
| 1005 | =head2 decoded_user($r): string | ||
| 1006 | |||
| 1007 | If you have set ${auth_name}Encoding, then this will return the decoded value of | ||
| 1008 | C<< $r->connection->user >>. | ||
| 1009 | |||
| 1010 | =head2 decoded_requires($r): arrayref | ||
| 1011 | |||
| 1012 | This method returns the C<< $r->requires >> array, with the C<requirement> | ||
| 1013 | values decoded if C<${auth_name}RequiresEncoding> is in effect for this | ||
| 1014 | request. | ||
| 1015 | |||
| 1016 | =head2 handle_cache(): void | ||
| 1017 | |||
| 1018 | If C<${auth_name}Cache> is defined, this sets up the response so that the | ||
| 1019 | client will not cache the result. This sents C<no_cache> in the apache request | ||
| 1020 | object and sends the appropriate headers so that the client will not cache the | ||
| 1021 | response. | ||
| 1022 | |||
| 1023 | =head2 remove_cookie(): void | ||
| 1024 | |||
| 1025 | Adds a C<Set-Cookie> header that instructs the client to delete the cookie | ||
| 1026 | immediately. | ||
| 1027 | |||
| 1028 | =head2 params($r): Apache::AuthCookie::Params | ||
| 1029 | |||
| 1030 | Get the params object for this request. | ||
| 1031 | |||
| 1032 | =head2 login($r) | ||
| 1033 | |||
| 1034 | This method handles the submission of the login form. It will call | ||
| 1035 | the C<authen_cred()> method, passing it C<$r> and all the submitted | ||
| 1036 | data with names like C<"credential_#">, where # is a number. These will | ||
| 1037 | be passed in a simple array, so the prototype is | ||
| 1038 | C<$self-E<gt>authen_cred($r, @credentials)>. After calling | ||
| 1039 | C<authen_cred()>, we set the user's cookie and redirect to the | ||
| 1040 | URL contained in the C<"destination"> submitted form field. | ||
| 1041 | |||
| 1042 | =head2 untaint_destination($uri) | ||
| 1043 | |||
| 1044 | This method returns a modified version of the destination parameter | ||
| 1045 | before embedding it into the response header. Per default it escapes | ||
| 1046 | CR, LF and TAB characters of the uri to avoid certain types of | ||
| 1047 | security attacks. You can override it to more limit the allowed | ||
| 1048 | destinations, e.g., only allow relative uris, only special hosts or | ||
| 1049 | only limited set of characters. | ||
| 1050 | |||
| 1051 | =head2 logout($r) | ||
| 1052 | |||
| 1053 | This is simply a convenience method that unsets the session key for | ||
| 1054 | you. You can call it in your logout scripts. Usually this looks like | ||
| 1055 | C<$r-E<gt>auth_type-E<gt>logout($r);>. | ||
| 1056 | |||
| 1057 | =head2 authenticate($r) | ||
| 1058 | |||
| 1059 | This method is one you'll use in a server config file (httpd.conf, | ||
| 1060 | .htaccess, ...) as a PerlAuthenHandler. If the user provided a | ||
| 1061 | session key in a cookie, the C<authen_ses_key()> method will get | ||
| 1062 | called to check whether the key is valid. If not, or if there is no | ||
| 1063 | key provided, we redirect to the login form. | ||
| 1064 | |||
| 1065 | =head2 login_form() | ||
| 1066 | |||
| 1067 | This method is responsible for displaying the login form. The default | ||
| 1068 | implementation will make an internal redirect and display the URL you | ||
| 1069 | specified with the C<PerlSetVar WhatEverLoginScript> configuration | ||
| 1070 | directive. You can overwrite this method to provide your own | ||
| 1071 | mechanism. | ||
| 1072 | |||
| 1073 | =head2 login_form_status($r) | ||
| 1074 | |||
| 1075 | This method returns the HTTP status code that will be returned with the login | ||
| 1076 | form response. The default behaviour is to return FORBIDDEN, except for some | ||
| 1077 | known browsers which ignore HTML content for FORBIDDEN responses (e.g.: | ||
| 1078 | SymbianOS). You can override this method to return custom codes. | ||
| 1079 | |||
| 1080 | Note that FORBIDDEN is the most correct code to return as the given request was | ||
| 1081 | not authorized to view the requested page. You should only change this if | ||
| 1082 | FORBIDDEN does not work. | ||
| 1083 | |||
| 1084 | =head2 get_satisfy(): string | ||
| 1085 | |||
| 1086 | Get the C<Satisfy> value for the current request, or C<all> if it is not | ||
| 1087 | configured. | ||
| 1088 | |||
| 1089 | =head2 authorize($r) | ||
| 1090 | |||
| 1091 | This will step through the C<require> directives you've given for | ||
| 1092 | protected documents and make sure the user passes muster. The | ||
| 1093 | C<require valid-user> and C<require user joey-jojo> directives are | ||
| 1094 | handled for you. You can implement custom directives, such as | ||
| 1095 | C<require species hamster>, by defining a method called C<species()> | ||
| 1096 | in your subclass, which will then be called. The method will be | ||
| 1097 | called as C<$r-E<gt>species($r, $args)>, where C<$args> is everything | ||
| 1098 | on your C<require> line after the word C<species>. The method should | ||
| 1099 | return OK on success and FORBIDDEN on failure. | ||
| 1100 | |||
| 1101 | =head2 send_cookie($session_key) | ||
| 1102 | |||
| 1103 | By default this method simply sends out the session key you give it. | ||
| 1104 | If you need to change the default behavior (perhaps to update a | ||
| 1105 | timestamp in the key) you can override this method. | ||
| 1106 | |||
| 1107 | =head2 send_p3p(): void | ||
| 1108 | |||
| 1109 | Set a P3P response header if C<${auth_name}P3P> is configured. The value of | ||
| 1110 | the header is whatever is in the C<${auth_name}P3P> setting. | ||
| 1111 | |||
| 1112 | =head2 cookie_string(%args): string | ||
| 1113 | |||
| 1114 | Generate a cookie string. C<%args> are: | ||
| 1115 | |||
| 1116 | =over 4 | ||
| 1117 | |||
| 1118 | =item * | ||
| 1119 | |||
| 1120 | request | ||
| 1121 | |||
| 1122 | The Apache request object | ||
| 1123 | |||
| 1124 | =item * | ||
| 1125 | |||
| 1126 | key | ||
| 1127 | |||
| 1128 | The Cookie name | ||
| 1129 | |||
| 1130 | =item * | ||
| 1131 | |||
| 1132 | value | ||
| 1133 | |||
| 1134 | the Cookie value | ||
| 1135 | |||
| 1136 | =item * | ||
| 1137 | |||
| 1138 | expires (optional) | ||
| 1139 | |||
| 1140 | When the cookie expires. See L<Apache::AuthCookie::Util/expires()>. Uses C<${auth_name}Expires> if not given. | ||
| 1141 | |||
| 1142 | =back | ||
| 1143 | |||
| 1144 | All other cookie settings come from C<PerlSetVar> settings. | ||
| 1145 | |||
| 1146 | =head2 key() | ||
| 1147 | |||
| 1148 | This method will return the current session key, if any. This can be | ||
| 1149 | handy inside a method that implements a C<require> directive check | ||
| 1150 | (like the C<species> method discussed above) if you put any extra | ||
| 1151 | information like clearances or whatever into the session key. | ||
| 1152 | |||
| 1153 | =head2 get_cookie_path(): string | ||
| 1154 | |||
| 1155 | Returns the value of C<PerlSetVar ${auth_name}Path>. | ||
| 1156 | |||
| 1157 | =head1 EXAMPLE | ||
| 1158 | |||
| 1159 | For an example of how to use Apache::AuthCookie, you may want to check | ||
| 1160 | out the test suite, which runs AuthCookie through a few of its paces. | ||
| 1161 | The documents are located in t/eg/, and you may want to peruse | ||
| 1162 | t/real.t to see the generated httpd.conf file (at the bottom of | ||
| 1163 | real.t) and check out what requests it's making of the server (at the | ||
| 1164 | top of real.t). | ||
| 1165 | |||
| 1166 | =head1 THE LOGIN SCRIPT | ||
| 1167 | |||
| 1168 | You will need to create a login script (called login.pl above) that | ||
| 1169 | generates an HTML form for the user to fill out. You might generate | ||
| 1170 | the page using an Apache::Registry script, or an HTML::Mason | ||
| 1171 | component, or perhaps even using a static HTML page. It's usually | ||
| 1172 | useful to generate it dynamically so that you can define the | ||
| 1173 | 'destination' field correctly (see below). | ||
| 1174 | |||
| 1175 | The following fields must be present in the form: | ||
| 1176 | |||
| 1177 | =over 4 | ||
| 1178 | |||
| 1179 | =item 1. | ||
| 1180 | |||
| 1181 | The ACTION of the form must be /LOGIN (or whatever you defined in your | ||
| 1182 | server configuration as handled by the ->login() method - see example | ||
| 1183 | in the SYNOPSIS section). | ||
| 1184 | |||
| 1185 | =item 2. | ||
| 1186 | |||
| 1187 | The various user input fields (username, passwords, etc.) must be | ||
| 1188 | named 'credential_0', 'credential_1', etc. on the form. These will | ||
| 1189 | get passed to your authen_cred() method. | ||
| 1190 | |||
| 1191 | =item 3. | ||
| 1192 | |||
| 1193 | You must define a form field called 'destination' that tells | ||
| 1194 | AuthCookie where to redirect the request after successfully logging | ||
| 1195 | in. Typically this value is obtained from C<$r-E<gt>prev-E<gt>uri>. | ||
| 1196 | See the login.pl script in t/eg/. | ||
| 1197 | |||
| 1198 | =back | ||
| 1199 | |||
| 1200 | In addition, you might want your login page to be able to tell why | ||
| 1201 | the user is being asked to log in. In other words, if the user sent | ||
| 1202 | bad credentials, then it might be useful to display an error message | ||
| 1203 | saying that the given username or password are invalid. Also, it | ||
| 1204 | might be useful to determine the difference between a user that sent | ||
| 1205 | an invalid auth cookie, and a user that sent no auth cookie at all. To | ||
| 1206 | cope with these situations, B<AuthCookie> will set | ||
| 1207 | C<$r-E<gt>subprocess_env('AuthCookieReason')> to one of the following values. | ||
| 1208 | |||
| 1209 | =over 4 | ||
| 1210 | |||
| 1211 | =item I<no_cookie> | ||
| 1212 | |||
| 1213 | The user presented no cookie at all. Typically this means the user is | ||
| 1214 | trying to log in for the first time. | ||
| 1215 | |||
| 1216 | =item I<bad_cookie> | ||
| 1217 | |||
| 1218 | The cookie the user presented is invalid. Typically this means that the user | ||
| 1219 | is not allowed access to the given page. | ||
| 1220 | |||
| 1221 | =item I<bad_credentials> | ||
| 1222 | |||
| 1223 | The user tried to log in, but the credentials that were passed are invalid. | ||
| 1224 | |||
| 1225 | =back | ||
| 1226 | |||
| 1227 | You can examine this value in your login form by examining | ||
| 1228 | C<$r-E<gt>prev-E<gt>subprocess_env('AuthCookieReason')> (because it's | ||
| 1229 | a sub-request). | ||
| 1230 | |||
| 1231 | Of course, if you want to give more specific information about why | ||
| 1232 | access failed when a cookie is present, your C<authen_ses_key()> | ||
| 1233 | method can set arbitrary entries in C<$r-E<gt>subprocess_env>. | ||
| 1234 | |||
| 1235 | =head1 THE LOGOUT SCRIPT | ||
| 1236 | |||
| 1237 | If you want to let users log themselves out (something that can't be | ||
| 1238 | done using Basic Auth), you need to create a logout script. For an | ||
| 1239 | example, see t/htdocs/docs/logout.pl. Logout scripts may want to take | ||
| 1240 | advantage of AuthCookie's C<logout()> method, which will set the | ||
| 1241 | proper cookie headers in order to clear the user's cookie. This | ||
| 1242 | usually looks like C<$r-E<gt>auth_type-E<gt>logout($r);>. | ||
| 1243 | |||
| 1244 | Note that if you don't necessarily trust your users, you can't count | ||
| 1245 | on cookie deletion for logging out. You'll have to expire some | ||
| 1246 | server-side login information too. AuthCookie doesn't do this for | ||
| 1247 | you, you have to handle it yourself. | ||
| 1248 | |||
| 1249 | =head1 ENCODING AND CHARACTER SETS | ||
| 1250 | |||
| 1251 | =head2 Encoding | ||
| 1252 | |||
| 1253 | AuthCookie provides support for decoding POST/GET data if you tell it what the | ||
| 1254 | client encoding is. You do this by setting the C<< ${auth_name}Encoding >> | ||
| 1255 | setting in C<httpd.conf>. E.g.: | ||
| 1256 | |||
| 1257 | PerlSetVar WhateEverEncoding UTF-8 | ||
| 1258 | # and you also need to arrange for charset=UTF-8 at the end of the | ||
| 1259 | # Content-Type header with something like: | ||
| 1260 | AddDefaultCharset UTF-8 | ||
| 1261 | |||
| 1262 | Note that you B<can> use charsets other than C<UTF-8>, however, you need to | ||
| 1263 | arrange for the browser to send the right encoding back to the server. | ||
| 1264 | |||
| 1265 | If you have turned on Encoding support by setting C<< ${auth_name}Encoding >>, | ||
| 1266 | this has the following effects: | ||
| 1267 | |||
| 1268 | =over 4 | ||
| 1269 | |||
| 1270 | =item * | ||
| 1271 | |||
| 1272 | The internal pure-perl params processing subclass will be used, even if | ||
| 1273 | libapreq is installed. libapreq does not handle encoding. | ||
| 1274 | |||
| 1275 | =item * | ||
| 1276 | |||
| 1277 | POST/GET data intercepted by AuthCookie will be decoded to perl's internal | ||
| 1278 | format using L<Encode/decode>. | ||
| 1279 | |||
| 1280 | =item * | ||
| 1281 | |||
| 1282 | The value stored in C<< $r-E<gt>connection-E<gt>user >> will be encoded as | ||
| 1283 | B<bytes>, not characters using the configured encoding name. This is because | ||
| 1284 | the value stored by mod_perl is a C API string, and not a perl string. You can | ||
| 1285 | use L</decoded_user()> to get user string encoded using B<character> semantics. | ||
| 1286 | |||
| 1287 | =back | ||
| 1288 | |||
| 1289 | This does has some caveats: | ||
| 1290 | |||
| 1291 | =over 4 | ||
| 1292 | |||
| 1293 | =item * | ||
| 1294 | |||
| 1295 | your L</authen_cred()> and L</authen_ses_key()> function is expected to return | ||
| 1296 | a decoded username, either by passing it through L<Encode/decode()>, or, by | ||
| 1297 | turning on the UTF8 flag if appropriate. | ||
| 1298 | |||
| 1299 | =item * | ||
| 1300 | |||
| 1301 | Due to the way HTTP works, cookies cannot contain non-ASCII characters. | ||
| 1302 | Because of this, if you are including the username in your generated session | ||
| 1303 | key, you will need to escape any non-ascii characters in the session key | ||
| 1304 | returned by L</authen_cred()>. | ||
| 1305 | |||
| 1306 | =item * | ||
| 1307 | |||
| 1308 | Similarly, you must reverse this escaping process in L</authen_ses_key()> and | ||
| 1309 | return a L<Encode/decode()> decoded username. If your L</authen_cred()> | ||
| 1310 | function already only generates ASCII-only session keys then you do not need to | ||
| 1311 | worry about any of this. | ||
| 1312 | |||
| 1313 | =item * | ||
| 1314 | |||
| 1315 | The value stored in C<< $r-E<gt>connection-E<gt>user >> will be encoded using | ||
| 1316 | bytes semantics using the configured B<Encoding>. If you want the decoded user | ||
| 1317 | value, use L</decoded_user()> instead. | ||
| 1318 | |||
| 1319 | =back | ||
| 1320 | |||
| 1321 | =head2 Requires | ||
| 1322 | |||
| 1323 | You can also specify what the charset is of the Apache C<< $r-E<gt>requires >> | ||
| 1324 | data is by setting C<< ${auth_name}RequiresEncoding >> in httpd.conf. | ||
| 1325 | |||
| 1326 | E.g.: | ||
| 1327 | |||
| 1328 | PerlSetVar WhatEverRequiresEncoding UTF-8 | ||
| 1329 | |||
| 1330 | This will make it so that AuthCookie will decode your C<requires> directives | ||
| 1331 | using the configured character set. You really only need to do this if you | ||
| 1332 | have used non-ascii characters in any of your C<requires> directives in | ||
| 1333 | httpd.conf. e.g.: | ||
| 1334 | |||
| 1335 | requires user programmør | ||
| 1336 | |||
| 1337 | =head1 ABOUT SESSION KEYS | ||
| 1338 | |||
| 1339 | Unlike the sample AuthCookieHandler, you have you verify the user's | ||
| 1340 | login and password in C<authen_cred()>, then you do something | ||
| 1341 | like: | ||
| 1342 | |||
| 1343 | my $date = localtime; | ||
| 1344 | my $ses_key = MD5->hexhash(join(';', $date, $PID, $PAC)); | ||
| 1345 | |||
| 1346 | save C<$ses_key> along with the user's login, and return C<$ses_key>. | ||
| 1347 | |||
| 1348 | Now C<authen_ses_key()> looks up the C<$ses_key> passed to it and | ||
| 1349 | returns the saved login. I use Oracle to store the session key and | ||
| 1350 | retrieve it later, see the ToDo section below for some other ideas. | ||
| 1351 | |||
| 1352 | =head2 TO DO | ||
| 1353 | |||
| 1354 | =over 4 | ||
| 1355 | |||
| 1356 | =item * | ||
| 1357 | |||
| 1358 | It might be nice if the logout method could accept some parameters | ||
| 1359 | that could make it easy to redirect the user to another URI, or | ||
| 1360 | whatever. I'd have to think about the options needed before I | ||
| 1361 | implement anything, though. | ||
| 1362 | |||
| 1363 | =back | ||
| 1364 | |||
| 1365 | =head1 HISTORY | ||
| 1366 | |||
| 1367 | Originally written by Eric Bartley <bartley@purdue.edu> | ||
| 1368 | |||
| 1369 | versions 2.x were written by Ken Williams <ken@forum.swarthmore.edu> | ||
| 1370 | |||
| 1371 | =head1 SEE ALSO | ||
| 1372 | |||
| 1373 | L<perl(1)>, L<mod_perl(1)>, L<Apache(1)>. | ||
| 1374 | |||
| 1375 | =head1 SOURCE | ||
| 1376 | |||
| 1377 | The development version is on github at L<https://github.com/mschout/apache-authcookie> | ||
| 1378 | and may be cloned from L<https://github.com/mschout/apache-authcookie.git> | ||
| 1379 | |||
| 1380 | =head1 BUGS | ||
| 1381 | |||
| 1382 | Please report any bugs or feature requests on the bugtracker website | ||
| 1383 | L<https://github.com/mschout/apache-authcookie/issues> | ||
| 1384 | |||
| 1385 | When submitting a bug or request, please include a test-file or a | ||
| 1386 | patch to an existing test-file that illustrates the bug or desired | ||
| 1387 | feature. | ||
| 1388 | |||
| 1389 | =head1 AUTHOR | ||
| 1390 | |||
| 1391 | Michael Schout <mschout@cpan.org> | ||
| 1392 | |||
| 1393 | =head1 COPYRIGHT AND LICENSE | ||
| 1394 | |||
| 1395 | This software is copyright (c) 2000 by Ken Williams. | ||
| 1396 | |||
| 1397 | This is free software; you can redistribute it and/or modify it under | ||
| 1398 | the same terms as the Perl 5 programming language system itself. | ||
| 1399 | |||
| 1400 | =cut | ||
| 1401 | |||
| 1402 | __END__ |