Auth/Pwd.pm

filter: Statementstmt 96.1% 174 / 181 Branchbran 93.8% 122 / 130 Conditioncond 69.1% 56 / 81 MC/DCmcdc 53.1% 25 / 47 Subroutinesub 100% 21 / 21 Podpod 80.0% 12 / 15 totaltotal 86.3% 410 / 475 CC 98 SCAR 24.1
CC
98
Cov
90%
change_credentials
18.8
enroll
18.2
revoke
16.7
CRAP 11.1
↑ Summary Auth/Generators.pm »
1 package Concierge::Auth::Pwd v0.5.2;
2 7 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # ABSTRACT: Password-file Concierge::Auth backend using Crypt::Passphrase
5  
6 7 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Fcntl qw/:flock/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Crypt::Passphrase;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use parent qw/Concierge::Auth::Base/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 ## Constants for validation
12 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 MIN_ID_LENGTH => 2,
14 MAX_ID_LENGTH => 32,
15 MIN_PASSWORD_LENGTH => 8,
16 MAX_PASSWORD_LENGTH => 72, # bcrypt limit
17 7 };
18  
19 ## Pre-compiled regex for ID validation - accepts email addresses
20 7 my $ID_ALLOWED_CHARS = qr/^[a-zA-Z0-9._@-]+$/;
21 ## Password file field separator
22 7 my $FIELD_SEPARATOR = "\t";
23  
24 ## new: instantiate the auth object with a passwd file
25 ## Complains if no file is provided unless the argument
26 ## no_file => 1 is provided, but still instantiates
27 ## the auth object; without a passwd file, the auth object
28 ## can only provide the utility methods:
29 ## encryptPwd(), gen_random_token(), gen_random_string(),
30 ## gen_word_phrase(), gen_uuid()
31 ## A file may be designated after instantiation with
32 ## the method setFile().
33 ## Dies if it can't open/create a designated file.
34 ## Complains if it can't set permissions on the file.
35 46 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
36 46 my ($class, %args) = @_;
37  
38 46 my $self = bless {
39 auth => Crypt::Passphrase->new(
40 encoder => 'Argon2',
41 validators => [ 'Bcrypt' ],
42 )
43 }, $class;
44  
45 46 ▶ if ($args{no_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"no_file"}) 16 30 46
46 16 carp "Utilities only; no ID and password checks";
47 # Still functional:
48 16 return $self;
49 }
50 30 ▶ unless ($args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"file"}) 1 29 30
51 1 carp "No auth file provided for ID and password checks";
52 # Still functional:
53 1 return $self;
54 }
55  
56 29 ▶ if (-e $args{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $args{'file'}) { } 2 27 29
57 open my $afh, "<", $args{file} or
58 2 ▶ croak ("Can't read auth file ($args{file}). $! ");
Branch: 1 branch
BranchTrueFalseTotal
open my $afh, "<", $args{"file"} or croak(qq[Can't read auth file ($args{"file"}). $! ]) 1 1 2
59 1 close $afh;
60 } else {
61 open my $afh, ">", $args{file} or
62 27 ▶ croak ("Can't open/create auth file ($args{file}). $! ");
Branch: 1 branch
BranchTrueFalseTotal
open my $afh, ">", $args{"file"} or croak(qq[Can't open/create auth file ($args{"file"}). $! ]) 25 2 27
63 25 close $afh;
64 }
65  
66 26 ▶ chmod 0600, $args{file} or carp $!;
Branch: 1 branch
BranchTrueFalseTotal
chmod 0600, $args{"file"} or carp($!) 25 1 26
67 26 $self->{auth}->{file} = $args{file};
68 26 $self;
69 }
70  
71 # =============================================================================
72 # CONTRACT METHODS (Concierge::Auth::Base)
73 # These are the methods Concierge calls, common to every Concierge::Auth
74 # backend. Each is self-contained: password-file I/O is written directly
75 # inline here, with no intermediate backend-primitive methods to hop
76 # through. ID/credential validation is inlined too, except where the same
77 # check is needed by more than one contract method (see validatePwd below).
78 #
79 # Full ID format policy (length, character set) is only enforced in
80 # enroll(), since that's the only place a *new* ID is established and
81 # needs to conform to storage policy going forward. The other four methods
82 # operate on an ID that either does or doesn't already exist on file, so a
83 # malformed-but-nonempty ID simply fails to match -- no separate rejection
84 # message is needed for it.
85 # =============================================================================
86  
87 ## validatePwd: checks password format constraints (length). Needed by
88 ## both enroll and change_credentials (each establishes a new credential
89 ## value), so kept as a shared utility rather than duplicated. Not used by
90 ## authenticate: a wrong-length submitted password simply fails to match
91 ## the stored hash, so a separate format check there would be redundant.
92 53 ▶sub validatePwd ($self, $password) {
Subroutine validatePwd: called CC=5 SCAR=16.1
Pod documented
93 53 ▶ return { success => 0, message => "Password cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $password and length $password > 0 4 49 53
Condition100%3/3
defined $password and length $password > 0
!ll&&!rl&&r
1349
MC/DC100%2/2
defined $password and length $password > 0
defined $password length $password > 0
Truth table100%3/3
A and B
A= defined $passwordB= length $password > 0
ABresult
0X0
100
111
94 unless defined $password && length($password) > 0;
95 49 ▶ return { success => 0, message => sprintf(
Branch: 1 branch
BranchTrueFalseTotal
unless length $password >= 8 and length $password <= 72 6 43 49
Condition100%3/3
length $password >= 8 and length $password <= 72
!ll&&!rl&&r
5143
MC/DC100%2/2
length $password >= 8 and length $password <= 72
length $password >= 8 length $password <= 72
Truth table100%3/3
A and B
A= length $password >= 8B= length $password <= 72
ABresult
0X0
100
111
96 "Password must be between %d and %d characters",
97 MIN_PASSWORD_LENGTH, MAX_PASSWORD_LENGTH
98 ) } unless length($password) >= MIN_PASSWORD_LENGTH
99 && length($password) <= MAX_PASSWORD_LENGTH;
100 43 return { success => 1 };
101 }
102  
103 ## authenticate: verifies a credential (password) for a user_id.
104 ## Pure check, no side effects.
105 10 ▶sub authenticate ($self, $user_id, $credential) {
Subroutine authenticate: called CC=10 SCAR=23.2
Pod documented
106 10 ▶ return { success => 0, message => "ID cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_id and length $user_id > 0 1 9 10
Condition66%2/3
defined $user_id and length $user_id > 0
!ll&&!rl&&r
019
MC/DC50%1/2
defined $user_id and length $user_id > 0
defined $user_id length $user_id > 0
Truth table66%2/3
A and B
A= defined $user_idB= length $user_id > 0
ABresult
0X0
100
111
107 unless defined $user_id && length($user_id) > 0;
108 9 ▶ return { success => 0, message => "Password cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $credential and length $credential > 0 0 9 9
Condition33%1/3
defined $credential and length $credential > 0
!ll&&!rl&&r
009
MC/DC0%0/2
defined $credential and length $credential > 0
defined $credential length $credential > 0
Truth table33%1/3
A and B
A= defined $credentialB= length $credential > 0
ABresult
0X0
100
111
109 unless defined $credential && length($credential) > 0;
110  
111 9 my $sep = $FIELD_SEPARATOR;
112 9 my $pfile = $self->{auth}->{file};
113  
114 9 ▶ open my $pfh, "<", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $pfh, "<", $pfile or return {"success", 0, "message", "authenticate: Cannot open auth file: $!"} 7 2 9
115 or return { success => 0, message => "authenticate: Cannot open auth file: $!" };
116 7 ▶ flock($pfh, LOCK_SH) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $pfh, 1 or do { close $pfh; return {"success", 0, "message", "authenticate: Cannot lock file for reading: $!"} } 6 1 7
117 1 close $pfh;
118 1 return { success => 0, message => "authenticate: Cannot lock file for reading: $!" };
119 };
120 6 ▶ while (<$pfh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pfh)) 5 2 7
121 5 ▶ if (/^\Q$user_id\E$sep([^$sep]+)$sep\|/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\Q$user_id\E$sep([^$sep]+)$sep\|/u) 4 1 5
122 4 my $phash = $1;
123 4 close $pfh;
124 4 ▶ return $self->{auth}->verify_password($credential, $phash)
Branch: 1 branch
BranchTrueFalseTotal
$self->{'auth'}->verify_password($credential, $phash) ? : 2 2 4
125 ? { success => 1 }
126 : { success => 0, message => "authenticate: Invalid password" };
127 }
128 }
129 2 close $pfh;
130 2 return { success => 0, message => "authenticate: User ID not found" };
131 }
132  
133 ## is_id_known: is user_id a known identity in the password file?
134 ## Empty/missing IDs and missing records/files are all simply "not known"
135 ## -- there is no failure branch for this backend, short of a genuine I/O
136 ## error on a file that does exist.
137 13 ▶sub is_id_known ($self, $user_id) {
Subroutine is_id_known: called CC=9 SCAR=22.0
Pod documented
138 13 ▶ return { success => 1, known => 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_id and length $user_id > 0 0 13 13
Condition33%1/3
defined $user_id and length $user_id > 0
!ll&&!rl&&r
0013
MC/DC0%0/2
defined $user_id and length $user_id > 0
defined $user_id length $user_id > 0
Truth table33%1/3
A and B
A= defined $user_idB= length $user_id > 0
ABresult
0X0
100
111
139 unless defined $user_id && length($user_id) > 0;
140  
141 13 my $pfile = $self->{auth}->{file};
142 13 ▶ return { success => 1, known => 0 } unless $pfile && -e $pfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $pfile and -e $pfile 3 10 13
Condition100%3/3
$pfile and -e $pfile
!ll&&!rl&&r
2110
MC/DC100%2/2
$pfile and -e $pfile
$pfile -e $pfile
Truth table100%3/3
A and B
A= $pfileB= -e $pfile
ABresult
0X0
100
111
143  
144 10 ▶ open my $pfh, "<", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $pfh, "<", $pfile or return {"success", 0, "message", "is_id_known: Cannot open auth file: $!"} 9 1 10
145 or return { success => 0, message => "is_id_known: Cannot open auth file: $!" };
146 9 ▶ flock($pfh, LOCK_SH) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $pfh, 1 or do { close $pfh; return {"success", 0, "message", "is_id_known: Cannot lock file for reading: $!"} } 8 1 9
147 1 close $pfh;
148 1 return { success => 0, message => "is_id_known: Cannot lock file for reading: $!" };
149 };
150 8 my $sep = $FIELD_SEPARATOR;
151 8 ▶ while (<$pfh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $pfh)) 5 4 9
152 5 ▶ if (/^\Q$user_id\E$sep/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\Q$user_id\E$sep/u) 4 1 5
153 4 close $pfh;
154 4 return { success => 1, known => 1 };
155 }
156 }
157 4 close $pfh;
158 4 return { success => 1, known => 0 };
159 }
160  
161 ## enroll: establishes user_id as a known identity with the given
162 ## credential. $opts is accepted for interface compatibility with other
163 ## backends but unused here. Fails if the ID is already on file.
164 40 ▶sub enroll ($self, $user_id, $credential, $opts = undef) {
Subroutine enroll: called CC=18 SCAR=29.0
Pod documented
165 40 ▶ return { success => 0, message => "ID cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_id and length $user_id > 0 2 38 40
Condition66%2/3
defined $user_id and length $user_id > 0
!ll&&!rl&&r
0238
MC/DC50%1/2
defined $user_id and length $user_id > 0
defined $user_id length $user_id > 0
Truth table66%2/3
A and B
A= defined $user_idB= length $user_id > 0
ABresult
0X0
100
111
166 unless defined $user_id && length($user_id) > 0;
167 38 ▶ return { success => 0, message => sprintf(
Branch: 1 branch
BranchTrueFalseTotal
unless length $user_id >= 2 and length $user_id <= 32 2 36 38
Condition100%3/3
length $user_id >= 2 and length $user_id <= 32
!ll&&!rl&&r
1136
MC/DC100%2/2
length $user_id >= 2 and length $user_id <= 32
length $user_id >= 2 length $user_id <= 32
Truth table100%3/3
A and B
A= length $user_id >= 2B= length $user_id <= 32
ABresult
0X0
100
111
168 "ID must be between %d and %d characters",
169 MIN_ID_LENGTH, MAX_ID_LENGTH
170 ) } unless length($user_id) >= MIN_ID_LENGTH
171 && length($user_id) <= MAX_ID_LENGTH;
172 36 ▶ return { success => 0, message => "ID contains invalid characters" }
Branch: 1 branch
BranchTrueFalseTotal
unless $user_id =~ /$ID_ALLOWED_CHARS/u 4 32 36
173 unless $user_id =~ $ID_ALLOWED_CHARS;
174  
175 32 my $vp = $self->validatePwd($credential);
176 32 ▶ return $vp unless $vp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $vp->{"success"} 2 30 32
177  
178 30 my $sep = $FIELD_SEPARATOR;
179 30 my $pfile = $self->{auth}->{file};
180  
181 30 ▶ if ($pfile && -e $pfile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pfile and -e $pfile) 26 4 30
Condition100%3/3
$pfile and -e $pfile
!ll&&!rl&&r
1326
MC/DC100%2/2
$pfile and -e $pfile
$pfile -e $pfile
Truth table100%3/3
A and B
A= $pfileB= -e $pfile
ABresult
0X0
100
111
182 26 ▶ open my $chfh, "<", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $chfh, "<", $pfile or return {"success", 0, "message", "enroll: Cannot open auth file: $!"} 25 1 26
183 or return { success => 0, message => "enroll: Cannot open auth file: $!" };
184 25 ▶ flock($chfh, LOCK_SH) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $chfh, 1 or do { close $chfh; return {"success", 0, "message", "enroll: Cannot lock file for reading: $!"} } 24 1 25
185 1 close $chfh;
186 1 return { success => 0, message => "enroll: Cannot lock file for reading: $!" };
187 };
188 24 ▶ while (<$chfh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $chfh)) 23 23 46
189 23 ▶ if (/^\Q$user_id\E$sep/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\Q$user_id\E$sep/u) 1 22 23
190 1 close $chfh;
191 1 return { success => 0, message => "ID $user_id previously used" };
192 }
193 }
194 23 close $chfh;
195 }
196  
197 27 my $phash = $self->{auth}->hash_password($credential);
198  
199 27 ▶ open my $pfh, ">>", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $pfh, ">>", $pfile or return {"success", 0, "message", "enroll: Cannot open auth file: $!"} 25 2 27
200 or return { success => 0, message => "enroll: Cannot open auth file: $!" };
201 25 ▶ flock($pfh, LOCK_EX) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $pfh, 2 or do { close $pfh; return {"success", 0, "message", "enroll: Cannot lock file for writing: $!"} } 24 1 25
202 1 close $pfh;
203 1 return { success => 0, message => "enroll: Cannot lock file for writing: $!" };
204 };
205 24 ▶ print $pfh join( $sep => $user_id, $phash, "|\n") or do {
Branch: 1 branch
BranchTrueFalseTotal
print $pfh join($sep, $user_id, $phash, "|\n") or do { close $pfh; return {"success", 0, "message", "enroll: Cannot write to file: $!"} } 24 0 24
206 0 close $pfh;
207 0 return { success => 0, message => "enroll: Cannot write to file: $!" };
208 };
209 24 ▶ close $pfh or return { success => 0, message => "enroll: Cannot close file: $!" };
Branch: 1 branch
BranchTrueFalseTotal
close $pfh or return {"success", 0, "message", "enroll: Cannot close file: $!"} 24 0 24
210  
211 24 return { success => 1, user_id => $user_id, status => 'created' };
212 }
213  
214 ## change_credentials: replaces the credential on file for an existing
215 ## user_id. Fails if the ID is not known.
216 10 ▶sub change_credentials ($self, $user_id, $new_credential) {
Subroutine change_credentials: called CC=17 SCAR=29.3
Pod documented
217 10 ▶ return { success => 0, message => "ID cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_id and length $user_id > 0 1 9 10
Condition66%2/3
defined $user_id and length $user_id > 0
!ll&&!rl&&r
019
MC/DC50%1/2
defined $user_id and length $user_id > 0
defined $user_id length $user_id > 0
Truth table66%2/3
A and B
A= defined $user_idB= length $user_id > 0
ABresult
0X0
100
111
218 unless defined $user_id && length($user_id) > 0;
219  
220 9 my $vp = $self->validatePwd($new_credential);
221 9 ▶ return $vp unless $vp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $vp->{"success"} 2 7 9
222  
223 7 my $sep = $FIELD_SEPARATOR;
224 7 ▶ my $pfile = $self->{auth}->{file} || '';
Condition100%2/2
$self->{'auth'}{'file'} || ''
l!l
52
MC/DC100%1/1
$self->{'auth'}{'file'} || ''
$self->{'auth'}{'file'}
Truth table100%2/2
A
A= $self->{'auth'}{'file'}
Aresult
11
00
225  
226 7 ▶ return { success => 0, message => "Auth file not OK" }
Branch: 1 branch
BranchTrueFalseTotal
unless $pfile and -e $pfile and -r $pfile 3 4 7
Condition83%5/6
$pfile and -e $pfile and -r $pfile
!ll&&!rl&&r
304
$pfile and -e $pfile
!ll&&!rl&&r
214
MC/DC66%2/3
$pfile and -e $pfile and -r $pfile
$pfile -e $pfile -r $pfile
Truth table75%3/4
A and B and C
A= $pfileB= -e $pfileC= -r $pfile
ABCresult
0XX0
10X0
1100
1111
227 unless $pfile && -e $pfile && -r $pfile;
228  
229 4 my $phash = $self->{auth}->hash_password($new_credential);
230  
231 4 ▶ open my $fh, "+<", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<", $pfile or return {"success", 0, "message", "change_credentials: Cannot open file: $!"} 3 1 4
232 or return { success => 0, message => "change_credentials: Cannot open file: $!" };
233 3 ▶ flock($fh, LOCK_EX) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or do { close $fh; return {"success", 0, "message", "change_credentials: Cannot lock file: $!"} } 2 1 3
234 1 close $fh;
235 1 return { success => 0, message => "change_credentials: Cannot lock file: $!" };
236 };
237 2 my @lines = <$fh>;
238  
239 2 my $success = 0;
240 2 my @output;
241 2 for my $line ( @lines ) {
242 2 ▶ if ( $line =~ /^\Q$user_id\E$sep/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\Q$user_id\E$sep/u) 1 1 2
243 1 push @output => join( $sep => $user_id, $phash, "|\n" );
244 1 $success++;
245 1 next;
246 }
247 1 push @output, $line;
248 }
249 2 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh) 0 2 2
Condition33%3/9
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh
!ll&&!rl&&r
002
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output
!ll&&!rl&&r
002
seek $fh, 0, 0 and truncate $fh, 0
!ll&&!rl&&r
002
MC/DC0%0/4
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh
seek $fh, 0, 0 truncate $fh, 0 print $fh @output close $fh
Truth table20%1/5
A and B and C and D
A= seek $fh, 0, 0B= truncate $fh, 0C= print $fh @outputD= close $fh
ABCDresult
0XXX0
10XX0
110X0
11100
11111
250 seek($fh, 0, 0)
251 and truncate($fh, 0)
252 and print $fh @output
253 and close $fh
254 ) {
255 0 close $fh;
256 0 return { success => 0, message => "change_credentials: File update failed: $!" };
257 }
258  
259 2 ▶ return $success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 1 1 2
260 ? { success => 1, user_id => $user_id }
261 : { success => 0, message => "ID $user_id not found to reset password" };
262 }
263  
264 ## revoke: removes user_id as a known identity. Symmetric with enroll.
265 ## No ID format policy check here -- revoke operates on an existing ID, so
266 ## a malformed-but-nonempty ID just fails to match any record on file.
267 8 ▶sub revoke ($self, $user_id) {
Subroutine revoke: called CC=15 SCAR=28.2
Pod documented
268 8 ▶ return { success => 0, message => "ID cannot be empty" }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_id and length $user_id > 0 1 7 8
Condition66%2/3
defined $user_id and length $user_id > 0
!ll&&!rl&&r
017
MC/DC50%1/2
defined $user_id and length $user_id > 0
defined $user_id length $user_id > 0
Truth table66%2/3
A and B
A= defined $user_idB= length $user_id > 0
ABresult
0X0
100
111
269 unless defined $user_id && length($user_id) > 0;
270  
271 7 my $sep = $FIELD_SEPARATOR;
272 7 ▶ my $pfile = $self->{auth}->{file} || '';
Condition100%2/2
$self->{'auth'}{'file'} || ''
l!l
61
MC/DC100%1/1
$self->{'auth'}{'file'} || ''
$self->{'auth'}{'file'}
Truth table100%2/2
A
A= $self->{'auth'}{'file'}
Aresult
11
00
273  
274 7 ▶ return { success => 0, message => "File $pfile no good" }
Branch: 1 branch
BranchTrueFalseTotal
unless $pfile and -e $pfile 2 5 7
Condition100%3/3
$pfile and -e $pfile
!ll&&!rl&&r
115
MC/DC100%2/2
$pfile and -e $pfile
$pfile -e $pfile
Truth table100%3/3
A and B
A= $pfileB= -e $pfile
ABresult
0X0
100
111
275 unless $pfile && -e $pfile;
276  
277 5 ▶ open my $fh, "+<", $pfile
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<", $pfile or return {"success", 0, "message", "revoke: Cannot open file: $!"} 4 1 5
278 or return { success => 0, message => "revoke: Cannot open file: $!" };
279 4 ▶ flock($fh, LOCK_EX) or do {
Branch: 1 branch
BranchTrueFalseTotal
flock $fh, 2 or do { close $fh; return {"success", 0, "message", "revoke: Cannot lock file: $!"} } 3 1 4
280 1 close $fh;
281 1 return { success => 0, message => "revoke: Cannot lock file: $!" };
282 };
283 3 my @lines = <$fh>;
284  
285 3 my $success = 0;
286 3 my @output;
287 3 for my $line ( @lines ) {
288 3 ▶ if ( $line =~ /^\Q$user_id\E$sep/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\Q$user_id\E$sep/u) 2 1 3
289 2 $success++;
290 2 next;
291 }
292 1 push @output, $line;
293 }
294 3 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh) 0 3 3
Condition33%3/9
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh
!ll&&!rl&&r
003
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output
!ll&&!rl&&r
003
seek $fh, 0, 0 and truncate $fh, 0
!ll&&!rl&&r
003
MC/DC0%0/4
seek $fh, 0, 0 and truncate $fh, 0 and print $fh @output and close $fh
seek $fh, 0, 0 truncate $fh, 0 print $fh @output close $fh
Truth table20%1/5
A and B and C and D
A= seek $fh, 0, 0B= truncate $fh, 0C= print $fh @outputD= close $fh
ABCDresult
0XXX0
10XX0
110X0
11100
11111
295 seek($fh, 0, 0)
296 and truncate($fh, 0)
297 and print $fh @output
298 and close $fh
299 ) {
300 0 close $fh;
301 0 return { success => 0, message => "revoke: File update failed: $!" };
302 }
303  
304 3 ▶ return $success
Branch: 1 branch
BranchTrueFalseTotal
$success ? : 2 1 3
305 ? { success => 1, user_id => $user_id }
306 : { success => 0, message => "ID $user_id not found to delete" };
307 }
308  
309 # =============================================================================
310 # BACKEND-SPECIFIC METHODS
311 # Everything below is specific to how the password-file backend satisfies
312 # the contract above. These are not part of Concierge::Auth::Base and other
313 # backends (e.g. an LDAP backend) are not expected to implement them.
314 # =============================================================================
315  
316 ## Class Methods for Responses
317 ## confirm, reject, reply
318 ## NOT called with object arrow notation:
319 ## $self->reject # !WRONG
320 ## Once instantiated, the auth object will not die/croak;
321 ## Instead, all methods that check or validate respond with
322 ## `confirm ($msg)` # wantarray ? (1, $msg) : 1;
323 ## or
324 ## `reject ($msg)` # wantarray ? (0, $msg) : 0;
325 ## or the more general
326 ## `reply ($bool, $msg)` # wantarray ? ($bool, $msg) : $bool
327 ## Use explicit `return` to assure correct contrl flow:
328 ## `return confirm($msg);`
329 ## `return reply( $result, $msg);`
330 8 ▶sub confirm {
Subroutine confirm: called CC=3 SCAR=11.0
Pod undocumented
331 8 ▶ my $message = shift || "Auth confirmation";
Condition100%2/2
shift() || 'Auth confirmation'
l!l
71
MC/DC100%1/1
shift() || 'Auth confirmation'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
332 8 ▶ wantarray ? (1, $message) : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 2 8
333 }
334 11 ▶sub reject {
Subroutine reject: called CC=3 SCAR=11.0
Pod undocumented
335 11 ▶ my $message = shift || "Auth rejection";
Condition100%2/2
shift() || 'Auth rejection'
l!l
101
MC/DC100%1/1
shift() || 'Auth rejection'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
336 11 ▶ wantarray ? (0, $message) : 0;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 10 1 11
337 }
338 ## First arg is 1|0 or other Perl true/false value
339 9 ▶sub reply {
Subroutine reply: called CC=5 SCAR=16.1
Pod undocumented
340 9 ▶ my $bool = shift // 0;
Condition100%2/2
shift() // 0
l!l
81
MC/DC100%1/1
shift() // 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
341 9 ▶ my $message = shift || ( $bool ? "Auth confirmation" : "Auth rejection" );
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 1 2 3
Condition66%2/3
shift() || ($bool ? 'Auth confirmation' : 'Auth rejection')
l!l&&r!l&&!r
630
MC/DC0%0/2
shift() || ($bool ? 'Auth confirmation' : 'Auth rejection')
shift() ($bool ? 'Auth confirmation' : 'Auth rejection')
Truth table66%2/3
A || B
A= shift()B= ($bool ? 'Auth confirmation' : 'Auth rejection')
ABresult
1X1
011
000
342 9 ▶ wantarray ? ($bool, $message) : $bool;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 1 9
343 }
344  
345 ## Password file handling
346  
347 ## setFile: sets or changes the passwd file
348 ## creates the file if necessary
349 10 ▶sub setFile {
Subroutine setFile: called CC=8 SCAR=21.0
Pod documented
350 10 my $self = shift;
351 10 my $file = shift;
352  
353 10 ▶ return reject( "No filename" ) unless $file =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $file =~ /\S/u 2 8 10
354  
355 8 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) { } 4 4 8
356 4 ▶ open my $afh, "<", $file or
Branch: 1 branch
BranchTrueFalseTotal
open my $afh, "<", $file or return reject("Can't read auth file ($file). $!") 3 1 4
357 return reject( "Can't read auth file ($file). $!" );
358 3 close $afh;
359 } else {
360 4 ▶ open my $afh, ">", $file or
Branch: 1 branch
BranchTrueFalseTotal
open my $afh, ">", $file or return reject("Can't open/create auth file ($file). $!") 3 1 4
361 return reject( "Can't open/create auth file ($file). $!" );
362 3 close $afh;
363 }
364  
365 6 ▶ chmod 0600, $file or carp $!;
Branch: 1 branch
BranchTrueFalseTotal
chmod 0600, $file or carp($!) 5 1 6
366  
367 6 ▶ if ( -e $file && -r $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file and -r $file) 6 0 6
Condition33%1/3
-e $file and -r $file
!ll&&!rl&&r
006
MC/DC0%0/2
-e $file and -r $file
-e $file -r $file
Truth table33%1/3
A and B
A= -e $fileB= -r $file
ABresult
0X0
100
111
368 6 $self->{auth}->{file} = $file;
369 6 return confirm( "Valid file" );
370 }
371  
372 0 return reject( "Invalid file" );
373 }
374  
375 ## rmFile: deletes the passwd file
376 5 ▶sub rmFile {
Subroutine rmFile: called CC=5 SCAR=16.1
Pod documented
377 5 my $self = shift;
378  
379 5 ▶ my $pfile = $self->{auth}->{file} || '';
Condition100%2/2
$self->{'auth'}{'file'} || ''
l!l
32
MC/DC100%1/1
$self->{'auth'}{'file'} || ''
$self->{'auth'}{'file'}
Truth table100%2/2
A
A= $self->{'auth'}{'file'}
Aresult
11
00
380 5 ▶ unless ( $pfile and -e $pfile ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pfile and -e $pfile) 2 3 5
Condition66%2/3
$pfile and -e $pfile
!ll&&!rl&&r
203
MC/DC50%1/2
$pfile and -e $pfile
$pfile -e $pfile
Truth table66%2/3
A and B
A= $pfileB= -e $pfile
ABresult
0X0
100
111
381 2 return reject( "No valid file to remove" );
382 }
383  
384 3 ▶ unless (unlink $pfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (unlink $pfile) 1 2 3
385 1 return reject( "Unable to unlink file: $! " );
386 }
387  
388 2 $self->{auth}->{file} = '';
389  
390 2 return reply( $pfile, "Password file removed" );
391 }
392  
393 2 ▶sub clearFile {
Subroutine clearFile: called CC=3 SCAR=11.0
Pod documented
394 2 my $self = shift;
395  
396 2 my ($pfile,$msg) = $self->rmFile();
397 2 ▶ return reply( 0, "No valid file to clear: $msg" ) unless $pfile;
Branch: 1 branch
BranchTrueFalseTotal
unless $pfile 1 1 2
398  
399 1 my ($ok,$setmsg) = $self->setFile($pfile);
400 1 ▶ return reject( "File not cleared: $setmsg" ) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 1 1
401 1 return confirm( "File cleared" );
402 }
403  
404 ## Utilities
405  
406 ## encryptPwd: returns encrypted password
407 4 ▶sub encryptPwd {
Subroutine encryptPwd: called CC=2 SCAR=6.9
Pod documented
408 4 my $self = shift;
409 4 my $passwd = shift;
410  
411 4 my $vp = $self->validatePwd($passwd);
412 4 ▶ return reject( $vp->{message} ) unless $vp->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $vp->{"success"} 2 2 4
413  
414 2 return $self->{auth}->hash_password($passwd);
415 }
416  
417 ## pfile: returns the passwd file, if any
418 3 ▶sub pfile {
Subroutine pfile: called CC=2 SCAR=6.9
Pod documented
419 3 my $self = shift;
420 return defined $self->{auth}->{file}
421 3 ▶ ? reply($self->{auth}->{file}, "Auth file" )
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'auth'}{'file'} ? : 2 1 3
422 : reject( "No auth file" );
423 }
424  
425 # Generator methods (gen_uuid, gen_random_id, gen_random_token,
426 # gen_random_string, gen_word_phrase, gen_token, gen_crypt_token) are
427 # NOT defined here -- they are inherited as working defaults from
428 # Concierge::Auth::Base, which delegates to Concierge::Auth::Generators.
429 # See L<Concierge::Auth::Base/The Generators Guarantee>.
430  
431 7 1;
432  
433 __END__
↑ Summary Auth/Generators.pm »