Articulate/Authentication/Internal.pm

filter: Statementstmt 41.4% 17 / 41 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 45.4% 5 / 11 Podpod 100% 4 / 4 totaltotal 35.6% 26 / 73 SCAR 31.3
CC
7
Cov
31%
authenticate
20.0
verify_password
12.0
set_password
6.0
CRAP 22.8
« Articulate/Service/SimpleForms.pm ↑ Summary Articulate/Service/Simple.pm »
1 package Articulate::Authentication::Internal;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 4 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Time::HiRes; # overrides time()
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Articulate::Authentication::Internal
14  
15 =cut
16  
17 =head1 METHODS
18  
19 =cut
20  
21 =head3 authenticate
22  
23 $self->authenticate( $credentials );
24  
25 Accepts and returns the credentials if the C<password> matches the C<user_id>. Always returns the credentials passed in.
26  
27 =cut
28  
29 4 has extra_salt => (
30 is => 'rw',
31 default => "If you haven't already, try powdered vegetable bouillon"
32 );
33  
34 sub authenticate {
35 0 ▶ my $self = shift;
Subroutine authenticate: not called CC=4 SCAR=30.0
Pod documented
36 0 my $credentials = shift;
37 0 ▶ my $user_id = $credentials->fields->{user_id} // return;
Condition0%0/2
$credentials->fields->{"user_id"} // (return)
l!l
00
MC/DC0%0/1
$credentials->fields->{"user_id"} // (return)
$credentials->fields->{"user_id"}
Truth table0%0/2
A
A= $credentials->fields->{"user_id"}
Aresult
11
00
38 0 ▶ my $password = $credentials->fields->{password} // return;
Condition0%0/2
$credentials->fields->{"password"} // (return)
l!l
00
MC/DC0%0/1
$credentials->fields->{"password"} // (return)
$credentials->fields->{"password"}
Truth table0%0/2
A
A= $credentials->fields->{"password"}
Aresult
11
00
39  
40 0 ▶ if ( $self->verify_password ( $user_id, $password ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_password($user_id, $password)) 0 0 0
41 0 return $credentials->accept('Passwords match');
42 }
43 # if we ever need to know if the user does not exist, now is the time to ask,
44 # but we do not externally expose the difference between
45 # "user not found" and "password doesn't match"
46 0 return $credentials;
47 }
48  
49 sub _password_salt_and_hash {
50 0 ▶ my $self = shift;
Subroutine _password_salt_and_hash: not called CC=1 SCAR=6.9
51 0 return Digest::SHA::sha512_base64 (
52 $_[0] . $_[1] #:5.10 doesn't like shift . shift
53 );
54 }
55  
56 sub _generate_salt {
57 # pseudorandom salt
58 0 ▶ my $self = shift;
Subroutine _generate_salt: not called CC=1 SCAR=6.9
59 0 return Digest::SHA::sha512_base64 (
60 time . (
61 $self->extra_salt # don't allow the admin not to set a salt:
62 )
63 );
64 }
65  
66 =head3 verify_password
67  
68 $self->verify_password( $user_id, $password );
69  
70 Hashes the password provided with the user's salt and checks to see if the string matches the encrypted password in the user's meta.
71  
72 Returns the result of C<eq>.
73  
74 =cut
75  
76  
77 sub verify_password {
78 0 ▶ my ($self, $user_id, $plaintext_password) = @_;
Subroutine verify_password: not called CC=3 SCAR=24.8
Pod documented
79  
80 0 my $user_meta = $self->storage->get_meta ("/users/$user_id");
81 0 my $real_encrypted_password = $user_meta->{encrypted_password};
82 0 my $salt = $user_meta->{salt};
83  
84 0 ▶ return undef unless defined $real_encrypted_password and defined $plaintext_password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $real_encrypted_password and defined $plaintext_password 0 0 0
Condition0%0/3
defined $real_encrypted_password and defined $plaintext_password
!ll&&!rl&&r
000
MC/DC0%0/2
defined $real_encrypted_password and defined $plaintext_password
defined $real_encrypted_password defined $plaintext_password
Truth table0%0/3
A and B
A= defined $real_encrypted_passwordB= defined $plaintext_password
ABresult
0X0
100
111
85  
86 return (
87 0 $real_encrypted_password
88 eq
89 $self->_password_salt_and_hash ($plaintext_password, $salt)
90 );
91 }
92  
93 =head3 set_password
94  
95 $self->set_password( $user_id, $password );
96  
97 Creates a new pseudorandom salt and uses it to hash the password provided.
98  
99 Amends the C<encrypted_password> and C<salt> fields of the user's meta.
100  
101 =cut
102  
103 # note: currently this implicitly creates a user. Should set/patch create new content, or just edit it?
104 # maybe a create verb - but is is this going to be compatible with kvp stores? How will this work when you have content and meta and settings all to be created?
105 sub set_password {
106 0 ▶ my ($self, $user_id, $plaintext_password) = @_;
Subroutine set_password: not called CC=2 SCAR=17.9
Pod documented
107 0 ▶ return undef unless $plaintext_password; # as empty passwords will only cause trouble.
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext_password 0 0 0
108 0 my $new_salt = $self->_generate_salt;
109 0 $self->storage->patch_meta ( "/user/$user_id", {
110 encrypted_password => $self->_password_salt_and_hash ($plaintext_password, $new_salt),
111 salt => $new_salt
112 } );
113 }
114  
115 =head3 create_user
116  
117 $self->create_user( $user_id, $password );
118  
119 Creates a new user and sets the C<encrypted_password> and C<salt> fields of the user's meta.
120  
121 =cut
122  
123 sub create_user {
124 0 ▶ my ( $self, $user_id, $plaintext_password ) = @_;
Subroutine create_user: not called CC=1 SCAR=6.9
Pod documented
125 0 $self->storage->create("/user/$user_id");
126 0 $self->storage->set_password( $user_id, $plaintext_password );
127 }
128  
129 4 1;
« Articulate/Service/SimpleForms.pm ↑ Summary Articulate/Service/Simple.pm »