Authentication/Model/Role/Local.pm

filter: Statementstmt 100% 52 / 52 Branchbran 75.0% 15 / 20 Conditioncond 88.8% 8 / 9 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 93.6% 103 / 110 CC 16 SCAR 10.5
CC
16
Cov
93%
validate_user
6.1
user_for_save
4.0
coerce_crypt_passphrase
3.0
CRAP 2.9
« Authentication.pm ↑ Summary Authentication/Util.pm »
1 package MojoX::Authentication::Model::Role::Local;
2 14 { our $VERSION = '0.099' }
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.1
11 31 ▶ return $x if blessed($x);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($x) 3 28 31
12 28 require Crypt::Passphrase;
13 28 ▶ return Crypt::Passphrase->new(encoder => 'Argon2', ($x // {})->%*);
Condition50%1/2
$x // {}
l!l
028
MC/DC0%0/1
$x // {}
$x
Truth table50%1/2
A
A= $x
Aresult
11
00
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
BranchTrueFalseTotal
unless defined $x 0 31 31
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
BranchTrueFalseTotal
if ref $x ne "ARRAY" 0 31 31
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
BranchTrueFalseTotal
unless defined $name 0 26 26
54 26 ▶ return 0xdef if $self->load_user_by_username($controller, $name);
Branch: 1 branch
BranchTrueFalseTotal
if $self->load_user_by_username($controller, $name) 19 7 26
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
BranchTrueFalseTotal
if length($u{$key} // "") and $is_secret_cleartext 3 5 8
Condition100%5/5
length($u{$key} // "") and $is_secret_cleartext
!ll&&!rl&&r
233
$u{$key} // ""
l!l
62
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $name 0 27 27
83 27 ▶ my $user = $self->load_user_by_username($controller, $name) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $user = $self->load_user_by_username($controller, $name) or return 27 0 27
84 27 my $hash = $user->{$self->key_for_secret};
85 27 ▶ return $self->run_fake_validation unless length($hash // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($hash // "") 7 20 27
Condition100%2/2
$hash // ""
l!l
225
MC/DC100%1/1
$hash // ""
$hash
Truth table100%2/2
A
A= $hash
Aresult
11
00
86 20 ▶ $self->crypt_passphrase->verify_password($secret, $hash)
Branch: 1 branch
BranchTrueFalseTotal
$self->crypt_passphrase->verify_password($secret, $hash) or ouch(401, "Unauthorized") 13 7 20
87 or ouch 401, 'Unauthorized';
88 13 return $name;
89 }
90  
91 14 1;
« Authentication.pm ↑ Summary Authentication/Util.pm »