CDA.pm 2.96 KB
Newer Older
Yadd's avatar
Yadd committed
1 2 3 4
package Lemonldap::NG::Handler::Lib::CDA;

use strict;

Yadd's avatar
Yadd committed
5 6
our $VERSION = '2.0.0';

Yadd's avatar
Yadd committed
7 8
sub run {
    my ( $class, $req, $rule, $protection ) = @_;
9
    my $uri = $req->{env}->{REQUEST_URI};
Yadd's avatar
Yadd committed
10 11
    my $cn  = $class->tsv->{cookieName};
    my ( $id, $session );
Yadd's avatar
Tidy  
Yadd committed
12
    if ( $uri =~ s/[\?&;]${cn}cda=(\w+)$//oi ) {
13 14
        if (    $id = $class->fetchId($req)
            and $session = $class->retrieveSession( $req, $id ) )
Yadd's avatar
Tidy  
Yadd committed
15
        {
Yadd's avatar
Yadd committed
16 17 18 19 20 21 22
            $class->logger->info(
                'CDA asked for an already available session, skipping');
        }
        else {
            my $cdaid = $1;
            $class->logger->debug("CDA request with id $cdaid");

23
            my $cdaInfos = $class->getCDAInfos( $req, $cdaid );
Yadd's avatar
Yadd committed
24 25 26 27 28
            unless ( $cdaInfos->{cookie_value} and $cdaInfos->{cookie_name} ) {
                $class->logger->error("CDA request for id $cdaid is not valid");
                return $class->FORBIDDEN;
            }

29
            my $redirectUrl = $class->_buildUrl( $req, $uri );
Yadd's avatar
Yadd committed
30 31
            my $redirectHttps = ( $redirectUrl =~ m/^https/ );
            $class->set_header_out(
32
                $req,
Yadd's avatar
Yadd committed
33 34 35
                'Location'   => $redirectUrl,
                'Set-Cookie' => $cdaInfos->{cookie_name} . "=" . 'c:'
                  . $class->tsv->{cipher}->encrypt(
36
                    $cdaInfos->{cookie_value} . ' ' . $class->resolveAlias($req)
Yadd's avatar
Yadd committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
                  )
                  . "; path=/"
                  . ( $redirectHttps          ? "; secure"   : "" )
                  . ( $class->tsv->{httpOnly} ? "; HttpOnly" : "" )
                  . (
                    $class->tsv->{cookieExpiration}
                    ? "; expires="
                      . expires( $class->tsv->{cookieExpiration}, 'cookie' )
                    : ""
                  )
            );
            return $class->REDIRECT;
        }
    }
    return $class->SUPER::init( $req, $rule, $protection );
}

## @rmethod protected hash getCDAInfos(id)
# Tries to retrieve the CDA session, get infos and delete session
# @return CDA session infos
sub getCDAInfos {
58
    my ( $class, $req, $id ) = @_;
Yadd's avatar
Yadd committed
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
    my $infos = {};

    # Get the session
    my $cdaSession = Lemonldap::NG::Common::Session->new(
        {
            storageModule        => $class->tsv->{sessionStorageModule},
            storageModuleOptions => $class->tsv->{sessionStorageOptions},
            cacheModule          => $class->tsv->{sessionCacheModule},
            cacheModuleOptions   => $class->tsv->{sessionCacheOptions},
            id                   => $id,
            kind                 => "CDA",
        }
    );

    unless ( $cdaSession->error ) {
        $class->logger->debug("Get CDA session $id");

        $infos->{cookie_value} = $cdaSession->data->{cookie_value};
        $infos->{cookie_name}  = $cdaSession->data->{cookie_name};

        $cdaSession->remove;
    }
    else {
        $class->logger->info("CDA Session $id can't be retrieved");
        $class->logger->info( $cdaSession->error );
    }

    return $infos;
}

1;