CGI::Session::Auth::DBI - Authenticated sessions for CGI scripts
use CGI; use CGI::Session; use CGI::Session::Auth::DBI; my $cgi = new CGI; my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'}); my $auth = new CGI::Session::Auth::DBI({ CGI => $cgi, Session => $session, DSN => 'dbi:mysql:host=localhost,database=cgiauth', }); $auth->authenticate(); if ($auth->loggedIn) { showSecretPage; } else { showLoginPage; }
CGI::Session::Auth::DBI is a subclass of CGI::Session::Auth. It uses a relational database for storing the authentication data, using the DBI module as database interface.
Use your favourite database administration tool to create and populate the database:
CREATE TABLE auth_user ( userid CHAR(32) NOT NULL, username VARCHAR(30) NOT NULL, passwd VARCHAR(32) NOT NULL default '', PRIMARY KEY (userid), UNIQUE username (username) );
INSERT INTO auth_user VALUES ( '325684ec1b028eaf562dd484c5607a65', 'admin', 'qwe123' ); INSERT INTO auth_user VALUES ( 'ef19a80d627b5c48728d388c11900f3f', 'guest', 'guest' );
CREATE TABLE auth_group ( groupname VARCHAR(30) NOT NULL, userid CHAR(32) NOT NULL, PRIMARY KEY (groupname) );
CREATE TABLE auth_ip ( network char(15) NOT NULL, netmask char(15) NOT NULL, userid char(32) NOT NULL, PRIMARY KEY (network, netmask) );
INSERT INTO auth_ip VALUES ('127.0.0.1', '255.0.0.0', 'ef19a80d627b5c48728d388c11900f3f' );
Mandatory columns in auth_user are userid, username and passwd. All additional columns will also be stored and accessible as user profile fields.
auth_user
userid
username
passwd
userid is a 32-character string and can be generated randomly by
perl -MCGI::Session::Auth -e 'print CGI::Session::Auth::uniqueUserID("myname"), "\n";'
The auth_ip table is used for IP address based authentication. Every row combines a pair of network address and subnet mask (both in dotted quad notation) with a user ID. The userid column is used as a foreign key into the auth_user table.
auth_ip
Additional to the standard parameters used by the new constructor of all CGI::Session::Auth classes, CGI::Session::Auth::DBI understands the following parameters:
new
CGI::Session::Auth
Jochen Lillich, <geewiz@cpan.org>
Copyright 2003-2010 by Jochen Lillich
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install CGI::Session::Auth, copy and paste the appropriate command in to your terminal.
cpanm
cpanm CGI::Session::Auth
CPAN shell
perl -MCPAN -e shell install CGI::Session::Auth
For more information on module installation, please visit the detailed CPAN module installation guide.