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

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