blib/lib/AuthCAS.pm

filter: Statementstmt 40.7% 86 / 211 Branchbran 27.0% 20 / 74 Conditioncond 48.4% 16 / 33 MC/DCmcdc 11.7% 2 / 17 Subroutinesub 50.0% 11 / 22 Podpod 100% 16 / 16 totaltotal 40.4% 151 / 373 CC 55 SCAR 30.5
CC
55
Cov
38%
dump_var
56.0
get_https2
33.9
validatePT
30.0
CRAP 21.2
↑ Summary
1  
2 package AuthCAS;
3  
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw( $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 $VERSION = '1.7';
8  
9 =encoding utf8
10  
11 =head1 NAME
12  
13 AuthCAS - Client library for JA-SIG CAS 2.0 authentication server
14  
15 =head1 VERSION
16  
17 Version 1.7
18  
19 =head1 DESCRIPTION
20  
21 AuthCAS aims at providing a Perl API to JA-SIG Central Authentication System (CAS).
22 Only a basic Perl library is provided with CAS whereas AuthCAS is a full object-oriented library.
23  
24 =head1 PREREQUISITES
25  
26 This script requires IO::Socket::SSL and LWP::UserAgent
27  
28 =head1 SYNOPSIS
29  
30 A simple example with a direct CAS authentication
31  
32 use AuthCAS;
33 my $cas = new AuthCAS(casUrl => 'https://cas.myserver,
34 CAFile => '/etc/httpd/conf/ssl.crt/ca-bundle.crt',
35 );
36  
37 my $login_url = $cas->getServerLoginURL('http://myserver/app.cgi');
38  
39 ## The user should be redirected to the $login_url
40 ## When coming back from the CAS server a ticket is provided in the QUERY_STRING
41  
42 ## $ST should contain the receaved Service Ticket
43 my $user = $cas->validateST('http://myserver/app.cgi', $ST);
44  
45 printf "User authenticated as %s\n", $user;
46  
47  
48 In the following example a proxy is requesting a Proxy Ticket for the target application
49  
50 $cas->proxyMode(pgtFile => '/tmp/pgt.txt',
51 pgtCallbackUrl => 'https://myserver/proxy.cgi?callback=1
52 );
53
54 ## Same as before but the URL is the proxy URL
55 my $login_url = $cas->getServerLoginURL('http://myserver/proxy.cgi');
56  
57 ## Like in the previous example we should receave a $ST
58  
59 my $user = $cas->validateST('http://myserver/proxy.cgi', $ST);
60  
61 ## Process errors
62 printf STDERR "Error: %s\n", &AuthCAS::get_errors() unless (defined $user);
63  
64 ## Now we request a Proxy Ticket for the target application
65 my $PT = $cas->retrievePT('http://myserver/app.cgi');
66
67 ## This piece of code is executed by the target application
68 ## It received a Proxy Ticket from the proxy
69 my ($user, @proxies) = $cas->validatePT('http://myserver/app.cgi', $PT);
70  
71 printf "User authenticated as %s via %s proxies\n", $user, join(',',@proxies);
72  
73  
74 =head1 DESCRIPTION
75  
76 Jasig CAS is Yale University's web authentication system, heavily inspired by Kerberos.
77 Release 2.0 of CAS provides "proxied credential" feature that allows authentication
78 tickets to be carried by intermediate applications (Portals for instance), they are
79 called proxy.
80  
81 This AuthCAS Perl module provides required subroutines to validate and retrieve CAS tickets.
82  
83 =cut
84  
85 3 my @ISA = qw(Exporter);
86 3 my @EXPORT = qw($errors);
87  
88 3 my $errors;
89  
90 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 =pod
93  
94 =head2 new
95  
96 my $cas = new AuthCAS(
97 casUrl => 'https://cas.myserver',
98 CAFile => '/etc/httpd/conf/ssl.crt/ca-bundle.crt',
99 );
100  
101 The C<new> constructor lets you create a new B<AuthCAS> object.
102  
103 =over
104  
105 =item casUrl - REQUIRED
106  
107 =item CAFile
108  
109 =item CAPath
110  
111 =item loginPath - '/login'
112  
113 =item logoutPath - '/logout'
114  
115 =item serviceValidatePath - '/serviceValidate'
116  
117 =item proxyPath - '/proxy'
118  
119 =item proxyValidatePath - '/proxyValidate'
120  
121 =item SSL_version - unset
122  
123 Sets the version of the SSL protocol used to transmit data. If the default causes connection issues, setting it to 'SSLv3' may help.
124 see the documentation for L<IO::Socket::SSL/"METHODS"> for more information
125 see L<http://www.perlmonks.org/?node_id=746493> for more details.
126  
127 =back
128  
129 Returns a new B<AuthCAS> or dies on error.
130  
131 =cut
132  
133 2 ▶sub new {
Subroutine new: called CC=6 SCAR=20.3
Pod documented
134 2 my ( $pkg, %param ) = @_;
135 my $cas_server = {
136 url => $param{'casUrl'},
137 CAFile => $param{'CAFile'},
138 CAPath => $param{'CAPath'},
139  
140 loginPath => $param{'loginPath'} || '/login',
141 logoutPath => $param{'logoutPath'} || '/logout',
142 serviceValidatePath => $param{'serviceValidatePath'}
143 || '/serviceValidate',
144 proxyPath => $param{'proxyPath'} || '/proxy',
145 proxyValidatePath => $param{'proxyValidatePath'} || '/proxyValidate',
146 SSL_version => $param{SSL_version},
147 2 ▶ };
Condition50%5/10
$param{'loginPath'} || '/login'
l!l
02
$param{'logoutPath'} || '/logout'
l!l
02
$param{'serviceValidatePath'} || '/serviceValidate'
l!l
02
$param{'proxyPath'} || '/proxy'
l!l
02
$param{'proxyValidatePath'} || '/proxyValidate'
l!l
02
MC/DC0%0/1
$param{'loginPath'} || '/login'
$param{'loginPath'}
MC/DC0%0/1
$param{'logoutPath'} || '/logout'
$param{'logoutPath'}
MC/DC0%0/1
$param{'serviceValidatePath'} || '/serviceValidate'
$param{'serviceValidatePath'}
MC/DC0%0/1
$param{'proxyPath'} || '/proxy'
$param{'proxyPath'}
MC/DC0%0/1
$param{'proxyValidatePath'} || '/proxyValidate'
$param{'proxyValidatePath'}
Truth table50%1/2
A
A= $param{'loginPath'}
Aresult
11
00
Truth table50%1/2
A
A= $param{'logoutPath'}
Aresult
11
00
Truth table50%1/2
A
A= $param{'serviceValidatePath'}
Aresult
11
00
Truth table50%1/2
A
A= $param{'proxyPath'}
Aresult
11
00
Truth table50%1/2
A
A= $param{'proxyValidatePath'}
Aresult
11
00
148  
149 2 bless $cas_server, $pkg;
150  
151 2 return $cas_server;
152 }
153  
154 =pod
155  
156 =head2 get_errors
157  
158 Return module errors
159  
160 =cut
161  
162 0 ▶sub get_errors {
Subroutine get_errors: not called CC=1 SCAR=6.9
Pod documented
163 0 return $errors;
164 }
165  
166 =pod
167  
168 =head2 proxyMode
169  
170 Use the CAS object as a proxy
171  
172  
173 =over
174  
175 =item pgtFile
176 =item pgtCallbackUrl
177  
178 =back
179  
180  
181 =cut
182  
183 0 ▶sub proxyMode {
Subroutine proxyMode: not called CC=1 SCAR=6.9
Pod documented
184 0 my $self = shift;
185 0 my %param = @_;
186  
187 0 $self->{'pgtFile'} = $param{'pgtFile'};
188 0 $self->{'pgtCallbackUrl'} = $param{'pgtCallbackUrl'};
189 0 $self->{'proxy'} = 1;
190  
191 0 return 1;
192 }
193  
194 ## Escape dangerous chars in URLS
195 5 ▶sub _escape_chars {
Subroutine _escape_chars: called CC=2 SCAR=6.9
196 5 my $s = shift;
197  
198 ## Escape chars
199 ## !"#$%&'()+,:;<=>?[] AND accented chars
200 ## escape % first
201 # foreach my $i (0x25,0x20..0x24,0x26..0x2c,0x3a..0x3f,0x5b,0x5d,0x80..0x9f,0xa0..0xff) {
202 5 foreach my $i (0x26) {
203 5 my $hex_i = sprintf "%lx", $i;
204 5 $s =~ s/\x$hex_i/%$hex_i/g;
205 }
206  
207 5 return $s;
208 }
209  
210 =pod
211  
212 =head2 dump_var
213  
214  
215 =cut
216  
217 0 ▶sub dump_var {
Subroutine dump_var: not called CC=7 SCAR=40.3
Pod documented
218 0 my ( $var, $level, $fd ) = @_;
219  
220 0 ▶ if ( ref($var) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $var) { } 0 0 0
221 0 ▶ if ( ref($var) eq 'ARRAY' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $var eq 'ARRAY') { } 0 0 0
elsif (ref $var eq 'HASH') { } 0 0 0
222 0 foreach my $index ( 0 .. $#{$var} ) {
223 0 print $fd "\t" x $level . $index . "\n";
224 0 &dump_var( $var->[$index], $level + 1, $fd );
225 }
226 }
227 elsif ( ref($var) eq 'HASH' ) {
228 0 foreach my $key ( sort keys %{$var} ) {
229 0 print $fd "\t" x $level . '_' . $key . '_' . "\n";
230 0 &dump_var( $var->{$key}, $level + 1, $fd );
231 }
232 }
233 }
234 else {
235 0 ▶ if ( defined $var ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $var) { } 0 0 0
236 0 print $fd "\t" x $level . "'$var'" . "\n";
237 }
238 else {
239 0 print $fd "\t" x $level . "UNDEF\n";
240 }
241 }
242 }
243  
244 ## Parse an HTTP URL
245 3 ▶sub _parse_url {
Subroutine _parse_url: called CC=6 SCAR=21.9
246 3 my $url = shift;
247  
248 3 my ( $host, $port, $path );
249  
250 3 ▶ if ( $url =~ /^(https?):\/\/([^:\/]+)(:(\d+))?(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($url =~ m[^(https?)://([^:/]+)(:(\d+))?(.*)$]) { } 3 0 3
251 3 $host = $2;
252 3 $path = $5;
253 3 ▶ if ( $1 eq 'http' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($1 eq 'http') { } 0 3 3
elsif ($1 eq 'https') { } 3 0 3
254 0 ▶ $port = $4 || 80;
Condition0%0/2
$4 || 80
l!l
00
MC/DC0%0/1
$4 || 80
$4
Truth table0%0/2
A
A= $4
Aresult
11
00
255 }
256 elsif ( $1 eq 'https' ) {
257 3 ▶ $port = $4 || 443;
Condition100%2/2
$4 || 443
l!l
21
MC/DC100%1/1
$4 || 443
$4
Truth table100%2/2
A
A= $4
Aresult
11
00
258 }
259 else {
260 0 $errors = sprintf "Unknown protocol '%s'\n", $1;
261 0 return undef;
262 }
263 }
264 else {
265 0 $errors = sprintf "Unable to parse URL '%s'\n", $url;
266 0 return undef;
267 }
268  
269 3 return ( $host, $port, $path );
270 }
271  
272 ## Simple XML parser
273 0 ▶sub _parse_xml {
Subroutine _parse_xml: not called CC=3 SCAR=24.8
274 0 my $data = shift;
275  
276 0 my %xml_struct;
277  
278 0 ▶ while ( $data =~ /^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(<\/\1>)/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ m[^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(</\1>)]m) 0 0 0
279 0 my ( $new_tag, $new_data ) = ( $1, $3 );
280 0 chomp $new_data;
281 0 $new_data =~ s/^[\s\n]+//m;
282 0 $data =~ s/^<$new_tag(\s+[^\s>]+)*>([\s\S\n]*)(<\/$new_tag>)//m;
283 0 $data =~ s/^[\s\n]+//m;
284  
285 ## Check if data still includes XML tags
286 0 my $struct;
287 0 ▶ if ( $new_data =~ /^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(<\/\1>)/m ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_data =~ m[^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(</\1>)]m) { } 0 0 0
288 0 $struct = &_parse_xml($new_data);
289 }
290 else {
291 0 $struct = $new_data;
292 }
293 0 push @{ $xml_struct{$new_tag} }, $struct;
294 }
295  
296 0 return \%xml_struct;
297 }
298  
299 =pod
300  
301 =head2 getServerLoginURL($service)
302  
303 Returns a URL that you can redirect the browser to, which includes the URL to return to
304  
305 TODO: it escapes the return URL, but I've noticed some issues with more complicated URL's
306  
307 =cut
308  
309 2 ▶sub getServerLoginURL {
Subroutine getServerLoginURL: called CC=1 SCAR=0.0
Pod documented
310 2 my $self = shift;
311 2 my $service = shift;
312  
313 return
314 $self->{'url'}
315 2 . $self->{'loginPath'}
316 . '?service='
317 . &_escape_chars($service);
318 }
319  
320 =pod
321  
322 =head2 getServerLoginGatewayURL($service)
323  
324 Returns non-blocking login URL
325 ie: if user is logged in, return the ticket, otherwise do not prompt for login
326  
327 =cut
328  
329 0 ▶sub getServerLoginGatewayURL {
Subroutine getServerLoginGatewayURL: not called CC=1 SCAR=6.9
Pod documented
330 0 my $self = shift;
331 0 my $service = shift;
332  
333 return
334 $self->{'url'}
335 0 . $self->{'loginPath'}
336 . '?service='
337 . &_escape_chars($service)
338 . '&gateway=1';
339 }
340  
341 =pod
342  
343 =head2 getServerLogoutURL($service)
344  
345 Return logout URL
346 After logout user is redirected back to the application
347  
348 =cut
349  
350 0 ▶sub getServerLogoutURL {
Subroutine getServerLogoutURL: not called CC=1 SCAR=6.9
Pod documented
351 0 my $self = shift;
352 0 my $service = shift;
353  
354 return
355 $self->{'url'}
356 0 . $self->{'logoutPath'}
357 . '?service='
358 . &_escape_chars($service)
359 . '&gateway=1';
360 }
361  
362 =pod
363  
364 =head2 getServerServiceValidateURL($service, $ticket, $pgtUrl)
365  
366 Returns
367  
368 =cut
369  
370 3 ▶sub getServerServiceValidateURL {
Subroutine getServerServiceValidateURL: called CC=2 SCAR=7.1
Pod documented
371 3 my $self = shift;
372 3 my $service = shift;
373 3 my $ticket = shift;
374 3 my $pgtUrl = shift;
375  
376 3 my $query_string =
377 'service=' . &_escape_chars($service) . '&ticket=' . $ticket;
378 3 ▶ if ( defined $pgtUrl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pgtUrl) 0 3 3
379 0 $query_string .= '&pgtUrl=' . &_escape_chars($pgtUrl);
380 }
381  
382 ## URL was /validate with CAS 1.0
383 return
384 $self->{'url'}
385 3 . $self->{'serviceValidatePath'} . '?'
386 . $query_string;
387 }
388  
389 =pod
390  
391 =head2 getServerProxyURL($targetService, $pgt)
392  
393 Returns
394  
395 =cut
396  
397 0 ▶sub getServerProxyURL {
Subroutine getServerProxyURL: not called CC=1 SCAR=6.9
Pod documented
398 0 my $self = shift;
399 0 my $targetService = shift;
400 0 my $pgt = shift;
401  
402 return
403 $self->{'url'}
404 0 . $self->{'proxyPath'}
405 . '?targetService='
406 . &_escape_chars($targetService) . '&pgt='
407 . &_escape_chars($pgt);
408 }
409  
410 =pod
411  
412 =head2 getServerProxyValidateURL($service, $ticket)
413  
414 Returns
415  
416 =cut
417  
418 0 ▶sub getServerProxyValidateURL {
Subroutine getServerProxyValidateURL: not called CC=1 SCAR=6.9
Pod documented
419 0 my $self = shift;
420 0 my $service = shift;
421 0 my $ticket = shift;
422  
423 return
424 $self->{'url'}
425 0 . $self->{'proxyValidatePath'}
426 . '?service='
427 . &_escape_chars($service)
428 . '&ticket='
429 . &_escape_chars($ticket);
430  
431 }
432  
433 =pod
434  
435 =head2 validateST($service, $ticket)
436  
437 Validate a Service Ticket
438 Also used to get a PGT
439  
440  
441 Returns the login that created the ticket, if the ticket is valid for that $service URL
442  
443 returns undef if the ticket is not valid.
444  
445 =cut
446  
447 5 ▶sub validateST {
Subroutine validateST: called CC=10 SCAR=33.8
Pod documented
448 5 my $self = shift;
449 5 my $service = shift;
450 5 my $ticket = shift;
451
452 5 ▶ if (!defined($service)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service) 1 4 5
453 1 $errors = 'Need a service url to validate ticket.';
454 1 return undef;
455 }
456 4 ▶ if (!defined($ticket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ticket) 1 3 4
457 1 $errors = 'No ticket to validate.';
458 1 return undef;
459 }
460  
461 3 my $pgtUrl = $self->{'pgtCallbackUrl'};
462  
463 3 my $xml =
464 $self->callCAS(
465 $self->getServerServiceValidateURL( $service, $ticket, $pgtUrl ) );
466  
467 3 ▶ if ( defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:authenticationFailure"}) 0 3 3
468 {
469 $errors = sprintf "Failed to validate Service Ticket %s : %s\n",
470 $ticket,
471 0 $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}[0];
472 0 return undef;
473 }
474  
475 my $user =
476 $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]
477 3 {'cas:user'}[0];
478  
479 ## If in Proxy mode, also retreave a PGT
480 3 ▶ if ( $self->{'proxy'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"proxy"}) 0 3 3
481 0 my $pgtIou;
482 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:authenticationSuccess"}[0]{"cas:proxyGrantingTicket"}) 0 0 0
483 defined $xml->{'cas:serviceResponse'}[0]
484 {'cas:authenticationSuccess'}[0]{'cas:proxyGrantingTicket'} )
485 {
486 $pgtIou =
487 $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]
488 0 {'cas:proxyGrantingTicket'}[0];
489 }
490  
491 0 ▶ unless ( defined $self->{'pgtFile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"pgtFile"}) 0 0 0
492 0 $errors = sprintf "pgtFile not defined\n";
493 0 return undef;
494 }
495  
496 ## Check stored PGT
497 0 ▶ unless ( open STORE, $self->{'pgtFile'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open STORE, $self->{"pgtFile"}) 0 0 0
498 0 $errors = sprintf "Unable to read %s\n", $self->{'pgtFile'};
499 0 return undef;
500 }
501  
502 0 my $pgtId;
503 0 ▶ while (<STORE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline STORE)) 0 0 0
504 0 ▶ if (/^$pgtIou\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^$pgtIou\s+(.+)$/) 0 0 0
505 0 $pgtId = $1;
506 0 last;
507 }
508 }
509  
510 0 $self->{'pgtId'} = $pgtId;
511 }
512  
513 3 return ($user);
514 }
515  
516 =pod
517  
518 =head2 validatePT($service, $ticket)
519  
520 Validate a Proxy Ticket
521  
522 Returns the login that created the ticket, if the ticket is valid for that $service URL,
523 and a list of Proxies used.
524
525 user returned == undef if its not a valid ticket
526  
527 =cut
528  
529 0 ▶sub validatePT {
Subroutine validatePT: not called CC=5 SCAR=34.0
Pod documented
530 0 my $self = shift;
531 0 my $service = shift;
532 0 my $ticket = shift;
533  
534 0 ▶ if (!defined($service)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service) 0 0 0
535 0 $errors = 'Need a service url to validate ticket.';
536 0 return undef;
537 }
538 0 ▶ if (!defined($ticket)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ticket) 0 0 0
539 0 $errors = 'No ticket to validate.';
540 0 return undef;
541 }
542  
543 0 my $xml =
544 $self->callCAS( $self->getServerProxyValidateURL( $service, $ticket ) );
545  
546 0 ▶ if ( defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:authenticationFailure"}) 0 0 0
547 {
548 $errors = sprintf "Failed to validate Proxy Ticket %s : %s\n", $ticket,
549 0 $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}[0];
550 0 return undef;
551 }
552  
553 my $user =
554 $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]
555 0 {'cas:user'}[0];
556  
557 0 my @proxies;
558 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:authenticationSuccess"}[0]{"cas:proxies"}) 0 0 0
559 defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]
560 {'cas:proxies'} )
561 {
562 @proxies =
563 0 @{ $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]
564 0 {'cas:proxies'}[0]{'cas:proxy'} };
565 }
566  
567 0 return ( $user, @proxies );
568 }
569  
570 =pod
571  
572 =head2 callCAS($url)
573  
574 ## Access a CAS URL and parses received XML
575  
576 Returns
577  
578 =cut
579  
580 3 ▶sub callCAS {
Subroutine callCAS: called CC=4 SCAR=17.9
Pod documented
581 3 my $self = shift;
582 3 my $url = shift;
583  
584 3 my ( $host, $port, $path ) = &_parse_url($url);
585  
586 my $xml = get_https2(
587 $host, $port, $path,
588 {
589 'cafile' => $self->{'CAFile'},
590 'capath' => $self->{'CAPath'},
591 3 'SSL_version' => $self->{'SSL_version'}
592 }
593 );
594  
595 #use Data::Dumper; die '--'.$#$xml.': '.Dumper($xml);
596  
597 3 ▶ unless ($xml && $#$xml >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml and $xml->$#* >= 0) 3 0 3
Condition33%1/3
$xml and $xml->$#* >= 0
!ll&&!rl&&r
300
MC/DC0%0/2
$xml and $xml->$#* >= 0
$xml $xml->$#* >= 0
Truth table33%1/3
A and B
A= $xmlB= $xml->$#* >= 0
ABresult
0X0
100
111
598 3 warn $errors;
599 3 return undef;
600 }
601  
602 ## Skip HTTP header fields
603 0 my $line = shift @$xml;
604 0 ▶ while ( $line !~ /^\s*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^\s*$/) 0 0 0
605 0 $line = shift @$xml;
606 }
607  
608 0 return &_parse_xml( join( '', @$xml ) );
609 }
610  
611 =pod
612  
613 =head2 storePGT($pgtIou, $pgtId)
614  
615 =cut
616  
617 0 ▶sub storePGT {
Subroutine storePGT: not called CC=2 SCAR=17.9
Pod documented
618 0 my $self = shift;
619 0 my $pgtIou = shift;
620 0 my $pgtId = shift;
621  
622 0 ▶ unless ( open STORE, ">>$self->{'pgtFile'}" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open STORE, qq[>>$self->{"pgtFile"}]) 0 0 0
623 0 $errors = sprintf "Unable to write to %s\n", $self->{'pgtFile'};
624 0 return undef;
625 }
626 0 printf STORE "%s\t%s\n", $pgtIou, $pgtId;
627 0 close STORE;
628  
629 0 return 1;
630 }
631  
632 =pod
633  
634 =head2 retrievePT($service)
635  
636 Returns
637  
638 =cut
639  
640 0 ▶sub retrievePT {
Subroutine retrievePT: not called CC=3 SCAR=24.8
Pod documented
641 0 my $self = shift;
642 0 my $service = shift;
643  
644 my $xml =
645 0 $self->callCAS( $self->getServerProxyURL( $service, $self->{'pgtId'} ) );
646  
647 0 ▶ if ( defined $xml->{'cas:serviceResponse'}[0]{'cas:proxyFailure'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:proxyFailure"}) 0 0 0
648 $errors = sprintf "Failed to get PT : %s\n",
649 0 $xml->{'cas:serviceResponse'}[0]{'cas:proxyFailure'}[0];
650 0 return undef;
651 }
652  
653 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $xml->{"cas:serviceResponse"}[0]{"cas:proxySuccess"}[0]{"cas:proxyTicket"}) 0 0 0
654 defined $xml->{'cas:serviceResponse'}[0]{'cas:proxySuccess'}[0]
655 {'cas:proxyTicket'} )
656 {
657 return $xml->{'cas:serviceResponse'}[0]{'cas:proxySuccess'}[0]
658 0 {'cas:proxyTicket'}[0];
659 }
660  
661 0 return undef;
662 }
663  
664 =pod
665  
666 =head2 get_https2
667  
668 request a document using https, return status and content
669  
670 Sven suspects this is intended to be private.
671  
672 Returns
673  
674 =cut
675  
676 3 ▶sub get_https2 {
Subroutine get_https2: called CC=16 SCAR=35.2
Pod documented
677 3 my $host = shift;
678 3 my $port = shift;
679 3 my $path = shift;
680  
681 3 my $ssl_data = shift;
682  
683 3 my $trusted_ca_file = $ssl_data->{'cafile'};
684 3 my $trusted_ca_path = $ssl_data->{'capath'};
685  
686 3 ▶ if ( ( $trusted_ca_file && !( -r $trusted_ca_file ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($trusted_ca_file and not -r $trusted_ca_file or $trusted_ca_path and not -d $trusted_ca_path) 1 2 3
Condition55%5/9
$trusted_ca_file and not -r $trusted_ca_file or $trusted_ca_path and not -d $trusted_ca_path
l!l&&r!l&&!r
102
$trusted_ca_file and not -r $trusted_ca_file
!ll&&!rl&&r
201
$trusted_ca_path and not -d $trusted_ca_path
!ll&&!rl&&r
200
MC/DC25%1/4
$trusted_ca_file and not -r $trusted_ca_file or $trusted_ca_path and not -d $trusted_ca_path
$trusted_ca_file not -r $trusted_ca_file $trusted_ca_path not -d $trusted_ca_path
Truth table28%2/7
A and not(B) or C and not(D)
A= $trusted_ca_fileB= not -r $trusted_ca_fileC= $trusted_ca_pathD= not -d $trusted_ca_path
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
687 || ( $trusted_ca_path && !( -d $trusted_ca_path ) ) )
688 {
689 1 ▶ $errors = sprintf
Condition50%2/4
$trusted_ca_file || '<empty>'
l!l
10
$trusted_ca_path || '<empty>'
l!l
01
MC/DC0%0/1
$trusted_ca_file || '<empty>'
$trusted_ca_file
MC/DC0%0/1
$trusted_ca_path || '<empty>'
$trusted_ca_path
Truth table50%1/2
A
A= $trusted_ca_file
Aresult
11
00
Truth table50%1/2
A
A= $trusted_ca_path
Aresult
11
00
690 "error : incorrect access to cafile ".($trusted_ca_file||'<empty>')." or capath ".($trusted_ca_path||'<empty>')."\n";
691 1 return undef;
692 }
693  
694 2 ▶ unless ( eval "require IO::Socket::SSL" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require IO::Socket::SSL") 0 2 2
695 0 $errors = sprintf
696 "Unable to use SSL library, IO::Socket::SSL required, install IO-Socket-SSL (CPAN) first\n";
697 0 return undef;
698 }
699 2 require IO::Socket::SSL;
700  
701 2 ▶ unless ( eval "require LWP::UserAgent" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "require LWP::UserAgent") 0 2 2
702 0 $errors = sprintf
703 "Unable to use LWP library, LWP::UserAgent required, install LWP (CPAN) first\n";
704 0 return undef;
705 }
706 2 require LWP::UserAgent;
707  
708 2 my $ssl_socket;
709  
710 2 my %ssl_options = (
711 SSL_use_cert => 0,
712 PeerAddr => $host,
713 PeerPort => $port,
714 Proto => 'tcp',
715 Timeout => '5'
716 );
717  
718 2 ▶ $ssl_options{'SSL_ca_file'} = $trusted_ca_file if ($trusted_ca_file);
Branch: 1 branch
BranchTrueFalseTotal
if $trusted_ca_file 0 2 2
719 2 ▶ $ssl_options{'SSL_ca_path'} = $trusted_ca_path if ($trusted_ca_path);
Branch: 1 branch
BranchTrueFalseTotal
if $trusted_ca_path 0 2 2
720  
721 ## If SSL_ca_file or SSL_ca_path => verify peer certificate
722 2 ▶ $ssl_options{'SSL_verify_mode'} = 0x01
Branch: 1 branch
BranchTrueFalseTotal
if $trusted_ca_file or $trusted_ca_path 0 2 2
Condition33%1/3
$trusted_ca_file or $trusted_ca_path
l!l&&r!l&&!r
002
MC/DC0%0/2
$trusted_ca_file or $trusted_ca_path
$trusted_ca_file $trusted_ca_path
Truth table33%1/3
A or B
A= $trusted_ca_fileB= $trusted_ca_path
ABresult
1X1
011
000
723 if ( $trusted_ca_file || $trusted_ca_path );
724  
725 $ssl_options{'SSL_version'} = $ssl_data->{'SSL_version'}
726 2 ▶ if defined( $ssl_data->{'SSL_version'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ssl_data->{"SSL_version"} 2 0 2
727  
728 2 $ssl_socket = new IO::Socket::SSL(%ssl_options);
729  
730 2 ▶ unless ($ssl_socket) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ssl_socket) 2 0 2
731 2 $errors = sprintf "error %s unable to connect https://%s:%s/\n",
732 &IO::Socket::SSL::errstr, $host, $port;
733 2 return undef;
734 }
735  
736 0 my $request = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
737 0 print $ssl_socket "$request";
738  
739 0 my @result;
740 0 ▶ while ( my $line = $ssl_socket->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $ssl_socket->getline) 0 0 0
741 0 push @result, $line;
742 }
743  
744 0 $ssl_socket->close( SSL_no_shutdown => 1 );
745  
746 0 return \@result;
747 }
748  
749 =pod
750  
751 =head1 SEE ALSO
752  
753 JA-SIG Central Authentication Service L<http://www.jasig.org/cas>
754  
755 was Yale Central Authentication Service L<http://www.yale.edu/tp/auth/>
756
757 phpCAS L<http://esup-phpcas.sourceforge.net/>
758  
759 =head1 COPYRIGHT
760  
761 Copyright (C) 2003, 2005,2006,2007,2009 Olivier Salaun - Comité Réseau des Universités L<http://www.cru.fr>
762 2012 Sven Dowideit - L<mailto:SvenDowideit@fosiki.com>
763  
764  
765 This library is free software; you can redistribute it and/or modify
766 it under the same terms as Perl itself.
767  
768 =head1 AUTHORS
769  
770 Olivier Salaun
771 Sven Dowideit
772  
773 =cut
774  
775 3 1;
776  
↑ Summary