| 1 | package Password::OWASP::AbstractBase; | ||||||||||
| 2 | 7 | our $VERSION = '0.005'; | |||||||||
| 3 | 7 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 7 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | # ABSTRACT: Abstract base class to implement OWASP password recommendations | ||||||||||
| 7 | |||||||||||
| 8 | 7 | ▶ | use Authen::Passphrase; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 7 | ▶ | use Digest::SHA; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 7 | ▶ | use Moose::Util::TypeConstraints qw(enum); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 7 | ▶ | use Try::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 7 | requires qw(ppr); | |||||||||
| 14 | |||||||||||
| 15 | 7 | has cost => ( | |||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => 'Int', | ||||||||||
| 18 | default => 12, | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 7 | has hashing => ( | |||||||||
| 22 | is => 'ro', | ||||||||||
| 23 | isa => enum([qw(sha1 sha256 sha512 none sha384 sha224)]), | ||||||||||
| 24 | lazy => 1, | ||||||||||
| 25 | builder => '_build_hashing_default', | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | 7 | has update_method => ( | |||||||||
| 29 | is => 'ro', | ||||||||||
| 30 | isa => 'CodeRef', | ||||||||||
| 31 | predicate => 'has_update_method', | ||||||||||
| 32 | ); | ||||||||||
| 33 | |||||||||||
| 34 | 6 | ▶ | sub crypt_password { | ||||||||
|
Subroutine
crypt_password: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 35 | 6 | my $self = shift; | |||||||||
| 36 | 6 | my $pass = shift; | |||||||||
| 37 | 6 | my $ppr = $self->ppr->new( | |||||||||
| 38 | cost => $self->cost, | ||||||||||
| 39 | salt_random => 1, | ||||||||||
| 40 | passphrase => $self->prehash_password($pass), | ||||||||||
| 41 | ); | ||||||||||
| 42 | 6 | return $ppr->as_rfc2307; | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 22 | ▶ | sub check_password { | ||||||||
|
Subroutine
check_password: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 46 | 22 | my $self = shift; | |||||||||
| 47 | 22 | my $given = shift; | |||||||||
| 48 | 22 | my $want = shift; | |||||||||
| 49 | |||||||||||
| 50 | 22 | ▶ | my $ok = try { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 51 | 22 | my $ppr = $self->ppr->from_rfc2307($want); | |||||||||
| 52 | 15 | ▶ | return 1 if $ppr->match($self->prehash_password($given)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 22 | }; | |||||||||
| 54 | 22 | ▶ | return 1 if $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 19 | ▶ | return 1 if $self->check_legacy_password($given, $want); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 7 | return 0; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | 19 | ▶ | sub check_legacy_password { | ||||||||
|
Subroutine
check_legacy_password: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 60 | 19 | my ($self, $given, $want) = @_; | |||||||||
| 61 | |||||||||||
| 62 | 19 | ▶ | my $ok = try { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 63 | 19 | my $ppr = $self->ppr->from_rfc2307($want); | |||||||||
| 64 | 12 | ▶ | return 1 if $ppr->match($self->_prehash_password($given)); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 9 | ▶ | return 1 if $ppr->match($given); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 19 | }; | |||||||||
| 67 | 19 | ▶ | if ($ok) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 6 | $self->update_password($given); | |||||||||
| 69 | 6 | return 1; | |||||||||
| 70 | } | ||||||||||
| 71 | |||||||||||
| 72 | 13 | ▶ | $ok = try { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 73 | 13 | my $ppr = Authen::Passphrase->from_rfc2307($want); | |||||||||
| 74 | 8 | return $ppr->match($given); | |||||||||
| 75 | 13 | }; | |||||||||
| 76 | 13 | ▶ | return 0 unless $ok; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 6 | $self->update_password($given); | |||||||||
| 78 | 6 | return 1; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 0 | ▶ | sub _build_hashing_default { | ||||||||
|
Subroutine
_build_hashing_default: not called CC=1 SCAR=6.9
| |||||||||||
| 82 | 0 | my $self = shift; | |||||||||
| 83 | 0 | warn "DEPRECATION: Please supply a hashing default"; | |||||||||
| 84 | 0 | return 'sha512'; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 21 | ▶ | sub prehash_password { | ||||||||
|
Subroutine
prehash_password: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 88 | 21 | my $self = shift; | |||||||||
| 89 | 21 | my $pass = shift; | |||||||||
| 90 | |||||||||||
| 91 | 21 | ▶ | if ($self->hashing eq 'none') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 3 | return $pass; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 18 | my $sha = Digest::SHA->new($self->hashing); | |||||||||
| 96 | 18 | $sha->add($pass); | |||||||||
| 97 | 18 | return $sha->b64digest; | |||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | 12 | ▶ | sub _prehash_password { | ||||||||
|
Subroutine
_prehash_password: called CC=1 SCAR=0.0
| |||||||||||
| 101 | 12 | my $self = shift; | |||||||||
| 102 | 12 | my $pass = shift; | |||||||||
| 103 | |||||||||||
| 104 | 12 | my $sha = Digest::SHA->new('sha512'); | |||||||||
| 105 | 12 | $sha->add($pass); | |||||||||
| 106 | 12 | return $sha->b64digest; | |||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | 0 | ▶ | sub hash_password { | ||||||||
|
Subroutine
hash_password: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 110 | 0 | my $self = shift; | |||||||||
| 111 | 0 | warn "DEPRECATION: Please use prehash_password instead"; | |||||||||
| 112 | 0 | return $self->prehash_password(@_); | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 12 | ▶ | sub update_password { | ||||||||
|
Subroutine
update_password: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 116 | 12 | my ($self, $given) = @_; | |||||||||
| 117 | 12 | ▶ | return 0 unless $self->has_update_method; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | 3 | $self->update_method->($self->crypt_password($given)); | |||||||||
| 119 | 3 | return 1; | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | 7 | 1; | |||||||||
| 123 | |||||||||||
| 124 | __END__ | ||||||||||