| 1 | package Crypt::Passphrase; | ||||||||||||||||||
| 2 | 6 | $Crypt::Passphrase::VERSION = '0.024'; | |||||||||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 6 | ▶ | use Carp (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | 6 | ▶ | use Scalar::Util (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 8 | 6 | ▶ | use Encode (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 6 | ▶ | use Unicode::Normalize (); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | |||||||||||||||||||
| 11 | 6 | our @CARP_NOT; | |||||||||||||||||
| 12 | 6 | ▶ | sub import { | ||||||||||||||||
|
Subroutine
import: called CC=7 SCAR=21.4
| |||||||||||||||||||
| 13 | 6 | my ($class, @args) = @_; | |||||||||||||||||
| 14 | 6 | for my $arg (@args) { | |||||||||||||||||
| 15 | 6 | my $caller = caller; | |||||||||||||||||
| 16 | 6 | ▶ | if ($arg eq '-encoder') { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 17 | 3 | require Crypt::Passphrase::Encoder; | |||||||||||||||||
| 18 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 19 | 3 | ▶ | push @{"$caller\::ISA"}, 'Crypt::Passphrase::Encoder' unless $caller->isa('Crypt::Passphrase::Encoder'); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 20 | } | ||||||||||||||||||
| 21 | elsif ($arg eq '-validator') { | ||||||||||||||||||
| 22 | 0 | require Crypt::Passphrase::Validator; | |||||||||||||||||
| 23 | 6 | ▶ | no strict 'refs'; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 24 | 0 | ▶ | push @{"$caller\::ISA"}, 'Crypt::Passphrase::Validator' unless $caller->isa('Crypt::Passphrase::Validator'); | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 25 | } | ||||||||||||||||||
| 26 | elsif ($arg eq '-integration') { | ||||||||||||||||||
| 27 | 3 | push @CARP_NOT, $caller; | |||||||||||||||||
| 28 | } | ||||||||||||||||||
| 29 | else { | ||||||||||||||||||
| 30 | 0 | Carp::croak("Unknown import argument $arg"); | |||||||||||||||||
| 31 | } | ||||||||||||||||||
| 32 | } | ||||||||||||||||||
| 33 | 6 | return; | |||||||||||||||||
| 34 | } | ||||||||||||||||||
| 35 | |||||||||||||||||||
| 36 | 24 | ▶ | sub _load_extension { | ||||||||||||||||
|
Subroutine
_load_extension: called CC=2 SCAR=7.0
| |||||||||||||||||||
| 37 | 24 | my $short_name = shift; | |||||||||||||||||
| 38 | 24 | ▶ | my $module_name = $short_name =~ s/^(\+)?/$1 ? '' : 'Crypt::Passphrase::'/re; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 39 | 24 | my $file_name = "$module_name.pm" =~ s{::}{/}gr; | |||||||||||||||||
| 40 | 24 | require $file_name; | |||||||||||||||||
| 41 | 24 | return $module_name; | |||||||||||||||||
| 42 | } | ||||||||||||||||||
| 43 | |||||||||||||||||||
| 44 | 20 | ▶ | sub _load_encoder { | ||||||||||||||||
|
Subroutine
_load_encoder: called CC=3 SCAR=11.1
| |||||||||||||||||||
| 45 | 20 | my $encoder = shift; | |||||||||||||||||
| 46 | 20 | ▶ | if (Scalar::Util::blessed($encoder)) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 47 | 0 | return $encoder; | |||||||||||||||||
| 48 | } | ||||||||||||||||||
| 49 | elsif (ref $encoder) { | ||||||||||||||||||
| 50 | 16 | my %encoder_conf = %{ $encoder }; | |||||||||||||||||
| 51 | 16 | my $encoder_module = _load_extension(delete $encoder_conf{module}); | |||||||||||||||||
| 52 | 16 | return $encoder_module->new(%encoder_conf); | |||||||||||||||||
| 53 | } | ||||||||||||||||||
| 54 | else { | ||||||||||||||||||
| 55 | 4 | return _load_extension($encoder)->new; | |||||||||||||||||
| 56 | } | ||||||||||||||||||
| 57 | } | ||||||||||||||||||
| 58 | |||||||||||||||||||
| 59 | 4 | ▶ | sub _load_validator { | ||||||||||||||||
|
Subroutine
_load_validator: called CC=4 SCAR=20.7
| |||||||||||||||||||
| 60 | 4 | my $validator = shift; | |||||||||||||||||
| 61 | 4 | ▶ | if (Scalar::Util::blessed($validator)) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 62 | 0 | return $validator; | |||||||||||||||||
| 63 | } | ||||||||||||||||||
| 64 | elsif (ref($validator) eq 'HASH') { | ||||||||||||||||||
| 65 | 0 | my %validator_conf = %{ $validator }; | |||||||||||||||||
| 66 | 0 | my $validator_module = _load_extension(delete $validator_conf{module}); | |||||||||||||||||
| 67 | 0 | return $validator_module->new(%validator_conf); | |||||||||||||||||
| 68 | } | ||||||||||||||||||
| 69 | elsif (ref($validator) eq 'CODE') { | ||||||||||||||||||
| 70 | 0 | require Crypt::Passphrase::Fallback; | |||||||||||||||||
| 71 | 0 | return Crypt::Passphrase::Fallback->new(callback => $validator); | |||||||||||||||||
| 72 | } | ||||||||||||||||||
| 73 | else { | ||||||||||||||||||
| 74 | 4 | return _load_extension($validator)->new; | |||||||||||||||||
| 75 | } | ||||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 6 | my %normalizer_for = ( | |||||||||||||||||
| 79 | C => \&Unicode::Normalize::NFC, | ||||||||||||||||||
| 80 | D => \&Unicode::Normalize::NFD, | ||||||||||||||||||
| 81 | KC => \&Unicode::Normalize::NFKC, | ||||||||||||||||||
| 82 | KD => \&Unicode::Normalize::NFKD, | ||||||||||||||||||
| 83 | ); | ||||||||||||||||||
| 84 | 19 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||
| 85 | 19 | my ($class, %args) = @_; | |||||||||||||||||
| 86 | 19 | ▶ | Carp::croak('No encoder given to Crypt::Passphrase->new') if not $args{encoder}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 87 | 19 | my $encoder = _load_encoder($args{encoder}); | |||||||||||||||||
| 88 | 19 | my @validators = map { _load_validator($_) } @{ $args{validators} }; | |||||||||||||||||
| 89 | 19 | ▶ | my $normalization = $args{normalization} // 'C'; | ||||||||||||||||
|
Condition50%1/2
$args{"normalization"} // "C"
MC/DC0%0/1
$args{"normalization"} // "C"
$args{"normalization"}
Truth table50%1/2
A
A= $args{"normalization"}
| |||||||||||||||||||
| 90 | 19 | ▶ | my $normalizer = ref $normalization eq 'CODE' ? $normalization : $normalizer_for{$normalization}; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 91 | 19 | ▶ | Carp::croak("Invalid normalization form $normalization") if ref $normalizer ne 'CODE'; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 92 | |||||||||||||||||||
| 93 | 19 | my $self = bless { | |||||||||||||||||
| 94 | encoder => $encoder, | ||||||||||||||||||
| 95 | validators => \@validators, | ||||||||||||||||||
| 96 | normalizer => $normalizer, | ||||||||||||||||||
| 97 | }, $class; | ||||||||||||||||||
| 98 | |||||||||||||||||||
| 99 | 19 | return $self; | |||||||||||||||||
| 100 | } | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | 45 | ▶ | sub _normalize_password { | ||||||||||||||||
|
Subroutine
_normalize_password: called CC=2 SCAR=7.1
| |||||||||||||||||||
| 103 | 45 | my ($self, $password) = @_; | |||||||||||||||||
| 104 | 45 | ▶ | my $normalized = $self->{normalizer}->($password // ''); | ||||||||||||||||
|
Condition50%1/2
$password // ""
MC/DC0%0/1
$password // ""
$password
Truth table50%1/2
A
A= $password
| |||||||||||||||||||
| 105 | 45 | return Encode::encode('utf-8-strict', $normalized); | |||||||||||||||||
| 106 | } | ||||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 18 | ▶ | sub hash_password { | ||||||||||||||||
|
Subroutine
hash_password: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 109 | 18 | my ($self, $password) = @_; | |||||||||||||||||
| 110 | 18 | my $normalized = $self->_normalize_password($password); | |||||||||||||||||
| 111 | 18 | return $self->{encoder}->hash_password($normalized); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 23 | ▶ | sub needs_rehash { | ||||||||||||||||
|
Subroutine
needs_rehash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 115 | 23 | my ($self, $hash) = @_; | |||||||||||||||||
| 116 | 23 | return $self->{encoder}->needs_rehash($hash); | |||||||||||||||||
| 117 | } | ||||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 31 | ▶ | sub verify_password { | ||||||||||||||||
|
Subroutine
verify_password: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||
| 120 | 31 | my ($self, $password, $hash) = @_; | |||||||||||||||||
| 121 | |||||||||||||||||||
| 122 | 31 | ▶ | return !!0 if not defined $hash; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 123 | 27 | for my $validator ($self->{encoder}, @{ $self->{validators} }) { | |||||||||||||||||
| 124 | 37 | ▶ | if ($validator->accepts_hash($hash)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 125 | 27 | my $normalized = $self->_normalize_password($password); | |||||||||||||||||
| 126 | 27 | return $validator->verify_password($normalized, $hash); | |||||||||||||||||
| 127 | } | ||||||||||||||||||
| 128 | } | ||||||||||||||||||
| 129 | |||||||||||||||||||
| 130 | 0 | return !!0; | |||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 0 | ▶ | sub recode_hash { | ||||||||||||||||
|
Subroutine
recode_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 134 | 0 | my ($self, @args) = @_; | |||||||||||||||||
| 135 | 0 | return $self->{encoder}->recode_hash(@args); | |||||||||||||||||
| 136 | } | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 0 | ▶ | sub curry_with_hash { | ||||||||||||||||
|
Subroutine
curry_with_hash: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||
| 139 | 0 | my ($self, $hash) = @_; | |||||||||||||||||
| 140 | 0 | require Crypt::Passphrase::PassphraseHash; | |||||||||||||||||
| 141 | 0 | return Crypt::Passphrase::PassphraseHash->new($self, $hash); | |||||||||||||||||
| 142 | } | ||||||||||||||||||
| 143 | |||||||||||||||||||
| 144 | 6 | 1; | |||||||||||||||||
| 145 | |||||||||||||||||||
| 146 | # ABSTRACT: A module for managing passwords in a cryptographically agile manner | ||||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | __END__ | ||||||||||||||||||