Plack-Middleware-Auth-Htpasswd
view release on metacpan or search on metacpan
lib/Plack/Middleware/Auth/Htpasswd.pm view on Meta::CPAN
package Plack::Middleware::Auth::Htpasswd;
BEGIN {
$Plack::Middleware::Auth::Htpasswd::VERSION = '0.02';
}
use strict;
use warnings;
use base 'Plack::Middleware::Auth::Basic';
use Plack::Util::Accessor qw(file file_root);
use Plack::Request;
use Authen::Htpasswd;
use MIME::Base64;
use Path::Class ();
# ABSTRACT: http basic authentication through apache-style .htpasswd files
sub prepare_app {
my $self = shift;
$self->authenticator(sub { $self->authenticate(@_) });
die "must specify either file or file_root"
unless defined $self->file || $self->file_root;
return $self->SUPER::prepare_app;
}
sub _check_password {
my $self = shift;
my ($file, $user, $pass) = @_;
my $htpasswd = Authen::Htpasswd->new($file);
my $htpasswd_user = $htpasswd->lookup_user($user);
return unless $htpasswd_user;
return $htpasswd_user->check_password($pass);
}
sub authenticate {
my $self = shift;
my ($user, $pass, $env) = @_;
return $self->_check_password($self->file, $user, $pass)
if defined $self->file;
my $path = Plack::Request->new($env)->path;
my $dir = Path::Class::Dir->new($self->file_root);
my @htpasswd = $path ne '/'
? reverse
map { $_->file('.htpasswd')->stringify }
map { $dir = $dir->subdir($_) }
split m{/}, $path
: ($dir->file('.htpasswd')->stringify);
for my $htpasswd (@htpasswd) {
next unless -f $htpasswd && -r _;
return $self->_check_password($htpasswd, $user, $pass);
}
return;
}
1;
__END__
=pod
=head1 NAME
Plack::Middleware::Auth::Htpasswd - http basic authentication through apache-style .htpasswd files
=head1 VERSION
version 0.02
=head1 SYNOPSIS
use Plack::Builder;
my $app = sub { ... };
builder {
enable "Auth::Htpasswd", file => '/path/to/.htpasswd';
$app;
};
or
builder {
enable "Auth::Htpasswd", file_root => '/path/to/my/static/files';
$app;
};
=head1 DESCRIPTION
This middleware enables HTTP Basic authenication, based on the users in an
L<Apache-style htpasswd file|http://httpd.apache.org/docs/2.0/programs/htpasswd.html>.
You can either specify the file directly, through the C<file> option, or use
the C<file_root> option to specify the root directory on the filesystem that
corresponds to the web application root. This second option is more useful when
using an app that is closely tied to the filesystem, such as
L<Plack::App::Directory>. If C<file_root> is used, the requested path will be
inspected, and a file named C<.htpasswd> will be checked in each containing
directory, up to the C<file_root>. The first one found will be used to validate
the requested user.
=head1 CONFIGURATION
=head2 file
Name of a .htpasswd file to read authentication information from. Required if
C<file_root> is not set.
( run in 1.023 second using v1.01-cache-2.11-cpan-71847e10f99 )