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

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