AuthCAS

 view release on metacpan or  search on metacpan

lib/AuthCAS.pm  view on Meta::CPAN


package AuthCAS;

use strict;
use vars qw( $VERSION);

$VERSION = '1.7';

=encoding utf8

=head1 NAME

AuthCAS - Client library for JA-SIG CAS 2.0 authentication server

=head1 VERSION

Version 1.7

=head1 DESCRIPTION

AuthCAS aims at providing a Perl API to JA-SIG 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

=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

Jasig 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.

=cut

my @ISA    = qw(Exporter);
my @EXPORT = qw($errors);

my $errors;

use Carp;

=pod

=head2 new

  my $cas = new AuthCAS(
		    casUrl => 'https://cas.myserver', 
		    CAFile => '/etc/httpd/conf/ssl.crt/ca-bundle.crt',
		    );

The C<new> constructor lets you create a new B<AuthCAS> object.

=over

=item casUrl - REQUIRED

=item CAFile

=item CAPath

=item loginPath - '/login'

=item logoutPath - '/logout'

=item serviceValidatePath - '/serviceValidate'

=item proxyPath - '/proxy'

=item proxyValidatePath - '/proxyValidate'

=item SSL_version - unset

Sets the version of the SSL protocol used to transmit data. If the default causes connection issues, setting it to 'SSLv3' may help.
see the documentation for L<IO::Socket::SSL/"METHODS"> for more information
see L<http://www.perlmonks.org/?node_id=746493> for more details.

=back

Returns a new B<AuthCAS> or dies on error.

=cut

sub new {
    my ( $pkg, %param ) = @_;
    my $cas_server = {
        url    => $param{'casUrl'},
        CAFile => $param{'CAFile'},
        CAPath => $param{'CAPath'},

        loginPath  => $param{'loginPath'}  || '/login',
        logoutPath => $param{'logoutPath'} || '/logout',
        serviceValidatePath => $param{'serviceValidatePath'}
          || '/serviceValidate',
        proxyPath         => $param{'proxyPath'}         || '/proxy',
        proxyValidatePath => $param{'proxyValidatePath'} || '/proxyValidate',
        SSL_version       => $param{SSL_version},
    };

    bless $cas_server, $pkg;

    return $cas_server;
}

=pod

=head2 get_errors

Return module errors

=cut

sub get_errors {
    return $errors;
}

=pod

=head2 proxyMode

Use the CAS object as a proxy


=over

=item pgtFile
=item pgtCallbackUrl

=back


=cut

sub proxyMode {
    my $self  = shift;

lib/AuthCAS.pm  view on Meta::CPAN

=head2 retrievePT($service)

Returns 

=cut

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;
}

=pod

=head2 get_https2

request a document using https, return status and content

Sven suspects this is intended to be private.

Returns 

=cut

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||'<empty>')." or capath ".($trusted_ca_path||'<empty>')."\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_options{'SSL_version'} = $ssl_data->{'SSL_version'}
      if defined( $ssl_data->{'SSL_version'} );

    $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;
}

=pod

=head1 SEE ALSO

JA-SIG Central Authentication Service L<http://www.jasig.org/cas>

was Yale Central Authentication Service L<http://www.yale.edu/tp/auth/>
 
phpCAS L<http://esup-phpcas.sourceforge.net/>

=head1 COPYRIGHT

Copyright (C) 2003, 2005,2006,2007,2009 Olivier Salaun - Comité Réseau des Universités L<http://www.cru.fr>
              2012 Sven Dowideit - L<mailto:SvenDowideit@fosiki.com>


This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHORS

  Olivier Salaun
  Sven Dowideit

=cut

1;



( run in 0.874 second using v1.01-cache-2.11-cpan-39bf76dae61 )