PlugAuth/Plugin/FlatAuth.pm

filter: Statementstmt 24.8% 33 / 133 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 25 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 42.8% 9 / 21 Podpod 85.7% 6 / 7 totaltotal 19.8% 48 / 242 CC 35 SCAR 32.6
CC
35
Cov
17%
_validate_pw
72.0
create_user_cb
46.8
refresh
30.0
CRAP 26.1
« PlugAuth/Plugin/FlatAuthz.pm ↑ Summary PlugAuth/Plugin/Audit.pm »
1 package PlugAuth::Plugin::FlatAuth;
2  
3 # ABSTRACT: Authentication using Flat Files for PlugAuth
4 8 our $VERSION = '0.39'; # VERSION
5  
6  
7 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Log::Log4perl qw( :easy );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 # TODO: maybe optionally use Crypt::Passwd::XS instead
12 8 ▶use Crypt::PasswdMD5 qw( unix_md5_crypt apache_md5_crypt );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Role::Tiny::With;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 with 'PlugAuth::Role::Plugin';
16 8 with 'PlugAuth::Role::Auth';
17 8 with 'PlugAuth::Role::Refresh';
18 8 with 'PlugAuth::Role::Flat';
19  
20 8 our %Userpw; # Keys are usernames, values are lists of crypted passwords.
21  
22 0 ▶sub init {
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
23 0 shift->flat_init;
24 }
25  
26  
27 0 ▶sub refresh {
Subroutine refresh: not called CC=5 SCAR=34.0
Pod documented
28 # Should be called with every request.
29 0 my $config = __PACKAGE__->global_config;
30 0 my @user_files = $config->user_file;
31 0 ▶ if ( grep has_changed($_), @user_files )
Branch: 1 branch
BranchTrueFalseTotal
if (grep has_changed($_), @user_files) 0 0 0
32 {
33 0 my @users = map +{ __PACKAGE__->read_file($_, lc_keys => 1) }, @user_files;
34 0 %Userpw = ();
35 0 for my $list (@users)
36 {
37 0 for my $user (map { lc $_ } keys %$list)
38 {
39 0 ▶ $Userpw{$user} //= [];
Condition0%0/2
$PlugAuth::Plugin::FlatAuth::Userpw{$user} //= []
l!l
00
MC/DC0%0/1
$PlugAuth::Plugin::FlatAuth::Userpw{$user} //= []
$PlugAuth::Plugin::FlatAuth::Userpw{$user}
Truth table0%0/2
A
A= $PlugAuth::Plugin::FlatAuth::Userpw{$user}
Aresult
11
00
40 0 push @{ $Userpw{$user} }, $list->{$user};
41 }
42 }
43  
44 # if the user file has changed, then that may mean the
45 # group file has to be reloaded, for example, for groups
46 # with wildcards * need to be updated.
47 0 mark_changed($config->group_file);
48 }
49 }
50  
51  
52 sub _validate_pw
53 0 ▶{
Subroutine _validate_pw: not called CC=8 SCAR=42.8
54 0 my($plain, $encrypted) = @_;
55 0 ▶ return 1 if do {
Branch: 1 branch
BranchTrueFalseTotal
if do { my $ret = crypt($plain, $encrypted); defined $ret and $ret eq $encrypted } 0 0 0
56 # crypt on an apache apr1 encrypted
57 # password seems to return undef
58 # on Debian 8 (probably others)
59 0 my $ret = crypt($plain, $encrypted);
60 0 ▶ (defined $ret) && ($ret eq $encrypted);
Condition0%0/3
defined $ret && $ret eq $encrypted
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ret && $ret eq $encrypted
defined $ret $ret eq $encrypted
Truth table0%0/3
A && B
A= defined $retB= $ret eq $encrypted
ABresult
0X0
100
111
61 };
62
63 # idea borrowed from Authen::Simple::Password
64 0 ▶ if($encrypted =~ /^\$(\w+)\$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($encrypted =~ /^\$(\w+)\$/) 0 0 0
65 {
66 0 ▶ return 1 if $1 eq 'apr1' && apache_md5_crypt( $plain, $encrypted ) eq $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq "apr1" and apache_md5_crypt($plain, $encrypted) eq $encrypted 0 0 0
Condition0%0/3
$1 eq "apr1" and apache_md5_crypt($plain, $encrypted) eq $encrypted
!ll&&!rl&&r
000
MC/DC0%0/2
$1 eq "apr1" and apache_md5_crypt($plain, $encrypted) eq $encrypted
$1 eq "apr1" apache_md5_crypt($plain, $encrypted) eq $encrypted
Truth table0%0/3
A and B
A= $1 eq "apr1"B= apache_md5_crypt($plain, $encrypted) eq $encrypted
ABresult
0X0
100
111
67  
68 # on at least modern Linux crypt will accept a UNIX
69 # MD5 password, so this may be redundant
70 0 ▶ return 1 if $1 eq '1' && unix_md5_crypt ( $plain, $encrypted ) eq $encrypted;
Branch: 1 branch
BranchTrueFalseTotal
if $1 eq 1 and unix_md5_crypt($plain, $encrypted) eq $encrypted 0 0 0
Condition0%0/3
$1 eq 1 and unix_md5_crypt($plain, $encrypted) eq $encrypted
!ll&&!rl&&r
000
MC/DC0%0/2
$1 eq 1 and unix_md5_crypt($plain, $encrypted) eq $encrypted
$1 eq 1 unix_md5_crypt($plain, $encrypted) eq $encrypted
Truth table0%0/3
A and B
A= $1 eq 1B= unix_md5_crypt($plain, $encrypted) eq $encrypted
ABresult
0X0
100
111
71 }
72 0 return 0;
73 }
74  
75 0 ▶sub check_credentials {
Subroutine check_credentials: not called CC=4 SCAR=30.0
Pod documented
76 0 my ($self, $user,$pw) = @_;
77 0 $user = lc $user;
78  
79 0 ▶ if($pw && $Userpw{$user})
Branch: 1 branch
BranchTrueFalseTotal
if ($pw and $PlugAuth::Plugin::FlatAuth::Userpw{$user}) 0 0 0
Condition0%0/3
$pw and $PlugAuth::Plugin::FlatAuth::Userpw{$user}
!ll&&!rl&&r
000
MC/DC0%0/2
$pw and $PlugAuth::Plugin::FlatAuth::Userpw{$user}
$pw $PlugAuth::Plugin::FlatAuth::Userpw{$user}
Truth table0%0/3
A and B
A= $pwB= $PlugAuth::Plugin::FlatAuth::Userpw{$user}
ABresult
0X0
100
111
80 {
81 0 ▶ return 1 if grep { _validate_pw($pw, $_) } @{ $Userpw{$user} };
Branch: 1 branch
BranchTrueFalseTotal
if grep {_validate_pw($pw, $_);} @{$PlugAuth::Plugin::FlatAuth::Userpw{$user};} 0 0 0
82 }
83 0 return $self->deligate_check_credentials($user, $pw);
84 }
85  
86  
87 sub all_users {
88 return sort keys %Userpw;
89 }
90  
91  
92 sub _created_encrypted_password
93 0 ▶{
Subroutine _created_encrypted_password: not called CC=1 SCAR=6.9
94 0 my($plain) = @_;
95 0 my $salt = join '', ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[rand 64, rand 64];
96 0 apache_md5_crypt($plain, $salt);
97 }
98  
99 0 ▶sub create_user { goto &create_user_cb }
Subroutine create_user: not called CC=1 SCAR=6.9
Pod documented
100  
101 sub create_user_cb
102 0 ▶{
Subroutine create_user_cb: not called CC=7 SCAR=38.5
Pod documented
103 0 my($self, $user, $password, $cb) = @_;
104  
105 0 ▶ unless($user && $password)
Branch: 1 branch
BranchTrueFalseTotal
unless ($user and $password) 0 0 0
Condition0%0/3
$user and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $password
$user $password
Truth table0%0/3
A and B
A= $userB= $password
ABresult
0X0
100
111
106 {
107 0 WARN "User or password not provided";
108 0 return 0;
109 }
110  
111 0 $user = lc $user;
112  
113 0 ▶ if(defined $Userpw{$user})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $PlugAuth::Plugin::FlatAuth::Userpw{$user}) 0 0 0
114 {
115 0 WARN "User $user already exists";
116 0 return 0;
117 }
118  
119 0 foreach my $filename ($self->global_config->user_file)
120 {
121 0 ▶ next unless -w $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -w $filename 0 0 0
122  
123 0 $password = _created_encrypted_password($password);
124  
125 0 ▶ my $ok = $self->lock_and_update_file($filename, sub {
Subroutine __ANON__: not called CC=3 SCAR=20.8
126 8 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 0 my($fh) = @_;
128  
129 0 my $buffer = '';
130 0 ▶ while(! eof $fh)
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
131 {
132 0 my $line = <$fh>;
133 0 chomp $line;
134 0 $buffer .= "$line\n";
135 }
136 0 $buffer .= join(':', $user, $password) . "\n";
137
138 # as a rule we don't update the data structure
139 # directly, we update the config files and let
140 # refresh do that on the next request, but in
141 # this case the callback is used to modify groups,
142 # and for that to work we need to update the
143 # userdatabase first.
144 0 $Userpw{$user} = $password;
145 0 ▶ $cb->() if defined $cb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cb 0 0 0
146
147 0 $buffer;
148 0 });
149  
150 0 ▶ return 0 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 0 0
151  
152 0 INFO "created user $user";
153 0 return 1;
154 }
155  
156 0 ERROR "None of the user files were writable";
157 0 return 0;
158 }
159  
160  
161 sub change_password
162 0 ▶{
Subroutine change_password: not called CC=5 SCAR=32.1
Pod documented
163 0 my($self, $user, $password) = @_;
164  
165 0 ▶ unless($user && $password)
Branch: 1 branch
BranchTrueFalseTotal
unless ($user and $password) 0 0 0
Condition0%0/3
$user and $password
!ll&&!rl&&r
000
MC/DC0%0/2
$user and $password
$user $password
Truth table0%0/3
A and B
A= $userB= $password
ABresult
0X0
100
111
166 {
167 0 WARN "User or password not provided";
168 0 return 0;
169 }
170  
171 0 $user = lc $user;
172  
173 0 ▶ unless(defined $Userpw{$user})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PlugAuth::Plugin::FlatAuth::Userpw{$user}) 0 0 0
174 {
175 0 WARN "User $user does not exist";
176 0 return 0;
177 }
178  
179 0 $password = _created_encrypted_password($password);
180  
181 0 foreach my $filename ($self->global_config->user_file)
182 {
183 0 ▶ $self->lock_and_update_file($filename, sub {
Subroutine __ANON__: not called CC=4 SCAR=26.3
184 8 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 0 my($fh) = @_;
186  
187 0 my $buffer = '';
188
189 0 ▶ while(! eof $fh)
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
190 {
191 0 my $line = <$fh>;
192 0 chomp $line;
193 0 my($thisuser, $oldpassword) = split /:/, $line;
194 0 ▶ if(defined $thisuser && lc($thisuser) eq $user)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $thisuser and lc $thisuser eq $user) { } 0 0 0
Condition0%0/3
defined $thisuser and lc $thisuser eq $user
!ll&&!rl&&r
000
MC/DC0%0/2
defined $thisuser and lc $thisuser eq $user
defined $thisuser lc $thisuser eq $user
Truth table0%0/3
A and B
A= defined $thisuserB= lc $thisuser eq $user
ABresult
0X0
100
111
195 {
196 0 $buffer .= join(':', $user, $password) . "\n";
197 }
198 else
199 {
200 0 $buffer .= "$line\n";
201 }
202 }
203
204 0 $buffer;
205 0 });
206 }
207  
208 0 INFO "user password changed $user";
209 0 return 1;
210 }
211  
212  
213 sub delete_user
214 0 ▶{
Subroutine delete_user: not called CC=3 SCAR=22.5
Pod documented
215 0 my($self, $user) = @_;
216  
217 0 $user = lc $user;
218  
219 0 ▶ unless(defined $Userpw{$user})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $PlugAuth::Plugin::FlatAuth::Userpw{$user}) 0 0 0
220 {
221 0 WARN "User $user does not exist";
222 0 return 0;
223 }
224  
225 0 foreach my $filename ($self->global_config->user_file)
226 {
227 0 ▶ $self->lock_and_update_file($filename, sub {
Subroutine __ANON__: not called CC=4 SCAR=25.8
228 8 ▶ use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
229 0 my($fh) = @_;
230  
231 0 my $buffer = '';
232 0 ▶ while(! eof $fh)
Branch: 1 branch
BranchTrueFalseTotal
unless (eof $fh) 0 0 0
233 {
234 0 my $line = <$fh>;
235 0 chomp $line;
236 0 my($thisuser, $password) = split /:/, $line;
237 0 ▶ next if ($thisuser//'') eq $user;
Branch: 1 branch
BranchTrueFalseTotal
if ($thisuser // "") eq $user 0 0 0
Condition0%0/2
$thisuser // ""
l!l
00
MC/DC0%0/1
$thisuser // ""
$thisuser
Truth table0%0/2
A
A= $thisuser
Aresult
11
00
238 0 $buffer .= "$line\n";
239 }
240 0 $buffer;
241 0 });
242 }
243  
244 0 INFO "deleted user $user";
245 0 return 1;
246 }
247  
248 8 1;
249  
250 __END__
« PlugAuth/Plugin/FlatAuthz.pm ↑ Summary PlugAuth/Plugin/Audit.pm »