Have you looked at
Apache::AuthCookie?
It takes care of authentication and authorization for you. You create a session key which is passed as the value of the cookie sent to the user, once the user has been authenticated. This key links to a server side copy of the key which is associated with the user's name. Every request a new cookie is created. This is called 'ticket based authentication' and is generally accepted as a best practice for authentication and cookie handling.
No critical information is kept in the cookie itself - just a link to a server side file which contains the username, remote IP, hostname, and whatever else you need to determine the user is actually who they say they are.
Usage is relatively simple, you need to subclass 2 methods and configure access in httpd.conf:
in your httpd.conf
<Location /protected>
AuthType My::Apache::AuthCookieHandler
AuthName MyProtectedArea
PerlAuthenHandler My::Apache::AuthCookieHandler->authenticate
PerlAuthzHandler My::Apache::AuthCookieHandler->authorize
require valid-user
PerlHandler My::Apache::PerlHandler
</Location>
in My::Apache::AuthCookieHandler
sub authen_cred ($$\@) { # Authenticates the user and returns a key
my $self = shift;
my $r = shift;
my @cred = @_;
my $user = My::User->new;
return unless $user->auth(@cred);
my $session_key = My::MD5->new(My::RandomData); # session_key is something like 'lkj125825yk523'
_save_to_disk({$session_key => $user});
return $session_key;
}
sub authen_ses_key ($$$) { # See if there is a user associated with this key
my ($self, $r, $session_key) = @_;
my $username = _get_from_disk($session_key);
$username->valid ? return $username : return;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.