File Coverage

blib/lib/Catalyst/Authentication/Store/Htpasswd.pm
Criterion Covered Total %
statement 36 36 100.0
branch 5 8 62.5
condition 2 4 50.0
subroutine 11 11 100.0
pod 4 4 100.0
total 58 63 92.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package Catalyst::Authentication::Store::Htpasswd; # git description: v1.005-4-g2915058
4             # ABSTRACT: Authen::Htpasswd based user storage/authentication
5              
6 4     4   1887324 use base qw/Class::Accessor::Fast/;
  4         10  
  4         1542  
7 4     4   10534 use strict;
  4         9  
  4         61  
8 4     4   17 use warnings;
  4         12  
  4         89  
9              
10 4     4   1509 use Authen::Htpasswd 0.13;
  4         37960  
  4         22  
11 4     4   1569 use Catalyst::Authentication::Store::Htpasswd::User;
  4         23  
  4         39  
12 4     4   145 use Scalar::Util qw/blessed/;
  4         9  
  4         260  
13              
14             our $VERSION = '1.006';
15              
16 4     4   37 BEGIN { __PACKAGE__->mk_accessors(qw/file user_field user_class/) }
17              
18             sub new {
19 3     3 1 26612 my ($class, $config, $app, $realm) = @_;
20              
21 3         10 my $file = delete $config->{file};
22 3 100       15 unless (ref $file) {
23 1 50       14 my $filename = ($file =~ m|^/|) ? $file : $app->path_to($file)->stringify;
24 1 50       867 die("Cannot find htpasswd file: $filename\n") unless (-r $filename);
25 1         7 $file = Authen::Htpasswd->new($filename);
26             }
27 3         4123 $config->{file} = $file;
28 3   50     26 $config->{user_class} ||= __PACKAGE__ . '::User';
29 3   50     18 $config->{user_field} ||= 'username';
30              
31 3         27 bless { %$config }, $class;
32             }
33              
34             sub find_user {
35 4     4 1 38513 my ($self, $authinfo, $c) = @_;
36 4         16 my $htpasswd_user = $self->file->lookup_user($authinfo->{$self->user_field});
37 4         1561 $self->user_class->new( $self, $htpasswd_user );
38             }
39              
40             sub user_supports {
41 3     3 1 16955 my $self = shift;
42              
43             # this can work as a class method, but in that case you can't have
44             # a custom user class
45 3 50       34 ref($self) ? $self->user_class->supports(@_)
46             : Catalyst::Authentication::Store::Htpasswd::User->supports(@_);
47             }
48              
49             sub from_session {
50 1     1 1 108 my ( $self, $c, $id ) = @_;
51 1         4 $self->find_user( { username => $id } );
52             }
53              
54             1;
55              
56             __END__
57              
58             =pod
59              
60             =encoding UTF-8
61              
62             =head1 NAME
63              
64             Catalyst::Authentication::Store::Htpasswd - Authen::Htpasswd based user storage/authentication
65              
66             =head1 VERSION
67              
68             version 1.006
69              
70             =head1 SYNOPSIS
71              
72             use Catalyst qw/
73             Authentication
74             /;
75              
76             __PACKAGE__->config(
77             authentication => {
78             default_realm => 'test',
79             realms => {
80             test => {
81             credential => {
82             class => 'Password',
83             password_field => 'password',
84             password_type => 'self_check',
85             },
86             store => {
87             class => 'Htpasswd',
88             file => 'htpasswd',
89             },
90             },
91             },
92             },
93             );
94              
95             sub login : Global {
96             my ( $self, $c ) = @_;
97              
98             $c->authenticate({ username => $c->req->param("login"), password => $c->req->param("password") });
99             }
100              
101             =head1 DESCRIPTION
102              
103             This plugin uses L<Authen::Htpasswd> to let your application use C<< .htpasswd >>
104             files for it's authentication storage.
105              
106             =head1 METHODS
107              
108             =head2 new
109              
110             Simple constructor, dies if the htpassword file can't be found
111              
112             =head2 find_user
113              
114             Looks up the user, and returns a Catalyst::Authentication::Store::Htpasswd::User object.
115              
116             =head2 user_supports
117              
118             Delegates to L<< Catalyst::Authentication::User->supports|Catalyst::Authentication::User/supports >> or an
119             override in L<user_class|/user_class>.
120              
121             =head2 from_session
122              
123             Delegates the user lookup to L<find_user|/find_user>
124              
125             =head1 CONFIGURATION
126              
127             =head2 file
128              
129             The path to the htpasswd file. If the path starts with a slash, then it is assumed to be a fully
130             qualified path, otherwise the path is fed through C<< $c->path_to >> and so normalised to the
131             application root.
132              
133             Alternatively, it is possible to pass in an L<Authen::Htpasswd> object here, and this will be
134             used as the htpasswd file.
135              
136             =head2 user_class
137              
138             Change the user class which this store returns. Defaults to L<Catalyst::Authentication::Store::Htpasswd::User>.
139             This can be used to add additional functionality to the user class by sub-classing it, but will not normally be
140             needed.
141              
142             =head2 user_field
143              
144             Change the field that the username is found in in the information passed into the call to C<< $c->authenticate() >>.
145              
146             This defaults to I< username >, and generally you should be able to use the module as shown in the synopsis, however
147             if you need a different field name then this setting can change the default.
148              
149             Example:
150              
151             __PACKAGE__->config( authentication => { realms => { test => {
152             store => {
153             class => 'Htpasswd',
154             user_field => 'email_address',
155             },
156             }}});
157             # Later in your code
158             $c->authenticate({ email_address => $c->req->param("email"), password => $c->req->param("password") });
159              
160             =head1 SEE ALSO
161              
162             L<Authen::Htpasswd>.
163              
164             =head1 SUPPORT
165              
166             Bugs may be submitted through L<the RT bug tracker|https://rt.cpan.org/Public/Dist/Display.html?Name=Catalyst-Authentication-Store-Htpasswd>
167             (or L<bug-Catalyst-Authentication-Store-Htpasswd@rt.cpan.org|mailto:bug-Catalyst-Authentication-Store-Htpasswd@rt.cpan.org>).
168              
169             There is also a mailing list available for users of this distribution, at
170             L<http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst>.
171              
172             There is also an irc channel available for users of this distribution, at
173             L<C<#catalyst> on C<irc.perl.org>|irc://irc.perl.org/#catalyst>.
174              
175             =head1 AUTHOR
176              
177             יובל קוג'מן (Yuval Kogman) <nothingmuch@woobling.org>
178              
179             =head1 CONTRIBUTORS
180              
181             =for stopwords David Kamholz Tomas Doran Karen Etheridge Tom Bloor Christopher Hoskin Ilmari Vacklin
182              
183             =over 4
184              
185             =item *
186              
187             David Kamholz <dkamholz@cpan.org>
188              
189             =item *
190              
191             Tomas Doran <bobtfish@bobtfish.net>
192              
193             =item *
194              
195             Karen Etheridge <ether@cpan.org>
196              
197             =item *
198              
199             Tom Bloor <t.bloor@shadowcat.co.uk>
200              
201             =item *
202              
203             Christopher Hoskin <christopher.hoskin@gmail.com>
204              
205             =item *
206              
207             Ilmari Vacklin <ilmari.vacklin@cs.helsinki.fi>
208              
209             =back
210              
211             =head1 COPYRIGHT AND LICENCE
212              
213             This software is copyright (c) 2005 by יובל קוג'מן (Yuval Kogman).
214              
215             This is free software; you can redistribute it and/or modify it under
216             the same terms as the Perl 5 programming language system itself.
217              
218             =cut