Credential.pm

filter: Statementstmt 92.6% 88 / 95 Branchbran 68.4% 26 / 38 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 80.7% 143 / 177 CC 27 SCAR 18.1
CC
27
Cov
82%
hash
12.0
parse
11.3
prepare
6.7
CRAP 6.1
« Credential/x509.pm ↑ Summary Credential/plain.pm »
1 #+##############################################################################
2 # #
3 # File: Authen/Credential.pm #
4 # #
5 # Description: abstraction of a credential #
6 # #
7 #-##############################################################################
8  
9 #
10 # module definition
11 #
12  
13 package Authen::Credential;
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 our $VERSION = "1.2";
17 2 our $REVISION = sprintf("%d.%02d", q$Revision: 1.12 $ =~ /(\d+)\.(\d+)/);
18  
19 #
20 # used modules
21 #
22  
23 2 ▶use No::Worries::Die qw(dief);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Params::Validate qw(validate_with validate_pos :types);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use URI::Escape qw(uri_escape uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #
28 # global variables
29 #
30  
31 our(
32 2 $_IdRe, # regexp matching an identifier
33 $_ValChars, # set of all allowed value characters
34 $_SepChars, # set of all allowed separator characters
35 %_LoadedModule, # hash of successfully loaded modules
36 %ValidationSpec, # per-scheme Params::Validate specification
37 %Preparator, # per-scheme and target preparator code
38 );
39  
40 2 $_IdRe = qr{[a-z][a-z0-9]*};
41 2 $_ValChars = q{a-zA-Z0-9/\-\+\_\~\.\:};
42 2 $_SepChars = q{\,\ };
43  
44 #+++############################################################################
45 # #
46 # helper functions #
47 # #
48 #---############################################################################
49  
50 #
51 # make sure a module is loaded
52 #
53  
54 19 ▶sub _require ($) {
Subroutine _require: called CC=3 SCAR=11.0
55 19 my($module) = @_;
56  
57 19 ▶ return if $_LoadedModule{$module};
Branch: 1 branch
BranchTrueFalseTotal
if $Authen::Credential::_LoadedModule{$module} 15 4 19
58 4 eval("require $module"); ## no critic 'ProhibitStringyEval'
59 4 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 1 3 4
60 1 $@ =~ s/\s+at\s.+?\sline\s+\d+\.?$//;
61 1 dief("failed to load %s: %s", $module, $@);
62 } else {
63 3 $_LoadedModule{$module} = 1;
64 }
65 }
66  
67 #
68 # check that the data matches the per-scheme specification
69 #
70  
71 27 ▶sub _check ($@) {
Subroutine _check: called CC=2 SCAR=7.0
72 27 my($scheme);
73  
74 27 $scheme = shift(@_);
75 dief("invalid credential scheme (missing validation spec): %s", $scheme)
76 27 ▶ unless $ValidationSpec{$scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless $Authen::Credential::ValidationSpec{$scheme} 0 27 27
77 return(validate_with(
78 params => \@_,
79 spec => {
80 27 %{ $ValidationSpec{$scheme} },
81 scheme => {
82 type => SCALAR,
83 regex => qr/^\Q$scheme\E$/,
84 default => $scheme,
85 },
86 },
87 stack_skip => 2,
88 ));
89 }
90  
91 #+++############################################################################
92 # #
93 # object oriented interface #
94 # #
95 #---############################################################################
96  
97 #
98 # constructors
99 #
100  
101 37 ▶sub new : method {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
102 37 my($class, %option, $cc, $scheme);
103  
104 37 $class = shift(@_);
105 37 ▶ if ($class eq __PACKAGE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class eq __PACKAGE__) 19 18 37
106 # toplevel constructor
107 19 %option = validate_with(
108 params => \@_,
109 spec => {
110 scheme => {
111 type => SCALAR,
112 regex => $_IdRe,
113 default => "none",
114 },
115 },
116 allow_extra => 1,
117 );
118 19 $cc = $class . "::" . $option{scheme};
119 19 _require($cc);
120 18 return($cc->new(\%option));
121 }
122 # inherited constructor
123 18 $scheme = substr($class, length(__PACKAGE__) + 2);
124 18 return(bless({ _check($scheme, @_) }, $class));
125 }
126  
127 23 ▶sub parse : method {
Subroutine parse: called CC=11 SCAR=24.3
Pod documented
128 23 my($class, $string, @list, %option);
129  
130 23 $class = shift(@_);
131 23 ▶ validate_pos(@_, { type => SCALAR })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "" 0 23 23
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
0023
@_ == 1 and defined $_[0]
!ll&&!rl&&r
0023
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq ""
@_ == 1 defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
132 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "";
133 23 $string = shift(@_);
134 23 ▶ return($class->new()) if $string eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $string eq "" 1 22 23
135 22 ▶ dief("invalid credential string: %s", $string)
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /^[$Authen::Credential::_ValChars$Authen::Credential::_SepChars\%\=]+$/o 1 21 22
136 unless $string =~ /^[${_ValChars}${_SepChars}\%\=]+$/o;
137 21 @list = split(/[${_SepChars}]+/o, $string);
138 21 ▶ dief("invalid credential string: %s", $string)
Branch: 1 branch
BranchTrueFalseTotal
unless @list and $list[0] =~ /^($Authen::Credential::_IdRe)$/o 2 19 21
Condition100%3/3
@list and $list[0] =~ /^($Authen::Credential::_IdRe)$/o
!ll&&!rl&&r
1119
MC/DC100%2/2
@list and $list[0] =~ /^($Authen::Credential::_IdRe)$/o
@list $list[0] =~ /^($Authen::Credential::_IdRe)$/o
Truth table100%3/3
A and B
A= @listB= $list[0] =~ /^($Authen::Credential::_IdRe)$/o
ABresult
0X0
100
111
139 unless @list and $list[0] =~ /^($_IdRe)$/o;
140 19 %option = (scheme => shift(@list));
141 19 foreach my $kv (@list) {
142 26 ▶ if ($kv =~ /^($_IdRe)\=([$_ValChars\%]*)$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kv =~ /^($Authen::Credential::_IdRe)\=([$Authen::Credential::_ValChars\%]*)$/o) { } 24 2 26
143 dief("duplicate credential key: %s", $1)
144 24 ▶ if exists($option{$1});
Branch: 1 branch
BranchTrueFalseTotal
if exists $option{$1} 1 23 24
145 23 $option{$1} = uri_unescape($2);
146 } else {
147 2 dief("invalid credential key=value: %s", $kv);
148 }
149 }
150 16 return($class->new(\%option));
151 }
152  
153 #
154 # transformers
155 #
156  
157 0 ▶sub hash : method {
Subroutine hash: not called CC=3 SCAR=24.8
Pod documented
158 0 my($self);
159  
160 0 $self = shift(@_);
161 0 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
162 0 ▶ return($self) unless wantarray();
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
163 0 return(%{ $self });
164 }
165  
166 3 ▶sub string : method {
Subroutine string: called CC=5 SCAR=16.2
Pod documented
167 3 my($self, @parts);
168  
169 3 $self = shift(@_);
170 3 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 3 3
171 3 ▶ dief("invalid credential: no scheme") unless $self->{scheme};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"scheme"} 0 3 3
172 3 @parts = ($self->{scheme});
173 3 foreach my $key (sort(keys(%{ $self }))) {
174 6 ▶ next if $key eq "scheme";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "scheme" 3 3 6
175 3 push(@parts, $key . "=" . uri_escape($self->{$key}, "^$_ValChars"));
176 }
177 3 return(join(" ", @parts));
178 }
179  
180 #
181 # accessors
182 #
183  
184 2 foreach my $name (qw(scheme)) {
185 2 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
186 2 ▶ *{ $name } = sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
187 11 my($self);
188 11 $self = shift(@_);
189 11 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 11 11
190 11 return($self->{$name});
191 2 };
192 }
193  
194 #
195 # generic check method using the Params::Validate specification
196 #
197  
198 9 ▶sub check : method {
Subroutine check: called CC=2 SCAR=7.0
Pod documented
199 9 my($self);
200  
201 9 $self = shift(@_);
202 9 ▶ validate_pos(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 9 9
203 9 return(_check($self->scheme(), $self));
204 }
205  
206 #
207 # generic prepare method using the declared preparators
208 #
209  
210 2 ▶sub prepare : method {
Subroutine prepare: called CC=5 SCAR=19.1
Pod documented
211 2 my($self, $target, $preparator);
212  
213 2 $self = shift(@_);
214 2 ▶ validate_pos(@_, { type => SCALAR })
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 and defined $_[0] and ref $_[0] eq "" 0 2 2
Condition33%2/6
@_ == 1 and defined $_[0] and ref $_[0] eq ""
!ll&&!rl&&r
002
@_ == 1 and defined $_[0]
!ll&&!rl&&r
002
MC/DC0%0/3
@_ == 1 and defined $_[0] and ref $_[0] eq ""
@_ == 1 defined $_[0] ref $_[0] eq ""
Truth table25%1/4
A and B and C
A= @_ == 1B= defined $_[0]C= ref $_[0] eq ""
ABCresult
0XX0
10X0
1100
1111
215 unless @_ == 1 and defined($_[0]) and ref($_[0]) eq "";
216 2 $target = shift(@_);
217 2 $preparator = $Preparator{$self->scheme()}{$target};
218 2 ▶ return($preparator->($self)) if $preparator;
Branch: 1 branch
BranchTrueFalseTotal
if $preparator 2 0 2
219 0 dief("invalid %s credential preparation target: %s",
220 $self->scheme(), $target);
221 }
222  
223 2 1;
224  
225 __DATA__
« Credential/x509.pm ↑ Summary Credential/plain.pm »