| 1 | package Apache2::AuthCookie; | ||
| 2 | $Apache2::AuthCookie::VERSION = '3.32'; | ||
| 3 | # ABSTRACT: Perl Authentication and Authorization via cookies | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | |||
| 7 | use Carp; | ||
| 8 | use base 'Apache2::AuthCookie::Base'; | ||
| 9 | use Apache2::Const qw(OK DECLINED SERVER_ERROR HTTP_FORBIDDEN); | ||
| 10 | use Apache::AuthCookie::Util qw(is_blank); | ||
| 11 | |||
| 12 | |||
| 13 | sub authorize { | ||
| 14 | my ($auth_type, $r) = @_; | ||
| 15 | |||
| 16 | my $debug = $r->dir_config("AuthCookieDebug") || 0; | ||
| 17 | |||
| 18 | $r->server->log_error('authorize() for '.$r->uri()) if $debug >= 3; | ||
| 19 | |||
| 20 | return OK unless $r->is_initial_req; #only the first internal request | ||
| 21 | |||
| 22 | if ($r->auth_type ne $auth_type) { | ||
| 23 | $r->server->log_error("auth type mismatch $auth_type != ".$r->auth_type) | ||
| 24 | if $debug >= 3; | ||
| 25 | return DECLINED; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $reqs_arr = $auth_type->decoded_requires($r) or return DECLINED; | ||
| 29 | |||
| 30 | my $user = $auth_type->decoded_user($r); | ||
| 31 | |||
| 32 | $r->server->log_error("authorize user=$user type=$auth_type") if $debug >=3; | ||
| 33 | |||
| 34 | if (is_blank($user)) { | ||
| 35 | # the authentication failed | ||
| 36 | $r->server->log_error("No user authenticated", $r->uri); | ||
| 37 | return HTTP_FORBIDDEN; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $satisfy = $auth_type->get_satisfy($r); | ||
| 41 | return SERVER_ERROR unless $auth_type->satisfy_is_valid($r,$satisfy); | ||
| 42 | my $satisfy_all = $satisfy eq 'all'; | ||
| 43 | |||
| 44 | my ($forbidden); | ||
| 45 | foreach my $req (@$reqs_arr) { | ||
| 46 | my ($requirement, $args) = split /\s+/, $req->{requirement}, 2; | ||
| 47 | $args = '' unless defined $args; | ||
| 48 | $r->server->log_error("requirement := $requirement, $args") if $debug >= 2; | ||
| 49 | |||
| 50 | if (lc($requirement) eq 'valid-user') { | ||
| 51 | if ($satisfy_all) { | ||
| 52 | next; | ||
| 53 | } | ||
| 54 | else { | ||
| 55 | return OK; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | if ($requirement eq 'user') { | ||
| 60 | if ($args =~ m/\b$user\b/) { | ||
| 61 | next if $satisfy_all; | ||
| 62 | return OK; # satisfy any | ||
| 63 | } | ||
| 64 | |||
| 65 | $forbidden = 1; | ||
| 66 | next; | ||
| 67 | } | ||
| 68 | |||
| 69 | # Call a custom method | ||
| 70 | my $ret_val = $auth_type->$requirement($r, $args); | ||
| 71 | $r->server->log_error("$auth_type->$requirement returned $ret_val") if $debug >= 3; | ||
| 72 | if ($ret_val == OK) { | ||
| 73 | next if $satisfy_all; | ||
| 74 | return OK; # satisfy any | ||
| 75 | } | ||
| 76 | |||
| 77 | # Nothing succeeded, deny access to this user. | ||
| 78 | $forbidden = 1; | ||
| 79 | } | ||
| 80 | |||
| 81 | return $forbidden ? HTTP_FORBIDDEN : OK; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | sub get_satisfy { | ||
| 86 | my ($auth_type, $r) = @_; | ||
| 87 | |||
| 88 | my $auth_name = $r->auth_name; | ||
| 89 | |||
| 90 | return lc( $r->dir_config("${auth_name}Satisfy") || 'all' ); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub satisfy_is_valid { | ||
| 95 | my ($auth_type, $r, $satisfy) = @_; | ||
| 96 | |||
| 97 | $satisfy = lc $satisfy; | ||
| 98 | |||
| 99 | if ($satisfy eq 'any' or $satisfy eq 'all') { | ||
| 100 | return 1; | ||
| 101 | } | ||
| 102 | else { | ||
| 103 | my $auth_name = $r->auth_name; | ||
| 104 | $r->server->log_error("PerlSetVar ${auth_name}Satisfy $satisfy invalid",$r->uri); | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | 1; | ||
| 110 | |||
| 111 | =pod | ||
| 112 | |||
| 113 | =encoding UTF-8 | ||
| 114 | |||
| 115 | =head1 NAME | ||
| 116 | |||
| 117 | Apache2::AuthCookie - Perl Authentication and Authorization via cookies | ||
| 118 | |||
| 119 | =head1 VERSION | ||
| 120 | |||
| 121 | version 3.32 | ||
| 122 | |||
| 123 | =head1 SYNOPSIS | ||
| 124 | |||
| 125 | Make sure your mod_perl is at least 2.0.0-RC5, with StackedHandlers, | ||
| 126 | MethodHandlers, Authen, and Authz compiled in. | ||
| 127 | |||
| 128 | # In httpd.conf or .htaccess: | ||
| 129 | PerlModule Sample::Apache2::AuthCookieHandler | ||
| 130 | PerlSetVar WhatEverPath / | ||
| 131 | PerlSetVar WhatEverLoginScript /login.pl | ||
| 132 | |||
| 133 | # use to alter how "require" directives are matched. Can be "Any" or "All". | ||
| 134 | # If its "Any", then you must only match Any of the "require" directives. If | ||
| 135 | # its "All", then you must match All of the require directives. | ||
| 136 | # | ||
| 137 | # Default: All | ||
| 138 | PerlSetVar WhatEverSatisfy Any | ||
| 139 | |||
| 140 | # The following line is optional - it allows you to set the domain | ||
| 141 | # scope of your cookie. Default is the current domain. | ||
| 142 | PerlSetVar WhatEverDomain .yourdomain.com | ||
| 143 | |||
| 144 | # Use this to only send over a secure connection | ||
| 145 | PerlSetVar WhatEverSecure 1 | ||
| 146 | |||
| 147 | # Use this if you want user session cookies to expire if the user | ||
| 148 | # doesn't request a auth-required or recognize_user page for some | ||
| 149 | # time period. If set, a new cookie (with updated expire time) | ||
| 150 | # is set on every request. | ||
| 151 | PerlSetVar WhatEverSessionTimeout +30m | ||
| 152 | |||
| 153 | # to enable the HttpOnly cookie property, use HttpOnly. | ||
| 154 | # This is an MS extension. See: | ||
| 155 | # http://msdn.microsoft.com/workshop/author/dhtml/httponly_cookies.asp | ||
| 156 | PerlSetVar WhatEverHttpOnly 1 | ||
| 157 | |||
| 158 | # to enable the SameSite cookie property, set SameSite to "lax" or "strict". | ||
| 159 | # See: https://www.owasp.org/index.php/SameSite | ||
| 160 | PerlSetVar WhatEverSameSite strict | ||
| 161 | |||
| 162 | # Usually documents are uncached - turn off here | ||
| 163 | PerlSetVar WhatEverCache 1 | ||
| 164 | |||
| 165 | # Use this to make your cookies persistent (+2 hours here) | ||
| 166 | PerlSetVar WhatEverExpires +2h | ||
| 167 | |||
| 168 | # Use to make AuthCookie send a P3P header with the cookie | ||
| 169 | # see http://www.w3.org/P3P/ for details about what the value | ||
| 170 | # of this should be | ||
| 171 | PerlSetVar WhatEverP3P "CP=\"...\"" | ||
| 172 | |||
| 173 | # optional: enable decoding of intercepted GET/POST params: | ||
| 174 | PerlSetVar WhatEverEncoding UTF-8 | ||
| 175 | |||
| 176 | # optional: enable decoding of httpd.conf "Requires" directives | ||
| 177 | PerlSetVar WhatEverRequiresEncoding UTF-8 | ||
| 178 | |||
| 179 | # optional: enforce that the destination argument from the login form is | ||
| 180 | # local to the server | ||
| 181 | PerlSetVar WhatEverEnforceLocalDestination 1 | ||
| 182 | |||
| 183 | # optional: specify a default destination for when the destination argument | ||
| 184 | # of the login form is invalid or unspecified | ||
| 185 | PerlSetVar WhatEverDefaultDestination /protected/user/ | ||
| 186 | |||
| 187 | # These documents require user to be logged in. | ||
| 188 | <Location /protected> | ||
| 189 | AuthType Sample::Apache2::AuthCookieHandler | ||
| 190 | AuthName WhatEver | ||
| 191 | PerlAuthenHandler Sample::Apache2::AuthCookieHandler->authenticate | ||
| 192 | PerlAuthzHandler Sample::Apache2::AuthCookieHandler->authorize | ||
| 193 | require valid-user | ||
| 194 | </Location> | ||
| 195 | |||
| 196 | # These documents don't require logging in, but allow it. | ||
| 197 | <FilesMatch "\.ok$"> | ||
| 198 | AuthType Sample::Apache2::AuthCookieHandler | ||
| 199 | AuthName WhatEver | ||
| 200 | PerlFixupHandler Sample::Apache2::AuthCookieHandler->recognize_user | ||
| 201 | </FilesMatch> | ||
| 202 | |||
| 203 | # This is the action of the login.pl script above. | ||
| 204 | <Files LOGIN> | ||
| 205 | AuthType Sample::Apache2::AuthCookieHandler | ||
| 206 | AuthName WhatEver | ||
| 207 | SetHandler perl-script | ||
| 208 | PerlResponseHandler Sample::Apache2::AuthCookieHandler->login | ||
| 209 | </Files> | ||
| 210 | |||
| 211 | =head1 DESCRIPTION | ||
| 212 | |||
| 213 | This module is for mod_perl version 2. If you are running mod_perl version 1, | ||
| 214 | you should be using B<Apache::AuthCookie> instead. | ||
| 215 | |||
| 216 | B<Apache2::AuthCookie> allows you to intercept a user's first | ||
| 217 | unauthenticated access to a protected document. The user will be | ||
| 218 | presented with a custom form where they can enter authentication | ||
| 219 | credentials. The credentials are posted to the server where AuthCookie | ||
| 220 | verifies them and returns a session key. | ||
| 221 | |||
| 222 | The session key is returned to the user's browser as a cookie. As a | ||
| 223 | cookie, the browser will pass the session key on every subsequent | ||
| 224 | accesses. AuthCookie will verify the session key and re-authenticate | ||
| 225 | the user. | ||
| 226 | |||
| 227 | All you have to do is write a custom module that inherits from | ||
| 228 | AuthCookie. Your module is a class which implements two methods: | ||
| 229 | |||
| 230 | =over 4 | ||
| 231 | |||
| 232 | =item C<authen_cred()> | ||
| 233 | |||
| 234 | Verify the user-supplied credentials and return a session key. The | ||
| 235 | session key can be any string - often you'll use some string | ||
| 236 | containing username, timeout info, and any other information you need | ||
| 237 | to determine access to documents, and append a one-way hash of those | ||
| 238 | values together with some secret key. | ||
| 239 | |||
| 240 | =item C<authen_ses_key()> | ||
| 241 | |||
| 242 | Verify the session key (previously generated by C<authen_cred()>, | ||
| 243 | possibly during a previous request) and return the user ID. This user | ||
| 244 | ID will be fed to C<$r-E<gt>user()> to set Apache's idea of who's logged in. | ||
| 245 | |||
| 246 | =back | ||
| 247 | |||
| 248 | By using AuthCookie versus Apache's built-in AuthBasic you can design | ||
| 249 | your own authentication system. There are several benefits. | ||
| 250 | |||
| 251 | =over 4 | ||
| 252 | |||
| 253 | =item 1. | ||
| 254 | |||
| 255 | The client doesn't *have* to pass the user credentials on every | ||
| 256 | subsequent access. If you're using passwords, this means that the | ||
| 257 | password can be sent on the first request only, and subsequent | ||
| 258 | requests don't need to send this (potentially sensitive) information. | ||
| 259 | This is known as "ticket-based" authentication. | ||
| 260 | |||
| 261 | =item 2. | ||
| 262 | |||
| 263 | When you determine that the client should stop using the | ||
| 264 | credentials/session key, the server can tell the client to delete the | ||
| 265 | cookie. Letting users "log out" is a notoriously impossible-to-solve | ||
| 266 | problem of AuthBasic. | ||
| 267 | |||
| 268 | =item 3. | ||
| 269 | |||
| 270 | AuthBasic dialog boxes are ugly. You can design your own HTML login | ||
| 271 | forms when you use AuthCookie. | ||
| 272 | |||
| 273 | =item 4. | ||
| 274 | |||
| 275 | You can specify the domain of a cookie using PerlSetVar commands. For | ||
| 276 | instance, if your AuthName is C<WhatEver>, you can put the command | ||
| 277 | |||
| 278 | PerlSetVar WhatEverDomain .yourhost.com | ||
| 279 | |||
| 280 | into your server setup file and your access cookies will span all | ||
| 281 | hosts ending in C<.yourhost.com>. | ||
| 282 | |||
| 283 | =item 5. | ||
| 284 | |||
| 285 | You can optionally specify the name of your cookie using the C<CookieName> | ||
| 286 | directive. For instance, if your AuthName is C<WhatEver>, you can put the | ||
| 287 | command | ||
| 288 | |||
| 289 | PerlSetVar WhatEverCookieName MyCustomName | ||
| 290 | |||
| 291 | into your server setup file and your cookies for this AuthCookie realm will be | ||
| 292 | named MyCustomName. Default is AuthType_AuthName. | ||
| 293 | |||
| 294 | =item 6. | ||
| 295 | |||
| 296 | By default users must satisfy ALL of the C<require> directives. If you | ||
| 297 | want authentication to succeed if ANY C<require> directives are met, use the | ||
| 298 | C<Satisfy> directive. For instance, if your AuthName is C<WhatEver>, you can | ||
| 299 | put the command | ||
| 300 | |||
| 301 | PerlSetVar WhatEverSatisfy Any | ||
| 302 | |||
| 303 | into your server startup file and authentication for this realm will succeed if | ||
| 304 | ANY of the C<require> directives are met. | ||
| 305 | |||
| 306 | =back | ||
| 307 | |||
| 308 | This is the flow of the authentication handler, less the details of the | ||
| 309 | redirects. Two HTTP_MOVED_TEMPORARILY's are used to keep the client from | ||
| 310 | displaying the user's credentials in the Location field. They don't really | ||
| 311 | change AuthCookie's model, but they do add another round-trip request to the | ||
| 312 | client. | ||
| 313 | |||
| 314 | (-----------------------) +---------------------------------+ | ||
| 315 | ( Request a protected ) | AuthCookie sets custom error | | ||
| 316 | ( page, but user hasn't )---->| document and returns | | ||
| 317 | ( authenticated (no ) | HTTP_FORBIDDEN. Apache abandons | | ||
| 318 | ( session key cookie) ) | current request and creates sub | | ||
| 319 | (-----------------------) | request for the error document. |<-+ | ||
| 320 | | Error document is a script that | | | ||
| 321 | | generates a form where the user | | | ||
| 322 | return | enters authentication | | | ||
| 323 | ^------------------->| credentials (login & password). | | | ||
| 324 | / \ False +---------------------------------+ | | ||
| 325 | / \ | | | ||
| 326 | / \ | | | ||
| 327 | / \ V | | ||
| 328 | / \ +---------------------------------+ | | ||
| 329 | / Pass \ | User's client submits this form | | | ||
| 330 | / user's \ | to the LOGIN URL, which calls | | | ||
| 331 | | credentials |<------------| AuthCookie->login(). | | | ||
| 332 | \ to / +---------------------------------+ | | ||
| 333 | \authen_cred/ | | ||
| 334 | \ function/ | | ||
| 335 | \ / | | ||
| 336 | \ / | | ||
| 337 | \ / +------------------------------------+ | | ||
| 338 | \ / return | Authen cred returns a session | +--+ | ||
| 339 | V------------->| key which is opaque to AuthCookie.*| | | ||
| 340 | True +------------------------------------+ | | ||
| 341 | | | | ||
| 342 | +--------------------+ | +---------------+ | ||
| 343 | | | | | If we had a | | ||
| 344 | V | V | cookie, add | | ||
| 345 | +----------------------------+ r | ^ | a Set-Cookie | | ||
| 346 | | If we didn't have a session| e |T / \ | header to | | ||
| 347 | | key cookie, add a | t |r / \ | override the | | ||
| 348 | | Set-Cookie header with this| u |u / \ | invalid cookie| | ||
| 349 | | session key. Client then | r |e / \ +---------------+ | ||
| 350 | | returns session key with | n | / pass \ ^ | ||
| 351 | | successive requests | | / session \ | | ||
| 352 | +----------------------------+ | / key to \ return | | ||
| 353 | | +-| authen_ses_key|------------+ | ||
| 354 | V \ / False | ||
| 355 | +-----------------------------------+ \ / | ||
| 356 | | Tell Apache to set Expires header,| \ / | ||
| 357 | | set user to user ID returned by | \ / | ||
| 358 | | authen_ses_key, set authentication| \ / | ||
| 359 | | to our type (e.g. AuthCookie). | \ / | ||
| 360 | +-----------------------------------+ \ / | ||
| 361 | V | ||
| 362 | (---------------------) ^ | ||
| 363 | ( Request a protected ) | | ||
| 364 | ( page, user has a )--------------+ | ||
| 365 | ( session key cookie ) | ||
| 366 | (---------------------) | ||
| 367 | |||
| 368 | |||
| 369 | * The session key that the client gets can be anything you want. For | ||
| 370 | example, encrypted information about the user, a hash of the | ||
| 371 | username and password (similar in function to Digest | ||
| 372 | authentication), or the user name and password in plain text | ||
| 373 | (similar in function to HTTP Basic authentication). | ||
| 374 | |||
| 375 | The only requirement is that the authen_ses_key function that you | ||
| 376 | create must be able to determine if this session_key is valid and | ||
| 377 | map it back to the originally authenticated user ID. | ||
| 378 | |||
| 379 | =head1 METHODS | ||
| 380 | |||
| 381 | =head2 authorize(): int | ||
| 382 | |||
| 383 | This will step through the C<require> directives you've given for protected | ||
| 384 | documents and make sure the user passes muster. The C<require valid-user> and | ||
| 385 | C<require user joey-jojo> directives are handled for you. You can implement | ||
| 386 | custom directives, such as C<require species hamster>, by defining a method | ||
| 387 | called C<species()> in your subclass, which will then be called. The method | ||
| 388 | will be called as C<$r-E<gt>species($r, $args)>, where C<$args> is everything | ||
| 389 | on your C<require> line after the word C<species>. The method should return OK | ||
| 390 | on success and HTTP_FORBIDDEN on failure. | ||
| 391 | |||
| 392 | =head2 get_satisfy(): string | ||
| 393 | |||
| 394 | Get the value of C<${auth_name}Satisfy>, or C<all> if it is not set. | ||
| 395 | |||
| 396 | =head2 satisfy_is_valid(): bool | ||
| 397 | |||
| 398 | return true if the configured C<${auth_name}Satisfy> is valid, false otherwise. | ||
| 399 | |||
| 400 | =head2 authen_cred(): string | ||
| 401 | |||
| 402 | You must define this method yourself in your subclass of | ||
| 403 | C<Apache2::AuthCookie>. Its job is to create the session key that will | ||
| 404 | be preserved in the user's cookie. The arguments passed to it are: | ||
| 405 | |||
| 406 | sub authen_cred ($$\@) { | ||
| 407 | my $self = shift; # Package name (same as AuthName directive) | ||
| 408 | my $r = shift; # Apache request object | ||
| 409 | my @cred = @_; # Credentials from login form | ||
| 410 | |||
| 411 | ...blah blah blah, create a session key... | ||
| 412 | return $session_key; | ||
| 413 | } | ||
| 414 | |||
| 415 | The only limitation on the session key is that you should be able to | ||
| 416 | look at it later and determine the user's username. You are | ||
| 417 | responsible for implementing your own session key format. A typical | ||
| 418 | format is to make a string that contains the username, an expiration | ||
| 419 | time, whatever else you need, and an MD5 hash of all that data | ||
| 420 | together with a secret key. The hash will ensure that the user | ||
| 421 | doesn't tamper with the session key. More info in the Eagle book. | ||
| 422 | |||
| 423 | =head2 authen_ses_key($r, $session_key): string | ||
| 424 | |||
| 425 | You must define this method yourself in your subclass of | ||
| 426 | Apache2::AuthCookie. Its job is to look at a session key and determine | ||
| 427 | whether it is valid. If so, it returns the username of the | ||
| 428 | authenticated user. | ||
| 429 | |||
| 430 | sub authen_ses_key ($$$) { | ||
| 431 | my ($self, $r, $session_key) = @_; | ||
| 432 | ...blah blah blah, check whether $session_key is valid... | ||
| 433 | return $ok ? $username : undef; | ||
| 434 | } | ||
| 435 | |||
| 436 | Optionally, return an array of 2 or more items that will be passed to method | ||
| 437 | custom_errors. It is the responsibility of this method to return the correct | ||
| 438 | response to the main Apache module. | ||
| 439 | |||
| 440 | =head2 custom_errors($r,@_): int | ||
| 441 | |||
| 442 | Note: this interface is experimental. | ||
| 443 | |||
| 444 | This method handles the server response when you wish to access the Apache | ||
| 445 | custom_response method. Any suitable response can be used. this is | ||
| 446 | particularly useful when implementing 'by directory' access control using | ||
| 447 | the user authentication information. i.e. | ||
| 448 | |||
| 449 | /restricted | ||
| 450 | /one user is allowed access here | ||
| 451 | /two not here | ||
| 452 | /three AND here | ||
| 453 | |||
| 454 | The authen_ses_key method would return a normal response when the user attempts | ||
| 455 | to access 'one' or 'three' but return (NOT_FOUND, 'File not found') if an | ||
| 456 | attempt was made to access subdirectory 'two'. Or, in the case of expired | ||
| 457 | credentials, (AUTH_REQUIRED,'Your session has timed out, you must login | ||
| 458 | again'). | ||
| 459 | |||
| 460 | example 'custom_errors' | ||
| 461 | |||
| 462 | sub custom_errors { | ||
| 463 | my ($self,$r,$CODE,$msg) = @_; | ||
| 464 | # return custom message else use the server's standard message | ||
| 465 | $r->custom_response($CODE, $msg) if $msg; | ||
| 466 | return($CODE); | ||
| 467 | } | ||
| 468 | |||
| 469 | where CODE is a valid code from Apache2::Const | ||
| 470 | |||
| 471 | =head1 ENCODING AND CHARACTER SETS | ||
| 472 | |||
| 473 | =head2 Encoding | ||
| 474 | |||
| 475 | AuthCookie provides support for decoding POST/GET data if you tell it what the | ||
| 476 | client encoding is. You do this by setting the C<< ${auth_name}Encoding >> | ||
| 477 | setting in C<httpd.conf>. E.g.: | ||
| 478 | |||
| 479 | PerlSetVar WhateEverEncoding UTF-8 | ||
| 480 | # and you also need to arrange for charset=UTF-8 at the end of the | ||
| 481 | # Content-Type header with something like: | ||
| 482 | AddDefaultCharset UTF-8 | ||
| 483 | |||
| 484 | Note that you B<can> use charsets other than C<UTF-8>, however, you need to | ||
| 485 | arrange for the browser to send the right encoding back to the server. | ||
| 486 | |||
| 487 | If you have turned on Encoding support by setting C<< ${auth_name}Encoding >>, | ||
| 488 | this has the following effects: | ||
| 489 | |||
| 490 | =over 4 | ||
| 491 | |||
| 492 | =item * | ||
| 493 | |||
| 494 | The internal pure-perl params processing subclass will be used, even if | ||
| 495 | libapreq2 is installed. libapreq2 does not have any support for encoding or | ||
| 496 | unicode. | ||
| 497 | |||
| 498 | =item * | ||
| 499 | |||
| 500 | POST/GET data intercepted by AuthCookie will be decoded to perl's internal | ||
| 501 | format using L<Encode/decode>. | ||
| 502 | |||
| 503 | =item * | ||
| 504 | |||
| 505 | The value stored in C<< $r-E<gt>user >> will be encoded as B<bytes>, not | ||
| 506 | characters using the configured encoding name. This is because the value | ||
| 507 | stored by mod_perl is a C API string, and not a perl string. You can use | ||
| 508 | L<decoded_user()> to get user string encoded using B<character> semantics. | ||
| 509 | |||
| 510 | =back | ||
| 511 | |||
| 512 | This does has some caveats: | ||
| 513 | |||
| 514 | =over 4 | ||
| 515 | |||
| 516 | =item * | ||
| 517 | |||
| 518 | your L<authen_cred()> and L<authen_ses_key()> function is expected to return | ||
| 519 | a decoded username, either by passing it through L<Encode/decode()>, or, by | ||
| 520 | turning on the UTF8 flag if appropriate. | ||
| 521 | |||
| 522 | =item * | ||
| 523 | |||
| 524 | Due to the way HTTP works, cookies cannot contain non-ASCII characters. | ||
| 525 | Because of this, if you are including the username in your generated session | ||
| 526 | key, you will need to escape any non-ascii characters in the session key | ||
| 527 | returned by L<authen_cred()>. | ||
| 528 | |||
| 529 | =item * | ||
| 530 | |||
| 531 | Similarly, you must reverse this escaping process in L<authen_ses_key()> and | ||
| 532 | return a L<Encode/decode()> decoded username. If your L<authen_cred()> | ||
| 533 | function already only generates ASCII-only session keys then you do not need to | ||
| 534 | worry about any of this. | ||
| 535 | |||
| 536 | =item * | ||
| 537 | |||
| 538 | The value stored in C<< $r-E<gt>user >> will be encoded using bytes semantics | ||
| 539 | using the configured B<Encoding>. If you want the decoded user value, use | ||
| 540 | L<decoded_user()> instead. | ||
| 541 | |||
| 542 | =back | ||
| 543 | |||
| 544 | =head2 Requires | ||
| 545 | |||
| 546 | You can also specify what the charset is of the Apache C<< $r-E<gt>requires >> | ||
| 547 | data is by setting C<< ${auth_name}RequiresEncoding >> in httpd.conf. | ||
| 548 | |||
| 549 | E.g.: | ||
| 550 | |||
| 551 | PerlSetVar WhatEverRequiresEncoding UTF-8 | ||
| 552 | |||
| 553 | This will make it so that AuthCookie will decode your C<requires> directives | ||
| 554 | using the configured character set. You really only need to do this if you | ||
| 555 | have used non-ascii characters in any of your C<requires> directives in | ||
| 556 | httpd.conf. e.g.: | ||
| 557 | |||
| 558 | requires user programmør | ||
| 559 | |||
| 560 | =head1 EXAMPLE | ||
| 561 | |||
| 562 | For an example of how to use Apache2::AuthCookie, you may want to check | ||
| 563 | out the test suite, which runs AuthCookie through a few of its paces. | ||
| 564 | The documents are located in t/eg/, and you may want to peruse | ||
| 565 | t/real.t to see the generated httpd.conf file (at the bottom of | ||
| 566 | real.t) and check out what requests it's making of the server (at the | ||
| 567 | top of real.t). | ||
| 568 | |||
| 569 | =head1 THE LOGIN SCRIPT | ||
| 570 | |||
| 571 | You will need to create a login script (called login.pl above) that | ||
| 572 | generates an HTML form for the user to fill out. You might generate | ||
| 573 | the page using a ModPerl::Registry script, a HTML::Mason component, an Apache | ||
| 574 | handler, or perhaps even using a static HTML page. It's usually useful to | ||
| 575 | generate it dynamically so that you can define the 'destination' field | ||
| 576 | correctly (see below). | ||
| 577 | |||
| 578 | The following fields must be present in the form: | ||
| 579 | |||
| 580 | =over 4 | ||
| 581 | |||
| 582 | =item 1. | ||
| 583 | |||
| 584 | The ACTION of the form must be /LOGIN (or whatever you defined in your | ||
| 585 | server configuration as handled by the ->login() method - see example | ||
| 586 | in the SYNOPSIS section). | ||
| 587 | |||
| 588 | =item 2. | ||
| 589 | |||
| 590 | The various user input fields (username, passwords, etc.) must be | ||
| 591 | named 'credential_0', 'credential_1', etc. on the form. These will | ||
| 592 | get passed to your authen_cred() method. | ||
| 593 | |||
| 594 | =item 3. | ||
| 595 | |||
| 596 | You must define a form field called 'destination' that tells | ||
| 597 | AuthCookie where to redirect the request after successfully logging | ||
| 598 | in. Typically this value is obtained from C<$r-E<gt>prev-E<gt>uri>. | ||
| 599 | See the login.pl script in t/eg/. | ||
| 600 | |||
| 601 | =back | ||
| 602 | |||
| 603 | In addition, you might want your login page to be able to tell why | ||
| 604 | the user is being asked to log in. In other words, if the user sent | ||
| 605 | bad credentials, then it might be useful to display an error message | ||
| 606 | saying that the given username or password are invalid. Also, it | ||
| 607 | might be useful to determine the difference between a user that sent | ||
| 608 | an invalid auth cookie, and a user that sent no auth cookie at all. To | ||
| 609 | cope with these situations, B<AuthCookie> will set | ||
| 610 | C<$r-E<gt>subprocess_env('AuthCookieReason')> to one of the following values. | ||
| 611 | |||
| 612 | =over 4 | ||
| 613 | |||
| 614 | =item I<no_cookie> | ||
| 615 | |||
| 616 | The user presented no cookie at all. Typically this means the user is | ||
| 617 | trying to log in for the first time. | ||
| 618 | |||
| 619 | =item I<bad_cookie> | ||
| 620 | |||
| 621 | The cookie the user presented is invalid. Typically this means that the user | ||
| 622 | is not allowed access to the given page. | ||
| 623 | |||
| 624 | =item I<bad_credentials> | ||
| 625 | |||
| 626 | The user tried to log in, but the credentials that were passed are invalid. | ||
| 627 | |||
| 628 | =back | ||
| 629 | |||
| 630 | You can examine this value in your login form by examining | ||
| 631 | C<$r-E<gt>prev-E<gt>subprocess_env('AuthCookieReason')> (because it's | ||
| 632 | a sub-request). | ||
| 633 | |||
| 634 | Of course, if you want to give more specific information about why | ||
| 635 | access failed when a cookie is present, your C<authen_ses_key()> | ||
| 636 | method can set arbitrary entries in C<$r-E<gt>subprocess_env>. | ||
| 637 | |||
| 638 | =head1 THE LOGOUT SCRIPT | ||
| 639 | |||
| 640 | If you want to let users log themselves out (something that can't be | ||
| 641 | done using Basic Auth), you need to create a logout script. For an example, | ||
| 642 | see t/htdocs/docs/logout.pl. Logout scripts may want to take advantage of | ||
| 643 | AuthCookie's C<logout()> method, which will set the proper cookie headers in | ||
| 644 | order to clear the user's cookie. This usually looks like | ||
| 645 | C<$r-E<gt>auth_type-E<gt>logout($r);>. | ||
| 646 | |||
| 647 | Note that if you don't necessarily trust your users, you can't count | ||
| 648 | on cookie deletion for logging out. You'll have to expire some | ||
| 649 | server-side login information too. AuthCookie doesn't do this for | ||
| 650 | you, you have to handle it yourself. | ||
| 651 | |||
| 652 | =head1 ABOUT SESSION KEYS | ||
| 653 | |||
| 654 | Unlike the sample AuthCookieHandler, you have you verify the user's | ||
| 655 | login and password in C<authen_cred()>, then you do something | ||
| 656 | like: | ||
| 657 | |||
| 658 | my $date = localtime; | ||
| 659 | my $ses_key = MD5->hexhash(join(';', $date, $PID, $PAC)); | ||
| 660 | |||
| 661 | save C<$ses_key> along with the user's login, and return C<$ses_key>. | ||
| 662 | |||
| 663 | Now C<authen_ses_key()> looks up the C<$ses_key> passed to it and | ||
| 664 | returns the saved login. I use Oracle to store the session key and | ||
| 665 | retrieve it later, see the ToDo section below for some other ideas. | ||
| 666 | |||
| 667 | =head2 TO DO | ||
| 668 | |||
| 669 | =over 4 | ||
| 670 | |||
| 671 | =item * | ||
| 672 | |||
| 673 | It might be nice if the logout method could accept some parameters | ||
| 674 | that could make it easy to redirect the user to another URI, or | ||
| 675 | whatever. I'd have to think about the options needed before I | ||
| 676 | implement anything, though. | ||
| 677 | |||
| 678 | =back | ||
| 679 | |||
| 680 | =head1 HISTORY | ||
| 681 | |||
| 682 | Originally written by Eric Bartley <bartley@purdue.edu> | ||
| 683 | |||
| 684 | versions 2.x were written by Ken Williams <ken@forum.swarthmore.edu> | ||
| 685 | |||
| 686 | =head1 COPYRIGHT | ||
| 687 | |||
| 688 | Copyright (c) 2000 Ken Williams. All rights reserved. | ||
| 689 | |||
| 690 | This program is free software; you can redistribute it and/or modify it under | ||
| 691 | the same terms as Perl itself. | ||
| 692 | |||
| 693 | =head1 SEE ALSO | ||
| 694 | |||
| 695 | L<Apache2::AuthCookie::Base> | ||
| 696 | |||
| 697 | =head1 SOURCE | ||
| 698 | |||
| 699 | The development version is on github at L<https://github.com/mschout/apache-authcookie> | ||
| 700 | and may be cloned from L<https://github.com/mschout/apache-authcookie.git> | ||
| 701 | |||
| 702 | =head1 BUGS | ||
| 703 | |||
| 704 | Please report any bugs or feature requests on the bugtracker website | ||
| 705 | L<https://github.com/mschout/apache-authcookie/issues> | ||
| 706 | |||
| 707 | When submitting a bug or request, please include a test-file or a | ||
| 708 | patch to an existing test-file that illustrates the bug or desired | ||
| 709 | feature. | ||
| 710 | |||
| 711 | =head1 AUTHOR | ||
| 712 | |||
| 713 | Michael Schout <mschout@cpan.org> | ||
| 714 | |||
| 715 | =head1 COPYRIGHT AND LICENSE | ||
| 716 | |||
| 717 | This software is copyright (c) 2000 by Ken Williams. | ||
| 718 | |||
| 719 | This is free software; you can redistribute it and/or modify it under | ||
| 720 | the same terms as the Perl 5 programming language system itself. | ||
| 721 | |||
| 722 | =cut | ||
| 723 | |||
| 724 | __END__ |