Authentication/Model/Hash.pm

filter: Statementstmt 100% 71 / 71 Branchbran 88.8% 16 / 18 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 15 / 15 Podpod 100% 3 / 3 totaltotal 94.9% 112 / 118 CC 17 SCAR 17.4
CC
17
Cov
96%
_adjust_db
12.0
slurp
3.3
create
2.0
CRAP 5.7
« Authentication/Model/Role/Remap.pm ↑ Summary Authentication/Model/SAML2.pm »
1 package MojoX::Authentication::Model::Hash;
2 13 { our $VERSION = '0.100' }
3  
4 13 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use experimental qw< signatures >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use JSON::PP qw< decode_json >;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Ouch qw< :trytiny_var >;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use Storable qw< dclone >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶sub slurp ($path) {
Subroutine slurp: called CC=3 SCAR=12.0
13 1 ▶ open my $fh, '<:raw', $path
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $path or ouch(404, "configuration file not found", $path) 1 0 1
14 or ouch 404, 'configuration file not found', $path;
15 1 local $/;
16 1 ▶ my $contents = readline($fh)
Condition33%1/3
readline $fh // ouch(500, "failed reading configuration file", $MojoX::Authentication::Model::Hash::OS_ERROR)
l!l&&r!l&&!r
100
MC/DC0%0/2
readline $fh // ouch(500, "failed reading configuration file", $MojoX::Authentication::Model::Hash::OS_ERROR)
readline $fh ouch(500, "failed reading configuration file", $MojoX::Authentication::Model::Hash::OS_ERROR)
Truth table33%1/3
A // B
A= readline $fhB= ouch(500, "failed reading configuration file", $MojoX::Authentication::Model::Hash::OS_ERROR)
ABresult
1X1
011
000
17 // ouch 500, 'failed reading configuration file', $OS_ERROR;
18 1 return $contents;
19 }
20  
21 13 ▶use constant DEFAULT_NAME => 'hashy';
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 13 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 13 with 'MojoX::Authentication::Model::Role::Local';
26 13 with 'MojoX::Authentication::Model::Role::Creator';
27  
28 13 has db => (is => 'rwp', required => 1);
29 13 has name => (is => 'rw', default => DEFAULT_NAME);
30 13 has _secrets_are_cleartext => (is => 'ro', default => 0,
31 init_arg => 'secrets_are_cleartext');
32  
33 23 ▶sub create ($class, $config, %args) {
Subroutine create: called CC=2 SCAR=7.0
Pod documented
34 23 %args = $class->_create_args(DEFAULT_NAME, $config, %args);
35 23 ▶ ouch 404, 'missing argument for db' unless defined($args{db});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args{"db"} 0 23 23
36 23 my $self = $class->new(%args);
37 23 $self->_adjust_db;
38 21 return $self;
39 }
40  
41 24 ▶sub _adjust_db ($self) {
Subroutine _adjust_db: called CC=12 SCAR=24.8
42 24 my $db = $self->db;
43  
44 24 ▶ if (! ref($db)) { # passed in as a JSON string or a file path?
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $db) 2 22 24
45 2 ▶ $db = slurp($db) if $db !~ m{\A \s* \[ }mxs;
Branch: 1 branch
BranchTrueFalseTotal
unless $db =~ /\A \s* \[ /msux 1 1 2
46 2 $db = decode_json($db);
47 }
48  
49 24 $db = dclone($db);
50 24 my $ref = ref($db);
51 24 ▶ if ($ref eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 6 18 24
elsif ($ref eq 'HASH') { } 17 1 18
52 6 my %new_db;
53 6 for my $item ($db->@*) {
54 ouch 400, 'missing/undefined "username" in Hash db input record'
55 7 ▶ unless defined($item->{username});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item->{"username"} 1 6 7
56 6 $new_db{$item->{username}} = $item;
57 }
58 5 $db = \%new_db;
59 }
60 elsif ($ref eq 'HASH') {
61 17 $db->{$_}{username} = $_ for keys($db->%*);
62 }
63 else {
64 1 ouch 400, 'invalid input for Hash db';
65 }
66  
67 22 ▶ if ($self->_secrets_are_cleartext) { # need hashing?
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_secrets_are_cleartext) 17 5 22
68 17 my $key = $self->key_for_secret;
69 17 my $cp = $self->crypt_passphrase;
70 17 for my $item (values($db->%*)) {
71 20 ▶ next unless length($item->{$key} // '');
Branch: 1 branch
BranchTrueFalseTotal
unless length($item->{$key} // "") 3 17 20
Condition100%2/2
$item->{$key} // ""
l!l
182
MC/DC100%1/1
$item->{$key} // ""
$item->{$key}
Truth table100%2/2
A
A= $item->{$key}
Aresult
11
00
72 17 $item->{$key} = $cp->hash_password($item->{$key});
73 }
74 }
75  
76 22 $self->_set_db($db);
77 22 return;
78 }
79  
80  
81 # in this implementation, username and userid are the same
82 68 ▶sub load_user ($self, $ignored, $uid) {
Subroutine load_user: called CC=2 SCAR=6.9
Pod documented
83 68 ▶ my $item = $self->db->{$uid} // return;
Condition100%2/2
$self->db->{$uid} // (return)
l!l
617
MC/DC100%1/1
$self->db->{$uid} // (return)
$self->db->{$uid}
Truth table100%2/2
A
A= $self->db->{$uid}
Aresult
11
00
84 61 return { $item->%* }; # shallow copy out
85 }
86  
87 # this method is required for compositing
88 # MojoX::Authentication::Model::Role::Local
89 13 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
90 13 *{load_user_by_username} = \&load_user;
91 }
92  
93 3 ▶sub save_user ($self, $user, $is_secret_cleartext = 0) {
Subroutine save_user: called CC=2 SCAR=6.9
Pod documented
94 $self->db->{$user->{username}}
95 3 = $self->user_for_save($user, $is_secret_cleartext);
96 3 return $self;
97 }
98  
99 13 1;
« Authentication/Model/Role/Remap.pm ↑ Summary Authentication/Model/SAML2.pm »