| 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"
MC/DC0%0/1
$self->_config->{"password_field"} ||= "password"
$self->_config->{"password_field"}
Truth table50%1/2
A
A= $self->_config->{"password_field"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 7 | ▶ | $self->_config->{'password_type'} ||= 'clear'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_config->{"password_type"} ||= "clear"
MC/DC0%0/1
$self->_config->{"password_type"} ||= "clear"
$self->_config->{"password_type"}
Truth table50%1/2
A
A= $self->_config->{"password_type"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 7 | ▶ | $self->_config->{'password_hash_type'} ||= 'SHA-1'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_config->{"password_hash_type"} ||= "SHA-1"
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 6 | ▶ | if ($self->check_password($user_obj, $authinfo)) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 6 | return $user_obj; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 1 | ▶ | $c->log->debug( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | unless defined $storedpassword; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | if ($self->_config->{'password_type'} eq 'clear') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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'} || ''
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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $d->add($password); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | $d->add( $self->_config->{'password_post_salt'} || '' ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->_config->{'password_post_salt'} || ''
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'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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)
_secure_compare($computed, $storedpassword) or _secure_compare(unpack("H*", $computed), $storedpassword) or _secure_compare($b64computed, $storedpassword)
_secure_compare($computed, $storedpassword) or _secure_compare(unpack("H*", $computed), $storedpassword)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||||||||||