| 1 | package MojoX::Authentication::Model::Hash; | ||||||||||||||||||||
| 2 | 13 | { our $VERSION = '0.099001' } | |||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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)
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)
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 45 | 2 | ▶ | $db = slurp($db) if $db !~ m{\A \s* \[ }mxs; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
| |||||||||||||||||||||
| 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
Condition100%2/2
$item->{$key} // ""
MC/DC100%1/1
$item->{$key} // ""
$item->{$key}
Truth table100%2/2
A
A= $item->{$key}
| |||||||||||||||||||||
| 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)
MC/DC100%1/1
$self->db->{$uid} // (return)
$self->db->{$uid}
Truth table100%2/2
A
A= $self->db->{$uid}
| |||||||||||||||||||||
| 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; | |||||||||||||||||||