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 CC 7 SCAR 21.0
CC
7
Cov
31%
authenticate
20.0
verify_password
12.0
set_password
6.0
CRAP 8.2
« Articulate/Service/SimpleForms.pm ↑ Summary Articulate/Service/List.pm »
1 package Articulate::Authentication::Internal;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 ▶use Digest::SHA;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Time::HiRes; # overrides time()
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Articulate::Authentication::Internal
13  
14 =cut
15  
16 =head1 METHODS
17  
18 =cut
19  
20 =head3 authenticate
21  
22 $self->authenticate( $credentials );
23  
24 Accepts and returns the credentials if the C<password> matches the C<user_id>. Always returns the credentials passed in.
25  
26 =cut
27  
28 6 has extra_salt => (
29 is => 'rw',
30 default => "If you haven't already, try powdered vegetable bouillon"
31 );
32  
33 0 ▶sub authenticate {
Subroutine authenticate: not called CC=4 SCAR=30.0
Pod documented
34 0 my $self = shift;
35 0 my $credentials = shift;
36 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
37 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
38  
39 0 ▶ if ( $self->verify_password( $user_id, $password ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->verify_password($user_id, $password)) 0 0 0
40 0 return $credentials->accept('Passwords match');
41 }
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 0 ▶sub _password_salt_and_hash {
Subroutine _password_salt_and_hash: not called CC=1 SCAR=6.9
50 0 my $self = shift;
51 0 return Digest::SHA::sha512_base64(
52 $_[0] . $_[1] #:5.10 doesn't like shift . shift
53 );
54 }
55  
56 0 ▶sub _generate_salt {
Subroutine _generate_salt: not called CC=1 SCAR=6.9
57  
58 # pseudorandom salt
59 0 my $self = shift;
60 0 return Digest::SHA::sha512_base64(
61 time . (
62 $self->extra_salt # don't allow the admin not to set a salt:
63 )
64 );
65 }
66  
67 =head3 verify_password
68  
69 $self->verify_password( $user_id, $password );
70  
71 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.
72  
73 Returns the result of C<eq>.
74  
75 =cut
76  
77 0 ▶sub verify_password {
Subroutine verify_password: not called CC=3 SCAR=24.8
Pod documented
78 0 my ( $self, $user_id, $plaintext_password ) = @_;
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 return undef
85 0 ▶ 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
86  
87 0 return ( $real_encrypted_password eq
88 $self->_password_salt_and_hash( $plaintext_password, $salt ) );
89 }
90  
91 =head3 set_password
92  
93 $self->set_password( $user_id, $password );
94  
95 Creates a new pseudorandom salt and uses it to hash the password provided.
96  
97 Amends the C<encrypted_password> and C<salt> fields of the user's meta.
98  
99 =cut
100  
101 # note: currently this implicitly creates a user. Should set/patch create new content, or just edit it?
102 # 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?
103 0 ▶sub set_password {
Subroutine set_password: not called CC=2 SCAR=17.9
Pod documented
104 0 my ( $self, $user_id, $plaintext_password ) = @_;
105 return undef
106 0 ▶ unless $plaintext_password; # as empty passwords will only cause trouble.
Branch: 1 branch
BranchTrueFalseTotal
unless $plaintext_password 0 0 0
107 0 my $new_salt = $self->_generate_salt;
108 0 $self->storage->patch_meta(
109 "/user/$user_id",
110 {
111 encrypted_password =>
112 $self->_password_salt_and_hash( $plaintext_password, $new_salt ),
113 salt => $new_salt
114 }
115 );
116 }
117  
118 =head3 create_user
119  
120 $self->create_user( $user_id, $password );
121  
122 Creates a new user and sets the C<encrypted_password> and C<salt> fields of the user's meta.
123  
124 =cut
125  
126 0 ▶sub create_user {
Subroutine create_user: not called CC=1 SCAR=6.9
Pod documented
127 0 my ( $self, $user_id, $plaintext_password ) = @_;
128 0 $self->storage->create("/user/$user_id");
129 0 $self->storage->set_password( $user_id, $plaintext_password );
130 }
131  
132 6 1;
« Articulate/Service/SimpleForms.pm ↑ Summary Articulate/Service/List.pm »