blib/lib/CGI/Application/Plugin/Authentication/Driver.pm

filter: Statementstmt 91.3% 63 / 69 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 9 / 9 totaltotal 90.0% 99 / 110 CC 14 SCAR 14.8
CC
14
Cov
89%
filter
8.0
check_filtered
6.0
find_option
4.0
CRAP 4.4
« blib/lib/CGI/Application/Plugin/Authentication/Driver/Authen/Simple.pm ↑ Summary Build »
1 package CGI::Application::Plugin::Authentication::Driver;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 CGI::Application::Plugin::Authentication::Driver - Base module for building driver classes
11 for CGI::Application::Plugin::Authentication
12  
13  
14 =head1 SYNOPSIS
15  
16 package CGI::Application::Plugin::Authentication::Driver::MyDriver;
17 use base qw(CGI::Application::Plugin::Authentication::Driver);
18  
19 sub verify_credentials {
20 my $self = shift;
21 my @credentials = @_;
22  
23 if ( >>> Validate Credentials <<< ) {
24 return $credentials[0];
25 }
26 return;
27 }
28  
29  
30 =head1 DESCRIPTION
31  
32 This module is a base class for all driver classes for the L<CGI::Application::Plugin::Authentication>
33 plugin. Each driver class is required to provide only one method to validate the given credentials.
34 Normally only two credentials will be passed in (username and password), but you can configure the plugin
35 to handle any number of credentials (for example you may require the user to enter a group name, or domain name
36 as well as a username and password).
37  
38  
39 =head1 FIELD FILTERS
40  
41 It is quite common for passwords to be stored using some form of one way encryption. Unix crypt being the
42 old standard in the Unix community, however MD5 or SHA1 hashes are more popular today. In order to
43 simplify the validation routines some methods have been provided to help test these passwords. When
44 configuring a Driver (and if the driver supports it), you can specify which fields are encoded, and which
45 method is used for the encoding by specifying a filter on the field in question.
46  
47 CREDENTIALS => ['authen_username', 'authen_password'],
48 DRIVERS => [ 'DBI',
49 DSN => '...',
50 TABLE => 'users',
51 CONSTRAINTS => {
52 username => '__CREDENTIAL_1__',
53 'MD5:password' => '__CREDENTIAL_2__',
54 }
55 ],
56  
57 Here we are saying that the password field is encoded using an MD5 hash, and should be checked accordingly.
58  
59 =head2 Filter options
60  
61 Some of the filters may have multiple forms. For example there are three forms of MD5 hashes: binary, base64 and hex.
62 You can specify these extra options by using an underscore to separate it from the filter name.
63  
64 'MD5_base64:password'
65  
66  
67 =head2 Chained Filters
68  
69 it is possible to chain multiple filters. This can be useful if your MD5 strings are stored in hex format. Hex numbers are
70 case insensitive, so the may be stored in either upper or lower case. To make this consistent, you can MD5 encode the
71 password first, and then upper case the results. The filters are applied from the inside out:
72  
73 'uc:MD5_hex:password'
74  
75 =head2 Custom Filters
76  
77 If your field is encoded using a custom technique, then you can provide a custom filter function. This can be
78 be done by providing a FILTERS option that contains a hash of filter subroutines that are keyed by their name.
79 You can then use the filter name on any of the fields as if it was a builtin filter.
80  
81 CREDENTIALS => ['authen_username', 'authen_password'],
82 DRIVERS => [ 'DBI',
83 DSN => '...',
84 TABLE => 'users',
85 CONSTRAINTS => {
86 username => '__CREDENTIAL_1__',
87 'rot13:password' => '__CREDENTIAL_2__',
88 }
89 FILTERS => { rot13 => \&rot13_filter },
90 ],
91  
92 sub rot13_filter {
93 my $param = shift;
94 my $value = shift;
95 $value =~ tr/A-Za-z/N-ZA-Mn-za-m/;
96 return $value;
97 }
98  
99 Please see the documentation for the driver that you are using to make sure that it supports encoded
100 fields.
101  
102  
103 =head2 Builtin Filters
104  
105 Here is a list of the filters that are provided with this module:
106  
107 =over 4
108  
109 =item crypt - provided by perl C<crypt> function
110  
111 =item MD5 - requires Digest::MD5
112  
113 =item SHA1 - requires Digest::SHA1
114  
115 =item uc - provided by the perl C<uc> function
116  
117 =item lc - provided by the perl C<lc> function
118  
119 =item trim - removed whitespace from the start and end of the field
120  
121 =back
122  
123  
124 =head1 METHODS
125  
126 =head2 new
127  
128 This is a constructor that can create a new Driver object. It requires an Authentication object as its
129 first parameter, and any number of other parameters that will be used as options depending on which
130 Driver object is being created. You shouldn't need to call this as the Authentication plugin takes care
131 of creating Driver objects.
132  
133 =cut
134  
135 sub new {
136 116 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
137 116 my $self = {};
138 116 my $authen = shift;
139 116 my @options = @_;
140  
141 116 bless $self, $class;
142 116 $self->{authen} = $authen;
143 116 Scalar::Util::weaken($self->{authen}); # weaken circular reference
144 116 $self->{options} = \@options;
145 116 $self->initialize;
146 116 return $self;
147 }
148  
149 =head2 initialize
150  
151 This method will be called right after a new Driver object is created. So any startup customizations
152 can be dealt with here.
153  
154 =cut
155  
156 sub initialize {
157 116 ▶ my $self = shift;
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
158 # override this in the subclass if you need it
159 116 return;
160 }
161  
162 =head2 options
163  
164 This will return a list of options that were provided when this driver was configured by the user.
165  
166 =cut
167  
168 112 ▶sub options { return (@{$_[0]->{options}}) }
Subroutine options: called CC=1 SCAR=0.0
Pod documented
169  
170 =head2 authen
171  
172 This will return the underlying L<CGI::Application::Plugin::Authentication> object. In most cases it will
173 not be necessary to access this.
174  
175 =cut
176  
177 1 ▶sub authen { return $_[0]->{authen} }
Subroutine authen: called CC=1 SCAR=0.0
Pod documented
178  
179 =head2 find_option
180  
181 This method will search the Driver options for a specific key and return
182 the value it finds.
183  
184 =cut
185  
186 sub find_option {
187 12 ▶ my $self = shift;
Subroutine find_option: called CC=4 SCAR=13.9
Pod documented
188 12 my $key = shift;
189 12 my @options = $self->options;
190 12 my $marker = 0;
191 12 foreach my $option (@options) {
192 20 ▶ if ($marker) {
Branch: 2 branches
BranchTrueFalseTotal
if ($marker) { } 7 13 20
elsif ($option eq $key) { } 7 6 13
193 7 return $option;
194 } elsif ($option eq $key) {
195 # We need the next element
196 7 $marker = 1;
197 }
198 }
199 5 return;
200 }
201  
202 =head2 verify_credentials
203  
204 This method needs to be provided by the driver class. It needs to be an object method that accepts a list of
205 credentials, and will verify that the credentials are valid, and return a username that will be used to identify
206 this login (usually you will just return the value of the first credential, however you are not bound to that)..
207  
208 =cut
209  
210 sub verify_credentials {
211 1 ▶ die "verify_credentials must be implemented in the subclass";
Subroutine verify_credentials: called CC=1 SCAR=0.0
Pod documented
212 }
213  
214 =head2 filter
215  
216 This method can be used to filter a field (usually password fields) using a number of standard or
217 custom encoding techniques. See the section on Builtin Filters above to see what filters are available
218 When using a custom filter, you will need to provide a FILTERS option in the configuration of the DRIVER (See the
219 section on FIELD FILTERS above for an example). By default, if no filter is specified, it is
220 returned as is. This means that you can run all fields through this function even if they
221 don't have any filters to simplify the driver code.
222  
223 my $filtered = $self->filter('MD5_hex:password', 'foobar');
224  
225 - or -
226  
227 # custom filter
228 my $filtered = $self->filter('foobar:password', 'foo');
229  
230 - or -
231  
232 # effectively a noop
233 my $filtered = $self->filter('username', 'foo');
234  
235  
236  
237 =cut
238  
239 sub filter {
240 20 ▶ my $self = shift;
Subroutine filter: called CC=8 SCAR=20.8
Pod documented
241 20 my $field = shift;
242 20 my $plain = shift;
243 20 my @other = shift;
244  
245 20 ▶ return unless defined $plain;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $plain 0 20 20
246  
247 20 my @filters = split /\:/, $field;
248 20 my $fieldname = pop @filters;
249  
250 20 my $filtered = $plain;
251 20 foreach my $filter (reverse @filters) {
252  
253 27 my ($filter_name, $param) = split /_/, $filter;
254 27 my $class = 'CGI::Application::Plugin::Authentication::Driver::Filter::' . lc $filter_name;
255 27 ▶ if ( $class->require ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->require) { } 21 6 27
256 # found a filter
257 21 $filtered = $class->filter( $param, $filtered, @other );
258 } else {
259 # see if the configuration has a custom filter defined
260 6 my $custom_filters = $self->find_option('FILTERS');
261 6 ▶ if ( $custom_filters ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_filters) { } 4 2 6
262 4 ▶ die "the FILTERS configuration option must be a hashref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $custom_filters eq "HASH" 1 3 4
263 unless ref( $custom_filters ) eq 'HASH';
264 3 ▶ if ( $custom_filters->{$filter_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($custom_filters->{$filter_name}) { } 2 1 3
265 die "the '$filter' filter listed in FILTERS must be a subroutine reference"
266 2 ▶ unless ref( $custom_filters->{$filter_name} ) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $custom_filters->{$filter_name} eq "CODE" 1 1 2
267 1 $filtered = $custom_filters->{$filter_name}->( $param, $filtered, @other );
268 } else {
269 1 die "No filter found for '$filter_name'";
270 }
271 } else {
272 2 die "No filters found for '$filter'";
273 }
274 }
275 }
276 11 return $filtered;
277 }
278  
279 =head2 check_filtered
280  
281 This method can be used to test filtered fields (usually password fields) against a number of standard or
282 custom encoding techniques. The following encoding techniques are provided: plain, MD5, SHA1, crypt.
283 When using a custom encoder, you will need to provide it in the configuration of the DRIVERS (See the
284 section on ENCODED PASSWORDS above for an example). By default, if no encoding is specified, it is
285 assumed to be 'plain'. This means that you can run all fields through this function even if they
286 don't have any encoding to simplify the driver code.
287  
288 my $verified = $self->check_filtered('MD5:password', 'foobar', 'OFj2IjCsPJFfMAxmQxLGPw');
289  
290 - or -
291  
292 # custom encoder
293 my $verified = $self->check_filtered('foobar:password', 'foo', 'bar');
294  
295 - or -
296  
297 # a field that isn't filtered (effectively just checks for equality on second and third args)
298 my $verified = $self->check_filtered('username', 'foobar', 'foobar');
299 my $verified = $self->check_filtered('plain:username', 'foobar', 'foobar');
300  
301 =cut
302  
303 sub check_filtered {
304 0 ▶ my $self = shift;
Subroutine check_filtered: not called CC=2 SCAR=17.9
Pod documented
305 0 my $field = shift;
306 0 my $plain = shift;
307 0 my $filtered = shift;
308  
309 0 ▶ return ($self->filter($field, $plain, $filtered) eq $filtered) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->filter($field, $plain, $filtered) eq $filtered ? : 0 0 0
310 }
311  
312 =head2 strip_field_names
313  
314 This method will take a field name (or list of names) and strip off the leading encoding type.
315 For example if you passed in 'MD5:password' the method would return 'password'.
316  
317 my $fieldname = $self->strip_field_names('MD5:password');
318  
319 =cut
320  
321 sub strip_field_names {
322 2 ▶ my $self = shift;
Subroutine strip_field_names: called CC=3 SCAR=11.3
Pod documented
323 2 my @fields = @_;
324  
325 2 foreach (@fields) {
326 2 s/^.*://;
327 }
328 2 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
329 0 return @fields;
330 } else {
331 2 return $fields[0];
332 }
333 }
334  
335  
336 =head1 SEE ALSO
337  
338 L<CGI::Application::Plugin::Authentication>, perl(1)
339  
340  
341 =head1 AUTHOR
342  
343 Cees Hek <ceeshek@gmail.com>
344  
345  
346 =head1 LICENCE AND COPYRIGHT
347  
348 Copyright (c) 2005, SiteSuite. All rights reserved.
349  
350 This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
351  
352  
353 =head1 DISCLAIMER OF WARRANTY
354  
355 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR, OR CORRECTION.
356  
357 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
358  
359 =cut
360  
361 20 1;
« blib/lib/CGI/Application/Plugin/Authentication/Driver/Authen/Simple.pm ↑ Summary Build »