3 package Catalyst::Authentication::Store::Htpasswd;
4 # ABSTRACT: Authen::Htpasswd based user storage/authentication
6 use base qw/Class::Accessor::Fast/;
10 use Authen::Htpasswd 0.13;
11 use Catalyst::Authentication::Store::Htpasswd::User;
12 use Scalar::Util qw/blessed/;
14 our $VERSION = '1.005';
16 BEGIN { __PACKAGE__->mk_accessors(qw/file user_field user_class/) }
19 my ($class, $config, $app, $realm) = @_;
21 my $file = delete $config->{file};
23 my $filename = ($file =~ m|^/|) ? $file : $app->path_to($file)->stringify;
24 die("Cannot find htpasswd file: $filename\n") unless (-r $filename);
25 $file = Authen::Htpasswd->new($filename);
27 $config->{file} = $file;
28 $config->{user_class} ||= __PACKAGE__ . '::User';
29 $config->{user_field} ||= 'username';
31 bless { %$config }, $class;
35 my ($self, $authinfo, $c) = @_;
36 my $htpasswd_user = $self->file->lookup_user($authinfo->{$self->user_field});
37 $self->user_class->new( $self, $htpasswd_user );
43 # this can work as a class method, but in that case you can't have
45 ref($self) ? $self->user_class->supports(@_)
46 : Catalyst::Authentication::Store::Htpasswd::User->supports(@_);
50 my ( $self, $c, $id ) = @_;
51 $self->find_user( { username => $id } );
68 default_realm => 'test',
73 password_field => 'password',
74 password_type => 'self_check',
86 my ( $self, $c ) = @_;
88 $c->authenticate({ username => $c->req->param("login"), password => $c->req->param("password") });
93 This plugin uses L<Authen::Htpasswd> to let your application use C<<.htpasswd>>
94 files for it's authentication storage.
100 Simple constructor, dies if the htpassword file can't be found
104 Looks up the user, and returns a Catalyst::Authentication::Store::Htpasswd::User object.
108 Delegates to L<Catalyst::Authentication::Store::Htpasswd::User->user_supports|Catalyst::Authentication::Store::Htpasswd::User#user_supports>
112 Delegates the user lookup to C<< find_user >>
118 The path to the htpasswd file. If the path starts with a slash, then it is assumed to be a fully
119 qualified path, otherwise the path is fed through C<< $c->path_to >> and so normalised to the
122 Alternatively, it is possible to pass in an L<Authen::Htpasswd> object here, and this will be
123 used as the htpasswd file.
127 Change the user class which this store returns. Defaults to L<Catalyst::Authentication::Store::Htpasswd::User>.
128 This can be used to add additional functionality to the user class by sub-classing it, but will not normally be
133 Change the field that the username is found in in the information passed into the call to C<< $c->authenticate() >>.
135 This defaults to I< username >, and generally you should be able to use the module as shown in the synopsis, however
136 if you need a different field name then this setting can change the default.
140 __PACKAGE__->config( authentication => { realms => { test => {
143 user_field => 'email_address',
147 $c->authenticate({ email_address => $c->req->param("email"), password => $c->req->param("password") });