blib/lib/Lemonldap/NG/Portal/Auth/Combination.pm

filter: Statementstmt 85.9% 134 / 156 Branchbran 67.2% 39 / 58 Conditioncond 52.9% 9 / 17 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 88.8% 24 / 27 Podpod 0.0% 0 / 17 totaltotal 72.8% 207 / 284 CC 38 SCAR 18.2
CC
38
Cov
79%
init
14.7
try
10.4
authLogout
7.0
CRAP 6.2
« blib/lib/Lemonldap/NG/Portal/Register/AD.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CDA.pm »
1 package Lemonldap::NG::Portal::Auth::Combination;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Lemonldap::NG::Common::Combination::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Lemonldap::NG::Portal::Main::Constants qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
7 PE_CONFIRM
8 PE_ERROR
9 PE_FIRSTACCESS
10 PE_FORMEMPTY
11 PE_PASSWORD_OK
12 PE_OK
13 9 );
14 9 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 our $VERSION = '2.23.0';
17  
18 9 extends 'Lemonldap::NG::Portal::Main::Auth';
19 9 with 'Lemonldap::NG::Portal::Lib::OverConf';
20  
21 # PROPERTIES
22  
23 9 has stackSub => ( is => 'rw' );
24  
25 has wrapUserLogger => (
26 is => 'rw',
27 lazy => 1,
28 68 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
29 68 Lemonldap::NG::Portal::Lib::Combination::UserLogger->new(
30 $_[0]->userLogger );
31 }
32 9 );
33  
34 # INITIALIZATION
35  
36 136 ▶sub init {
Subroutine init: called CC=10 SCAR=26.9
Pod undocumented
37 136 my $self = shift;
38  
39 # Check if expression exists
40 136 ▶ unless ( $self->conf->{combination} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"combination"}) 0 136 136
41 0 $self->error('No combination found');
42 0 return 0;
43 }
44  
45 # Load all declared modules
46 136 my %mods;
47 136 foreach my $key ( keys %{ $self->conf->{combModules} } ) {
48 274 my @tmp = ( undef, undef );
49 274 my $mod = $self->conf->{combModules}->{$key};
50  
51 274 ▶ unless ( $mod->{type} and defined $mod->{for} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod->{"type"} and defined $mod->{"for"}) 0 274 274
Condition33%1/3
$mod->{"type"} and defined $mod->{"for"}
!ll&&!rl&&r
00274
MC/DC0%0/2
$mod->{"type"} and defined $mod->{"for"}
$mod->{"type"} defined $mod->{"for"}
Truth table33%1/3
A and B
A= $mod->{"type"}B= defined $mod->{"for"}
ABresult
0X0
100
111
52 0 $self->error("Malformed combination module $key");
53 0 return 0;
54 }
55  
56 # Override parameters
57  
58 # "for" key can have 3 values:
59 # 0: this module will be used for Auth and UserDB
60 # 1: this module will be used for Auth only
61 # 2: this module will be used for UserDB only
62  
63 # Load Auth module
64 274 ▶ if ( $mod->{for} < 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{"for"} < 2) 274 0 274
65 274 $tmp[0] = $self->loadPlugin( "::Auth::$mod->{type}", $mod->{over} );
66 274 ▶ unless ( $tmp[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp[0]) 0 274 274
67 0 $self->error("Unable to load Auth::$mod->{type}");
68 0 return 0;
69 }
70 274 $tmp[0]->{userLogger} = $self->wrapUserLogger;
71 274 weaken $tmp[0]->{userLogger};
72 }
73  
74 # Load UserDB module
75 274 ▶ unless ( $mod->{for} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod->{"for"} == 1) 272 2 274
76 $tmp[1] =
77 272 $self->loadPlugin( "::UserDB::$mod->{type}", $mod->{over} );
78 272 ▶ unless ( $tmp[1] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tmp[1]) 0 272 272
79 0 $self->error("Unable to load UserDB::$mod->{type}");
80 0 return 0;
81 }
82 272 $tmp[1]->{userLogger} = $self->wrapUserLogger;
83 272 weaken $tmp[1]->{userLogger};
84 }
85  
86 # Store modules as array
87 274 $mods{$key} = \@tmp;
88 }
89  
90 # Compile expression
91 136 eval {
92 $self->stackSub(
93 Lemonldap::NG::Common::Combination::Parser->parse(
94 \%mods, $self->conf->{combination}
95 )
96 136 );
97 };
98 136 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 136 136
99 0 $self->error("Bad combination: $@");
100 0 return 0;
101 }
102 136 return 1;
103 }
104  
105 # Each first method must call getStack() to get the auth scheme available for
106 # the current user
107  
108 ## Auth steps
109 #############
110 73 ▶sub extractFormInfo {
Subroutine extractFormInfo: called CC=1 SCAR=0.0
Pod undocumented
111 73 my $self = shift;
112 73 return $self->try( 0, 'extractFormInfo', @_ );
113 }
114  
115 34 ▶sub authenticate {
Subroutine authenticate: called CC=1 SCAR=0.0
Pod undocumented
116 34 my $self = shift;
117 34 return $self->try( 0, 'authenticate', @_ );
118 }
119  
120 32 ▶sub setAuthSessionInfo {
Subroutine setAuthSessionInfo: called CC=1 SCAR=0.0
Pod undocumented
121 32 my $self = shift;
122 32 return $self->try( 0, 'setAuthSessionInfo', @_ );
123 }
124  
125 7 ▶sub getDisplayType {
Subroutine getDisplayType: called CC=2 SCAR=7.0
Pod undocumented
126 7 my $self = shift;
127 7 my ($req) = @_;
128 return $self->conf->{combinationForms}
129 7 ▶ if ( $self->conf->{combinationForms} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->conf->{"combinationForms"} 0 7 7
130  
131 7 eval { $self->getStack( $req, 'getDisplayType' ); };
132 my ( $nb, $stack ) = (
133 $req->data->{dataKeep}->{combinationTry},
134 $req->data->{combinationStack}
135 7 );
136 7 my $res = $stack->[$nb]->[0]->( 'getDisplayType', @_ );
137 7 return $res;
138 }
139  
140 18 ▶sub authLogout {
Subroutine authLogout: called CC=6 SCAR=19.5
Pod undocumented
141 18 my $self = shift;
142 18 my ($req) = @_;
143 18 ▶ $self->getStack( $req, 'extractFormInfo' ) or return PE_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$self->getStack($req, "extractFormInfo") or return 24 18 0 18
144  
145 # Avoid warning msg at first access
146 18 ▶ $req->userData->{_combinationTry} ||= 0;
Condition50%1/2
$req->userData->{"_combinationTry"} ||= 0
l!l
018
MC/DC0%0/1
$req->userData->{"_combinationTry"} ||= 0
$req->userData->{"_combinationTry"}
Truth table50%1/2
A
A= $req->userData->{"_combinationTry"}
Aresult
11
00
147  
148 # Avoid out of bounds access
149 18 ▶ if ( $req->userData->{_combinationTry} <
Branch: 1 branch
BranchTrueFalseTotal
if ($req->userData->{"_combinationTry"} < @{$req->data->{"combinationStack"} // [];}) 18 0 18
150 18 ▶ @{ $req->data->{combinationStack} // [] } )
Condition50%1/2
$req->data->{"combinationStack"} // []
l!l
180
MC/DC0%0/1
$req->data->{"combinationStack"} // []
$req->data->{"combinationStack"}
Truth table50%1/2
A
A= $req->data->{"combinationStack"}
Aresult
11
00
151 {
152 my $sub =
153 18 $req->data->{combinationStack}->[ $req->userData->{_combinationTry} ]
154 ->[0];
155 18 ▶ if ($sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub) 18 0 18
156 18 my ( $res, $name ) = $sub->( 'authLogout', @_ );
157 18 $self->logger->debug(qq'User disconnected using scheme "$name"');
158 18 return $res;
159 }
160 }
161 0 $self->logger->warn( "Condition changed between login and logout for "
162 . $req->user
163 . ", unable to select good backend" );
164 0 return PE_OK;
165 }
166  
167 0 ▶sub authFinish {
Subroutine authFinish: not called CC=1 SCAR=6.9
Pod undocumented
168 0 return PE_OK;
169 }
170  
171 0 ▶sub authForce {
Subroutine authForce: not called CC=1 SCAR=6.9
Pod undocumented
172 0 return 0;
173 }
174  
175 14 ▶sub setSecurity {
Subroutine setSecurity: called CC=3 SCAR=11.2
Pod undocumented
176 14 my $self = shift;
177 14 my ($req) = @_;
178 14 ▶ $self->getStack( $req, 'extractFormInfo' ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->getStack($req, "extractFormInfo") or return 14 0 14
179 14 eval {
180 $req->data->{combinationStack}
181 14 ->[ $req->data->{dataKeep}->{combinationTry} ]->[0]
182 ->( 'setSecurity', @_ );
183 };
184 14 ▶ $self->logger->debug($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
185 }
186  
187 ## UserDB steps
188 ###############
189 # Note that UserDB::Combination uses the same object.
190 48 ▶sub getUser {
Subroutine getUser: called CC=1 SCAR=0.0
Pod undocumented
191 48 my $self = shift;
192 48 return $self->try( 1, 'getUser', @_ );
193 }
194  
195 0 ▶sub findUser {
Subroutine findUser: not called CC=1 SCAR=6.9
Pod undocumented
196 0 my $self = shift;
197 0 return $self->try( 1, 'findUser', @_ );
198 }
199  
200 33 ▶sub setSessionInfo {
Subroutine setSessionInfo: called CC=1 SCAR=0.0
Pod undocumented
201 33 my $self = shift;
202 33 return $self->try( 1, 'setSessionInfo', @_ );
203 }
204  
205 32 ▶sub setGroups {
Subroutine setGroups: called CC=1 SCAR=0.0
Pod undocumented
206 32 my $self = shift;
207 32 return $self->try( 1, 'setGroups', @_ );
208 }
209  
210 108 ▶sub getStack {
Subroutine getStack: called CC=4 SCAR=14.1
Pod undocumented
211 108 my ( $self, $req, @steps ) = @_;
212 return $req->data->{combinationStack}
213 108 ▶ if ( $req->data->{combinationStack} );
Branch: 1 branch
BranchTrueFalseTotal
if $req->data->{"combinationStack"} 21 87 108
214 87 my $stack = $req->data->{combinationStack} = $self->stackSub->( $req->env );
215 87 ▶ unless ($stack) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stack) 0 87 87
216 0 $self->logger->error('No authentication scheme for this user');
217 }
218 87 @{ $req->data->{combinationSteps} } = ( @steps, @{ $req->steps } );
219 87 ▶ $req->data->{dataKeep}->{combinationTry} ||= 0;
Condition50%1/2
$req->data->{"dataKeep"}{"combinationTry"} ||= 0
l!l
087
MC/DC0%0/1
$req->data->{"dataKeep"}{"combinationTry"} ||= 0
$req->data->{"dataKeep"}{"combinationTry"}
Truth table50%1/2
A
A= $req->data->{"dataKeep"}{"combinationTry"}
Aresult
11
00
220 87 return $stack;
221 }
222  
223 # Main running method: launch the next scheme if the current fails
224 252 ▶sub try {
Subroutine try: called CC=10 SCAR=23.4
Pod undocumented
225 252 my ( $self, $type, $subname, $req, @args ) = @_;
226  
227 # Get available authentication schemes for this user if not done
228 252 ▶ unless ( defined $req->data->{combinationStack} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $req->data->{"combinationStack"}) 69 183 252
229 69 ▶ $self->getStack( $req, $subname ) or return PE_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
$self->getStack($req, $subname) or return 24 69 0 69
230 }
231 my ( $nb, $stack ) = (
232 $req->data->{dataKeep}->{combinationTry},
233 $req->data->{combinationStack}
234 252 );
235  
236 # If more than 1 scheme is available
237 252 my ( $res, $name );
238 252 ▶ unless ( ref $stack->[$nb]->[$type] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $stack->[$nb][$type]) 0 252 252
239 0 $self->logger->error(
240 'Something went wrong in combination, unable to find any auth scheme (try == '
241 . ( $nb + 1 )
242 . ')' );
243 0 return PE_ERROR;
244 }
245  
246 252 my $stop = 0;
247 252 ▶ if ( $nb < @$stack - 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nb < @$stack - 1) { } 45 207 252
248  
249 45 ( $res, $name ) = $stack->[$nb]->[$type]->( $subname, $req, @args );
250  
251 # On error, restart authentication with next scheme
252 45 ▶ unless ( $stop = $self->stop( $stack->[$nb]->[$type], $res ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($stop = $self->stop($stack->[$nb][$type], $res)) 4 41 45
253 4 $self->logger->info(qq'Scheme "$name" returned $res, trying next');
254 4 $req->data->{dataKeep}->{combinationTry}++;
255 4 $req->steps( [ @{ $req->data->{combinationSteps} } ] );
256 4 $req->continue(1);
257 4 return PE_OK;
258 }
259 }
260 else {
261 207 ( $res, $name ) = $stack->[$nb]->[$type]->( $subname, $req, @args );
262 }
263 248 $req->sessionInfo->{ [ '_auth', '_userDB' ]->[$type] } = $name;
264 $req->sessionInfo->{_combinationTry} =
265 248 $req->data->{dataKeep}->{combinationTry};
266 248 ▶ if ( $res > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res > 0) 37 211 248
267 37 ▶ if ($stop) {
Branch: 2 branches
BranchTrueFalseTotal
if ($stop) { } 0 37 37
elsif ($res != 9) { } 12 25 37
268 0 $self->userLogger->info(
269 "Combination stopped by plugin $name (code $res)");
270 }
271 elsif ( $res != PE_FIRSTACCESS ) {
272 12 ▶ $self->userLogger->warn( 'All schemes failed'
Branch: 1 branch
BranchTrueFalseTotal
$req->user ? : 12 0 12
273 . ( $req->user ? ' for user ' . $req->user : '' ) . ' ('
274 . $req->address
275 . ')' );
276 }
277 }
278 248 return $res;
279 }
280  
281 # try() stores real Auth/UserDB module in sessionInfo
282 # This method reads them. It is called by getModule()
283 # (see Main::Run)
284 66 ▶sub name {
Subroutine name: called CC=3 SCAR=11.1
Pod undocumented
285 66 my ( $self, $req, $type ) = @_;
286 66 ▶ return $req->sessionInfo->{ ( $type eq 'auth' ? '_auth' : '_userDB' ) }
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'auth' ? : 33 33 66
Condition50%1/2
$req->sessionInfo->{$type eq 'auth' ? '_auth' : '_userDB'} || 'Combination'
l!l
660
MC/DC0%0/1
$req->sessionInfo->{$type eq 'auth' ? '_auth' : '_userDB'} || 'Combination'
$req->sessionInfo->{$type eq 'auth' ? '_auth' : '_userDB'}
Truth table50%1/2
A
A= $req->sessionInfo->{$type eq 'auth' ? '_auth' : '_userDB'}
Aresult
11
00
287 || 'Combination';
288 }
289  
290 45 ▶sub stop {
Subroutine stop: called CC=6 SCAR=18.5
Pod undocumented
291 45 my ( $self, $mod, $res ) = @_;
292 45 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $res <= 0 or $res == 42 or $res == 35 41 4 45
Condition66%4/6
$res <= 0 or $res == 42 or $res == 35
l!l&&r!l&&!r
4104
$res <= 0 or $res == 42
l!l&&r!l&&!r
4104
MC/DC33%1/3
$res <= 0 or $res == 42 or $res == 35
$res <= 0 $res == 42 $res == 35
Truth table50%2/4
A or B or C
A= $res <= 0B= $res == 42C= $res == 35
ABCresult
1XX1
01X1
0011
0000
293 if (
294 $res <= 0 # PE_OK
295 or $res == PE_CONFIRM
296 or $res == PE_PASSWORD_OK
297  
298 # TODO: adding this may generate behavior change
299 #or $res == PE_FIRSTACCESS
300 #or $res == PE_FORMEMPTY
301 );
302 4 my ( $ret, $name );
303 4 $ret = $mod->( 'can', 'stop' );
304 4 ▶ if ($ret) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret) 1 3 4
305 1 eval { ( $ret, $name ) = $mod->( 'stop', $res ) };
306 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
307  
308 0 $self->logger->error(
309 "Optional ${name}::stop() method failed: " . $@ );
310 0 return 0;
311 }
312 }
313 4 return $ret;
314 }
315  
316 package Lemonldap::NG::Portal::Lib::Combination::UserLogger;
317  
318 # This logger rewrite "warn" to "notice"
319 68 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
320 68 my ( $class, $realLogger ) = @_;
321 68 return bless { logger => $realLogger }, $class;
322 }
323  
324 18 ▶sub warn {
Subroutine warn: called CC=1 SCAR=0.0
325 18 my ($auth) = caller(0);
326 18 $_[0]->{logger}->notice("Combination ($auth): $_[1]");
327 }
328  
329 43 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=2 SCAR=7.0
330 9 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
331 43 $AUTOLOAD =~ s/.*:://;
332 43 ▶ return $_[0]->{logger}->$AUTOLOAD( $_[1] )
Branch: 1 branch
BranchTrueFalseTotal
if $AUTOLOAD =~ /^(?:notice|debug|error|info)$/ 0 43 43
333 if ( $AUTOLOAD =~ /^(?:notice|debug|error|info)$/ );
334 }
335  
336 9 1;
« blib/lib/Lemonldap/NG/Portal/Register/AD.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Plugins/CDA.pm »