tokuhirom's Blog

How to authenticate with basic auth + htpasswd

use strict;
use warnings;
use Authen::Htpasswd;
use Plack::Builder;

my $app = sub {
    [200, [], ['OK']]
};

builder {
    my $authen = Authen::Htpasswd->new('user.txt', {encrypt_hash => 'md5'});
    enable 'Auth::Basic',
        authenticator => sub {
            my ($username, $password) = @_;
            my $user = $authen->lookup_user($username);
            return $user && $user->check_password($password);
        };
    $app;
};