# $Id: Auth.pm,v 1.17 2004/01/28 07:05:46 cmdrwalrus Exp $ package CGI::Auth; use strict; use Carp; use vars qw/$VERSION/; $VERSION = '3.00'; # This delimiter cannot be a regex special character, unfortunately, since it # is used with split. So, for instance, the pipe (|) is not allowed, as well # as the dash (-), caret (^), dot (.), etc... use constant DELIMITER => ':'; =pod =head1 NAME CGI::Auth - Simple session-based password authentication for CGI applications =head1 SYNOPSIS require CGI::Auth; my $auth = new CGI::Auth({ -authdir => 'auth', -formaction => "myscript.pl", -authfields => [ {id => 'user', display => 'User Name', hidden => 0, required => 1}, {id => 'pw', display => 'Password', hidden => 1, required => 1}, ], }); $auth->check; =head1 DESCRIPTION C provides password authentication for web-based applications. It uses server-based session files which are referred to by a parameter in all links and forms inside the scripts guarded by C. At the beginning of each script, a C object should be created and its C method called. When this happens, C checks for a 'session_file' CGI parameter. If that parameter exists and has a matching session file in the session directory, C returns, and the rest of the script can execute. If the session file parameter or the file itself doesn't exist, C presents the user with a login form and exits the script. The login form will then be submitted to the same script (specified in C<-formaction>). When C is called this time, it verifies the user's login information in the userfile, creates a session file and provides the session file parameter to the rest of the script. =head1 CREATING AND CONFIGURING Before anything can be done with C, an object must be created: my $auth = new CGI::Auth( \%options ); =head2 Parameters to C The C method creates and configures a C object using parameters that are passed via a hash reference that can/should contain the following items (optional ones are indicated): =over 4 =item C<-cgi> I<(optional)> This parameter provides C with a CGI object reference so that the extra overhead of creating another object can be avoided. If your script is going to use CGI.pm, it is most efficient to create the CGI object and pass it to C, rather than both your script and Auth having to create separate objects. Note: As of version 2.4.3, C can now be used with C. This hasn't been tested thoroughly yet, so use caution if you decide to do so. =item C<-admin> I<(optional if C<-formaction> given)> This parameter should be used by command-line utilities that perform administration of the user database. If Auth is given this parameter, it will only allow command-line execution (execution from CGI will be aborted). =item C<-authdir> I<(required)> Directory where Auth will look for its files. In other words, if C<-sessdir>, C<-userfile>, C<-logintmpl>, C<-loginheader> or C<-loginfooter> are scalars and do not begin with a slash (i.e., are not absolute paths), this directory will be prepended to them. =item C<-sessdir> I<(optional, default = 'sess')> Directory where Auth will store session files. These files should be pruned periodically (i.e., nightly or weekly) since a session file will remain here if a user does not log out. =item C<-userfile> I<(optional, default = 'user.dat')> File containing definitions of users, including login information and any extra parameters. This file will be created, edited and read by C and its command-line administration tool. =item C<-logintmpl> I<(optional, excludes C<-loginheader> and C<-loginfooter> if present)> Template for use with C. The template can be given in one of three ways: =over 4 =item 1 An C object reference, =item 2 A hash containing parameters for Cnew>, or =item 3 A filename (then C<-logintmplpath> can be the path parameter). =back The template must contain a form for the user to fill out, and it is recommended that the form not contain any elements with names beginning with 'auth_', since these are reserved for C fields. A sample template file (C) is included in the extra subdirectory of this package. For a list of what should be included in the template, see L