blib/lib/Lemonldap/NG/Portal/Lib/CAS.pm

filter: Statementstmt 32.2% 163 / 505 Branchbran 26.1% 33 / 126 Conditioncond 17.8% 10 / 56 MC/DCmcdc 6.4% 2 / 31 Subroutinesub 53.8% 28 / 52 Podpod 35.7% 10 / 28 totaltotal 30.8% 246 / 798 CC 102 SCAR 34.6
CC
102
Cov
30%
validateST
132.0
getAttributesFromServiceValidateResponse
132.0
updateCasSecondarySessions
56.0
CRAP 31.8
« blib/lib/Lemonldap/NG/Portal/Plugins/AdaptativeAuthenticationLevel.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/RESTProxy.pm »
1 package Lemonldap::NG::Portal::Lib::CAS;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 21 ▶use Lemonldap::NG::Common::FormEncode;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 21 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 21 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 21 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 21 ▶use Lemonldap::NG::Common::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 21 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 21 ▶use Crypt::URandom;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 21 our $VERSION = '2.23.0';
15  
16 # PROPERTIES
17  
18 # return LWP::UserAgent object
19 has ua => (
20 is => 'rw',
21 lazy => 1,
22 4 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
23  
24 # TODO : LWP options to use a proxy for example
25 4 my $ua = Lemonldap::NG::Common::UserAgent->new( $_[0]->{conf} );
26 4 $ua->env_proxy();
27 4 return $ua;
28 }
29 21 );
30  
31 61 ▶has casSrvList => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
32 61 ▶has casAppList => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
33 61 ▶has srvRules => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
34 61 ▶has spLevelRules => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
35 61 ▶has spRules => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
36 61 ▶has spMacros => ( is => 'rw', default => sub { {} }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 # XML parser
39 has parser => (
40 is => 'rw',
41 61 ▶ builder => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
42 61 return XML::LibXML->new( load_ext_dtd => 0, expand_entities => 0 );
43 }
44 21 );
45  
46 # RUNNING METHODS
47  
48 # Load CAS server list
49 15 ▶sub loadSrv {
Subroutine loadSrv: called CC=6 SCAR=23.0
Pod undocumented
50 15 my ($self) = @_;
51 15 ▶ unless ( $self->conf->{casSrvMetaDataOptions}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"casSrvMetaDataOptions"} and %{$self->conf->{"casSrvMetaDataOptions"};}) 0 15 15
Condition33%1/3
$self->conf->{"casSrvMetaDataOptions"} and %{$self->conf->{"casSrvMetaDataOptions"};}
!ll&&!rl&&r
0015
MC/DC0%0/2
$self->conf->{"casSrvMetaDataOptions"} and %{$self->conf->{"casSrvMetaDataOptions"};}
$self->conf->{"casSrvMetaDataOptions"} %{$self->conf->{"casSrvMetaDataOptions"};}
Truth table33%1/3
A and B
A= $self->conf->{"casSrvMetaDataOptions"}B= %{$self->conf->{"casSrvMetaDataOptions"};}
ABresult
0X0
100
111
52 15 and %{ $self->conf->{casSrvMetaDataOptions} } )
53 {
54 0 $self->logger->error("No CAS servers found in configuration");
55 0 return 0;
56 }
57 15 $self->casSrvList( $self->conf->{casSrvMetaDataOptions} );
58  
59 # Set rule
60 15 foreach ( keys %{ $self->conf->{casSrvMetaDataOptions} } ) {
61 my $cond = $self->conf->{casSrvMetaDataOptions}->{$_}
62 19 ->{casSrvMetaDataOptionsResolutionRule};
63 19 ▶ if ( length $cond ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $cond) 0 19 19
64 0 my $rule_sub =
65 $self->p->buildRule( $cond, "CAS server resolution" );
66 0 ▶ if ($rule_sub) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule_sub) 0 0 0
67 0 $self->srvRules->{$_} = $rule_sub;
68 }
69 }
70 }
71 15 return 1;
72 }
73  
74 # Load CAS application list
75 44 ▶sub loadApp {
Subroutine loadApp: called CC=15 SCAR=32.9
Pod undocumented
76 44 my ($self) = @_;
77 44 ▶ unless ( $self->conf->{casAppMetaDataOptions}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"casAppMetaDataOptions"} and %{$self->conf->{"casAppMetaDataOptions"};}) 0 44 44
Condition33%1/3
$self->conf->{"casAppMetaDataOptions"} and %{$self->conf->{"casAppMetaDataOptions"};}
!ll&&!rl&&r
0044
MC/DC0%0/2
$self->conf->{"casAppMetaDataOptions"} and %{$self->conf->{"casAppMetaDataOptions"};}
$self->conf->{"casAppMetaDataOptions"} %{$self->conf->{"casAppMetaDataOptions"};}
Truth table33%1/3
A and B
A= $self->conf->{"casAppMetaDataOptions"}B= %{$self->conf->{"casAppMetaDataOptions"};}
ABresult
0X0
100
111
78 44 and %{ $self->conf->{casAppMetaDataOptions} } )
79 {
80 0 $self->logger->info("No CAS apps found in configuration");
81 }
82  
83 44 foreach ( keys %{ $self->conf->{casAppMetaDataOptions} } ) {
84  
85 86 $self->logger->debug("Processing CAS application: $_");
86  
87 86 my $valid = 1;
88  
89 # Check if CAS application is activated
90 86 ▶ unless ( $self->conf->{casAppMetaDataOptions}->{$_}
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsActivation"} // 1) 1 85 86
Condition100%2/2
$self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsActivation"} // 1
l!l
284
MC/DC100%1/1
$self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsActivation"} // 1
$self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsActivation"}
Truth table100%2/2
A
A= $self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsActivation"}
Aresult
11
00
91 ->{casAppMetaDataOptionsActivation} // 1 )
92 {
93 1 $self->logger->debug("Processing $_: CAS application deactivated");
94 1 next;
95 }
96  
97 # Load access rule
98 my $rule =
99 $self->conf->{casAppMetaDataOptions}->{$_}
100 85 ->{casAppMetaDataOptionsRule};
101 85 ▶ if ( length $rule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $rule) 15 70 85
102 15 $rule = $self->p->buildRule( $rule, "access rule for App $_" );
103 15 ▶ unless ($rule) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rule) 0 15 15
104 0 $valid = 0;
105 }
106 }
107  
108 # Required authentication level rule
109 my $levelrule =
110 $self->conf->{casAppMetaDataOptions}->{$_}
111 ->{casAppMetaDataOptionsAuthnLevel}
112 || $self->conf->{defaultAuthnLevel}
113 85 ▶ || 0;
Condition40%2/5
$self->conf->{'casAppMetaDataOptions'}{$_}{'casAppMetaDataOptionsAuthnLevel'} || $self->conf->{'defaultAuthnLevel'} || 0
l!l
085
$self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsAuthnLevel"} or $self->conf->{"defaultAuthnLevel"}
l!l&&r!l&&!r
0085
MC/DC0%0/2
$self->conf->{'casAppMetaDataOptions'}{$_}{'casAppMetaDataOptionsAuthnLevel'} || $self->conf->{'defaultAuthnLevel'} || 0
$self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsAuthnLevel"} $self->conf->{"defaultAuthnLevel"}
Truth table33%1/3
A or B
A= $self->conf->{"casAppMetaDataOptions"}{$_}{"casAppMetaDataOptionsAuthnLevel"}B= $self->conf->{"defaultAuthnLevel"}
ABresult
1X1
011
000
114 85 $levelrule = $self->p->buildRule( $levelrule,
115 "Required authentication level rule for App $_" );
116 85 ▶ unless ($levelrule) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($levelrule) 0 85 85
117 0 $valid = 0;
118 }
119  
120 # Load per-application macros
121 85 my $macros = $self->conf->{casAppMetaDataMacros}->{$_};
122 85 my $compiledMacros = {};
123 85 for my $macroAttr ( keys %{$macros} ) {
124 0 my $macroRule = $macros->{$macroAttr};
125 0 ▶ if ( length $macroRule ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $macroRule) 0 0 0
126 0 $macroRule = $self->p->HANDLER->substitute($macroRule);
127 0 ▶ if ( $macroRule = $self->p->HANDLER->buildSub($macroRule) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($macroRule = $self->p->HANDLER->buildSub($macroRule)) { } 0 0 0
128 0 $compiledMacros->{$macroAttr} = $macroRule;
129 }
130 else {
131 $self->logger->error(
132 "Unable to build macro $macroAttr for CAS Application $_: "
133 0 . $self->p->HANDLER->tsv->{jail}->error );
134 0 $valid = 0;
135 }
136 }
137 }
138  
139 85 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) { } 85 0 85
140 $self->casAppList->{$_} =
141 85 $self->conf->{casAppMetaDataOptions}->{$_};
142 85 $self->spRules->{$_} = $rule;
143 85 $self->spLevelRules->{$_} = $levelrule;
144 85 $self->spMacros->{$_} = $compiledMacros;
145 }
146 else {
147 0 $self->logger->error(
148 "CAS Application $_ has errors and will be ignored");
149  
150 }
151 }
152 44 return 1;
153 }
154  
155 8 ▶sub sendSoapResponse {
Subroutine sendSoapResponse: called CC=1 SCAR=0.0
Pod undocumented
156 8 my ( $self, $req, $s ) = @_;
157 8 $self->logger->debug("Send response: $s");
158 8 return $self->p->sendTextResponse( $req, $s,
159 type => 'application/soap+xml' );
160 }
161  
162 # Try to recover the CAS session corresponding to id and return session data
163 # If id is set to undef, return a new session
164 26 ▶sub getCasSession {
Subroutine getCasSession: called CC=6 SCAR=23.0
Pod documented
165 26 my ( $self, $id, $info, $hs ) = @_;
166  
167 my %storage = (
168 storageModule => $self->conf->{casStorage},
169 storageModuleOptions => $self->conf->{casStorageOptions},
170 26 );
171 26 ▶ unless ( $storage{storageModule} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storage{"storageModule"}) 0 26 26
172 %storage = (
173 storageModule => $self->conf->{globalStorage},
174 storageModuleOptions => $self->conf->{globalStorageOptions},
175 0 );
176 }
177  
178 my $casSession = Lemonldap::NG::Common::Session->new( {
179 %storage,
180 cacheModule => $self->conf->{localSessionStorage},
181 cacheModuleOptions => $self->conf->{localSessionStorageOptions},
182 id => $id,
183 kind => $self->sessionKind,
184 ( $info ? ( info => $info ) : () ),
185 hashStore => $hs // $self->conf->{hashedSessionStore},
186 }
187 26 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$info ? : 14 12 26
Condition66%2/3
$hs // $self->conf->{"hashedSessionStore"}
l!l&&r!l&&!r
01214
MC/DC50%1/2
$hs // $self->conf->{"hashedSessionStore"}
$hs $self->conf->{"hashedSessionStore"}
Truth table66%2/3
A // B
A= $hsB= $self->conf->{"hashedSessionStore"}
ABresult
1X1
011
000
188  
189 26 ▶ if ( $casSession->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($casSession->error) 0 26 26
190 0 ▶ if ($id) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) { } 0 0 0
191 0 $self->userLogger->notice("CAS session $id not found");
192 }
193 else {
194 0 $self->logger->error("Unable to create new CAS session");
195 0 $self->logger->error( $casSession->error );
196 }
197 0 return undef;
198 }
199  
200 26 return $casSession;
201 }
202  
203 # Try to recover the SSO-CAS session corresponding to the ticket and return session data
204 # If id is set to undef, return a new session
205 0 ▶sub getSSOSessionIdByTicket {
Subroutine getSSOSessionIdByTicket: not called CC=5 SCAR=34.0
Pod undocumented
206 0 my ( $self, $ticket ) = @_;
207  
208 0 my $moduleOptions;
209 0 ▶ if ( $self->conf->{casStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'casStorage'}) { } 0 0 0
210 0 ▶ $moduleOptions = $self->conf->{casStorageOptions} || {};
Condition0%0/2
$self->conf->{'casStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'casStorageOptions'} || {}
$self->conf->{'casStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'casStorageOptions'}
Aresult
11
00
211 0 $moduleOptions->{backend} = $self->conf->{casStorage};
212 }
213 else {
214 0 ▶ $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
215 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
216 }
217 0 my $module = "Lemonldap::NG::Common::Apache::Session";
218  
219 0 my $cas_sessions =
220 $module->searchOn( $moduleOptions, "auth_cas_ticket", $ticket );
221  
222 0 my (undef, $cas_session) = %$cas_sessions;
223 0 ▶ return $cas_session && $cas_session->{_cas_id};
Condition0%0/3
$cas_session && $cas_session->{'_cas_id'}
!ll&&!rl&&r
000
MC/DC0%0/2
$cas_session && $cas_session->{'_cas_id'}
$cas_session $cas_session->{'_cas_id'}
Truth table0%0/3
A && B
A= $cas_sessionB= $cas_session->{'_cas_id'}
ABresult
0X0
100
111
224 }
225  
226 # Return an error for CAS VALIDATE request
227 0 ▶sub returnCasValidateError {
Subroutine returnCasValidateError: not called CC=1 SCAR=6.9
Pod documented
228 0 my ( $self, $req ) = @_;
229  
230 0 return $self->p->sendBinaryResponse( $req, "no\n\n" );
231 }
232  
233 # Return success for CAS VALIDATE request
234 0 ▶sub returnCasValidateSuccess {
Subroutine returnCasValidateSuccess: not called CC=1 SCAR=6.9
Pod documented
235 0 my ( $self, $req, $username, $pgtIou, $proxies, $attributes ) = @_;
236  
237 0 return $self->sendSoapResponse( $req, "yes\n$username\n" );
238 }
239  
240 # Return an error for CAS SERVICE VALIDATE request
241 0 ▶sub returnCasServiceValidateError {
Subroutine returnCasServiceValidateError: not called CC=3 SCAR=24.8
Pod documented
242 0 my ( $self, $req, $code, $text ) = @_;
243  
244 0 ▶ $code ||= 'INTERNAL_ERROR';
Condition0%0/2
$code ||= "INTERNAL_ERROR"
l!l
00
MC/DC0%0/1
$code ||= "INTERNAL_ERROR"
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
245 0 ▶ $text ||= 'No description provided';
Condition0%0/2
$text ||= "No description provided"
l!l
00
MC/DC0%0/1
$text ||= "No description provided"
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
246  
247 0 return $self->sendSoapResponse(
248 $req, "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
249 \t<cas:authenticationFailure code=\"$code\">
250 \t\t$text
251 \t</cas:authenticationFailure>
252 </cas:serviceResponse>\n"
253 );
254 }
255  
256 # Return success for CAS SERVICE VALIDATE request
257 8 ▶sub returnCasServiceValidateSuccess {
Subroutine returnCasServiceValidateSuccess: called CC=7 SCAR=24.6
Pod documented
258 8 my ( $self, $req, $username, $pgtIou, $proxies, $attributes ) = @_;
259  
260 8 my $s = "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
261 \t<cas:authenticationSuccess>
262 \t\t<cas:user>$username</cas:user>\n";
263 8 ▶ if ( defined $attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 8 0 8
264 8 $s .= "\t\t<cas:attributes>\n";
265 8 foreach my $attribute ( keys %$attributes ) {
266 0 foreach my $value (
267 split(
268 $self->conf->{multiValuesSeparator},
269 $attributes->{$attribute}
270 )
271 )
272 {
273 0 $s .= "\t\t\t<cas:$attribute>$value</cas:$attribute>\n";
274 }
275 }
276 8 $s .= "\t\t</cas:attributes>\n";
277 }
278 8 ▶ if ( defined $pgtIou ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pgtIou) 0 8 8
279 0 $self->logger->debug("Add proxy granting ticket $pgtIou in response");
280 0 $s .=
281 "\t\t<cas:proxyGrantingTicket>$pgtIou</cas:proxyGrantingTicket>\n";
282 }
283 8 ▶ if ($proxies) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxies) 0 8 8
284 0 $self->logger->debug("Add proxies $proxies in response");
285 0 $s .= "\t\t<cas:proxies>\n";
286 $s .= "\t\t\t<cas:proxy>$_</cas:proxy>\n"
287 0 foreach (
288 reverse( split( $self->conf->{multiValuesSeparator}, $proxies ) ) );
289 0 $s .= "\t\t</cas:proxies>\n";
290 }
291 8 $s .= "\t</cas:authenticationSuccess>\n</cas:serviceResponse>\n";
292  
293 8 return $self->sendSoapResponse( $req, $s );
294 }
295  
296 # Return an error for CAS PROXY request
297 0 ▶sub returnCasProxyError {
Subroutine returnCasProxyError: not called CC=3 SCAR=24.8
Pod documented
298 0 my ( $self, $req, $code, $text ) = @_;
299  
300 0 ▶ $code ||= 'INTERNAL_ERROR';
Condition0%0/2
$code ||= "INTERNAL_ERROR"
l!l
00
MC/DC0%0/1
$code ||= "INTERNAL_ERROR"
$code
Truth table0%0/2
A
A= $code
Aresult
11
00
301 0 ▶ $text ||= 'No description provided';
Condition0%0/2
$text ||= "No description provided"
l!l
00
MC/DC0%0/1
$text ||= "No description provided"
$text
Truth table0%0/2
A
A= $text
Aresult
11
00
302  
303 0 $self->logger->debug("Return CAS proxy error $code ($text)");
304  
305 0 return $self->sendSoapResponse(
306 $req, "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
307 \t<cas:proxyFailure code=\"$code\">
308 \t\t$text
309 \t</cas:proxyFailure>
310 </cas:serviceResponse>\n"
311 );
312 }
313  
314 # Return success for CAS PROXY request
315 0 ▶sub returnCasProxySuccess {
Subroutine returnCasProxySuccess: not called CC=1 SCAR=6.9
Pod documented
316 0 my ( $self, $req, $ticket ) = @_;
317  
318 0 $self->logger->debug("Return CAS proxy success with ticket $ticket");
319  
320 0 return $self->sendSoapResponse(
321 $req, "<cas:serviceResponse xmlns:cas='http://www.yale.edu/tp/cas'>
322 \t<cas:proxySuccess>
323 \t\t<cas:proxyTicket>$ticket</cas:proxyTicket>
324 \t</cas:proxySuccess>
325 </cas:serviceResponse>\n"
326 );
327 }
328  
329 # Change the ID of secondary sessions (during upgrade)
330 0 ▶sub updateCasSecondarySessions {
Subroutine updateCasSecondarySessions: not called CC=7 SCAR=40.3
Pod undocumented
331 0 my ( $self, $req, $old_session_id, $new_session_id ) = @_;
332  
333 # Find CAS sessions
334 0 my $moduleOptions;
335 0 ▶ if ( $self->conf->{casStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'casStorage'}) { } 0 0 0
336 0 ▶ $moduleOptions = $self->conf->{casStorageOptions} || {};
Condition0%0/2
$self->conf->{'casStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'casStorageOptions'} || {}
$self->conf->{'casStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'casStorageOptions'}
Aresult
11
00
337 0 $moduleOptions->{backend} = $self->conf->{casStorage};
338 }
339 else {
340 0 ▶ $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
341 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
342 }
343 0 my $module = "Lemonldap::NG::Common::Apache::Session";
344  
345 0 my $cas_sessions =
346 $module->searchOn( $moduleOptions, "_cas_id", $old_session_id );
347  
348 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my(@cas_sessions_keys) = grep({$cas_sessions->{$_}{"_session_kind"} eq $self->sessionKind;} keys %$cas_sessions)) 0 0 0
349 my @cas_sessions_keys =
350 0 grep { $cas_sessions->{$_}->{_session_kind} eq $self->sessionKind }
351 keys %$cas_sessions
352 )
353 {
354  
355 0 foreach my $cas_session (@cas_sessions_keys) {
356  
357 # Get session
358 0 $self->logger->debug("Retrieve CAS session $cas_session");
359  
360 0 my $casSession = $self->getCasSession( $cas_session, undef, 0 );
361  
362 # Delete session
363 0 ▶ if ($casSession) {
Branch: 1 branch
BranchTrueFalseTotal
if ($casSession) 0 0 0
364 0 $casSession->update( { _cas_id => $new_session_id } );
365 }
366 }
367 }
368  
369 0 return;
370 }
371  
372 # Find and delete CAS sessions bounded to a primary session
373 14 ▶sub deleteCasSecondarySessions {
Subroutine deleteCasSecondarySessions: called CC=6 SCAR=22.6
Pod documented
374 14 my ( $self, $session_id ) = @_;
375 14 my $result = 1;
376  
377 # Find CAS sessions
378 14 my $moduleOptions;
379 14 ▶ if ( $self->conf->{casStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'casStorage'}) { } 14 0 14
380 14 ▶ $moduleOptions = $self->conf->{casStorageOptions} || {};
Condition50%1/2
$self->conf->{'casStorageOptions'} || {}
l!l
140
MC/DC0%0/1
$self->conf->{'casStorageOptions'} || {}
$self->conf->{'casStorageOptions'}
Truth table50%1/2
A
A= $self->conf->{'casStorageOptions'}
Aresult
11
00
381 14 $moduleOptions->{backend} = $self->conf->{casStorage};
382 }
383 else {
384 0 ▶ $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
385 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
386 }
387 14 my $module = "Lemonldap::NG::Common::Apache::Session";
388  
389 14 my $cas_sessions =
390 $module->searchOn( $moduleOptions, "_cas_id", $session_id );
391  
392 14 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my(@cas_sessions_keys) = grep({$cas_sessions->{$_}{'_session_kind'} eq $self->sessionKind;} keys %$cas_sessions)) { } 0 14 14
393 my @cas_sessions_keys =
394 0 grep { $cas_sessions->{$_}->{_session_kind} eq $self->sessionKind }
395 keys %$cas_sessions
396 )
397 {
398  
399 0 foreach my $cas_session (@cas_sessions_keys) {
400  
401 # Get session
402 0 $self->logger->debug("Retrieve CAS session $cas_session");
403  
404 0 my $casSession = $self->getCasSession( $cas_session, undef, 0 );
405  
406 # Delete session
407 0 $result = $self->deleteCasSession($casSession);
408 }
409 }
410 else {
411 14 $self->logger->debug("No CAS session found for session $session_id ");
412 }
413  
414 14 return $result;
415  
416 }
417  
418 # Delete an opened CAS session
419 8 ▶sub deleteCasSession {
Subroutine deleteCasSession: called CC=4 SCAR=17.3
Pod documented
420 8 my ( $self, $session ) = @_;
421  
422 # Check session object
423 8 ▶ unless ( $session && $session->data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session and $session->data) 0 8 8
Condition33%1/3
$session and $session->data
!ll&&!rl&&r
008
MC/DC0%0/2
$session and $session->data
$session $session->data
Truth table33%1/3
A and B
A= $sessionB= $session->data
ABresult
0X0
100
111
424 0 $self->logger->error("No session to delete");
425 0 return 0;
426 }
427  
428 # Get session_id
429 8 my $session_id = $session->id;
430  
431 # Delete session
432 8 ▶ unless ( $session->remove ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($session->remove) 0 8 8
433 0 $self->logger->error( $session->error );
434 0 return 0;
435 }
436  
437 8 $self->logger->debug("CAS session $session_id deleted");
438  
439 8 return 1;
440 }
441  
442 # Call proxy granting URL on CAS client
443 0 ▶sub callPgtUrl {
Subroutine callPgtUrl: not called CC=2 SCAR=17.9
Pod documented
444 0 my ( $self, $pgtUrl, $pgtIou, $pgtId ) = @_;
445  
446 # Build URL
447 0 ▶ my $url =
Branch: 1 branch
BranchTrueFalseTotal
$pgtUrl =~ /\?/ ? : 0 0 0
448 $pgtUrl
449 . ( $pgtUrl =~ /\?/ ? '&' : '?' )
450 . build_urlencoded( pgtIou => $pgtIou, pgtId => $pgtId );
451  
452 0 $self->logger->debug("Call URL $url");
453  
454 # GET URL
455 0 my $response = $self->ua->get($url);
456  
457 # Return result
458 0 return $response->is_success();
459 }
460  
461 # Get Server Login URL
462 46 ▶sub getServerLoginURL {
Subroutine getServerLoginURL: called CC=1 SCAR=0.0
Pod undocumented
463 46 my ( $self, $service, $srvConf ) = @_;
464  
465 46 return "$srvConf->{casSrvMetaDataOptionsUrl}/login?"
466 . build_urlencoded( service => $service );
467 }
468  
469 # Get Server Logout URL
470 0 ▶sub getServerLogoutURL {
Subroutine getServerLogoutURL: not called CC=1 SCAR=6.9
Pod undocumented
471 0 my ( $self, $service, $srvUrl ) = @_;
472  
473 0 return "$srvUrl/logout?" . build_urlencoded( service => $service );
474 }
475  
476 # Validate ST
477 0 ▶sub validateST {
Subroutine validateST: not called CC=11 SCAR=48.8
Pod undocumented
478 0 my ( $self, $req, $service, $ticket, $srvConf, $proxied ) = @_;
479  
480 0 my %prm = ( service => $service, ticket => $ticket );
481  
482 0 my $samlValidate = $srvConf->{casSrvMetaDataOptionsSamlValidate};
483  
484 0 my $proxy_url;
485 0 ▶ if (%$proxied) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$proxied) 0 0 0
486 0 $proxy_url = $self->p->fullUrl($req);
487  
488 # TODO: @coudot: why die here without any message ?
489 0 ▶ die if ( $proxy_url =~ /casProxy=1/ );
Branch: 1 branch
BranchTrueFalseTotal
if $proxy_url =~ /casProxy=1/ 0 0 0
490 0 ▶ $proxy_url .= ( $proxy_url =~ /\?/ ? '&' : '?' ) . 'casProxy=1';
Branch: 1 branch
BranchTrueFalseTotal
$proxy_url =~ /\?/ ? : 0 0 0
491  
492 0 $self->logger->debug("CAS Proxy URL: $proxy_url");
493  
494 0 $req->data->{casProxyUrl} = $proxy_url;
495  
496 0 $prm{pgtUrl} = $proxy_url;
497 }
498  
499 0 my $request = do {
500 0 ▶ if ($samlValidate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($samlValidate) { } 0 0 0
501 $self->_buildRequestForSamlValidate(
502 0 $srvConf->{casSrvMetaDataOptionsUrl}, %prm );
503 }
504 else {
505 $self->_buildRequestForServiceValidate(
506 0 $srvConf->{casSrvMetaDataOptionsUrl}, %prm );
507 }
508 };
509  
510 0 my $response = $self->ua->request($request);
511  
512 0 $self->logger->debug(
513 "Get CAS serviceValidate response: " . $response->as_string );
514  
515 0 ▶ return 0 if $response->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_error 0 0 0
516  
517 0 my $xml = $response->decoded_content( default_charset => 'UTF-8' );
518  
519 0 my $extract_result = do {
520 0 ▶ if ($samlValidate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($samlValidate) { } 0 0 0
521 0 $self->getAttributesFromSamlValidateResponse($xml);
522 }
523 else {
524 0 $self->getAttributesFromServiceValidateResponse($xml);
525 }
526 };
527  
528 0 ▶ if ( $extract_result->{success} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($extract_result->{'success'}) { } 0 0 0
529 0 my $user = $extract_result->{user};
530 0 my $attributes = $extract_result->{attributes};
531  
532 # Flatten each list of attribute values
533 0 my $result = {};
534 0 ▶ while ( my ( $attribute, $values ) = each %$attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attributes) 0 0 0
535 $result->{$attribute} =
536 0 join( $self->conf->{multiValuesSeparator}, @$values );
537 }
538  
539 0 ▶ if ( $proxy_url and $extract_result->{pgtId} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proxy_url and $extract_result->{"pgtId"}) 0 0 0
Condition0%0/3
$proxy_url and $extract_result->{"pgtId"}
!ll&&!rl&&r
000
MC/DC0%0/2
$proxy_url and $extract_result->{"pgtId"}
$proxy_url $extract_result->{"pgtId"}
Truth table0%0/3
A and B
A= $proxy_urlB= $extract_result->{"pgtId"}
ABresult
0X0
100
111
540 0 $self->logger->debug("Storing PGT id");
541 0 $req->data->{pgtId} = $extract_result->{pgtId};
542 }
543  
544 0 return ( $user, $result );
545 }
546 else {
547 0 my $error = $extract_result->{message};
548 0 $self->logger->error(
549 "Failed to validate Service Ticket $ticket: $error");
550 0 return 0;
551 }
552 }
553  
554 0 ▶sub _buildRequestForServiceValidate {
Subroutine _buildRequestForServiceValidate: not called CC=1 SCAR=6.9
555 0 my ( $self, $url, %prm ) = @_;
556  
557 0 my $serviceValidateUrl = "$url/serviceValidate?" . build_urlencoded(%prm);
558 0 $self->logger->debug("Validate ST on CAS URL $serviceValidateUrl");
559  
560 0 return HTTP::Request->new( GET => $serviceValidateUrl );
561 }
562  
563 0 ▶sub _buildRequestForSamlValidate {
Subroutine _buildRequestForSamlValidate: not called CC=1 SCAR=6.9
564 0 my ( $self, $url, %prm ) = @_;
565  
566 my $serviceValidateUrl =
567 0 "$url/samlValidate?" . build_urlencoded( TARGET => $prm{service} );
568  
569 0 $self->logger->debug("Validate ST on CAS URL $serviceValidateUrl");
570  
571 return HTTP::Request->new(
572 POST => $serviceValidateUrl,
573 [ 'Content-Type' => 'text/xml; charset=UTF-8' ],
574 $self->buildSamlValidateRequest( $prm{ticket} )
575 0 );
576 }
577  
578 # Store PGT IOU and PGT ID
579 0 ▶sub storePGT {
Subroutine storePGT: not called CC=1 SCAR=6.9
Pod undocumented
580 0 my ( $self, $pgtIou, $pgtId ) = @_;
581  
582 0 my $infos = {
583 type => 'casPgtId',
584 _utime => time,
585 pgtIou => $pgtIou,
586 pgtId => $pgtId
587 };
588  
589 0 my $pgtSession = $self->getCasSession( undef, $infos );
590  
591 0 return $pgtSession->id;
592 }
593  
594 # Retrieve Proxy Ticket
595 0 ▶sub retrievePT {
Subroutine retrievePT: not called CC=3 SCAR=24.8
Pod undocumented
596 0 my ( $self, $service, $pgtId, $srvConf ) = @_;
597  
598 0 my $proxyUrl = "$srvConf->{casSrvMetaDataOptionsUrl}/proxy?"
599 . build_urlencoded( targetService => $service, pgt => $pgtId );
600  
601 0 my $response = $self->ua->get($proxyUrl);
602  
603 0 $self->logger->debug( "Get CAS proxy response: " . $response->as_string );
604  
605 0 ▶ return 0 if $response->is_error;
Branch: 1 branch
BranchTrueFalseTotal
if $response->is_error 0 0 0
606  
607 0 my $casResponse = $self->parser->parse_string( $response->decoded_content )
608 ->documentElement;
609  
610 0 ▶ if ( my $failure = $casResponse->getElementsByTagName('cas:proxyFailure') )
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $casResponse->getElementsByTagName("cas:proxyFailure")) 0 0 0
611 {
612 0 $self->logger->error(
613 "Failed to get PT: " . $failure->string_value =~ s/\R//r );
614 0 return 0;
615 }
616  
617 0 my $pt =
618 $casResponse->find('//cas:proxySuccess/cas:proxyTicket')->string_value;
619  
620 0 return $pt;
621 }
622  
623 # Get CAS App from service URL
624 25 ▶sub getCasApp {
Subroutine getCasApp: called CC=10 SCAR=23.0
Pod undocumented
625 25 my ( $self, $uri_param ) = @_;
626  
627 25 my $uri = URI->new($uri_param);
628 25 my $hostname = $uri->authority;
629 25 my $uriCanon = $uri->canonical;
630 25 ▶ return undef unless $hostname;
Branch: 1 branch
BranchTrueFalseTotal
unless $hostname 0 25 25
631  
632 25 my $prefixConfKey;
633 25 my $longestCandidate = "";
634 25 my $hostnameConfKey;
635  
636 25 for my $app ( sort keys %{ $self->casAppList } ) {
637  
638 47 for my $appservice (
639 split(
640 /\s+/, $self->casAppList->{$app}->{casAppMetaDataOptionsService}
641 )
642 )
643 {
644 53 my $candidateUri = URI->new($appservice);
645 53 my $candidateHost = $candidateUri->authority;
646 53 my $candidateCanon = $candidateUri->canonical;
647  
648 # Try to match prefix, remembering the longest match found
649 53 ▶ if ( index( $uriCanon, $candidateCanon ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($uriCanon, $candidateCanon) == 0) 10 43 53
650 10 ▶ if ( length($longestCandidate) < length($candidateCanon) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $longestCandidate < length $candidateCanon) 10 0 10
651 10 $longestCandidate = $candidateCanon;
652 10 $prefixConfKey = $app;
653 }
654 }
655  
656 # Try to match host, only if strict matching is disabled
657 53 ▶ unless ( $self->conf->{casStrictMatching} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->conf->{"casStrictMatching"}) 32 21 53
658 32 ▶ $hostnameConfKey = $app if ( $hostname eq $candidateHost );
Branch: 1 branch
BranchTrueFalseTotal
if $hostname eq $candidateHost 18 14 32
659 }
660 }
661 }
662  
663 # Application found by prefix has priority
664 25 ▶ return $prefixConfKey if $prefixConfKey;
Branch: 1 branch
BranchTrueFalseTotal
if $prefixConfKey 8 17 25
665 17 ▶ $self->logger->warn(
Branch: 1 branch
BranchTrueFalseTotal
if $hostnameConfKey 12 5 17
666 "Matched CAS service $hostnameConfKey based on hostname only. "
667 . "This will be deprecated in a future version" )
668 if $hostnameConfKey;
669 17 return $hostnameConfKey;
670 }
671  
672 # This method returns the host part of the given URL
673 # If the URL has no scheme, return it completely
674 # http://example.com/uri => example.com
675 # foo.bar => foo.bar
676 0 ▶sub _getHostForService {
Subroutine _getHostForService: not called CC=3 SCAR=24.8
677 0 my ( $self, $service ) = @_;
678 0 ▶ return undef unless $service;
Branch: 1 branch
BranchTrueFalseTotal
unless $service 0 0 0
679  
680 0 my $uri = URI->new($service);
681 0 ▶ return $uri->scheme ? $uri->host : $uri->as_string;
Branch: 1 branch
BranchTrueFalseTotal
$uri->scheme ? : 0 0 0
682 }
683  
684 # Build the XML Logout request that gets sent to CAS applications
685 4 ▶sub buildLogoutRequest {
Subroutine buildLogoutRequest: called CC=1 SCAR=0.0
Pod undocumented
686 4 my ( $self, $ticket ) = @_;
687  
688 4 my $now = strftime( "%Y-%m-%dT%TZ", gmtime() );
689  
690 4 my $doc = XML::LibXML::Document->new( '1.0', 'UTF-8' );
691 4 my $LogoutRequest =
692 $doc->createElementNS( "urn:oasis:names:tc:SAML:2.0:protocol",
693 "samlp:LogoutRequest" );
694  
695 # NB: $ticket is a good enough random ID :-)
696 # NB: although it looks alike SAML, the CAS SLO protocol is specific
697 4 $LogoutRequest->setAttribute( "ID", $ticket );
698 4 $LogoutRequest->setAttribute( "Version", "2.0" );
699 4 $LogoutRequest->setAttribute( "IssueInstant", $now );
700 4 my $NameID = $doc->createElementNS( "urn:oasis:names:tc:SAML:2.0:assertion",
701 "saml:NameID" );
702 4 $NameID->appendText('@NOT_USED@');
703 4 $LogoutRequest->appendChild($NameID);
704 4 my $SessionIndex =
705 $doc->createElementNS( "urn:oasis:names:tc:SAML:2.0:protocol",
706 "samlp:SessionIndex" );
707 4 $SessionIndex->appendText($ticket);
708 4 $LogoutRequest->appendChild($SessionIndex);
709 4 my $logout_request_text = $LogoutRequest->toString;
710  
711 4 $self->logger->debug("Generated CAS logout request: $logout_request_text");
712 4 return $logout_request_text;
713 }
714  
715 0 ▶sub buildSamlValidateRequest {
Subroutine buildSamlValidateRequest: not called CC=1 SCAR=6.9
Pod undocumented
716 0 my ( $self, $ticket ) = @_;
717  
718 0 my $now = strftime( "%Y-%m-%dT%TZ", gmtime() );
719  
720 0 my $document = XML::LibXML->createDocument( "1.0", "UTF-8" );
721  
722 0 my $envelope =
723 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
724 "SOAP-ENV:Envelope" );
725 0 $document->setDocumentElement($envelope);
726  
727 0 my $body =
728 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
729 "SOAP-ENV:Body" );
730 0 $envelope->appendChild($body);
731  
732 0 my $request =
733 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
734 "samlp:Request" );
735 0 $request->setAttribute( "MajorVersion", 1 );
736 0 $request->setAttribute( "MinorVersion", 1 );
737 0 $request->setAttribute( "RequestID",
738 "_" . unpack( "H*", Crypt::URandom::urandom(16) ) );
739 0 $request->setAttribute( "IssueInstant", $now );
740 0 $body->appendChild($request);
741  
742 0 my $assert_art =
743 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
744 "samlp:AssertionArtifact" );
745 0 $assert_art->appendText($ticket);
746 0 $request->appendChild($assert_art);
747  
748 0 my $request_text = $envelope->toString;
749 0 $self->logger->debug("Generated CAS samlValidate request: $request_text");
750 0 return $request_text;
751 }
752  
753 0 ▶sub getServiceTicketFromSamlRequest {
Subroutine getServiceTicketFromSamlRequest: not called CC=2 SCAR=17.9
Pod undocumented
754 0 my ( $self, $soap_message ) = @_;
755 0 my $artifact = eval {
756 0 my $dom = $self->parser->parse_string($soap_message);
757 0 my $xpc = XML::LibXML::XPathContext->new($dom);
758 0 $xpc->registerNs( 'samlp', 'urn:oasis:names:tc:SAML:1.0:protocol' );
759 0 $xpc->registerNs( 'saml', 'urn:oasis:names:tc:SAML:1.0:assertion' );
760 0 $xpc->registerNs( 'SOAP-ENV',
761 'http://schemas.xmlsoap.org/soap/envelope/' );
762 0 $xpc->find('//samlp:AssertionArtifact/text()')->string_value();
763  
764 };
765 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
766 0 $self->logger->error("Could not process SamlValidate request: $@");
767 0 return;
768 }
769 0 return $artifact;
770 }
771  
772 0 ▶sub getAttributesFromServiceValidateResponse {
Subroutine getAttributesFromServiceValidateResponse: not called CC=11 SCAR=48.8
Pod undocumented
773 0 my ( $self, $xml ) = @_;
774  
775 0 my $casResponse = $self->parser->parse_string($xml)->documentElement;
776  
777 0 ▶ unless ( $casResponse->nodeName eq "cas:serviceResponse" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($casResponse->nodeName eq "cas:serviceResponse") 0 0 0
778 return {
779 0 success => 0,
780 message => (
781 "unexpected top-level XML element: " . $casResponse->nodeName
782 ),
783 };
784 }
785  
786 0 ▶ if ( my $failure =
Branch: 1 branch
BranchTrueFalseTotal
if (my $failure = $casResponse->getElementsByTagName("cas:authenticationFailure")) 0 0 0
787 $casResponse->getElementsByTagName('cas:authenticationFailure') )
788 {
789 return {
790 0 success => 0,
791 message => ( $failure->string_value =~ s/\R//r ),
792 };
793 }
794  
795 0 my $pgtId;
796  
797 # Get proxy data and store pgtId
798 0 my $pgtIou =
799 $casResponse->find('//cas:authenticationSuccess/cas:proxyGrantingTicket')
800 ->string_value;
801  
802 0 ▶ if ($pgtIou) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pgtIou) 0 0 0
803 0 my $moduleOptions;
804 0 ▶ if ( $self->conf->{casStorage} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->conf->{'casStorage'}) { } 0 0 0
805 0 ▶ $moduleOptions = $self->conf->{casStorageOptions} || {};
Condition0%0/2
$self->conf->{'casStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'casStorageOptions'} || {}
$self->conf->{'casStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'casStorageOptions'}
Aresult
11
00
806 0 $moduleOptions->{backend} = $self->conf->{casStorage};
807 }
808 else {
809 0 ▶ $moduleOptions = $self->conf->{globalStorageOptions} || {};
Condition0%0/2
$self->conf->{'globalStorageOptions'} || {}
l!l
00
MC/DC0%0/1
$self->conf->{'globalStorageOptions'} || {}
$self->conf->{'globalStorageOptions'}
Truth table0%0/2
A
A= $self->conf->{'globalStorageOptions'}
Aresult
11
00
810 0 $moduleOptions->{backend} = $self->conf->{globalStorage};
811 }
812 0 my $module = "Lemonldap::NG::Common::Apache::Session";
813  
814 0 my $pgtIdSessions =
815 $module->searchOn( $moduleOptions, "pgtIou", $pgtIou );
816  
817 0 foreach my $id (
818 0 grep { $pgtIdSessions->{$_}->{_session_kind} eq $self->sessionKind }
819 keys %$pgtIdSessions
820 )
821 {
822  
823 # There should be only on session
824 0 ▶ my $pgtIdSession = $self->getCasSession( $id, undef, 0 ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pgtIdSession = $self->getCasSession($id, undef, 0) or next 0 0 0
825 0 $pgtId = $pgtIdSession->data->{pgtId};
826 0 $pgtIdSession->remove;
827 }
828 }
829  
830 0 my $user =
831 $casResponse->find('//cas:authenticationSuccess/cas:user')->string_value;
832 0 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
833 return {
834 0 success => 0,
835 message => "Could not extract cas:user field from XML response",
836 };
837 }
838  
839 0 my $attrs = Hash::MultiValue->new;
840 0 ▶ if ( my $casAttr =
Branch: 1 branch
BranchTrueFalseTotal
if (my $casAttr = $casResponse->find("//cas:authenticationSuccess/cas:attributes/cas:*")) 0 0 0
841 $casResponse->find('//cas:authenticationSuccess/cas:attributes/cas:*') )
842 {
843 $casAttr->foreach(
844 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
845 0 my $k = $_[0]->localname;
846 0 my $v = $_[0]->textContent;
847 0 utf8::encode($v);
848 0 $attrs->add( $k => $v );
849 }
850 0 );
851 }
852 return {
853 0 success => 1,
854 user => $user,
855 attributes => $attrs->multi,
856 pgtId => $pgtId,
857 };
858 }
859  
860 0 ▶sub getAttributesFromSamlValidateResponse {
Subroutine getAttributesFromSamlValidateResponse: not called CC=3 SCAR=24.8
Pod undocumented
861 0 my ( $self, $soap_message ) = @_;
862 0 my $result = eval {
863 0 my $dom = $self->parser->parse_string($soap_message);
864 0 my $xpc = XML::LibXML::XPathContext->new($dom);
865 0 $xpc->registerNs( 'samlp', 'urn:oasis:names:tc:SAML:1.0:protocol' );
866 0 $xpc->registerNs( 'saml', 'urn:oasis:names:tc:SAML:1.0:assertion' );
867 0 $xpc->registerNs( 'SOAP-ENV',
868 'http://schemas.xmlsoap.org/soap/envelope/' );
869 0 my $response =
870 $xpc->find('/SOAP-ENV:Envelope/SOAP-ENV:Body/samlp:Response')->pop;
871 0 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) { } 0 0 0
872 0 return $self->_saml_validate_response($response);
873 }
874 else {
875 return {
876 0 success => 0,
877 message =>
878 "samlValidate response did not contain a Response element",
879 };
880 }
881 };
882 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
883 return {
884 0 success => 0,
885 message => "Could not process samlValidate response: $@",
886 };
887 }
888 0 return $result;
889 }
890  
891 0 ▶sub _saml_validate_response {
Subroutine _saml_validate_response: not called CC=5 SCAR=34.0
892 0 my ( $self, $response ) = @_;
893 0 my $status =
894 $response->find('./samlp:Status/samlp:StatusCode/@Value')->string_value();
895 0 ▶ if ( $status and $status eq "samlp:Success" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status and $status eq 'samlp:Success') { } 0 0 0
Condition0%0/3
$status and $status eq "samlp:Success"
!ll&&!rl&&r
000
MC/DC0%0/2
$status and $status eq "samlp:Success"
$status $status eq "samlp:Success"
Truth table0%0/3
A and B
A= $statusB= $status eq "samlp:Success"
ABresult
0X0
100
111
896 0 my $user =
897 $response->find( './saml:Assertion'
898 . '/*[self::saml:AttributeStatement or self::saml:AuthenticationStatement]'
899 . '/saml:Subject/saml:NameIdentifier/text()' )->string_value();
900  
901 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
902 0 my $attributes = $self->_saml_validate_get_attributes($response);
903 return {
904 0 success => 1,
905 user => $user,
906 attributes => $attributes,
907 };
908 }
909 else {
910 return {
911 0 success => 0,
912 message => "No subject found in samlValidate response",
913 };
914 }
915 }
916 else {
917 0 ▶ my $display_status = $status // '<not found>';
Condition0%0/2
$status // "<not found>"
l!l
00
MC/DC0%0/1
$status // "<not found>"
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
918 return {
919 0 success => 0,
920 message => "samlValidate response status is $display_status",
921 };
922 }
923 }
924  
925 0 ▶sub _saml_validate_get_attributes {
Subroutine _saml_validate_get_attributes: not called CC=4 SCAR=30.0
926 0 my ( $self, $response ) = @_;
927 0 my $attributes = {};
928 0 my @attribute_list = $response->find(
929 './saml:Assertion' . '/saml:AttributeStatement/saml:Attribute' )
930 ->get_nodelist;
931 0 for my $attribute (@attribute_list) {
932 0 my $attribute_name = $attribute->getAttribute("AttributeName");
933 0 ▶ if ($attribute_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute_name) 0 0 0
934 0 my @attribute_values =
935 $attribute->find("./saml:AttributeValue")->get_nodelist;
936 0 ▶ if (@attribute_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@attribute_values) 0 0 0
937 $attributes->{$attribute_name} = [
938 map {
939 0 my $text = $_->textContent;
940 0 utf8::encode($text);
941 0 $text =~ s/^\s+//;
942 0 $text =~ s/\s+$//;
943 0 $text;
944 } @attribute_values
945 ];
946 }
947 }
948 }
949  
950 0 return $attributes;
951 }
952  
953 0 ▶sub returnSamlValidateSuccess {
Subroutine returnSamlValidateSuccess: not called CC=5 SCAR=34.0
Pod undocumented
954 0 my ( $self, $req, $username, $pgtIou, $proxies, $attributes ) = @_;
955  
956 0 my $document = XML::LibXML->createDocument( "1.0", "UTF-8" );
957  
958 0 my $envelope =
959 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
960 "SOAP-ENV:Envelope" );
961 0 $document->setDocumentElement($envelope);
962  
963 0 my $body =
964 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
965 "SOAP-ENV:Body" );
966 0 $envelope->appendChild($body);
967  
968 0 my $issueInstant = strftime( "%Y-%m-%dT%TZ", gmtime() );
969 0 my $notAfter = strftime( "%Y-%m-%dT%TZ", gmtime( time + 300 ) );
970 my $authInstant = strftime( "%Y-%m-%dT%TZ",
971 0 gmtime( $req->sessionInfo->{_lastAuthnUTime} ) );
972 0 my $response =
973 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
974 "Response" );
975 0 $response->setNamespace( "urn:oasis:names:tc:SAML:1.0:assertion",
976 "saml", 0 );
977 0 $response->setNamespace( "urn:oasis:names:tc:SAML:1.0:protocol",
978 "samlp", 0 );
979  
980 0 $response->setAttribute( "IssueInstant", $issueInstant );
981 0 $response->setAttribute( "MajorVersion", "1" );
982 0 $response->setAttribute( "MinorVersion", "1" );
983 0 $body->appendChild($response);
984  
985 0 my $status =
986 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
987 "Status" );
988 0 $response->appendChild($status);
989  
990 0 my $status_code =
991 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
992 "StatusCode" );
993 0 $status_code->setAttribute( "Value", "samlp:Success" );
994 0 $status->appendChild($status_code);
995  
996 0 my $assertion =
997 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
998 "Assertion" );
999 0 $assertion->setAttribute( "AssertionID",
1000 "_" . unpack( "H*", Crypt::URandom::urandom(16) ) );
1001 0 $assertion->setAttribute( "IssueInstant", $issueInstant );
1002 0 $assertion->setAttribute( "MajorVersion", "1" );
1003 0 $assertion->setAttribute( "MinorVersion", "1" );
1004  
1005 0 $response->appendChild($assertion);
1006  
1007 0 my $assertion_conditions =
1008 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
1009 "Conditions" );
1010 0 $assertion_conditions->setAttribute( "NotBefore", $issueInstant );
1011 0 $assertion_conditions->setAttribute( "NotOnOrAfter", $notAfter );
1012 0 $assertion->appendChild($assertion_conditions);
1013  
1014 0 my $authentication_statement =
1015 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
1016 "AuthenticationStatement" );
1017 0 $authentication_statement->setAttribute( "AuthenticationInstant",
1018 $authInstant );
1019 0 $authentication_statement->setAttribute( "AuthenticationMethod",
1020 "urn:oasis:names:tc:SAML:1.0:am:unspecified" );
1021 0 $assertion->appendChild($authentication_statement);
1022  
1023 0 my $subject =
1024 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
1025 "Subject" );
1026 0 $authentication_statement->appendChild($subject);
1027 0 my $name_identifier =
1028 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
1029 "NameIdentifier" );
1030 0 $name_identifier->appendText($username);
1031 0 $subject->appendChild($name_identifier);
1032  
1033 0 my $attribute_statement =
1034 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:assertion",
1035 "AttributeStatement" );
1036 0 $attribute_statement->appendChild( $subject->cloneNode(1) );
1037 0 $assertion->appendChild($attribute_statement);
1038  
1039 0 ▶ if ( defined $attributes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attributes) 0 0 0
1040 0 foreach my $attribute ( keys %$attributes ) {
1041 0 my $saml_attribute = $document->createElementNS(
1042 "urn:oasis:names:tc:SAML:1.0:assertion", "Attribute" );
1043 0 $saml_attribute->setAttribute( "AttributeName", $attribute );
1044 0 $saml_attribute->setAttribute( "AttributeNamespace",
1045 "http://www.ja-sig.org/products/cas/" );
1046 0 my $has_value;
1047  
1048 0 foreach my $value (
1049 split(
1050 $self->conf->{multiValuesSeparator},
1051 $attributes->{$attribute}
1052 )
1053 )
1054 {
1055 0 my $attribute_value = $document->createElementNS(
1056 "urn:oasis:names:tc:SAML:1.0:assertion",
1057 "AttributeValue" );
1058 0 utf8::downgrade($value);
1059 0 $attribute_value->appendText($value);
1060 0 $saml_attribute->appendChild($attribute_value);
1061 0 $has_value = 1;
1062 }
1063  
1064 0 ▶ $attribute_statement->appendChild($saml_attribute) if $has_value;
Branch: 1 branch
BranchTrueFalseTotal
if $has_value 0 0 0
1065 }
1066 }
1067  
1068 0 my $soap_response = $envelope->toString;
1069 0 utf8::encode($soap_response);
1070 0 return $self->sendSoapResponse( $req, $soap_response );
1071 }
1072  
1073 0 ▶sub returnSamlValidateError {
Subroutine returnSamlValidateError: not called CC=2 SCAR=17.9
Pod undocumented
1074 0 my ( $self, $req, $code, $reason ) = @_;
1075  
1076 my $saml_code =
1077 0 ▶ { INVALID_REQUEST => "Requester", INVALID_TICKET => "Requester" }->{$code}
Condition0%0/2
{"INVALID_REQUEST", "Requester", "INVALID_TICKET", "Requester"}->{$code} // "Responder"
l!l
00
MC/DC0%0/1
{"INVALID_REQUEST", "Requester", "INVALID_TICKET", "Requester"}->{$code} // "Responder"
{"INVALID_REQUEST", "Requester", "INVALID_TICKET", "Requester"}->{$code}
Truth table0%0/2
A
A= {"INVALID_REQUEST", "Requester", "INVALID_TICKET", "Requester"}->{$code}
Aresult
11
00
1078 // "Responder";
1079  
1080 0 my $document = XML::LibXML->createDocument( "1.0", "UTF-8" );
1081  
1082 0 my $envelope =
1083 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
1084 "SOAP-ENV:Envelope" );
1085 0 $document->setDocumentElement($envelope);
1086  
1087 0 my $body =
1088 $document->createElementNS( "http://schemas.xmlsoap.org/soap/envelope/",
1089 "SOAP-ENV:Body" );
1090 0 $envelope->appendChild($body);
1091  
1092 0 my $response =
1093 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
1094 "Response" );
1095 0 $response->setNamespace( "urn:oasis:names:tc:SAML:1.0:assertion",
1096 "saml", 0 );
1097 0 $response->setNamespace( "urn:oasis:names:tc:SAML:1.0:protocol",
1098 "samlp", 0 );
1099  
1100 0 $response->setAttribute( "IssueInstant",
1101 strftime( "%Y-%m-%dT%TZ", gmtime() ) );
1102 0 $response->setAttribute( "MajorVersion", "1" );
1103 0 $response->setAttribute( "MinorVersion", "1" );
1104 0 $body->appendChild($response);
1105  
1106 0 my $status =
1107 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
1108 "Status" );
1109 0 $response->appendChild($status);
1110  
1111 0 my $status_code =
1112 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
1113 "StatusCode" );
1114 0 $status_code->setAttribute( "Value", "samlp:$saml_code" );
1115 0 $status->appendChild($status_code);
1116  
1117 0 my $status_message =
1118 $document->createElementNS( "urn:oasis:names:tc:SAML:1.0:protocol",
1119 "StatusMessage" );
1120 0 $status_message->appendText($reason);
1121 0 $status->appendChild($status_message);
1122  
1123 0 my $soap_response = $envelope->toString;
1124 0 utf8::encode($soap_response);
1125 0 return $self->sendSoapResponse( $req, $soap_response );
1126 }
1127  
1128 21 1;
1129 __END__
« blib/lib/Lemonldap/NG/Portal/Plugins/AdaptativeAuthenticationLevel.pm ↑ Summary blib/lib/Lemonldap/NG/Portal/Lib/RESTProxy.pm »