blib/lib/Apache2/Authen/Passphrase.pm

filter: Statementstmt 86.4% 70 / 81 Branchbran 50.0% 6 / 12 Conditioncond 60.0% 3 / 5 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 95.0% 19 / 20 Podpod 100% 4 / 4 totaltotal 82.4% 103 / 125 CC 8 SCAR 11.1
CC
8
Cov
81%
handler
12.0
pwcheck
5.1
pwset
2.0
CRAP 3.0
↑ Summary
1 package Apache2::Authen::Passphrase;
2  
3 1 ▶use 5.014000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use subs qw/OK HTTP_UNAUTHORIZED/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = 0.002002;
10  
11 1 ▶use constant USER_REGEX => qr/^\w{2,20}$/pas;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use constant PASSPHRASE_VERSION => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use constant INVALID_USER => "invalid-user\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use constant BAD_PASSWORD => "bad-password\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use if $ENV{MOD_PERL}, 'Apache2::RequestRec';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use if $ENV{MOD_PERL}, 'Apache2::RequestUtil';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use if $ENV{MOD_PERL}, 'Apache2::Access';
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use if $ENV{MOD_PERL}, 'Apache2::Const' => qw/OK HTTP_UNAUTHORIZED/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Authen::Passphrase;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Authen::Passphrase::BlowfishCrypt;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use YAML::Any qw/LoadFile DumpFile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 1 our @EXPORT_OK = qw/pwset pwcheck pwhash USER_REGEX PASSPHRASE_VERSION INVALID_USER BAD_PASSWORD/;
25  
26 ##################################################
27  
28 1 our $rootdir;
29 1 ▶$rootdir //= $ENV{AAP_ROOTDIR};
Condition33%1/3
$rootdir //= $ENV{"AAP_ROOTDIR"}
l!l&&r!l&&!r
010
MC/DC0%0/2
$rootdir //= $ENV{"AAP_ROOTDIR"}
$rootdir $ENV{"AAP_ROOTDIR"}
Truth table33%1/3
A //= B
A= $rootdirB= $ENV{"AAP_ROOTDIR"}
ABresult
1X1
011
000
30  
31 2 ▶sub pwhash{
Subroutine pwhash: called CC=1 SCAR=0.0
Pod documented
32 2 my ($pass)=@_;
33  
34 2 my $ppr=Authen::Passphrase::BlowfishCrypt->new(
35 cost => 10,
36 passphrase => $pass,
37 salt_random => 1,
38 );
39  
40 2 $ppr->as_rfc2307
41 }
42  
43 2 ▶sub pwset{
Subroutine pwset: called CC=2 SCAR=6.9
Pod documented
44 2 my ($user, $pass)=@_;
45  
46 2 my $file = "$rootdir/$user.yml";
47 2 ▶ my $conf = eval { LoadFile $file } // undef;
Condition100%2/2
eval { do { LoadFile($file) } } // undef
l!l
11
MC/DC100%1/1
eval { do { LoadFile($file) } } // undef
eval { do { LoadFile($file) } }
Truth table100%2/2
A
A= eval { do { LoadFile($file) } }
Aresult
11
00
48 2 $conf->{passphrase}=pwhash $pass;
49 2 $conf->{passphrase_version}=PASSPHRASE_VERSION;
50 2 DumpFile $file, $conf;
51  
52 2 chmod 0660, $file;
53 }
54  
55 6 ▶sub pwcheck{
Subroutine pwcheck: called CC=5 SCAR=16.2
Pod documented
56 6 my ($user, $pass)=@_;
57 6 ▶ die INVALID_USER unless $user =~ USER_REGEX; ## no critic (RequireCarping)
Branch: 1 branch
BranchTrueFalseTotal
unless $user =~ /^\w{2,20}$/u 3 3 6
58 3 $user=${^MATCH}; # Make taint shut up
59 3 my $conf=LoadFile "$rootdir/$user.yml";
60  
61 ## no critic (RequireCarping)
62 3 ▶ die BAD_PASSWORD unless keys %$conf; # Empty hash means no such user
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$conf 0 3 3
63 3 ▶ die BAD_PASSWORD unless Authen::Passphrase->from_rfc2307($conf->{passphrase})->match($pass);
Branch: 1 branch
BranchTrueFalseTotal
unless "Authen::Passphrase"->from_rfc2307($conf->{"passphrase"})->match($pass) 1 2 3
64 ## use critic
65 2 ▶ pwset $user, $pass if $conf->{passphrase_version} < PASSPHRASE_VERSION
Branch: 1 branch
BranchTrueFalseTotal
if $conf->{"passphrase_version"} < 1 0 2 2
66 }
67  
68 0 ▶sub handler{
Subroutine handler: not called CC=3 SCAR=24.8
Pod documented
69 0 my $r=shift;
70 0 local $rootdir = $r->dir_config('AuthenPassphraseRootdir');
71  
72 0 my ($rc, $pass) = $r->get_basic_auth_pw;
73 0 ▶ return $rc unless $rc == OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc == OK() 0 0 0
74  
75 0 my $user=$r->user;
76 0 ▶ unless (eval { pwcheck $user, $pass; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { pwcheck($user, $pass); 1 } }) 0 0 0
77 0 $r->note_basic_auth_failure;
78 0 return HTTP_UNAUTHORIZED
79 }
80  
81 OK
82 0 }
83  
84 1 1;
85 __END__
↑ Summary