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