blib/lib/Catalyst/Plugin/Authentication/Credential/CHAP.pm

filter: Statementstmt 34.8% 23 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 53.8% 7 / 13 Podpod 100% 4 / 4 totaltotal 32.6% 34 / 104 CC 11 SCAR 24.4
CC
11
Cov
27%
setup
20.0
chap_init
20.0
_check_password
20.0
CRAP 11.4
↑ Summary
1 package Catalyst::Plugin::Authentication::Credential::CHAP;
2 2 ▶use base qw/ Catalyst::Plugin::Authentication::Credential::Password /;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use vars qw/ $VERSION /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 $VERSION = '0.03';
9  
10 2 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Catalyst::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Digest ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub setup {
Subroutine setup: not called CC=4 SCAR=30.0
Pod documented
15  
16 0 my $c = shift;
17  
18 0 ▶ unless ($c->registered_plugins ('Session')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->registered_plugins("Session")) 0 0 0
19 0 my $err = "The CHAP plugin requires Catalyst::Plugin::Session to be loaded!";
20 0 $c->log->fatal ($err);
21 0 Catalyst::Exception->throw ($err);
22 }
23  
24 # default values
25 0 ▶ $c->config->{authentication}->{chap}->{length} ||= 40;
Condition0%0/2
$c->config->{"authentication"}{"chap"}{"length"} ||= 40
l!l
00
MC/DC0%0/1
$c->config->{"authentication"}{"chap"}{"length"} ||= 40
$c->config->{"authentication"}{"chap"}{"length"}
Truth table0%0/2
A
A= $c->config->{"authentication"}{"chap"}{"length"}
Aresult
11
00
26 0 ▶ $c->config->{authentication}->{chap}->{hash_algorithm} ||= 'MD5';
Condition0%0/2
$c->config->{"authentication"}{"chap"}{"hash_algorithm"} ||= "MD5"
l!l
00
MC/DC0%0/1
$c->config->{"authentication"}{"chap"}{"hash_algorithm"} ||= "MD5"
$c->config->{"authentication"}{"chap"}{"hash_algorithm"}
Truth table0%0/2
A
A= $c->config->{"authentication"}{"chap"}{"hash_algorithm"}
Aresult
11
00
27 0 $c->config->{authentication}->{chap}->{chars} = ['a'..'z', 'A'..'Z', '0'..'9'];
28  
29 0 $c->NEXT::setup (@_);
30  
31 }
32  
33  
34 0 ▶sub chap_init {
Subroutine chap_init: not called CC=4 SCAR=30.0
Pod documented
35 0 my ($c, $force) = shift;
36 0 ▶ $force ||= 0;
Condition0%0/2
$force ||= 0
l!l
00
MC/DC0%0/1
$force ||= 0
$force
Truth table0%0/2
A
A= $force
Aresult
11
00
37 0 ▶ if ($c->session->{_chap_challenge_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->session->{"_chap_challenge_string"}) 0 0 0
38 0 ▶ return unless $force == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless $force == 1 0 0 0
39 }
40 0 $c->gen_challenge_string;
41 }
42  
43 0 ▶sub _generate_challenge_string {
Subroutine _generate_challenge_string: not called CC=2 SCAR=17.9
44 0 my $c = shift;
45 0 my $str;
46 0 my $length = $c->config->{authentication}->{chap}->{length};
47 0 foreach (1..$length) {
48 0 $str .= @{$c->config->{authentication}->{chap}->{chars}}[rand @{$c->config->{authentication}->{chap}->{chars}}];
49 }
50 0 return $str;
51 }
52  
53 0 ▶sub gen_challenge_string {
Subroutine gen_challenge_string: not called CC=1 SCAR=6.9
Pod documented
54 0 my $c = shift;
55 0 my $str = $c->_generate_challenge_string;
56 0 $c->session->{_chap_challenge_string} = $str;
57  
58 }
59  
60 0 ▶sub get_challenge_string {
Subroutine get_challenge_string: not called CC=1 SCAR=6.9
Pod documented
61 0 my $c = shift;
62 0 return $c->session->{_chap_challenge_string};
63 }
64  
65 0 ▶sub _check_password {
Subroutine _check_password: not called CC=4 SCAR=30.0
66 0 my ($c, $user, $password) = @_;
67  
68 0 my $user_password;
69  
70 0 ▶ if ($user->supports (qw/ password clear /)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($user->supports('password', 'clear')) { } 0 0 0
elsif ($user->supports('password', 'hashed')) { } 0 0 0
71 0 $user_password = $user->password;
72 } elsif ($user->supports (qw/ password hashed /)) {
73 0 $user_password = $user->hashed_password;
74 } else {
75 0 Catalyst::Exception->throw("The authentication mechanism must be either clear or hashed.");
76 }
77  
78 0 my $d = Digest->new($c->config->{authentication}->{chap}->{hash_algorithm});
79 0 $d->add ($user_password);
80 0 $d->add ($c->get_challenge_string);
81 0 my $chap_password = $d->hexdigest;
82  
83 0 ▶ if ($c->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->debug) 0 0 0
84 0 $c->log->debug ("User stored password: $user_password");
85 0 $c->log->debug ("User stored challenge: " . $c->get_challenge_string);
86 0 $c->log->debug ("CHAP Hash algorithm: " . $c->config->{authentication}->{chap}->{hash_algorithm});
87 0 $c->log->debug ("User chap password: $chap_password");
88 }
89  
90 0 return (($chap_password eq $password));
91 }
92  
93 2 __PACKAGE__;
94  
95 __END__
↑ Summary