CGI-Auth-Auto

 view release on metacpan or  search on metacpan

lib/CGI/Auth/Auto.pm  view on Meta::CPAN

   my $class = ref($proto) || $proto;
	my $self = {};
	bless $self, $class;

	my $param = shift;
	$param->{-authfields} ||= [
            {id => 'user', display => 'User Name', hidden => 0, required => 1},
            {id => 'pw', display => 'Password', hidden => 1, required => 1},
        ];      
   
	$param->{-authdir}      ||= _guess_authdir();
   
	$param->{-formaction}   ||= CGI::Scriptpaths::script_rel_path(); #_guess_formaction();
   $param->{-sessdir}      ||= $param->{-authdir}.'/sess';
 
   if (defined $param->{-logintmplpath} or defined $param->{-logintmpl}){
      $param->{-logintmplpath}   ||= $param->{-authdir};
      $param->{-logintmpl}       ||= 'login.html';
   }
   
   if (DEBUG){
      require Data::Dumper;
      printf STDERR __PACKAGE__."::new() params: %s\n", Data::Dumper::Dumper($param);

lib/CGI/Auth/Auto.pm  view on Meta::CPAN

	return $self->{logout_param_name};
}





# GUESSING SUBS


sub _guess_authdir {   
   my $dir = __guess_base().'/auth';   
   debug("$dir\n");
   return $dir;
}

sub __guess_base {
   my $cgibin = CGI::Scriptpaths::abs_cgibin();

   unless(defined $cgibin){
      $cgibin = script_abs_loc() or confess("cant get script's absolute location");   
   }
   debug($cgibin);
   return $cgibin;
}

sub _guess_sessdir {
   my $dir = __guess_authdir().'/sess';   
   debug("$dir\n");
   return $dir;
}


1;


__END__

lib/CGI/Auth/Auto.pm  view on Meta::CPAN

=head1 DESCRIPTION

This is a system to add one line into a cgi script and now.. voila, it requires authrentication
to run the rest of the code.
You don't have to change anything else of what your script is already doing.
It will work with CGI::Application instances as well.

=head2 MOTIVATION

CGI::Auth is a nice module- But I wanted to be able to use it without having to set up a
bunch of parameters to the constructor. This module attempts to make good guesses
about what those parameters should be.

The other thing this module addresses, is having to pass the session id around.
CGI::Auth makes you pass the "session id"- Via query string, in a form, a cookie, etc.

=head2 FEATURES

I wanted to be able to simply drop in a line into any cgi application and have it take 
care of authentication without any further change to the code.

lib/CGI/Auth/Auto.pm  view on Meta::CPAN

            {id => 'user', display => 'User Name', hidden => 0, required => 1},
            {id => 'pw', display => 'Password', hidden => 1, required => 1},
        ],
	   -authdir                => /home/myself/cgi-bin/auth",
	});

Shown are the defaults. You do not need to provide these parameters.


-formaction 
If you do not provide one, the module tries to guess what the rel path to the script is.
 

-authdir
Now a default value of $ENV{DOCUMENT_ROOT} ../cgi-bin/authdir is present.
That means for most hosting accounts if you have this kind of (very common) setup:
/path/to/home/
           |__ public_html/
           |__ cgi-bin/

You should place the support files that come with CGI::Auth as 

lib/CGI/Auth/Auto.pm  view on Meta::CPAN

Make this $ENV{DOCUMENT_ROOT}/../cgi-bin/auth.cgi to test it. Don't forget chmod 0755.

	#!/usr/bin/perl -w
	BEGIN { use CGI::Carp qw(fatalsToBrowser); eval qq|use lib '$ENV{DOCUMENT_ROOT}/../lib';|; } # or wherever your lib is 
	use strict;
	use CGI::Auth::Auto;
	use CGI qw(:all);
	
	my $auth = new CGI::Auth::Auto({
		-authdir => "$ENV{DOCUMENT_ROOT}/../cgi-bin/auth"
	}); # the program guesses for authdir, you can leave out if it resides alongside your script
	$auth->check;
	
   my $html =
	 header() .
	 start_html() .
	 h1("hello ".$auth->username) .
	 p('You are logged in now.') .
	 p('Would you like to log out? <a href="'.$ENV{SCRIPT_NAME}.'?logout=1">logout</a>');	
	
   print $html;

lib/CGI/Auth/Auto.pm  view on Meta::CPAN


To turn on debug info, in your cgi script, before you call check() :

   $CGI::Auth::Auto::DEBUG = 1;

=head1 ERRORS

The most common error is that you are not passing the right authdir to the object.

The authdir needs to exist and contain a user.dat simple text file.
If you do not provide an authdir argument, that's ok, we try to guess for it.
If your script is in /home/myself/cgi-bin/script.pl , then your auth dir is guessed as
/home/myself/cgi-bin/auth
And it must exist and contain the user.dat file. This can be a blank text file to begin with.
Make sure it is chown and chmod properly.

If your cgi is failing, turn on L<DEBUG> and run it again. A lot of useful information may be there.

=head2  Auth::check - Invalid 'User Name' field at ...

Erase your user.dat and recreate.



( run in 2.100 seconds using v1.01-cache-2.11-cpan-748bfb374f4 )