Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Perl/CGI login

by muclc7 (Initiate)
on Dec 02, 2005 at 22:38 UTC ( [id://513736]=perlquestion: print w/replies, xml ) Need Help??

muclc7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I would like to create a basic login page using Perl/CGI. However, rather than people creating usernames and passwords, I already have all the usernames and passwords I wish to allow. I have created a login form using HTML, but can't find any tutorials or scripts that would help me with the programming. This is for my personal webpage, so security is not as essential. Does anyone know of a Perl/CGI login script or tutorial that I can view? Thanks.

Replies are listed 'Best First'.
Re: Perl/CGI login
by b10m (Vicar) on Dec 02, 2005 at 22:51 UTC

    Of course Ovid's CGI Course would be a great way to start. You could write your own security module (or use any of the many available on CPAN), yet you could also just take a look at Apache's abilities to handle this (and some "easier" notes). If you use Apache, that is.

    --
    b10m

    All code is usually tested, but rarely trusted.
Re: Perl/CGI login
by hesco (Deacon) on Dec 02, 2005 at 23:32 UTC
    This is slightly more complicated than it seems.

    A login form's role is to _authenticate_ a user as one trusted to use resources behind the web form. The next step is to check whether a logged in user to is _authorized_ to access a particular resource. As your applications grow, you may find that some legitimate users should have access to some functions that should not be made available to others.

    You'll need a way to secure the connection over which the authentication (and any subsequent transmittal of private data) takes place. And a way to preserve the session, that is, to know that the next http request from your authenticated and authorized user is just as legitimate as the last one.

    I'm still experimenting with what approach I find easiest to work with.

    For secure connections, I use apache-ssl for its encrypted connections. I earlier rejected the apache .htaccess methods suggested by another responder because it passes plain text tokens in the clear, before creating the ssl connection. I prefer my login screens protected by encryption. If the data is really worth protecting, then there is no sense in exposing it to the vulnerability of snooped passwords on the login screen.

    For similar reasons, I mistrust cookies and hidden form values as they expose your session management to user manipulation. While I'll pass a token back and forth, it would not contain anything other than the session ID. Where security matters, I would choose to store all other session data on the server, related to the session ID.

    My latest experiment with a secure login front end to a multi-function cgi script starts out:

    use DBIx::UserDB; use DBIx::SearchProfiles; use CGI qw(:standard); use CGI::SecureState; use CGI::Carp qw(fatalsToBrowser); use WWW::Authenticate_he;
    WWW::Authenticate_he is my adaptation of WWW::Authenticate. I'd suggest you check out the DBIx modules listed above, as well as CGI::SecureState and WWW::Authenticate. Those just may prove useful to you.

    -- Hugh

Re: Perl/CGI login
by swkronenfeld (Hermit) on Dec 02, 2005 at 23:01 UTC
    There's modules that can do work for you, but if you're wanting to do something fairly simple and learn in the process, you can roll a pretty simple script. Here's a generic (and simplistic) skeleton to give you an idea.

    use CGI; my $cgi = new CGI; my $user = $cgi->param("username"); my $pass = $cgi->param("password"); my $valid = 0; # Assuming usernames/passwords in a delimited text file open(IN, "passwordFile") or die(...); while(<IN>) { if( /^$user\:$pass$/ ) { $valid = 1; last; } } close(IN) if(!$valid) { print "Sorry, you may not access this page."; exit; } #set authentication #set a cookie here using javascript or #add hidden fields with login info print "<input type='hidden' name='pass' value='...'>"; print "<input type='hidden' name='user' value='...'>"; Store usernames/passwords in a file as follows: johndoe:myPassw0rd user2:pass2 user3:pass3 etc


    You have to decide how you wish to keep users authenticated. There a number of approaches you can take. Since it sounds like you want something simple, the easiest ways may be to either set a cookie for the user or to pass around a hidden <input> field. Neither of those is very secure, but neither is a plain text password file. How worried are you about security, do you need more than that?
      Your skeleton is most helpful in giving me an idea of the process when using Perl/CGI (Perl is the first language I am learning). The login does not have to be too secure as it is just for my family members to view my online photo album (and an excuse to learn/use Perl). I have not used cookies or hidden fields yet, but I have a Perl book that references them. Which would you recommend I try first (i.e. which is simple to put together). Thank you.
Re: Perl/CGI login
by psychotic (Beadle) on Dec 02, 2005 at 22:49 UTC
    You pass parameters to your Perl script either via POST or GET. You "read" them by utilizing the CGI module. See here for documentation. Documentation is your utter resource for all things Perl. You might as well post the HTML code, but be careful because it is slightly OT (Off Topic).
Re: Perl/CGI login
by EvanCarroll (Chaplain) on Dec 03, 2005 at 02:49 UTC

    I personally use Apache2, with Mason, and mod_perl2, while mod_perl2 is only in dev it seems to perform just as well now as mod_perl.

    My login script consists of a simple sql lookup on a users table. If found, I create a new session using Apache::Sesssion::Postgres and then set a cookie such that its value is the session's id.

    Then in an autohandler in a folder named /auth_required/ or the like, I check for a cookie using Apache2::Cookie and then I check the cookie's value $cookie->value to make sure the value or session id does exist in my session table (you create this to use anthing Apache::Session::*). If all is there I move on.

    I save the session to a global variable $S, and the user data to a global variable $U to eliminate the need to pass it explicitly to other mason components.



    Evan Carroll
    www.EvanCarroll.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://513736]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-18 02:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found