File Coverage

blib/lib/String/Validator/Password.pm
Criterion Covered Total %
statement 72 72 100.0
branch 34 34 100.0
condition 6 6 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 122 122 100.0


line stmt bran cond sub pod time code
1             package String::Validator::Password;
2             $String::Validator::Password::VERSION = '2.01'; # TRIAL
3             # ABSTRACT: String::Validator Password Checking Module.
4              
5 12     12   835986 use 5.008;
  12         143  
6 12     12   68 use strict;
  12         25  
  12         239  
7 12     12   55 use warnings;
  12         22  
  12         394  
8 12     12   84 no warnings qw(uninitialized) ;
  12         46  
  12         594  
9 12     12   5705 use String::Validator::Common 2.00;
  12         460628  
  12         2310  
10              
11              
12             my $password_messages = {
13             password_mintypes => sub {
14             my $self = shift @_;
15             return "Input contained $self->{types_found} types of character, $self->{min_types} are required.";
16             },
17             password_minoftype => sub {
18             my ( $required, $type ) = @_;
19             if ( $type eq 'num') { $type = 'numeric'}
20             return "At least $required characters of type $type is required.";
21             },
22             password_typeprohibit => sub {
23             my $type = shift @_;
24             if ( $type eq 'num') { $type = 'numeric'}
25             return "character type $type is prohibited."
26             },
27             };
28              
29             sub new {
30 27     27 1 11376 my $class = shift ;
31 27         101 my $self = { @_ } ;
32 12     12   138 use base ( 'String::Validator::Common' ) ;
  12         32  
  12         7842  
33 27 100       106 unless ( defined $self->{ require_lc } ) { $self->{ require_lc } = 0 };
  26         71  
34 27 100       81 unless ( defined $self->{ require_uc } ) { $self->{ require_uc } = 0 };
  26         65  
35 27 100       77 unless ( defined $self->{ require_num } ) { $self->{ require_num } = 0 };
  22         49  
36 27 100       71 unless ( defined $self->{ require_punct } ) { $self->{ require_punct } = 0 };
  24         49  
37 27 100       101 unless ( defined $self->{ deny_punct } ) { $self->{ deny_punct } = 0 };
  24         56  
38 27 100       74 unless ( defined $self->{ deny_lc } ) { $self->{ deny_lc } = 0 };
  25         62  
39 27 100       85 unless ( defined $self->{ deny_uc } ) { $self->{ deny_uc } = 0 };
  26         64  
40 27 100       207 unless ( defined $self->{ deny_num } ) { $self->{ deny_num } = 0 };
  25         130  
41 27 100       104 unless ( defined $self->{ min_types } ) { $self->{ min_types } = 2 };
  11         91  
42 27 100       79 unless ( defined $self->{ min_len } ) { $self->{ min_len } = 6 };
  14         31  
43 27 100       68 unless ( defined $self->{ max_len } ) { $self->{ max_len } = 64 };
  25         52  
44             # Not implemented right now.
45             # unless ( defined $self->{ dictionary } ) { $self->{ dictionary } = [ 'default' ] }
46             # unless ( defined $self->{ custom_allow } ) { $self->{ custom_allow } = 0 }
47             # unless ( defined $self->{ custom_deny } ) { $self->{ custom_deny } = 0 }
48 27         59 $self->{ string } = '' ;
49 27         69 $self->{ error } = 0 ;
50 27         59 $self->{errstring} = '' ;
51 27         60 bless $self, $class ;
52             $self->{messages}
53             = String::Validator::Common::_Messages(
54 27         203 $password_messages, $self->{language}, $self->{custom_messages} );
55 27         1280 return $self ;
56             }
57              
58             # Does all the checks and returns the
59             # number of errors found. Used by the
60             # Is/IsNot_Valid. May be invoked directly.
61             sub Check{
62 112     112 1 13891 my ( $self, $string1, $string2 ) = @_ ;
63 112 100       479 if ( $self->Start( $string1, $string2 ) == 99 ) {
64 5         159 return $self->{ error } }
65 107         2117 $self->Length;
66             # The match operator cannot be directly used to count matches.
67             # substitution does count replacements, and by removing all other
68             # other character classes what is left over is "punct".
69 107         1368 $string2 = $string1 ; # make sure string is in string2.
70 107   100     789 $self->{num_lc} = $string2 =~ s/[a-z]//g || 0;
71 107   100     531 $self->{num_uc} = $string2 =~ s/[A-Z]//g || 0 ;
72 107   100     505 $self->{num_num} = $string2 =~ s/\d//g || 0;
73 107         238 $self->{num_punct} = length $string2; #What is left is punct.
74 107         187 $self->{ types_found } = 0;
75 107         240 for ( qw / num_lc num_uc num_num num_punct / ) {
76 428 100       1010 if ( $self->{ $_ } ) { $self->{ types_found }++ } }
  271         495  
77 107 100       270 if ( $self->{types_found} < $self->{ min_types } ) {
78             $self->IncreaseErr(
79 9         29 $self->{messages}{password_mintypes}->( $self ));
80             }
81 107         282 foreach my $type ( qw /lc num uc punct/ ) {
82 428         874 my $required = 'require_' . $type ;
83 428         713 my $denied = 'deny_' . $type ;
84 428         1031 my $num = 'num_' . $type ;
85 428 100       1035 unless ( $self->{ $required } <= $self->{ $num } ) {
86             $self->IncreaseErr(
87             $self->{messages}{password_minoftype}->(
88 19         56 $self->{ $required }, $type ) ) }
89 428 100       1194 if ( $self->{ $denied } ) {
90 25 100       84 if ( $self->{ $num } >= $self->{ $denied } )
91             { $self->IncreaseErr(
92 17         59 $self->{messages}{password_typeprohibit}->($type) ) } }
93             } #foreach ( lc num uc punct ).
94 107         603 return $self->{ error } ;
95             }
96              
97              
98             1; # End of Validator
99              
100             __END__