| 1 | package Password::Policy; | ||||||||||||||||||||
| 2 | 4 | $Password::Policy::VERSION = '0.06'; | |||||||||||||||||||
| 3 | # ABSTRACT: Make managing multiple password strength profiles easy | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 4 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 4 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 4 | ▶ | use Class::Load; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 4 | ▶ | use Clone qw/clone/; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 4 | ▶ | use Config::Any; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 4 | ▶ | use Try::Tiny; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 4 | ▶ | use Password::Policy::Exception; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 4 | ▶ | use Password::Policy::Exception::EmptyPassword; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 4 | ▶ | use Password::Policy::Exception::InvalidAlgorithm; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 4 | ▶ | use Password::Policy::Exception::InvalidProfile; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 4 | ▶ | use Password::Policy::Exception::InvalidRule; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | 4 | ▶ | use Password::Policy::Exception::NoAlgorithm; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 4 | ▶ | use Password::Policy::Exception::ReusedPassword; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 3 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 24 | 3 | my ($class, %args) = @_; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 3 | my $config_file = $args{config}; | |||||||||||||||||||
| 27 | 3 | ▶ | my $previous = $args{previous} || []; | ||||||||||||||||||
|
Condition100%2/2
$args{'previous'} || []
MC/DC100%1/1
$args{'previous'} || []
$args{'previous'}
Truth table100%2/2
A
A= $args{'previous'}
| |||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 3 | my $config = Config::Any->load_files({ files => [ $config_file ], use_ext => 1 }); | |||||||||||||||||||
| 30 | 3 | my $rules = {}; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 3 | $config = $config->[0]->{$config_file}; | |||||||||||||||||||
| 33 | 3 | my @profiles = keys(%{$config}); | |||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 3 | my $self = bless { | |||||||||||||||||||
| 36 | _config => $config, | ||||||||||||||||||||
| 37 | _rules => $rules, | ||||||||||||||||||||
| 38 | _previous => $previous, | ||||||||||||||||||||
| 39 | _profiles => \@profiles, | ||||||||||||||||||||
| 40 | } => $class; | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 3 | foreach my $key (@profiles) { | |||||||||||||||||||
| 43 | 12 | $rules->{$key} = $self->_parse_rules($key); | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 3 | $self->{_rules} = $rules; | |||||||||||||||||||
| 47 | 3 | return $self; | |||||||||||||||||||
| 48 | } | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 21 | ▶ | sub _parse_rules { | ||||||||||||||||||
|
Subroutine
_parse_rules: called CC=6 SCAR=17.9
| |||||||||||||||||||||
| 51 | 21 | my ($self, $profile_name) = @_; | |||||||||||||||||||
| 52 | 21 | my $rules; | |||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 21 | my $profile = clone $self->config->{$profile_name}; | |||||||||||||||||||
| 55 | 21 | ▶ | if(my $parent = delete $profile->{inherit}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 56 | 9 | $rules = $self->_parse_rules($parent); | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 21 | foreach my $key (keys(%{$profile})) { | |||||||||||||||||||
| 59 | 57 | ▶ | if($key eq 'algorithm') { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 15 | $rules->{algorithm} = $profile->{$key}; | |||||||||||||||||||
| 61 | 15 | next; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | 42 | ▶ | if($rules->{$key}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 64 | 9 | ▶ | $rules->{$key} = $profile->{$key} if($profile->{$key} > $rules->{$key}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | } else { | ||||||||||||||||||||
| 66 | 33 | $rules->{$key} = $profile->{$key}; | |||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | } | ||||||||||||||||||||
| 69 | 21 | return $rules; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 21 | ▶ | sub config { | ||||||||||||||||||
|
Subroutine
config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 73 | 21 | return (shift)->{_config}; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | 0 | ▶ | sub profiles { | ||||||||||||||||||
|
Subroutine
profiles: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 77 | 0 | return (shift)->{_profiles}; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 16 | ▶ | sub previous { | ||||||||||||||||||
|
Subroutine
previous: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 81 | 16 | return (shift)->{_previous}; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | 16 | ▶ | sub rules { | ||||||||||||||||||
|
Subroutine
rules: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||||||||||
| 85 | 16 | my $self = shift; | |||||||||||||||||||
| 86 | 16 | ▶ | my $profile = shift || 'default'; | ||||||||||||||||||
|
Condition100%2/2
shift() || 'default'
MC/DC100%1/1
shift() || 'default'
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||
| 87 | 16 | my $rules = $self->{_rules}; | |||||||||||||||||||
| 88 | 16 | ▶ | return $rules->{$profile} || Password::Policy::Exception::InvalidProfile->throw; | ||||||||||||||||||
|
Condition33%1/3
$rules->{$profile} || 'Password::Policy::Exception::InvalidProfile'->throw
MC/DC0%0/2
$rules->{$profile} || 'Password::Policy::Exception::InvalidProfile'->throw
$rules->{$profile}
'Password::Policy::Exception::InvalidProfile'->throw
Truth table33%1/3
A || B
A= $rules->{$profile}B= 'Password::Policy::Exception::InvalidProfile'->throw
| |||||||||||||||||||||
| 89 | } | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 12 | ▶ | sub process { | ||||||||||||||||||
|
Subroutine
process: called CC=10 SCAR=24.3
Pod
documented
| |||||||||||||||||||||
| 93 | 12 | my ($self, $args) = @_; | |||||||||||||||||||
| 94 | 12 | ▶ | my $password = $args->{password} || Password::Policy::Exception::EmptyPassword->throw; | ||||||||||||||||||
|
Condition33%1/3
$args->{'password'} || 'Password::Policy::Exception::EmptyPassword'->throw
MC/DC0%0/2
$args->{'password'} || 'Password::Policy::Exception::EmptyPassword'->throw
$args->{'password'}
'Password::Policy::Exception::EmptyPassword'->throw
Truth table33%1/3
A || B
A= $args->{'password'}B= 'Password::Policy::Exception::EmptyPassword'->throw
| |||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 12 | my $rules = $self->rules($args->{profile}); | |||||||||||||||||||
| 97 | 12 | ▶ | my $algorithm = $rules->{algorithm} || Password::Policy::Exception::NoAlgorithm->throw; | ||||||||||||||||||
|
Condition33%1/3
$rules->{'algorithm'} || 'Password::Policy::Exception::NoAlgorithm'->throw
MC/DC0%0/2
$rules->{'algorithm'} || 'Password::Policy::Exception::NoAlgorithm'->throw
$rules->{'algorithm'}
'Password::Policy::Exception::NoAlgorithm'->throw
Truth table33%1/3
A || B
A= $rules->{'algorithm'}B= 'Password::Policy::Exception::NoAlgorithm'->throw
| |||||||||||||||||||||
| 98 | 12 | ▶ | my $algorithm_args = $rules->{algorithm_args} || {}; | ||||||||||||||||||
|
Condition50%1/2
$rules->{'algorithm_args'} || {}
MC/DC0%0/1
$rules->{'algorithm_args'} || {}
$rules->{'algorithm_args'}
Truth table50%1/2
A
A= $rules->{'algorithm_args'}
| |||||||||||||||||||||
| 99 | 12 | foreach my $rule (sort keys(%{$rules})) { | |||||||||||||||||||
| 100 | 33 | ▶ | next if($rule eq 'algorithm'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 21 | my $rule_class = 'Password::Policy::Rule::' . ucfirst($rule); | |||||||||||||||||||
| 103 | 21 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 104 | 21 | Class::Load::load_class($rule_class); | |||||||||||||||||||
| 105 | 0 | ▶ | } catch { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 106 | 0 | Password::Policy::Exception::InvalidRule->throw; | |||||||||||||||||||
| 107 | 21 | }; | |||||||||||||||||||
| 108 | 21 | my $rule_obj = $rule_class->new($rules->{$rule}); | |||||||||||||||||||
| 109 | 21 | my $check = $rule_obj->check($password); | |||||||||||||||||||
| 110 | 17 | ▶ | unless($check) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 111 | # no idea what failed if we didn't get a more specific exception, so | ||||||||||||||||||||
| 112 | # throw a generic error | ||||||||||||||||||||
| 113 | 0 | Password::Policy::Exception->throw; | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 8 | my $enc_password = $self->encrypt({ | |||||||||||||||||||
| 118 | password => $password, | ||||||||||||||||||||
| 119 | algorithm => $algorithm, | ||||||||||||||||||||
| 120 | algorithm_args => $algorithm_args | ||||||||||||||||||||
| 121 | }); | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | # This is a post-encryption rule, so it's a special case. | ||||||||||||||||||||
| 124 | 8 | ▶ | if($self->previous) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 125 | 8 | foreach my $previous_password (@{$self->previous}) { | |||||||||||||||||||
| 126 | 5 | ▶ | Password::Policy::Exception::ReusedPassword->throw if($enc_password eq $previous_password); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | } | ||||||||||||||||||||
| 129 | 6 | return $enc_password; | |||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 8 | ▶ | sub encrypt { | ||||||||||||||||||
|
Subroutine
encrypt: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||
| 134 | 8 | my ($self, $args) = @_; | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 8 | ▶ | my $password = $args->{password} ||Password::Policy::Exception::EmptyPassword->throw; | ||||||||||||||||||
|
Condition33%1/3
$args->{'password'} || 'Password::Policy::Exception::EmptyPassword'->throw
MC/DC0%0/2
$args->{'password'} || 'Password::Policy::Exception::EmptyPassword'->throw
$args->{'password'}
'Password::Policy::Exception::EmptyPassword'->throw
Truth table33%1/3
A || B
A= $args->{'password'}B= 'Password::Policy::Exception::EmptyPassword'->throw
| |||||||||||||||||||||
| 137 | 8 | ▶ | my $algorithm = $args->{algorithm} ||Password::Policy::Exception::NoAlgorithm->throw; | ||||||||||||||||||
|
Condition33%1/3
$args->{'algorithm'} || 'Password::Policy::Exception::NoAlgorithm'->throw
MC/DC0%0/2
$args->{'algorithm'} || 'Password::Policy::Exception::NoAlgorithm'->throw
$args->{'algorithm'}
'Password::Policy::Exception::NoAlgorithm'->throw
Truth table33%1/3
A || B
A= $args->{'algorithm'}B= 'Password::Policy::Exception::NoAlgorithm'->throw
| |||||||||||||||||||||
| 138 | 8 | ▶ | my $algorithm_args = $args->{algorithm_args} || {}; | ||||||||||||||||||
|
Condition50%1/2
$args->{'algorithm_args'} || {}
MC/DC0%0/1
$args->{'algorithm_args'} || {}
$args->{'algorithm_args'}
Truth table50%1/2
A
A= $args->{'algorithm_args'}
| |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 8 | my $enc_class = 'Password::Policy::Encryption::' . $algorithm; | |||||||||||||||||||
| 141 | 8 | ▶ | try { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 142 | 8 | Class::Load::load_class($enc_class); | |||||||||||||||||||
| 143 | 0 | ▶ | } catch { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 144 | 0 | Password::Policy::Exception::InvalidAlgorithm->throw; | |||||||||||||||||||
| 145 | 8 | }; | |||||||||||||||||||
| 146 | 8 | my $enc_obj = $enc_class->new($algorithm_args); | |||||||||||||||||||
| 147 | 8 | my $new_password = $enc_obj->enc($password); | |||||||||||||||||||
| 148 | 8 | return $new_password; | |||||||||||||||||||
| 149 | } | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 4 | 1; | |||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | __END__ | ||||||||||||||||||||