NAME Apache::AuthCookie - Perl Authentication and Authorization via cookies SYNOPSIS `use mod_perl qw(1.07 StackedHandlers MethodHandlers Authen Authz);' # In mod_perl startup script: use Sample::AuthCookieHandler; # In httpd.conf or .htaccess: PerlSetVar WhatEverPath / PerlSetVar WhatEverLoginScript /login.pl # These documents require user to be logged in. AuthType Sample AuthName WhatEver PerlAuthenHandler Sample::AuthCookieHandler->authenticate PerlAuthzHandler Sample::AuthCookieHandler->authorize require valid-user # These documents don't require logging in, but allow it. AuthType Sample AuthName WhatEver PerlFixupHandler Sample::AuthCookieHandler->recognize_user # This is the action of the login.pl script above. AuthType Sample AuthName WhatEver SetHandler perl-script PerlHandler Sample::AuthCookieHandler->login DESCRIPTION Apache::AuthCookie allows you to intercept a user's first unauthenticated access to a protected document. The user will be presented with a custom form where they can enter authentication credentials. The credentials are posted to the server where AuthCookie verifies them and returns a session key. The session key is returned to the user's browser as a cookie. As a cookie, the browser will pass the session key on every subsequent accesses. AuthCookie will verify the session key and re-authenticate the user. All you have to do is write a custom module that inherits from AuthCookie. Your module implements two functions: `authen_cred()' Verify the user-supplied credentials and return a session key. The session key can be any string - often you'll use some string containing username, timeout info, and any other information you need to determine access to documents, and append a one-way hash of those values together with some secret key. `authen_ses_key()' Verify the session key (previously generated by `authen_cred()', possibly during a previous request) and return the user ID. This user ID will be fed to `$r->connection->user()' to set Apache's idea of who's logged in. By using AuthCookie versus Apache's built-in AuthBasic you can design your own authentication system. There are at least three main benefits. 1. The client doesn't *have* to pass the user credentials on every subsequent access. If you're using passwords, this means that the password can be sent on the first request only, and subsequent requests don't need to send this (potentially sensitive) information. This is known as "ticket-based" authentication. 2. When you determine that the client should stop using the credentials/session key, the server can tell the client to delete the cookie. Letting users "log out" is a notoriously impossible-to- solve problem of AuthBasic. 3. AuthBasic dialog boxes are ugly. You can design your own HTML login forms when you use AuthCookie. This is the flow of the authentication handler, less the details of the redirects. Two REDIRECT's are used to keep the client from displaying the user's credentials in the Location field. They don't really change AuthCookie's model, but they do add another round-trip request to the client. (-----------------------) +---------------------------------+ ( Request a protected ) | AuthCookie sets custom error | ( page, but user hasn't )---->| document and returns | ( authenticated (no ) | AUTH_REQUIRED. Apache abandons | ( session key cookie) ) | current request and creates sub | (-----------------------) | request for the error document. |<-+ | Error document is a script that | | | generates a form where the user | | return | enters authentication | | ^------------------->| credentials (login & password). | | / \ False +---------------------------------+ | / \ | | / \ | | / \ V | / \ +---------------------------------+ | / Pass \ | User's client submits this form | | / user's \ | to the LOGIN URL, which calls | | | credentials |<------------| AuthCookie->login(). | | \ to / +---------------------------------+ | \authen_cred/ | \ function/ | \ / | \ / | \ / +------------------------------------+ | \ / return | Authen cred returns a session | +--+ V------------->| key which is opaque to AuthCookie.*| | True +------------------------------------+ | | | +--------------------+ | +---------------+ | | | | If we had a | V | V | cookie, add | +----------------------------+ r | ^ | a Set-Cookie | | If we didn't have a session| e |T / \ | header to | | key cookie, add a | t |r / \ | override the | | Set-Cookie header with this| u |u / \ | invalid cookie| | session key. Client then | r |e / \ +---------------+ | returns session key with | n | / pass \ ^ | sucsesive requests | | / session \ | +----------------------------+ | / key to \ return | | +-| authen_ses_key|------------+ V \ / False +-----------------------------------+ \ / | Tell Apache to set Expires header,| \ / | set no-cache Pragma header, set | \ / | user to user ID returned by | \ / | authen_ses_key, set authentication| \ / | to our type (e.g. AuthCookie). | \ / +-----------------------------------+ V (---------------------) ^ ( Request a protected ) | ( page, user has a )--------------+ ( session key cookie ) (---------------------) * The session key that the client gets can be anything you want. For example, encrypted information about the user, a hash of the username and password (similar in function to Digest authentication), or the user name and password in plain text (similar in function to HTTP Basic authentication). The only requirement is that the authen_ses_key function that you create must be able to determine if this session_key is valid and map it back to the originally authenticated user ID. UPGRADING FROM VERSION 1.4 There are a couple of interface changes that you need to be aware of when migrating from version 1.4 to 2.0. First, the authen() and authz() methods are now deprecated, replaced by the new authenticate() and authorize() methods. The old methods will go away in a couple versions, but are maintained intact in this version to ease the task of upgrading. The use of these methods is essentially the same, though. Second, when you change to the new method names (see previous paragraph), you must change the action of your login forms to the location /LOGIN (or whatever URL will call your module's login() method). Third, you must put another field in your login forms (see the section on "THE LOGIN SCRIPT" below) that indicates how requests should be redirected after a successful login. EXAMPLE NOTE: THE FOLLOWING EXAMPLE CODE IS BASED ON AN OLD VERSION OF Apache::AuthCookie, AND IS NO LONGER SUPPORTED. I'll update the example stuff as soon as I get a chance. For now, please see the sample configuration code at the top of this document. Install the sample 1. Install eg/Sample into the site_perl directory in your perl5 library directory. 2. Install eg/unprotected into your Apache document root directory. 3. Add `use Sample::AuthCookieHandler;' to your mod_perl startup script or `Sample::AuthCookieHandler' to your PerlModule configuration directive. 4. Restart Apache so mod_perl picks up `Sample::AuthCookieHandler'. Try out the sample 1. Try to access /unprotected/protected/get_me.html. You should instead get a form requesting a login and password. The sample will validate two users. The first is login => programmer and password => Hero and the second is login => some-user with no/any password. You might want to set your browser to show you cookies before accepting them. Then you can see what AuthCookie is generating. 2. As distributed, the .htaccess file in eg/unprotected/protected will allow either of these user to access the document. However if you change the line `require valid-user' to `require dwarf' in .htaccess only the user "programmer" will have access. Look at the authorization function `dwarf()' in eg/Sample/AuthCookieHandler.pm to see how this works. THE LOGIN SCRIPT You will need to create a login script (called login.pl above) that generates an HTML form for the user to fill out. The following fields must be present in the form: 1. The ACTION of the form must be /LOGIN (or whatever you defined in your server configuration, as in the SYNOPSIS section). 2. The various user input fields (username, passwords, etc.) must be named 'credential_0', 'credential_1', etc. on the form. 3. You must define a form field called 'destination' that tells AuthCookie where to redirect the request after successfully logging in. Typically this value is obtained from `$r->prev->uri'. ABOUT SESSION KEYS Unlike the sample AuthCookieHandler, you have you verify the user's login and password in `authen_cred()', then you do something like: my $date = localtime; my $ses_key = MD5->hexhash(join(';', $date, $PID, $PAC)); save `$ses_key' along with the user's login, and return `$ses_key'. Now `authen_ses_key()' looks up the `$ses_key' passed to it and returns the saved login. I use Oracle to store the session key and retrieve it later, see the ToDo section below for some other ideas. KNOWN LIMITATIONS The login form uses the GET method to send the user's credentials, so they're visible on the query string. If the first unauthenticated request is a POST, it will be changed to a GET after the user fills out the login forms, and POSTed data will be lost. ToDo * There ought to be a way to solve the GET/POST problems in the LIMITATIONS section. They both involve being able to change a request back & forth between GET & POST. The second problem also involves being able to re-insert the POSTed content into the request stream after the user authenticates. If you knows of a way, please drop me a note. AUTHOR Ken Williams, ken@forum.swarthmore.edu Originally written by Eric Bartley, bartley@purdue.edu SEE ALSO the perl(1) manpage, the mod_perl(1) manpage, the Apache(1) manpage.