/usr/share/perl5/AuthCAS.pm is in libauthcas-perl 1.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 | package AuthCAS;
use strict;
use vars qw( $VERSION);
$VERSION = '1.5';
=head1 NAME
AuthCAS - Client library for CAS 2.0 authentication server
=head1 VERSION
Version 1.5
=head1 DESCRIPTION
AuthCAS aims at providing a Perl API to Yale's Central Authentication System (CAS). Only a basic Perl library is provided with CAS whereas AuthCAS is a full object-oriented library.
=head1 PREREQUISITES
This script requires IO::Socket::SSL and LWP::UserAgent
=pod OSNAMES
any
=pod SCRIPT CATEGORIES
Network
=head1 SYNOPSIS
A simple example with a direct CAS authentication
use AuthCAS;
my $cas = new AuthCAS(casUrl => 'https://cas.myserver,
CAFile => '/etc/httpd/conf/ssl.crt/ca-bundle.crt',
);
my $login_url = $cas->getServerLoginURL('http://myserver/app.cgi');
## The user should be redirected to the $login_url
## When coming back from the CAS server a ticket is provided in the QUERY_STRING
## $ST should contain the receaved Service Ticket
my $user = $cas->validateST('http://myserver/app.cgi', $ST);
printf "User authenticated as %s\n", $user;
In the following example a proxy is requesting a Proxy Ticket for the target application
$cas->proxyMode(pgtFile => '/tmp/pgt.txt',
pgtCallbackUrl => 'https://myserver/proxy.cgi?callback=1
);
## Same as before but the URL is the proxy URL
my $login_url = $cas->getServerLoginURL('http://myserver/proxy.cgi');
## Like in the previous example we should receave a $ST
my $user = $cas->validateST('http://myserver/proxy.cgi', $ST);
## Process errors
printf STDERR "Error: %s\n", &AuthCAS::get_errors() unless (defined $user);
## Now we request a Proxy Ticket for the target application
my $PT = $cas->retrievePT('http://myserver/app.cgi');
## This piece of code is executed by the target application
## It received a Proxy Ticket from the proxy
my ($user, @proxies) = $cas->validatePT('http://myserver/app.cgi', $PT);
printf "User authenticated as %s via %s proxies\n", $user, join(',',@proxies);
=head1 DESCRIPTION
CAS is Yale University's web authentication system, heavily inspired by Kerberos.
Release 2.0 of CAS provides "proxied credential" feature that allows authentication
tickets to be carried by intermediate applications (Portals for instance), they are
called proxy.
This AuthCAS Perl module provides required subroutines to validate and retrieve CAS tickets.
=head1 SEE ALSO
Yale Central Authentication Service (http://www.yale.edu/tp/auth/)
phpCAS (http://esup-phpcas.sourceforge.net/)
=head1 COPYRIGHT
Copyright (C) 2003 Comite Reseau des Universites (http://www.cru.fr). All rights reserved.
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=head1 AUTHORS
Olivier Salaun
=cut
my @ISA = qw(Exporter);
my @EXPORT = qw($errors);
my $errors;
use Carp;
sub new {
my($pkg, %param) = @_;
my $cas_server = {};
$cas_server->{'url'} = $param{'casUrl'};
$cas_server->{'CAFile'} = $param{'CAFile'};
$cas_server->{'CAPath'} = $param{'CAPath'};
$cas_server->{'loginPath'} = $param{'loginPath'} || '/login';
$cas_server->{'logoutPath'} = $param{'logoutPath'} || '/logout';
$cas_server->{'serviceValidatePath'} = $param{'serviceValidatePath'} || '/serviceValidate';
$cas_server->{'proxyPath'} = $param{'proxyPath'} || '/proxy';
$cas_server->{'proxyValidatePath'} = $param{'proxyValidatePath'} || '/proxyValidate';
bless $cas_server, $pkg;
return $cas_server;
}
## Return module errors
sub get_errors {
return $errors;
}
## Use the CAS object as a proxy
sub proxyMode {
my $self = shift;
my %param = @_;
$self->{'pgtFile'} = $param{'pgtFile'};
$self->{'pgtCallbackUrl'} = $param{'pgtCallbackUrl'};
$self->{'proxy'} = 1;
return 1;
}
## Escape dangerous chars in URLS
sub _escape_chars {
my $s = shift;
## Escape chars
## !"#$%&'()+,:;<=>?[] AND accented chars
## escape % first
# foreach my $i (0x25,0x20..0x24,0x26..0x2c,0x3a..0x3f,0x5b,0x5d,0x80..0x9f,0xa0..0xff) {
foreach my $i (0x26) {
my $hex_i = sprintf "%lx", $i;
$s =~ s/\x$hex_i/%$hex_i/g;
}
return $s;
}
sub dump_var {
my ($var, $level, $fd) = @_;
if (ref($var)) {
if (ref($var) eq 'ARRAY') {
foreach my $index (0..$#{$var}) {
print $fd "\t"x$level.$index."\n";
&dump_var($var->[$index], $level+1, $fd);
}
}elsif (ref($var) eq 'HASH') {
foreach my $key (sort keys %{$var}) {
print $fd "\t"x$level.'_'.$key.'_'."\n";
&dump_var($var->{$key}, $level+1, $fd);
}
}
}else {
if (defined $var) {
print $fd "\t"x$level."'$var'"."\n";
}else {
print $fd "\t"x$level."UNDEF\n";
}
}
}
## Parse an HTTP URL
sub _parse_url {
my $url = shift;
my ($host, $port, $path);
if ($url =~ /^(https?):\/\/([^:\/]+)(:(\d+))?(.*)$/) {
$host = $2;
$path = $5;
if ($1 eq 'http') {
$port = $4 || 80;
}elsif ($1 eq 'https') {
$port = $4 || 443;
}else {
$errors = sprintf "Unknown protocol '%s'\n", $1;
return undef;
}
}else {
$errors = sprintf "Unable to parse URL '%s'\n", $url;
return undef;
}
return ($host, $port, $path);
}
## Simple XML parser
sub _parse_xml {
my $data = shift;
my %xml_struct;
while ($data =~ /^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(<\/\1>)/m) {
my ($new_tag, $new_data) = ($1,$3);
chomp $new_data;
$new_data =~ s/^[\s\n]+//m;
$data =~ s/^<$new_tag(\s+[^\s>]+)*>([\s\S\n]*)(<\/$new_tag>)//m;
$data =~ s/^[\s\n]+//m;
## Check if data still includes XML tags
my $struct;
if ($new_data =~/^<([^\s>]+)(\s+[^\s>]+)*>([\s\S\n]*)(<\/\1>)/m) {
$struct = &_parse_xml($new_data);
}else {
$struct = $new_data;
}
push @{$xml_struct{$new_tag}}, $struct;
}
return \%xml_struct;
}
sub getServerLoginURL {
my $self = shift;
my $service = shift;
return $self->{'url'}.$self->{'loginPath'}.'?service='.&_escape_chars($service);
}
## Returns non-blocking login URL
## ie: if user is logged in, return the ticket, otherwise do not prompt for login
sub getServerLoginGatewayURL {
my $self = shift;
my $service = shift;
return $self->{'url'}.$self->{'loginPath'}.'?service='.&_escape_chars($service).'&gateway=1';
}
## Return logout URL
## After logout user is redirected back to the application
sub getServerLogoutURL {
my $self = shift;
my $service = shift;
return $self->{'url'}.$self->{'logoutPath'}.'?service='.&_escape_chars($service).'&gateway=1';
}
sub getServerServiceValidateURL {
my $self = shift;
my $service = shift;
my $ticket = shift;
my $pgtUrl = shift;
my $query_string = 'service='.&_escape_chars($service).'&ticket='.$ticket;
if (defined $pgtUrl) {
$query_string .= '&pgtUrl='.&_escape_chars($pgtUrl);
}
## URL was /validate with CAS 1.0
return $self->{'url'}.$self->{'serviceValidatePath'}.'?'.$query_string;
}
sub getServerProxyURL {
my $self = shift;
my $targetService = shift;
my $pgt = shift;
return $self->{'url'}.$self->{'proxyPath'}.'?targetService='.&_escape_chars($targetService).'&pgt='.&_escape_chars($pgt);
}
sub getServerProxyValidateURL {
my $self = shift;
my $service = shift;
my $ticket = shift;
return $self->{'url'}.$self->{'proxyValidatePath'}.'?service='.&_escape_chars($service).'&ticket='.&_escape_chars($ticket);
}
## Validate a Service Ticket
## Also used to get a PGT
sub validateST {
my $self = shift;
my $service = shift;
my $ticket = shift;
my $pgtUrl = $self->{'pgtCallbackUrl'};
my $xml = $self->callCAS($self->getServerServiceValidateURL($service, $ticket, $pgtUrl));
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}) {
$errors = sprintf "Failed to validate Service Ticket %s : %s\n", $ticket, $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}[0];
return undef;
}
my $user = $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:user'}[0];
## If in Proxy mode, also retreave a PGT
if ($self->{'proxy'}) {
my $pgtIou;
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:proxyGrantingTicket'}) {
$pgtIou = $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:proxyGrantingTicket'}[0];
}
unless (defined $self->{'pgtFile'}) {
$errors = sprintf "pgtFile not defined\n";
return undef;
}
## Check stored PGT
unless (open STORE, $self->{'pgtFile'}) {
$errors = sprintf "Unable to read %s\n", $self->{'pgtFile'};
return undef;
}
my $pgtId;
while (<STORE>) {
if (/^$pgtIou\s+(.+)$/) {
$pgtId = $1;
last;
}
}
$self->{'pgtId'} = $pgtId;
}
return ($user);
}
## Validate a Proxy Ticket
sub validatePT {
my $self = shift;
my $service = shift;
my $ticket = shift;
my $xml = $self->callCAS($self->getServerProxyValidateURL($service, $ticket));
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}) {
$errors = sprintf "Failed to validate Proxy Ticket %s : %s\n", $ticket, $xml->{'cas:serviceResponse'}[0]{'cas:authenticationFailure'}[0];
return undef;
}
my $user = $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:user'}[0];
my @proxies;
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:proxies'}) {
@proxies = @{$xml->{'cas:serviceResponse'}[0]{'cas:authenticationSuccess'}[0]{'cas:proxies'}[0]{'cas:proxy'}};
}
return ($user, @proxies);
}
## Access a CAS URL and parses received XML
sub callCAS {
my $self = shift;
my $url = shift;
my ($host, $port, $path) = &_parse_url($url);
my @xml = &get_https2($host, $port, $path,{'cafile' => $self->{'CAFile'}, 'capath' => $self->{'CAPath'}});
unless (@xml) {
warn $errors;
return undef;
}
## Skip HTTP header fields
my $line = shift @xml;
while ($line !~ /^\s*$/){
$line = shift @xml;
}
return &_parse_xml(join('', @xml));
}
sub storePGT {
my $self = shift;
my $pgtIou = shift;
my $pgtId = shift;
unless (open STORE, ">>$self->{'pgtFile'}") {
$errors = sprintf "Unable to write to %s\n", $self->{'pgtFile'};
return undef;
}
printf STORE "%s\t%s\n", $pgtIou, $pgtId;
close STORE;
return 1;
}
sub retrievePT {
my $self = shift;
my $service = shift;
my $xml = $self->callCAS($self->getServerProxyURL($service, $self->{'pgtId'}));
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:proxyFailure'}) {
$errors = sprintf "Failed to get PT : %s\n", $xml->{'cas:serviceResponse'}[0]{'cas:proxyFailure'}[0];
return undef;
}
if (defined $xml->{'cas:serviceResponse'}[0]{'cas:proxySuccess'}[0]{'cas:proxyTicket'}) {
return $xml->{'cas:serviceResponse'}[0]{'cas:proxySuccess'}[0]{'cas:proxyTicket'}[0];
}
return undef;
}
# request a document using https, return status and content
sub get_https2{
my $host = shift;
my $port = shift;
my $path = shift;
my $ssl_data= shift;
my $trusted_ca_file = $ssl_data->{'cafile'};
my $trusted_ca_path = $ssl_data->{'capath'};
if (($trusted_ca_file && !(-r $trusted_ca_file)) ||
($trusted_ca_path && !(-d $trusted_ca_path))) {
$errors = sprintf "error : incorrect access to cafile $trusted_ca_file or capath $trusted_ca_path\n";
return undef;
}
unless (eval "require IO::Socket::SSL") {
$errors = sprintf "Unable to use SSL library, IO::Socket::SSL required, install IO-Socket-SSL (CPAN) first\n";
return undef;
}
require IO::Socket::SSL;
unless (eval "require LWP::UserAgent") {
$errors = sprintf "Unable to use LWP library, LWP::UserAgent required, install LWP (CPAN) first\n";
return undef;
}
require LWP::UserAgent;
my $ssl_socket;
my %ssl_options = (SSL_use_cert => 0,
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp',
Timeout => '5'
);
$ssl_options{'SSL_ca_file'} = $trusted_ca_file if ($trusted_ca_file);
$ssl_options{'SSL_ca_path'} = $trusted_ca_path if ($trusted_ca_path);
## If SSL_ca_file or SSL_ca_path => verify peer certificate
$ssl_options{'SSL_verify_mode'} = 0x01 if ($trusted_ca_file || $trusted_ca_path);
$ssl_socket = new IO::Socket::SSL(%ssl_options);
unless ($ssl_socket) {
$errors = sprintf "error %s unable to connect https://%s:%s/\n",&IO::Socket::SSL::errstr,$host,$port;
return undef;
}
my $request = "GET $path HTTP/1.0\r\nHost: $host\r\n\r\n";
print $ssl_socket "$request";
my @result;
while (my $line = $ssl_socket->getline) {
push @result, $line;
}
$ssl_socket->close(SSL_no_shutdown => 1);
return (@result);
}
1;
|