File Coverage

blib/lib/Data/Validate/WithYAML/Plugin/PasswordPolicy.pm
Criterion Covered Total %
statement 46 46 100.0
branch 37 40 92.5
condition 13 15 86.6
subroutine 4 4 100.0
pod 1 1 100.0
total 101 106 95.2


line stmt bran cond sub pod time code
1             package Data::Validate::WithYAML::Plugin::PasswordPolicy;
2              
3 1     1   32708 use warnings;
  1         2  
  1         32  
4 1     1   6 use strict;
  1         2  
  1         34  
5              
6 1     1   6 use Carp;
  1         6  
  1         635  
7              
8             # ABSTRACT: Plugin to check passwords against a policy
9              
10             our $VERSION = '0.02';
11              
12             sub check {
13 129     129 1 42071 my ($class, $value, $options) = @_;
14            
15 129 100       297 return unless defined $value;
16 120 100       247 return if $value eq '';
17              
18 111 100       234 return 1 if !$options;
19              
20 105 50       118 my %policy = %{ $options->{'x-policy'} || {} };
  105         426  
21 105 50       412 return 1 if !%policy;
22              
23 105         147 my $return = 1;
24              
25 105 100 66     510 if ( $policy{length} && $policy{length} =~ /,/ ) {
    50          
26 84         394 my ($min,$max) = $policy{length} =~ /\s*(\d+)\s*,(?:\s*(\d+)\s*)?/;
27 84         110 my $bool = 1;
28              
29 84 100 66     360 if(defined $min and length $value < $min){
30 24         38 $bool = 0;
31             }
32              
33 84 100 100     291 if(defined $max and length $value > $max){
34 12         18 $bool = 0;
35             }
36              
37 84         130 $return &= $bool;
38             }
39             elsif ( $policy{length} ) {
40 21         48 $return &= ( $policy{length} == length $value );
41             }
42              
43 105 100       272 return if !$return;
44              
45 57 100 100     180 if ( defined $policy{chars} and !ref $policy{chars} ) {
46 6         14 $policy{chars} = [ $policy{chars} ];
47             }
48              
49             CLASS:
50 57 100       62 for my $class ( @{ $policy{chars} || [] } ) {
  57         211  
51 29         224 my $re = qr/[$class]/;
52 29 100       130 my $matches = $value =~ $re ? 1 : 0;
53 29         35 $return &= $matches;
54              
55 29 100       100 last CLASS if !$return;
56             }
57            
58 57 100       134 return if !$return;
59              
60 52 100 100     165 if ( defined $policy{chars_blacklist} and !ref $policy{chars_blacklist} ) {
61 9         23 $policy{chars_blacklist} = [ $policy{chars_blacklist} ];
62             }
63              
64             CLASS:
65 52 100       59 for my $class ( @{ $policy{chars_blacklist} || [] } ) {
  52         188  
66 18         90 my $re = qr/[$class]/;
67 18 100       76 my $matches = $value =~ $re ? 0 : 1;
68 18         22 $return &= $matches;
69              
70 18 100       65 last CLASS if !$return;
71             }
72            
73 52 100       120 return if !$return;
74 46         201 return 1;
75             }
76              
77             1;
78              
79             __END__