Client.pm

filter: Statementstmt 49.8% 126 / 253 Branchbran 26.1% 22 / 84 Conditioncond 65.9% 29 / 44 MC/DCmcdc 42.3% 11 / 26 Subroutinesub 71.8% 23 / 32 Podpod 92.3% 12 / 13 totaltotal 49.3% 223 / 452 CC 59 SCAR 30.5
CC
59
Cov
46%
authenticate
72.0
_parse_v2_validate_xml_response
72.0
_parse_v2_proxy_xml_response
42.0
CRAP 21.0
↑ Summary Client/Response/AuthenSuccess.pm »
1 package LibCAS::Client;
2  
3 3 require 5.008_008;
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 ▶use LibCAS::Client::Response::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use LibCAS::Client::Response::Failure;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use LibCAS::Client::Response::AuthenSuccess;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use LibCAS::Client::Response::ProxySuccess;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 LibCAS::Client - A perl module for authenticating and validating against Jasig's CAS server
21  
22 =head1 VERSION
23  
24 Version 0.01
25  
26 =cut
27  
28 3 our $VERSION = '0.01';
29  
30 =head1 SYNOPSIS
31  
32 LibCAS::Client provides an OO interface for generating URLs and validating tickets for
33 Jasig's Central Authentication Service (CAS).
34  
35 Using the module should hopefully be straight forward, something similar to:
36  
37 my $cas = LibCAS::Client->new(cas_url => 'https://my-cas-server/cas');
38 my $login_url = $cas->login_url(service => 'my_service_name');
39
40 # Do a HTTP redirect to $login_url to have CAS prompt for credentials
41 # or to have the CAS server issue a service ticket.
42
43 my $r = $cas->service_validate(service => 'my_service_name', ticket => 'ticket_from_login');
44
45 if ($r->is_success()) {
46 # Do things for successful authentication
47 } elsif ($r->is_failure()) {
48 # Do things for failed authentication
49 } else {
50 # Anything that makes it here is an error
51 }
52  
53 =cut
54  
55 3 my $cas_url = "https://localhost/cas";
56 3 my $cas_login_path = "/login";
57 3 my $cas_logout_path = "/logout";
58 3 my $cas_validate_path = "/validate"; # CAS 1.0
59 3 my $cas_proxy_path = "/proxy"; # CAS 2.0
60 3 my $cas_serviceValidate_path = "/serviceValidate"; # CAS 2.0
61 3 my $cas_proxyValidate_path = "/proxyValidate"; # CAS 2.0
62  
63 =head1 METHODS
64  
65 =head2 new
66  
67 Create a new instance of the LibCAS::Client object. Valid parameters are:
68  
69 =over
70  
71 =item I<cas_url> - The base URL to the CAS server, defaults to C<< https://localhost/cas >>
72  
73 =item I<cas_login_path> - The path to the CAS login service, defaults to C<< /login >>
74  
75 =item I<cas_logout_path> - The path to the CAS logout service, defaults to C<< /logout >>
76  
77 =item I<cas_validate_path> - The path to the CAS v1.0 validation service, defaults to C<< /validate >>
78  
79 =item I<cas_proxy_path> - The path to the CAS proxy service, defaults to C<< /proxy >>
80  
81 =item I<cas_serviceValidate_path> - The path to the CAS v2.0 service validation service, defaults to C<< /serviceValidate >>
82  
83 =item I<cas_proxyValidate_path> - The path to the CAS v2.0 proxy validation service, defaults to C<< /proxyValidate >>
84  
85 =back
86  
87 =cut
88  
89 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
90 1 my $this = shift;
91 1 my %args = @_;
92  
93 1 my $self = {
94 cas_url => $cas_url,
95 cas_login_path => $cas_login_path,
96 cas_logout_path => $cas_logout_path,
97 cas_validate_path => $cas_validate_path,
98 cas_proxy_path => $cas_proxy_path,
99 cas_serviceValidate_path => $cas_serviceValidate_path,
100 cas_proxyValidate_path => $cas_proxyValidate_path,
101 debug => 0
102 };
103
104 1 map { $self->{$_} = $args{$_} } keys %args;
105  
106 1 my $ssl_opts = {
107 verify_hostname => 0,
108 SSL_ca_path => undef,
109 SSL_ca_file => undef,
110 SSL_use_cert => 0,
111 SSL_verify_mode => 0
112 };
113  
114 1 $self->{_ua} = LWP::UserAgent->new(
115 agent => "Authen-CAS/$VERSION",
116 ssl_opts => $ssl_opts,
117 cookie_jar => HTTP::Cookies->new()
118 );
119
120 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
121
122 1 bless($self,$class);
123 1 return $self;
124 }
125  
126 =head2 login_url
127  
128 Generate the login url needed for the CAS server, depending on the C<< cas_url >> and C<< cas_login_path >>
129 parameters passed during object construction.
130  
131 Valid parameters to the C<< login_url >> method are:
132  
133 =over
134  
135 =item I<service> [optional] - The name of the service to authenticate for.
136  
137 =item I<renew> [optional] - Bypass any existing single sign-on session, and require the client to represent their credentials.
138  
139 =item I<gateway> [optional] - Do not require the client to present credentials if a single sign-on has not been established.
140  
141 =back
142  
143 =cut
144  
145 15 ▶sub login_url {
Subroutine login_url: called CC=6 SCAR=18.0
Pod documented
146 15 my $self = shift;
147 15 my %args = @_;
148
149 15 my %query_string = ();
150
151 15 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_login_path});
152
153 15 ▶ if ($args{'service'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"service"}) 2 13 15
154 2 $query_string{'service'} = $args{'service'};
155 }
156
157 15 ▶ if ($args{'renew'} && _is_true($args{'renew'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"renew"} and _is_true($args{"renew"})) 5 10 15
Condition66%2/3
$args{"renew"} and _is_true($args{"renew"})
!ll&&!rl&&r
1005
MC/DC50%1/2
$args{"renew"} and _is_true($args{"renew"})
$args{"renew"} _is_true($args{"renew"})
Truth table66%2/3
A and B
A= $args{"renew"}B= _is_true($args{"renew"})
ABresult
0X0
100
111
158 5 $query_string{'renew'} = 'true';
159 }
160
161 15 ▶ if ($args{'gateway'} && _is_true($args{'gateway'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"gateway"} and _is_true($args{"gateway"})) 5 10 15
Condition66%2/3
$args{"gateway"} and _is_true($args{"gateway"})
!ll&&!rl&&r
1005
MC/DC50%1/2
$args{"gateway"} and _is_true($args{"gateway"})
$args{"gateway"} _is_true($args{"gateway"})
Truth table66%2/3
A and B
A= $args{"gateway"}B= _is_true($args{"gateway"})
ABresult
0X0
100
111
162 5 $query_string{'gateway'} = 'true';
163 }
164
165 15 return _build_url($cas_uri, \%query_string);
166 }
167  
168 =head2 logout_url
169  
170 Generate the logout url needed for the CAS server, depending on the C<< cas_url >> and C<< cas_logout_path >>
171 parameters passed during object construction.
172  
173 B<NOTE:> Calling this method will destroy the single sign-on session, which may affect the client's ability
174 to access other applications protected by this CAS server.
175  
176 Valid parameters to the C<< logout_url >> method are:
177  
178 =over
179  
180 =item I<url> [optional] - A URL to be displayed on the logout page.
181  
182 =back
183  
184 =cut
185  
186 2 ▶sub logout_url {
Subroutine logout_url: called CC=2 SCAR=6.9
Pod documented
187 2 my $self = shift;
188 2 my %args = @_;
189
190 2 my %query_string = ();
191
192 2 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_logout_path});
193
194 2 ▶ if ($args{'url'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"}) 1 1 2
195 1 $query_string{'url'} = $args{'url'};
196 }
197
198 2 return _build_url($cas_uri, \%query_string);
199 }
200  
201 =head2 validate_url
202  
203 Generate the URL that performs CAS protocol version 1.0 service ticket validation.
204  
205 Valid parameters to the C<< validate_url >> method are:
206  
207 =over
208  
209 =item I<service> [required] - The name of the service which the ticket was issued for.
210  
211 =item I<ticket> [required] - The service ticket issued by the CAS server.
212  
213 =item I<renew> [optional] - If set, this option will only allow validation to pass if the ticket was
214 issued immediatly after the client presents their credentials. It will fail if the service ticket
215 that is presented was issued from a single sign-on session.
216  
217 =back
218  
219 =cut
220  
221 2 ▶sub validate_url {
Subroutine validate_url: called CC=2 SCAR=6.9
Pod documented
222 2 my $self = shift;
223
224 2 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_validate_path});
225  
226 2 ▶ my $query_string = _parse_validate_args(@_) || return;
Condition100%2/2
_parse_validate_args(@_) || (return)
l!l
11
MC/DC100%1/1
_parse_validate_args(@_) || (return)
_parse_validate_args(@_)
Truth table100%2/2
A
A= _parse_validate_args(@_)
Aresult
11
00
227
228 1 return _build_url($cas_uri, $query_string);
229 }
230  
231 =head2 service_validate_url
232  
233 Generate the URL that performs CAS protocol version 2.0 service ticket validation, and generate proxy-
234 granting tickets, if requested.
235  
236 Valid parameters to the C<< service_validate_url >> method are:
237  
238 =over
239  
240 =item I<service> [required] - The name of the service which the ticket was issued for.
241  
242 =item I<ticket> [required] - The service ticket issued by the CAS server.
243  
244 =item I<renew> [optional] - If set, this option will only allow validation to pass if the ticket was
245 issued immediatly after the client presents their credentials. It will fail if the service ticket
246 that is presented was issued from a single sign-on session.
247  
248 =item I<pgtUrl> [optional] - The URL of the proxy callback.
249  
250 =back
251  
252 =cut
253  
254 2 ▶sub service_validate_url {
Subroutine service_validate_url: called CC=2 SCAR=6.9
Pod documented
255 2 my $self = shift;
256
257 2 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_serviceValidate_path});
258
259 2 ▶ my $query_string = _parse_validate20_args(@_) || return;
Condition100%2/2
_parse_validate20_args(@_) || (return)
l!l
11
MC/DC100%1/1
_parse_validate20_args(@_) || (return)
_parse_validate20_args(@_)
Truth table100%2/2
A
A= _parse_validate20_args(@_)
Aresult
11
00
260
261 1 return _build_url($cas_uri, $query_string);
262 }
263  
264 =head2 proxy_url
265  
266 Generate the URL to the CAS server for generating proxy tickets.
267  
268 Valid parameters to the C<< proxy_url >> method are:
269  
270 =over
271  
272 =item I<pgt> [required] - The proxy granting ticket.
273  
274 =item I<targetService> [required] - The service identifier for the back-end service.
275  
276 =back
277  
278 =cut
279  
280 2 ▶sub proxy_url {
Subroutine proxy_url: called CC=3 SCAR=11.0
Pod documented
281 2 my $self = shift;
282 2 my %args = @_;
283
284 2 my %query_string = ();
285
286 2 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_proxy_path});
287
288 2 ▶ if (! $args{'pgt'} || ! $args{'targetService'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'pgt'} && $args{'targetService'}) { } 1 1 2
Condition66%2/3
$args{'pgt'} && $args{'targetService'}
!ll&&!rl&&r
101
MC/DC50%1/2
$args{'pgt'} && $args{'targetService'}
$args{'pgt'} $args{'targetService'}
Truth table66%2/3
A && B
A= $args{'pgt'}B= $args{'targetService'}
ABresult
0X0
100
111
289 1 $@ = "pgt and targetService parameters must be supplied";
290 1 return;
291 } else {
292 1 $query_string{'pgt'} = $args{'pgt'};
293 1 $query_string{'targetService'} = $args{'targetService'};
294 }
295
296 1 return _build_url($cas_uri, \%query_string);
297 }
298  
299 =head2 proxy_validate_url
300  
301 This method performs the same functions as the C<< service_validate_url >> method, with the added
302 benefit of being able to validate proxy tickets as well.
303  
304 Valid parameters for C<< proxy_validate_url >> are the same as they are for C<< service_validate_url >>
305  
306 =cut
307  
308 2 ▶sub proxy_validate_url {
Subroutine proxy_validate_url: called CC=2 SCAR=6.9
Pod documented
309 2 my $self = shift;
310
311 2 my $cas_uri = URI->new($self->{cas_url}.$self->{cas_proxyValidate_path});
312
313 2 ▶ my $query_string = _parse_validate20_args(@_) || return;
Condition100%2/2
_parse_validate20_args(@_) || (return)
l!l
11
MC/DC100%1/1
_parse_validate20_args(@_) || (return)
_parse_validate20_args(@_)
Truth table100%2/2
A
A= _parse_validate20_args(@_)
Aresult
11
00
314
315 1 return _build_url($cas_uri, $query_string);
316 }
317  
318 0 ▶sub authenticate {
Subroutine authenticate: not called CC=8 SCAR=42.8
Pod documented
319 0 my $self = shift;
320 0 my %args = @_;
321
322 0 my $r;
323
324 0 ▶ if (! $args{username} || ! $args{password}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'username'} && $args{'password'}) { } 0 0 0
Condition0%0/3
$args{'username'} && $args{'password'}
!ll&&!rl&&r
000
MC/DC0%0/2
$args{'username'} && $args{'password'}
$args{'username'} $args{'password'}
Truth table0%0/3
A && B
A= $args{'username'}B= $args{'password'}
ABresult
0X0
100
111
325 0 $r = LibCAS::Client::Response::Error->new(error => "username and password parameters must be supplied");
326 } else {
327 0 my $query_string = $self->_get_hidden_form_params();
328
329 0 $query_string->{username} = $args{username};
330 0 $query_string->{password} = $args{password};
331  
332 0 ▶ if ($args{service}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"service"}) 0 0 0
333 0 $query_string->{service} = $args{service};
334 }
335
336 0 ▶ if ($args{'warn'} && _is_true($args{'warn'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"warn"} and _is_true($args{"warn"})) 0 0 0
Condition0%0/3
$args{"warn"} and _is_true($args{"warn"})
!ll&&!rl&&r
000
MC/DC0%0/2
$args{"warn"} and _is_true($args{"warn"})
$args{"warn"} _is_true($args{"warn"})
Truth table0%0/3
A and B
A= $args{"warn"}B= _is_true($args{"warn"})
ABresult
0X0
100
111
337 0 $query_string->{'warn'} = $args{'warn'};
338 }
339
340 0 ▶ if (! $query_string->{'lt'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $query_string->{'lt'}) { } 0 0 0
341 0 $r = LibCAS::Client::Response::Error->new(error => $@);
342 } else {
343 0 my $response = $self->{_ua}->post($self->login_url(), Content => $query_string);
344
345 0 ▶ if ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
346 0 $r = LibCAS::Client::Response::AuthenSuccess->new(user => $args{username});
347 } else {
348 0 $r = LibCAS::Client::Response::Error->new(error=>_create_http_error_message($response));
349 }
350 }
351 }
352
353 0 return $r;
354 }
355  
356 =head2 validate
357  
358 Validate a service ticket using CAS protocol version 1.0. Supported arguments for this method are the
359 same as they are for the C<< validate_url >> method.
360  
361 Returns an LibCAS::Client::Response object to denote whether or not the validation was successful. Success,
362 failure, or error conditions can be checked by calling the C<< is_success() >>, C<< is_failure() >>, or
363 C<< is_error() >> methods on the returned object.
364  
365 =cut
366  
367 0 ▶sub validate {
Subroutine validate: not called CC=5 SCAR=34.0
Pod documented
368 0 my $self = shift;
369 0 my $r;
370
371 0 my $url = $self->validate_url(@_);
372
373 0 ▶ if (! $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
374 0 $r = LibCAS::Client::Response::Error->new(error => "URL generation failed: ".$@);
375 }
376  
377 0 my $response = $self->do_http_request($url);
378
379 0 ▶ if (! $response) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 0 0
380 0 $r = LibCAS::Client::Response::Error->new(error => $@);
381 }
382
383 0 ▶ if ($response =~ /^no\n\n$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response =~ /^no\n\n$/) { } 0 0 0
elsif ($response =~ /^yes\n([^\n]+)\n$/) { } 0 0 0
384 0 $r = LibCAS::Client::Response::Failure->new(code => 'V1_VALIDATE_FAILURE', response => $response);
385 } elsif ($response =~ /^yes\n([^\n]+)\n$/){
386 0 $r = LibCAS::Client::Response::AuthenSuccess->(user => $1, response => $response);
387 } else {
388 0 $r = LibCAS::Client::Response::Error->new(error => "Invalid response from CAS", response => $response);
389 }
390
391 0 return $r;
392 }
393  
394 =head2 service_validate
395  
396 Validate a service ticket using CAS protocol version 2.0. Supported arguments for this method are the
397 same as they are for the C<< service_validate_url >> method.
398  
399 Returns an LibCAS::Client::Response object to denote whether or not the validation was successful. Success,
400 failure, or error conditions can be checked by calling the C<< is_success() >>, C<< is_failure() >>, or
401 C<< is_error() >> methods on the returned object.
402  
403 =cut
404  
405 0 ▶sub service_validate {
Subroutine service_validate: not called CC=2 SCAR=17.9
Pod documented
406 0 my $self = shift;
407 0 my $r;
408
409 0 my $url = $self->service_validate_url(@_);
410
411 0 ▶ if (! $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url) { } 0 0 0
412 0 $r = LibCAS::Client::Response::Error->new(error => "URL generation failed: ".$@);
413 } else {
414 0 $r = $self->_do_v2_validation_request($url);
415 }
416
417 0 return $r;
418 }
419  
420 =head2 proxy
421  
422 Obtain a proxy ticket to services that have a proxy granting ticket, and will be using proxy
423 authentication to a back-end service. Supported arguments for this method are the
424 same as they are for the C<< service_validate_url >> method.
425  
426 Returns an LibCAS::Client::Response object to denote whether or not the validation was successful. Success,
427 failure, or error conditions can be checked by calling the C<< is_success() >>, C<< is_failure() >>, or
428 C<< is_error() >> methods on the returned object.
429  
430 =cut
431  
432 0 ▶sub proxy {
Subroutine proxy: not called CC=3 SCAR=24.8
Pod documented
433 0 my $self = shift;
434 0 my $r;
435
436 0 my $url = $self->proxy_url(@_);
437
438 0 ▶ if (! $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url) { } 0 0 0
439 0 $r = LibCAS::Client::Response::Error->new(error => "URL generation failed: ".$@);
440 } else {
441 0 my $response = $self->do_http_request($url);
442
443 0 ▶ if (! $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response) { } 0 0 0
444 0 $r = LibCAS::Client::Response::Error->new(error => $@);
445 } else {
446 0 $r = _parse_v2_proxy_xml_response($response);
447 }
448 }
449
450 0 return $r;
451 }
452  
453 =head2 proxy_validate
454  
455 Validate a service ticket, or a proxy ticket, using CAS protocol version 2.0. Supported arguments for this method are the
456 same as they are for the C<< proxy_validate_url >> method.
457  
458 Returns an LibCAS::Client::Response object to denote whether or not the validation was successful. Success,
459 failure, or error conditions can be checked by calling the C<< is_success() >>, C<< is_failure() >>, or
460 C<< is_error() >> methods on the returned object.
461  
462 =cut
463  
464 0 ▶sub proxy_validate {
Subroutine proxy_validate: not called CC=2 SCAR=17.9
Pod documented
465 0 my $self = shift;
466 0 my $r;
467
468 0 my $url = $self->proxy_validate_url(@_);
469
470 0 ▶ if (! $url) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $url) { } 0 0 0
471 0 $r = LibCAS::Client::Response::Error->new(error => "URL generation failed: ".$@);
472 } else {
473 0 $r = $self->_do_v2_validation_request($url);
474 }
475
476 0 return $r;
477 }
478  
479 1 ▶sub do_http_request {
Subroutine do_http_request: called CC=2 SCAR=7.1
Pod undocumented
480 1 my $self = shift;
481 1 my $url = shift;
482  
483 1 my $response = $self->{_ua}->get($url);
484  
485 1 ▶ if (! $response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response->is_success) { } 1 0 1
486 1 $@ = _create_http_error_message($response);
487 1 return;
488 } else {
489 0 return $response->content;
490 }
491 }
492  
493 0 ▶sub _get_hidden_form_params {
Subroutine _get_hidden_form_params: not called CC=4 SCAR=30.0
494 # There are a number of hidden form fields that are needed to successfully log in
495 # programatically.
496 0 my $self = shift;
497  
498 0 my $response = $self->{_ua}->get($self->login_url());
499 0 my $parser = XML::LibXML->new();
500
501 0 my %params;
502
503 0 eval {
504 0 ▶ if ($response->is_success()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_success) { } 0 0 0
505 0 my $doc = $parser->parse_html_string($response->content(), {recover => 1});
506 0 my @nodes = $doc->findnodes('//input[@type="hidden"]');
507
508 0 %params = map { $_->getAttribute('name') => $_->getAttribute('value') } @nodes;
509
510 0 ▶ if (! $params{'lt'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($params{"lt"}) 0 0 0
511 0 die "Could not find login ticket";
512 }
513 } else {
514 0 die _create_http_error_message($response);
515 }
516 };
517
518 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 0 0
519 0 return;
520 } else {
521 0 return \%params;
522 }
523 }
524  
525 0 ▶sub _parse_v2_proxy_xml_response {
Subroutine _parse_v2_proxy_xml_response: not called CC=6 SCAR=37.4
526 0 my $xml = shift;
527 0 my ($r, $doc, $node);
528  
529 0 my $parser = XML::LibXML->new();
530  
531 0 eval {
532 0 my $doc = $parser->parse_string($xml);
533
534 0 ▶ if ($node = $doc->find('/cas:serviceResponse/cas:proxySuccess')->get_node(1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node = $doc->find('/cas:serviceResponse/cas:proxySuccess')->get_node(1)) { } 0 0 0
elsif ($node = $doc->find('/cas:serviceResponse/cas:proxyFailure')->get_node(1)) { } 0 0 0
535 0 my $tkt = $node->find('./cas:proxyTicket')->get_node(1)->textContent;
536
537 0 ▶ if ($tkt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tkt) { } 0 0 0
538 0 $r = LibCAS::Client::Response::ProxySuccess->new(proxy_ticket => $tkt, response => $doc);
539 } else {
540 0 die "Invalid CAS Response, could not find proxyTicket information";
541 }
542 } elsif ($node = $doc->find('/cas:serviceResponse/cas:proxyFailure')->get_node(1)) {
543 0 ▶ if ($node->hasAttribute('code')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttribute('code')) { } 0 0 0
544 0 my $code = $node->getAttribute('code');
545 0 my $msg = $node->textContent;
546
547 0 $msg =~ s/^\s+//;
548 0 $msg =~ s/\s+$//;
549
550 0 $r = LibCAS::Client::Response::Failure->new(code => $code, message => $msg, response => $doc);
551 } else {
552 0 die "Invalid CAS Response, could not find proxy failure code attribute";
553 }
554 } else {
555 0 die "Invalid CAS Response"
556 }
557 };
558
559 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
560 0 $r = LibCAS::Client::Response::Error->new(error => $@, response => $doc);
561 }
562
563 0 return $r;
564 }
565  
566 0 ▶sub _parse_v2_validate_xml_response {
Subroutine _parse_v2_validate_xml_response: not called CC=8 SCAR=42.8
567 0 my $xml = shift;
568 0 my ($r, $doc, $node);
569  
570 0 my $parser = XML::LibXML->new();
571  
572 0 eval {
573 0 my $doc = $parser->parse_string($xml);
574
575 0 ▶ if ($node = $doc->find('/cas:serviceResponse/cas:authenticationSuccess')->get_node(1)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node = $doc->find('/cas:serviceResponse/cas:authenticationSuccess')->get_node(1)) { } 0 0 0
elsif ($node = $doc->find('/cas:serviceResponse/cas:authenticationFailure')->get_node(1)) { } 0 0 0
576 0 my $user = $node->find('./cas:user')->get_node(1)->textContent;
577
578 0 ▶ if ($user) {
Branch: 1 branch
BranchTrueFalseTotal
if ($user) { } 0 0 0
579 0 my $pgt = $node->find('./cas:proxyGrantingTicket')->get_node(1);
580 0 ▶ $pgt = $pgt->textContent if $pgt;
Branch: 1 branch
BranchTrueFalseTotal
if $pgt 0 0 0
581
582 0 my $proxies = $node->findnodes('./cas:proxies/cas:proxy');
583 0 ▶ $proxies = [ map $_->textContent, @$proxies ] if $proxies;
Branch: 1 branch
BranchTrueFalseTotal
if $proxies 0 0 0
584
585 0 $r = LibCAS::Client::Response::AuthenSuccess->new(
586 user => $user,
587 pgt => $pgt,
588 proxies => $proxies,
589 response => $doc
590 );
591 } else {
592 0 die "Invalid CAS Response, could not find user information";
593 }
594 } elsif ($node = $doc->find('/cas:serviceResponse/cas:authenticationFailure')->get_node(1)) {
595 0 ▶ if ($node->hasAttribute('code')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->hasAttribute('code')) { } 0 0 0
596 0 my $code = $node->getAttribute('code');
597 0 my $msg = $node->textContent;
598
599 0 $msg =~ s/^\s+//;
600 0 $msg =~ s/\s+$//;
601
602 0 $r = LibCAS::Client::Response::Failure->new(code => $code, message => $msg, response => $doc);
603 } else {
604 0 die "Invalid CAS Response, could not find validation failure code attribute";
605 }
606 } else {
607 0 die "Invalid CAS Response"
608 }
609 };
610
611 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
612 0 $r = LibCAS::Client::Response::Error(error => $@, response => $doc);
613 }
614
615 0 return $r;
616 }
617  
618 0 ▶sub _do_v2_validation_request {
Subroutine _do_v2_validation_request: not called CC=2 SCAR=17.9
619 0 my $self = shift;
620 0 my $url = shift;
621 0 my $r;
622
623 0 my $response = $self->do_http_request($url);
624
625 0 ▶ if (! $response) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response) { } 0 0 0
626 0 $r = LibCAS::Client::Response::Error->new(error => $@);
627 } else {
628 0 $r = _parse_v2_validate_xml_response($response);
629 }
630
631 0 return $r;
632 }
633  
634 21 ▶sub _build_url {
Subroutine _build_url: called CC=2 SCAR=7.0
635 21 my $uri = shift;
636 21 my $query_string = shift;
637
638 21 ▶ if ($query_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query_string) 21 0 21
639 21 $uri->query_form($query_string);
640 }
641
642 21 return $uri->canonical;
643 }
644  
645 4 ▶sub _parse_validate20_args {
Subroutine _parse_validate20_args: called CC=3 SCAR=11.0
646 4 my %args = @_;
647  
648 4 ▶ my $query_string = _parse_validate_args(%args) || return;
Condition100%2/2
_parse_validate_args(%args) || (return)
l!l
22
MC/DC100%1/1
_parse_validate_args(%args) || (return)
_parse_validate_args(%args)
Truth table100%2/2
A
A= _parse_validate_args(%args)
Aresult
11
00
649
650 2 ▶ if ($args{'pgtUrl'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"pgtUrl"}) 1 1 2
651 1 $query_string->{'pgtUrl'} = $args{'pgtUrl'};
652 }
653
654 2 return $query_string;
655 }
656  
657 6 ▶sub _parse_validate_args {
Subroutine _parse_validate_args: called CC=5 SCAR=16.1
658 6 my %args = @_;
659 6 my %query_string = ();
660  
661 6 ▶ if (! $args{'service'} || ! $args{'ticket'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $args{'service'} && $args{'ticket'}) { } 3 3 6
Condition100%3/3
$args{'service'} && $args{'ticket'}
!ll&&!rl&&r
123
MC/DC100%2/2
$args{'service'} && $args{'ticket'}
$args{'service'} $args{'ticket'}
Truth table100%3/3
A && B
A= $args{'service'}B= $args{'ticket'}
ABresult
0X0
100
111
662 3 $@ = "service and ticket parameters must be specified";
663 3 return;
664 } else {
665 3 $query_string{'service'} = $args{'service'};
666 3 $query_string{'ticket'} = $args{'ticket'};
667 }
668
669 3 ▶ if ($args{'renew'} && _is_true($args{'renew'})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"renew"} and _is_true($args{"renew"})) 1 2 3
Condition66%2/3
$args{"renew"} and _is_true($args{"renew"})
!ll&&!rl&&r
201
MC/DC50%1/2
$args{"renew"} and _is_true($args{"renew"})
$args{"renew"} _is_true($args{"renew"})
Truth table66%2/3
A and B
A= $args{"renew"}B= _is_true($args{"renew"})
ABresult
0X0
100
111
670 1 $query_string{'renew'} = 'true';
671 }
672
673 3 return \%query_string;
674 }
675  
676 11 ▶sub _is_true {
Subroutine _is_true: called CC=8 SCAR=21.3
677 11 my $arg = shift;
678 11 my $is_true = 0;
679
680 11 ▶ if (defined $arg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg) 11 0 11
681 11 ▶ if ($arg =~ /^\d+$/ && $arg > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^\d+$/ and $arg > 0) { } 4 7 11
Condition66%2/3
$arg =~ /^\d+$/ and $arg > 0
!ll&&!rl&&r
704
MC/DC50%1/2
$arg =~ /^\d+$/ and $arg > 0
$arg =~ /^\d+$/ $arg > 0
Truth table66%2/3
A and B
A= $arg =~ /^\d+$/B= $arg > 0
ABresult
0X0
100
111
682 4 $is_true = 1;
683 } else {
684 7 ▶ if ($arg =~ /^true$/i || $arg =~ /^t$/i ||
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^true$/i or $arg =~ /^t$/i or $arg =~ /^yes$/i or $arg =~ /^y$/i) 7 0 7
Condition77%7/9
$arg =~ /^true$/i or $arg =~ /^t$/i or $arg =~ /^yes$/i or $arg =~ /^y$/i
l!l&&r!l&&!r
430
$arg =~ /^true$/i or $arg =~ /^t$/i or $arg =~ /^yes$/i
l!l&&r!l&&!r
223
$arg =~ /^true$/i or $arg =~ /^t$/i
l!l&&r!l&&!r
205
MC/DC0%0/4
$arg =~ /^true$/i or $arg =~ /^t$/i or $arg =~ /^yes$/i or $arg =~ /^y$/i
$arg =~ /^true$/i $arg =~ /^t$/i $arg =~ /^yes$/i $arg =~ /^y$/i
Truth table60%3/5
A or B or C or D
A= $arg =~ /^true$/iB= $arg =~ /^t$/iC= $arg =~ /^yes$/iD= $arg =~ /^y$/i
ABCDresult
1XXX1
01XX1
001X1
00011
00000
685 $arg =~ /^yes$/i || $arg =~ /^y$/i) {
686 7 $is_true = 1;
687 }
688 }
689 }
690
691 11 return $is_true;
692 }
693  
694 1 ▶sub _create_http_error_message {
Subroutine _create_http_error_message: called CC=1 SCAR=0.0
695 1 my $response = shift;
696
697 1 return "HTTP request failed: ".$response->code.": ".$response->message." -> ".$response->content;
698 }
699  
700 =head1 AUTHOR
701  
702 "Mike Morris", C<< <"michael.m.morris at gmail.com"> >>
703  
704 =head1 BUGS
705  
706 =head1 SUPPORT
707  
708 You can find documentation for this module with the perldoc command.
709  
710 perldoc LibCAS::Client
711  
712 You can also look for information at:
713  
714 =over 4
715  
716 =item * RT: CPAN's request tracker (report bugs here)
717  
718 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=LibCAS>
719  
720 =item * AnnoCPAN: Annotated CPAN documentation
721  
722 L<http://annocpan.org/dist/LibCAS>
723  
724 =item * CPAN Ratings
725  
726 L<http://cpanratings.perl.org/d/LibCAS>
727  
728 =item * Search CPAN
729  
730 L<http://search.cpan.org/dist/LibCAS/>
731  
732 =back
733  
734 =head1 ACKNOWLEDGEMENTS
735  
736 This code is derived from L<Authen::CAS::Client|http://search.cpan.org/~pravus/Authen-CAS-Client-0.05/>
737 and L<AuthCAS|http://search.cpan.org/~osalaun/AuthCAS/>, with the added ability to customize the paths for
738 the services on the CAS server, and use URI and XML parsing libs.
739  
740 Documentation for the CAS protocol can be found at L<http://www.jasig.org/cas/protocol>
741  
742 =head1 LICENSE AND COPYRIGHT
743  
744 Copyright 2012 "Michael Morris".
745  
746 This program is free software; you can redistribute it and/or modify it
747 under the terms of either: the GNU General Public License as published
748 by the Free Software Foundation; or the Artistic License.
749  
750 See http://dev.perl.org/licenses/ for more information.
751  
752 =cut
753  
754 3 1;
↑ Summary Client/Response/AuthenSuccess.pm »