Authentication/Credential/Password.pm

filter: Statementstmt 79.6% 51 / 64 Branchbran 46.1% 12 / 26 Conditioncond 15.7% 3 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 60.0% 78 / 130 CC 23 SCAR 37.7
CC
23
Cov
61%
check_password
91.7
new
5.5
authenticate
4.0
CRAP 43.3
« Plugin/Authentication/Credential/Password.pm ↑ Summary Authentication/Credential/Remote.pm »
1 package Catalyst::Authentication::Credential::Password;
2 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 with 'MooseX::Emulate::Class::Accessor::Fast';
6  
7 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Catalyst::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Digest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 __PACKAGE__->mk_accessors(qw/_config realm/);
12  
13 7 ▶sub new {
Subroutine new: called CC=5 SCAR=17.0
Pod documented
14 7 my ($class, $config, $app, $realm) = @_;
15  
16 # Note _config is horrible back compat hackery!
17 7 my $self = { _config => $config };
18 7 bless $self, $class;
19  
20 7 $self->realm($realm);
21  
22 7 ▶ $self->_config->{'password_field'} ||= 'password';
Condition50%1/2
$self->_config->{"password_field"} ||= "password"
l!l
70
MC/DC0%0/1
$self->_config->{"password_field"} ||= "password"
$self->_config->{"password_field"}
Truth table50%1/2
A
A= $self->_config->{"password_field"}
Aresult
11
00
23 7 ▶ $self->_config->{'password_type'} ||= 'clear';
Condition50%1/2
$self->_config->{"password_type"} ||= "clear"
l!l
70
MC/DC0%0/1
$self->_config->{"password_type"} ||= "clear"
$self->_config->{"password_type"}
Truth table50%1/2
A
A= $self->_config->{"password_type"}
Aresult
11
00
24 7 ▶ $self->_config->{'password_hash_type'} ||= 'SHA-1';
Condition50%1/2
$self->_config->{"password_hash_type"} ||= "SHA-1"
l!l
07
MC/DC0%0/1
$self->_config->{"password_hash_type"} ||= "SHA-1"
$self->_config->{"password_hash_type"}
Truth table50%1/2
A
A= $self->_config->{"password_hash_type"}
Aresult
11
00
25  
26 7 my $passwordtype = $self->_config->{'password_type'};
27 7 ▶ if (!grep /$passwordtype/, ('none', 'clear', 'hashed', 'salted_hash', 'crypted', 'self_check')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep /$passwordtype/, ("none", "clear", "hashed", "salted_hash", "crypted", "self_check")) 0 7 7
28 0 Catalyst::Exception->throw(__PACKAGE__ . " used with unsupported password type: " . $self->_config->{'password_type'});
29 }
30 7 return $self;
31 }
32  
33 7 ▶sub authenticate {
Subroutine authenticate: called CC=4 SCAR=13.9
Pod documented
34 7 my ( $self, $c, $realm, $authinfo ) = @_;
35  
36 ## because passwords may be in a hashed format, we have to make sure that we remove the
37 ## password_field before we pass it to the user routine, as some auth modules use
38 ## all data passed to them to find a matching user...
39 7 my $userfindauthinfo = {%{$authinfo}};
40 7 delete($userfindauthinfo->{$self->_config->{'password_field'}});
41  
42 7 my $user_obj = $realm->find_user($userfindauthinfo, $c);
43 7 ▶ if (ref($user_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $user_obj) { } 6 1 7
44 6 ▶ if ($self->check_password($user_obj, $authinfo)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_password($user_obj, $authinfo)) 6 0 6
45 6 return $user_obj;
46 }
47 } else {
48 1 ▶ $c->log->debug(
Branch: 1 branch
BranchTrueFalseTotal
if $c->debug 0 1 1
49 'Unable to locate user matching user info provided in realm: '
50 . $realm->name
51 ) if $c->debug;
52 1 return;
53 }
54 }
55  
56 9 ▶sub check_password {
Subroutine check_password: called CC=14 SCAR=45.2
Pod documented
57 9 my ( $self, $user, $authinfo ) = @_;
58  
59 9 ▶ if ($self->_config->{'password_type'} eq 'self_check') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_config->{'password_type'} eq 'self_check') { } 0 9 9
60 0 return $user->check_password($authinfo->{$self->_config->{'password_field'}});
61 } else {
62 return !!1
63 9 ▶ if $self->_config->{'password_type'} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->_config->{"password_type"} eq "none" 0 9 9
64  
65 9 my $password = $authinfo->{$self->_config->{'password_field'}};
66 9 my $storedpassword = $user->get($self->_config->{'password_field'});
67  
68 # FIXME - Should we warn in the $storedpassword undef case,
69 # as the user probably fluffed the config?
70 9 ▶ return !!0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $storedpassword 1 8 9
71 unless defined $storedpassword;
72  
73 8 ▶ if ($self->_config->{'password_type'} eq 'clear') {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->_config->{'password_type'} eq 'clear') { } 8 0 8
elsif ($self->_config->{'password_type'} eq 'crypted') { } 0 0 0
elsif ($self->_config->{'password_type'} eq 'salted_hash') { } 0 0 0
elsif ($self->_config->{'password_type'} eq 'hashed') { } 0 0 0
74 8 return _secure_compare($password, $storedpassword);
75 } elsif ($self->_config->{'password_type'} eq 'crypted') {
76 0 return _secure_compare(crypt($password, $storedpassword), $storedpassword);
77 } elsif ($self->_config->{'password_type'} eq 'salted_hash') {
78 0 require Crypt::SaltedHash;
79 0 ▶ my $salt_len = $self->_config->{'password_salt_len'} ? $self->_config->{'password_salt_len'} : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_config->{'password_salt_len'} ? : 0 0 0
80 0 return Crypt::SaltedHash->validate( $storedpassword, $password,
81 $salt_len );
82 } elsif ($self->_config->{'password_type'} eq 'hashed') {
83  
84 0 my $d = Digest->new( $self->_config->{'password_hash_type'} );
85 0 ▶ $d->add( $self->_config->{'password_pre_salt'} || '' );
Condition0%0/2
$self->_config->{'password_pre_salt'} || ''
l!l
00
MC/DC0%0/1
$self->_config->{'password_pre_salt'} || ''
$self->_config->{'password_pre_salt'}
Truth table0%0/2
A
A= $self->_config->{'password_pre_salt'}
Aresult
11
00
86 0 $d->add($password);
87 0 ▶ $d->add( $self->_config->{'password_post_salt'} || '' );
Condition0%0/2
$self->_config->{'password_post_salt'} || ''
l!l
00
MC/DC0%0/1
$self->_config->{'password_post_salt'} || ''
$self->_config->{'password_post_salt'}
Truth table0%0/2
A
A= $self->_config->{'password_post_salt'}
Aresult
11
00
88  
89 0 my $computed = $d->clone()->digest;
90 0 my $b64computed = $d->clone()->b64digest;
91 0 ▶ return ( ( _secure_compare($computed, $storedpassword) )
Condition0%0/9
_secure_compare($computed, $storedpassword) || _secure_compare(unpack('H*', $computed), $storedpassword) || _secure_compare($b64computed, $storedpassword) || _secure_compare($b64computed . '=', $storedpassword)
l!l&&r!l&&!r
000
_secure_compare($computed, $storedpassword) or _secure_compare(unpack("H*", $computed), $storedpassword) or _secure_compare($b64computed, $storedpassword)
l!l&&r!l&&!r
000
_secure_compare($computed, $storedpassword) or _secure_compare(unpack("H*", $computed), $storedpassword)
l!l&&r!l&&!r
000
MC/DC0%0/4
_secure_compare($computed, $storedpassword) || _secure_compare(unpack('H*', $computed), $storedpassword) || _secure_compare($b64computed, $storedpassword) || _secure_compare($b64computed . '=', $storedpassword)
_secure_compare($computed, $storedpassword) _secure_compare(unpack("H*", $computed), $storedpassword) _secure_compare($b64computed, $storedpassword) _secure_compare($b64computed . '=', $storedpassword)
Truth table0%0/5
A or B or C || D
A= _secure_compare($computed, $storedpassword)B= _secure_compare(unpack("H*", $computed), $storedpassword)C= _secure_compare($b64computed, $storedpassword)D= _secure_compare($b64computed . '=', $storedpassword)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
92 || ( _secure_compare(unpack("H*", $computed), $storedpassword) )
93 || ( _secure_compare($b64computed, $storedpassword) )
94 || ( _secure_compare($b64computed.'=', $storedpassword)) );
95 }
96 }
97 }
98  
99 # Constant time comparison algorithm to prevent timing attacks. The secret
100 # string should be the second argument, to avoid leaking information about
101 # the length of the string.
102 # (lifted shamelessly from Mojo::Util 9.45)
103 14 ▶sub _secure_compare {
Subroutine _secure_compare: called CC=3 SCAR=11.0
104 14 my ($one, $two) = @_;
105 14 my $r = length $one != length $two;
106 14 ▶ $two = $one if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 2 12 14
107 14 $r |= ord(substr $one, $_) ^ ord(substr $two, $_) for 0 .. length($one) - 1;
108 14 return $r == 0;
109 }
110  
111 5 __PACKAGE__;
112  
113 __END__
« Plugin/Authentication/Credential/Password.pm ↑ Summary Authentication/Credential/Remote.pm »