CAS.pm 5.63 KB
Newer Older
Yadd's avatar
Yadd committed
1
package Lemonldap::NG::Portal::Auth::CAS;
Yadd's avatar
Yadd committed
2 3 4 5 6 7 8 9 10 11 12 13 14

use strict;
use Mouse;
use URI::Escape;
use Lemonldap::NG::Portal::Main::Constants qw(
  PE_ERROR
  PE_OK
  PE_REDIRECT
  PE_SENDRESPONSE
);

our $VERSION = '2.0.0';

Yadd's avatar
Yadd committed
15
extends 'Lemonldap::NG::Portal::Auth::Base', 'Lemonldap::NG::Portal::Lib::CAS';
Yadd's avatar
Yadd committed
16 17 18 19 20 21 22 23

# PROPERTIES

# Act as a proxy if proxied services configured
has proxy => (
    is      => 'rw',
    builder => sub {
        return
Yadd's avatar
Yadd committed
24 25
          ref( $_[0]->conf->{CAS_proxiedServices} ) eq 'HASH'
          ? ( %{ $_[0]->conf->{CAS_proxiedServices} } ? 1 : 0 )
Yadd's avatar
Yadd committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
          : 0;
    }
);

has cas => ( is => 'rw' );

# INITIALIZATION

sub init {
    my ($self) = @_;
    eval { require AuthCAS };
    if ($@) {
        $self->error("Unable to load AuthCAS: $@");
        return 0;
    }
    $self->cas(
        AuthCAS->new(
Yadd's avatar
Yadd committed
43 44
            casUrl => $self->conf->{CAS_url},
            CAFile => $self->conf->{CAS_CAFile},
Yadd's avatar
Yadd committed
45 46
        )
    );
Yadd's avatar
Yadd committed
47
    return 1;
Yadd's avatar
Yadd committed
48 49 50 51 52 53 54 55
}

# RUNNING METHODS

sub extractFormInfo {
    my ( $self, $req ) = @_;

    # Local URL
Yadd's avatar
Yadd committed
56
    my $local_url = $self->p->fullUrl($req);
Yadd's avatar
Yadd committed
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

    # Add request state parameters
    if ( $req->datas->{_url} ) {
        my $url_param = 'url=' . uri_escape( $req->datas->{_url} );
        $local_url .= ( $local_url =~ /\?/ ? '&' : '?' ) . $url_param;
    }
    if ( my $tmp = $req->param( $self->conf->{authChoiceParam} ) ) {
        my $url_param = $self->conf->{authChoiceParam} . '=' . uri_escape($tmp);
        $local_url .= ( $local_url =~ /\?/ ? '&' : '?' ) . $url_param;
    }

    # Forward hidden fields
    if ( $req->{portalHiddenFormValues}
        and %{ $req->{portalHiddenFormValues} } )
    {

        $self->lmLog( "Add hidden values to CAS redirect URL\n", 'debug' );

        foreach ( keys %{ $req->{portalHiddenFormValues} } ) {
            $local_url .=
                ( $local_url =~ /\?/ ? '&' : '?' )
              . $_ . '='
              . uri_escape( $req->{portalHiddenFormValues}->{$_} );
        }
    }

    if ( $self->proxy ) {
        $self->lmLog( "CAS: Proxy mode activated", 'debug' );
Yadd's avatar
Yadd committed
85
        my $proxy_url = $self->p->fullUrl($req) . '?casProxy=1';
Yadd's avatar
Yadd committed
86 87 88 89 90 91 92 93

        if ( my $tmp = $req->param( $self->conf->{authChoiceParam} ) ) {
            $proxy_url .= '&' . $self->conf->{authChoiceParam} . "=$tmp";
        }

        $self->lmLog( "CAS Proxy URL: $proxy_url", 'debug' );

        $self->cas->proxyMode(
Yadd's avatar
Yadd committed
94
            pgtFile        => $self->conf->{CAS_pgtFile},
Yadd's avatar
Yadd committed
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
            pgtCallbackUrl => $proxy_url
        );
    }

    # Catch proxy callback
    if ( $req->param('casProxy') ) {
        $self->lmLog( "CAS: Proxy callback detected", 'debug' );

        my $pgtIou = $req->param('pgtIou');
        my $pgtId  = $req->param('pgtId');

        if ( $pgtIou and $pgtId ) {

            # Store pgtId and pgtIou
            unless ( $self->cas->storePGT( $pgtIou, $pgtId ) ) {
                $self->lmLog( "CAS: error " . &AuthCAS::get_errors(), 'error' );
            }
            else {
                $self->lmLog( "CAS: Store pgtIou $pgtIou and pgtId $pgtId",
                    'debug' );
            }
        }

        # Exit
        $req->response( [ 200, [ 'Content-Length' => 0 ], [] ] );
        return PE_SENDRESPONSE;
    }

    # Build login URL
    my $login_url = $self->cas->getServerLoginURL($local_url);
Yadd's avatar
Yadd committed
125 126
    $login_url .= '&renew=true'   if $self->conf->{CAS_renew};
    $login_url .= '&gateway=true' if $self->conf->{CAS_gateway};
Yadd's avatar
Yadd committed
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

    # Check Service Ticket
    my $ticket = $req->param('ticket');

    # Unless a ticket has been found, we redirect the user
    unless ($ticket) {
        $self->lmLog( "CAS: Redirect user to $login_url", 'debug' );
        $req->{urldc} = $login_url;
        $req->steps( [] );
        return PE_REDIRECT;
    }

    $self->lmLog( "CAS: Service Ticket received: $ticket", 'debug' );

    # Ticket found, try to validate it
    unless ( $req->{user} = $self->cas->validateST( $local_url, $ticket ) ) {
        $self->lmLog( "CAS: error " . &AuthCAS::get_errors(), 'error' );
        return PE_ERROR;
    }
    else {
        $self->lmLog( "CAS: User $req->{user} found", 'debug' );
    }

    # Request proxy tickets for proxied services
    if ( $self->proxy ) {

        # Check we received a PGT
        my $pgtId = $self->cas->{pgtId};

        unless ($pgtId) {
            $self->lmLog( "CAS: Proxy mode activated, but no PGT received",
                'error' );
            return PE_ERROR;
        }

        # Get a proxy ticket for each proxied service
Yadd's avatar
Yadd committed
163 164
        foreach ( keys %{ $self->conf->{CAS_proxiedServices} } ) {
            my $service = $self->conf->{CAS_proxiedServices}->{$_};
Yadd's avatar
Yadd committed
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
            my $pt      = $self->cas->retrievePT($service);

            unless ($pt) {
                $self->lmLog(
                    "CAS: No proxy ticket recevied for service $service",
                    'error' );
                return PE_ERROR;
            }

            $self->lmLog( "CAS: Received proxy ticket $pt for service $service",
                'debug' );

            # Store it in session
            $req->{sessionInfo}->{ '_casPT' . $_ } = $pt;
        }

    }

    return PE_OK;
}

sub authenticate {
    PE_OK;
}

# Set authenticationLevel.
sub setAuthSessionInfo {
    my ( $self, $req ) = @_;
Yadd's avatar
Yadd committed
193
    $req->{sessionInfo}->{authenticationLevel} = $self->conf->{CAS_authnLevel};
Yadd's avatar
Yadd committed
194 195 196 197 198 199 200
    PE_OK;
}

sub authLogout {
    my ( $self, $req ) = @_;

    # Build CAS logout URL
Yadd's avatar
Yadd committed
201 202
    my $logout_url =
      $self->cas->getServerLogoutURL( uri_escape( $self->p->fullUrl($req) ) );
Yadd's avatar
Yadd committed
203 204 205 206 207 208 209 210 211 212 213 214

    $self->lmLog( "Build CAS logout URL: $logout_url", 'debug' );

    # Register CAS logout URL in logoutServices
    $req->datas->{logoutServices}->{CASserver} = $logout_url;

    PE_OK;
}

sub getDisplayType {
    return "logo";
}
Yadd's avatar
Yadd committed
215 216

1;