| 1 | package MojoX::Authentication::Model::Role::Local; | ||||||||||||||||||||||||||||||||
| 2 | 14 | { our $VERSION = '0.100' } | |||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 14 | ▶ | use v5.24; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 14 | ▶ | use Moo::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 14 | ▶ | use experimental qw< signatures >; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | 14 | ▶ | use Ouch qw< :trytiny_var >; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 8 | 14 | ▶ | use Scalar::Util qw< blessed >; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 31 | ▶ | sub coerce_crypt_passphrase ($x) { | ||||||||||||||||||||||||||||||
|
Subroutine
coerce_crypt_passphrase: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 11 | 31 | ▶ | return $x if blessed($x); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 12 | 28 | require Crypt::Passphrase; | |||||||||||||||||||||||||||||||
| 13 | 28 | ▶ | return Crypt::Passphrase->new(encoder => 'Argon2', ($x // {})->%*); | ||||||||||||||||||||||||||||||
|
Condition100%2/2
$x // {}
MC/DC100%1/1
$x // {}
$x
Truth table100%2/2
A
A= $x
| |||||||||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 14 | requires 'load_user_by_username'; | |||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | 14 | has crypt_passphrase => ( | |||||||||||||||||||||||||||||||
| 21 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 22 | default => undef, | ||||||||||||||||||||||||||||||||
| 23 | coerce => \&coerce_crypt_passphrase, | ||||||||||||||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | has key_for_secret => ( | ||||||||||||||||||||||||||||||||
| 27 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 28 | default => 'secret', | ||||||||||||||||||||||||||||||||
| 29 | 31 | ▶ | isa => sub ($x) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||
| 30 | 31 | ▶ | ouch 400, 'key_for_secret must be defined' unless defined($x); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 31 | }, | ||||||||||||||||||||||||||||||||
| 32 | 14 | ); | |||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | has protected_keys => ( | ||||||||||||||||||||||||||||||||
| 35 | is => 'ro', | ||||||||||||||||||||||||||||||||
| 36 | 30 | ▶ | default => sub { return [] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 37 | 31 | ▶ | isa => sub ($x) { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||||||
| 38 | 31 | ▶ | ouch 400, 'protected_keys must be an array reference' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | if ref($x) ne 'ARRAY'; | ||||||||||||||||||||||||||||||||
| 40 | }, | ||||||||||||||||||||||||||||||||
| 41 | 14 | init_arg => 'protected_keys', | |||||||||||||||||||||||||||||||
| 42 | ); | ||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||
| 44 | 14 | has _hashed_fake => (is => 'lazy'); | |||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | 7 | ▶ | sub _build__hashed_fake ($self) { $self->hash_secret('foo') } | ||||||||||||||||||||||||||||||
|
Subroutine
_build__hashed_fake: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||
| 48 | 32 | ▶ | sub protected_keys_list ($self) { | ||||||||||||||||||||||||||||||
|
Subroutine
protected_keys_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 49 | 32 | return ($self->key_for_secret, $self->protected_keys->@*); | |||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||
| 52 | 26 | ▶ | sub handles_username ($self, $controller, $name) { | ||||||||||||||||||||||||||||||
|
Subroutine
handles_username: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 53 | 26 | ▶ | return undef unless defined($name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | 26 | ▶ | return 0xdef if $self->load_user_by_username($controller, $name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 7 | return undef; | |||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 10 | ▶ | sub hash_secret ($self, $secret) { | ||||||||||||||||||||||||||||||
|
Subroutine
hash_secret: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 59 | 10 | $self->crypt_passphrase->hash_password($secret); | |||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 15 | ▶ | sub run_fake_validation ($self) { | ||||||||||||||||||||||||||||||
|
Subroutine
run_fake_validation: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 63 | 15 | $self->crypt_passphrase->verify_password(bar => $self->_hashed_fake); | |||||||||||||||||||||||||||||||
| 64 | 15 | ouch 401, 'Unauthorized'; | |||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | 16 | ▶ | sub tamed_user_record ($self, $user) { | ||||||||||||||||||||||||||||||
|
Subroutine
tamed_user_record: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 68 | 16 | my %u = $user->%*; | |||||||||||||||||||||||||||||||
| 69 | 16 | delete($u{$_}) for $self->protected_keys_list; | |||||||||||||||||||||||||||||||
| 70 | 16 | return \%u; | |||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||
| 73 | 8 | ▶ | sub user_for_save ($self, $user, $is_secret_cleartext) { | ||||||||||||||||||||||||||||||
|
Subroutine
user_for_save: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 74 | 8 | my %u = $user->%*; | |||||||||||||||||||||||||||||||
| 75 | 8 | my $key = $self->key_for_secret; | |||||||||||||||||||||||||||||||
| 76 | $u{$key} = $self->hash_secret($u{$key}) | ||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | if length($u{$key} // '') && $is_secret_cleartext; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
length($u{$key} // "") and $is_secret_cleartext
$u{$key} // ""
MC/DC100%2/2
length($u{$key} // "") and $is_secret_cleartext
$u{$key}
$is_secret_cleartext
Truth table100%3/3
A and B
A= $u{$key}B= $is_secret_cleartext
| |||||||||||||||||||||||||||||||||
| 78 | 8 | return \%u; | |||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | 27 | ▶ | sub validate_user ($self, $controller, $name, $secret, $extra) { | ||||||||||||||||||||||||||||||
|
Subroutine
validate_user: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 82 | 27 | ▶ | return unless defined($name); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 83 | 27 | ▶ | my $user = $self->load_user_by_username($controller, $name) or return; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 27 | my $hash = $user->{$self->key_for_secret}; | |||||||||||||||||||||||||||||||
| 85 | 27 | ▶ | return $self->run_fake_validation unless length($hash // ''); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$hash // ""
MC/DC100%1/1
$hash // ""
$hash
Truth table100%2/2
A
A= $hash
| |||||||||||||||||||||||||||||||||
| 86 | 20 | ▶ | $self->crypt_passphrase->verify_password($secret, $hash) | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | or ouch 401, 'Unauthorized'; | ||||||||||||||||||||||||||||||||
| 88 | 13 | return $name; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 14 | 1; | |||||||||||||||||||||||||||||||