| App-Context documentation | Contained in the App-Context distribution. |
App::Authentication::Htpasswd - Interface for authentication using an htpasswd file
use App;
$context = App->context();
$authentication = $context->service("Authentication"); # or ...
$authentication = $context->authentication();
if ($authentication->validate_password($username, $password)) {
...
}
An App::Authentication::Htpasswd service is a means by which a user may be authenticated using an htpasswd file.
* Signature: $username = $auth->validate_password();
* Param: void
* Return: $username string
* Throws: App::Exception::Authentication
* Since: 0.01
Sample Usage:
$username = $auth->validate_password();
* Author: Stephen Adkins <spadkins@gmail.com> * License: This is free software. It is licensed under the same terms as Perl itself.
App::Context|App::Context,
App::Service|App::Service
| App-Context documentation | Contained in the App-Context distribution. |
############################################################################# ## $Id: Htpasswd.pm 9817 2007-07-30 22:46:19Z spadkins $ ############################################################################# package App::Authentication::Htpasswd; $VERSION = (q$Revision: 9817 $ =~ /(\d[\d\.]*)/)[0]; # VERSION numbers generated by svn use App; use App::Authentication; @ISA = ( "App::Authentication" ); use strict;
############################################################################# # PUBLIC METHODS #############################################################################
############################################################################# # validate_password() #############################################################################
sub validate_password { &App::sub_entry if ($App::trace); my ($self, $username, $password) = @_; my $valid = 0; my $context = $self->{context}; my $htpasswd_file = $context->get_option("htpasswd_file"); if (-e $htpasswd_file) { my ($uname, $pword); if (open(PFILE, "< $htpasswd_file")) { while (<PFILE>) { chomp; ($uname, $pword) = split(/:/); last if ($uname eq $username); $uname = ""; } close(PFILE); if ($uname) { my $crypt = crypt($password, $pword); $valid = ($pword eq $crypt) ? 1 : 0; } } } &App::sub_exit($valid) if ($App::trace); return($valid); }
1;