2019-06-21 14:49:38 +02:00
|
|
|
package Lemonldap::NG::Portal::Plugins::ContextSwitching;
|
2019-06-19 18:13:17 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2019-07-03 06:47:33 +02:00
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_OK
|
|
|
|
PE_ERROR
|
|
|
|
PE_REDIRECT
|
|
|
|
PE_MALFORMEDUSER
|
2019-07-04 22:50:46 +02:00
|
|
|
PE_BADCREDENTIALS
|
2019-07-03 23:08:40 +02:00
|
|
|
PE_SESSIONEXPIRED
|
2019-07-04 22:50:46 +02:00
|
|
|
PE_IMPERSONATION_SERVICE_NOT_ALLOWED
|
2019-07-03 06:47:33 +02:00
|
|
|
);
|
2019-06-19 18:13:17 +02:00
|
|
|
|
|
|
|
our $VERSION = '2.0.6';
|
|
|
|
|
|
|
|
extends
|
|
|
|
qw(Lemonldap::NG::Portal::Main::Plugin Lemonldap::NG::Portal::Lib::_tokenRule);
|
|
|
|
|
|
|
|
# INITIALIZATION
|
|
|
|
|
2019-06-25 19:28:05 +02:00
|
|
|
has ott => (
|
2019-06-19 18:13:17 +02:00
|
|
|
is => 'rw',
|
|
|
|
lazy => 1,
|
|
|
|
default => sub {
|
|
|
|
my $ott =
|
|
|
|
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
|
|
|
|
$ott->timeout( $_[0]->{conf}->{formTimeout} );
|
|
|
|
return $ott;
|
|
|
|
}
|
|
|
|
);
|
2019-06-24 23:52:39 +02:00
|
|
|
has rule => ( is => 'rw', default => sub { 1 } );
|
|
|
|
has idRule => ( is => 'rw', default => sub { 1 } );
|
2019-06-19 18:13:17 +02:00
|
|
|
|
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
|
|
|
my $hd = $self->p->HANDLER;
|
2019-07-31 23:38:48 +02:00
|
|
|
$self->addAuthRoute( switchcontext => 'run', ['POST'] )
|
|
|
|
->addAuthRoute( switchcontext => 'display', ['GET'] );
|
2019-06-19 18:13:17 +02:00
|
|
|
|
|
|
|
# Parse activation rule
|
|
|
|
$self->logger->debug(
|
2019-06-21 14:49:38 +02:00
|
|
|
'ContextSwitching rule -> ' . $self->conf->{contextSwitchingRule} );
|
2019-06-19 18:13:17 +02:00
|
|
|
my $rule =
|
2019-06-21 14:49:38 +02:00
|
|
|
$hd->buildSub( $hd->substitute( $self->conf->{contextSwitchingRule} ) );
|
2019-06-19 18:13:17 +02:00
|
|
|
unless ($rule) {
|
|
|
|
$self->error(
|
2019-06-21 14:49:38 +02:00
|
|
|
'Bad contextSwitching rule -> ' . $hd->tsv->{jail}->error );
|
2019-06-19 18:13:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$self->rule($rule);
|
|
|
|
|
|
|
|
# Parse identity rule
|
2019-06-21 14:49:38 +02:00
|
|
|
$self->logger->debug( "ContextSwitching identities rule -> "
|
|
|
|
. $self->conf->{contextSwitchingIdRule} );
|
2019-06-19 18:13:17 +02:00
|
|
|
$rule =
|
2019-06-21 14:49:38 +02:00
|
|
|
$hd->buildSub( $hd->substitute( $self->conf->{contextSwitchingIdRule} ) );
|
2019-06-19 18:13:17 +02:00
|
|
|
unless ($rule) {
|
2019-06-21 16:24:50 +02:00
|
|
|
$self->error( "Bad contextSwitching identities rule -> "
|
|
|
|
. $hd->tsv->{jail}->error );
|
2019-06-19 18:13:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
$self->idRule($rule);
|
2019-06-23 22:07:05 +02:00
|
|
|
|
2019-06-19 18:13:17 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# RUNNING METHOD
|
|
|
|
|
|
|
|
sub display {
|
|
|
|
my ( $self, $req ) = @_;
|
2019-07-03 06:47:33 +02:00
|
|
|
my $realSessionId =
|
|
|
|
$req->userData->{"$self->{conf}->{impersonationPrefix}_session_id"};
|
|
|
|
my $realSession;
|
|
|
|
unless ( $realSession = $self->p->getApacheSession($realSessionId) ) {
|
2019-07-04 07:24:50 +02:00
|
|
|
$self->userLogger->info(
|
2019-07-06 23:27:45 +02:00
|
|
|
"ContextSwitching: session $realSessionId expired");
|
2019-07-03 23:12:15 +02:00
|
|
|
return $self->p->do( $req, [ sub { PE_SESSIONEXPIRED } ] );
|
2019-07-03 06:47:33 +02:00
|
|
|
}
|
2019-06-19 18:13:17 +02:00
|
|
|
|
2019-06-22 23:46:02 +02:00
|
|
|
# Check access rules
|
|
|
|
unless ( $self->rule->( $req, $req->userData )
|
|
|
|
|| $req->userData->{"$self->{conf}->{impersonationPrefix}_session_id"} )
|
|
|
|
{
|
2019-07-04 22:50:46 +02:00
|
|
|
$self->userLogger->warn('ContextSwitching service NOT authorized');
|
2019-06-22 23:46:02 +02:00
|
|
|
return $self->p->do( $req,
|
|
|
|
[ sub { PE_IMPERSONATION_SERVICE_NOT_ALLOWED } ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $req->userData->{"$self->{conf}->{impersonationPrefix}_session_id"} ) {
|
2019-06-27 21:54:14 +02:00
|
|
|
$self->logger->debug('Request to stop ContextSwitching');
|
2019-06-23 22:07:05 +02:00
|
|
|
if ( $self->conf->{contextSwitchingStopWithLogout} ) {
|
2019-07-04 07:24:50 +02:00
|
|
|
$self->userLogger->notice("Stop ContextSwitching for $req->{user}");
|
|
|
|
$self->userLogger->info("Remove real session $realSession");
|
2019-07-03 23:08:40 +02:00
|
|
|
$realSession->remove;
|
2019-06-23 22:07:05 +02:00
|
|
|
return $self->p->do( $req,
|
|
|
|
[ @{ $self->p->beforeLogout }, 'authLogout', 'deleteSession' ]
|
|
|
|
);
|
2019-07-03 23:08:40 +02:00
|
|
|
|
2019-06-23 22:07:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-07-03 06:47:33 +02:00
|
|
|
$req = $self->_abortImpersonation( $req, $req->{user},
|
2019-07-03 23:08:40 +02:00
|
|
|
$realSession->data->{ $self->conf->{whatToTrace} }, 0 );
|
2019-06-24 23:52:39 +02:00
|
|
|
$self->p->updateSession( $req, $req->userData );
|
2019-06-23 22:07:05 +02:00
|
|
|
return $self->p->do( $req, [ sub { PE_REDIRECT } ] );
|
|
|
|
}
|
2019-06-22 23:46:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:13:17 +02:00
|
|
|
# Display form
|
|
|
|
my $params = {
|
|
|
|
PORTAL => $self->conf->{portal},
|
|
|
|
MAIN_LOGO => $self->conf->{portalMainLogo},
|
2019-07-02 22:17:53 +02:00
|
|
|
SKIN => $self->p->getSkin($req),
|
2019-06-19 18:13:17 +02:00
|
|
|
LANGS => $self->conf->{showLanguages},
|
2019-06-23 10:51:00 +02:00
|
|
|
MSG => 'contextSwitching_ON',
|
2019-06-19 18:13:17 +02:00
|
|
|
ALERTE => 'alert-danger',
|
|
|
|
LOGIN => '',
|
2019-06-21 14:49:38 +02:00
|
|
|
SPOOFID => $self->conf->{contextSwitchingRule},
|
2019-06-19 18:13:17 +02:00
|
|
|
TOKEN => (
|
|
|
|
$self->ottRule->( $req, {} )
|
|
|
|
? $self->ott->createToken()
|
|
|
|
: ''
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2019-06-21 14:49:38 +02:00
|
|
|
return $self->p->sendHtml( $req, 'contextSwitching', params => $params, );
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ( $self, $req ) = @_;
|
2019-07-02 20:03:40 +02:00
|
|
|
my $statut = PE_OK;
|
2019-07-03 00:09:14 +02:00
|
|
|
my $realId = $req->{user};
|
2019-06-24 23:36:23 +02:00
|
|
|
my $spoofId = $req->param('spoofId') || ''; # ContextSwitching required ?
|
2019-06-19 18:13:17 +02:00
|
|
|
|
2019-06-21 16:24:50 +02:00
|
|
|
# Check activation rule
|
|
|
|
unless ( $self->rule->( $req, $req->userData ) ) {
|
2019-07-03 23:08:40 +02:00
|
|
|
$self->userLogger->warn('ContextSwitching service NOT authorized');
|
2019-06-21 16:24:50 +02:00
|
|
|
$spoofId = '';
|
2019-06-22 23:46:02 +02:00
|
|
|
return $self->p->do( $req,
|
|
|
|
[ sub { PE_IMPERSONATION_SERVICE_NOT_ALLOWED } ] );
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
# ContextSwitching required -> Check user Id
|
2019-06-22 23:46:02 +02:00
|
|
|
if ( $spoofId && $spoofId ne $req->{user} ) {
|
2019-06-19 18:13:17 +02:00
|
|
|
$self->logger->debug("Spoof Id: $spoofId");
|
2019-06-21 16:24:50 +02:00
|
|
|
unless ( $spoofId =~ /$self->{conf}->{userControl}/o ) {
|
2019-06-24 23:36:23 +02:00
|
|
|
$self->userLogger->warn('Malformed spoofed Id');
|
2019-06-21 16:24:50 +02:00
|
|
|
$self->logger->debug(
|
2019-07-03 23:08:40 +02:00
|
|
|
"ContextSwitching tried with spoofed Id: $spoofId");
|
2019-06-22 23:46:02 +02:00
|
|
|
return $self->p->do( $req, [ sub { PE_MALFORMEDUSER } ] );
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-21 16:24:50 +02:00
|
|
|
else {
|
2019-07-03 23:08:40 +02:00
|
|
|
$self->logger->debug("contextSwitching NOT required");
|
2019-06-22 23:46:02 +02:00
|
|
|
$req->urldc( $self->conf->{portal} );
|
|
|
|
return $self->p->do( $req, [ sub { PE_OK } ] );
|
2019-06-21 16:24:50 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
# Create spoofed session
|
|
|
|
$req = $self->_switchContext( $req, $spoofId );
|
2019-06-19 18:13:17 +02:00
|
|
|
if ( $req->error ) {
|
|
|
|
if ( $req->error == PE_BADCREDENTIALS ) {
|
2019-06-22 23:46:02 +02:00
|
|
|
$statut = PE_MALFORMEDUSER;
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-06-21 16:24:50 +02:00
|
|
|
$statut = $req->error;
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Main session
|
2019-06-24 23:36:23 +02:00
|
|
|
$self->p->updateSession( $req, $req->sessionInfo );
|
2019-07-03 00:09:14 +02:00
|
|
|
$self->userLogger->notice(
|
2019-07-04 07:24:50 +02:00
|
|
|
"ContextSwitching: Update $realId session with $spoofId session data");
|
2019-07-02 21:58:07 +02:00
|
|
|
|
2019-06-19 18:13:17 +02:00
|
|
|
return $self->p->do( $req, [ sub { $statut } ] );
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
sub _switchContext {
|
|
|
|
my ( $self, $req, $spoofId ) = @_;
|
|
|
|
my $realSessionId = $req->userData->{_session_id};
|
2019-07-03 00:09:14 +02:00
|
|
|
my $realId = $req->{user};
|
2019-06-25 19:28:05 +02:00
|
|
|
my $raz = 0;
|
2019-06-24 23:52:39 +02:00
|
|
|
$req->{user} = $spoofId;
|
2019-06-19 18:13:17 +02:00
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
# Search user in database & create session
|
2019-07-03 23:21:19 +02:00
|
|
|
$req->steps( [
|
|
|
|
'getUser', 'setAuthSessionInfo',
|
|
|
|
'setSessionInfo', 'setMacros',
|
|
|
|
'setGroups', 'setPersistentSessionInfo',
|
|
|
|
'setLocalGroups', 'store',
|
|
|
|
'buildCookie'
|
|
|
|
]
|
|
|
|
);
|
2019-06-19 18:13:17 +02:00
|
|
|
if ( my $error = $self->p->process($req) ) {
|
|
|
|
if ( $error == PE_BADCREDENTIALS ) {
|
|
|
|
$self->userLogger->warn(
|
2019-06-22 23:46:02 +02:00
|
|
|
'ContextSwitching requested for an unvalid user ('
|
2019-06-19 18:13:17 +02:00
|
|
|
. $req->{user}
|
|
|
|
. ")" );
|
|
|
|
}
|
|
|
|
$self->logger->debug("Process returned error: $error");
|
|
|
|
$req->error($error);
|
|
|
|
$raz = 1;
|
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
# Check identity rule if ContextSwitching required
|
2019-06-22 23:46:02 +02:00
|
|
|
unless ( $self->idRule->( $req, $req->sessionInfo ) ) {
|
|
|
|
$self->userLogger->warn(
|
|
|
|
'ContextSwitching requested for an unvalid user ('
|
|
|
|
. $req->{user}
|
|
|
|
. ")" );
|
2019-06-24 23:36:23 +02:00
|
|
|
$self->logger->debug('Identity NOT authorized');
|
2019-07-03 23:08:40 +02:00
|
|
|
$req->error(PE_MALFORMEDUSER); # Catch error to preserve protected Id
|
2019-06-22 23:46:02 +02:00
|
|
|
$raz = 1;
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
$req->sessionInfo->{"$self->{conf}->{impersonationPrefix}_session_id"} =
|
|
|
|
$realSessionId;
|
2019-07-03 00:09:14 +02:00
|
|
|
$self->userLogger->notice(
|
2019-07-04 07:24:50 +02:00
|
|
|
"Start ContextSwitching: $realId becomes $spoofId ")
|
2019-07-03 00:09:14 +02:00
|
|
|
unless $raz;
|
2019-06-19 18:13:17 +02:00
|
|
|
|
2019-07-03 00:09:14 +02:00
|
|
|
return $raz
|
|
|
|
? $self->_abortImpersonation( $req, $spoofId, $realId, 1 )
|
|
|
|
: $req;
|
2019-06-19 18:13:17 +02:00
|
|
|
}
|
|
|
|
|
2019-06-25 19:28:05 +02:00
|
|
|
sub _abortImpersonation {
|
2019-07-03 00:09:14 +02:00
|
|
|
my ( $self, $req, $spoofId, $realId, $abort ) = @_;
|
2019-06-25 19:28:05 +02:00
|
|
|
my $type = $abort ? 'sessionInfo' : 'userData';
|
2019-06-24 23:36:23 +02:00
|
|
|
my $realSessionId =
|
2019-06-25 19:28:05 +02:00
|
|
|
$req->{$type}->{"$self->{conf}->{impersonationPrefix}_session_id"};
|
2019-07-03 06:47:33 +02:00
|
|
|
my $session;
|
|
|
|
unless ( $session = $self->p->getApacheSession($realSessionId) ) {
|
2019-07-04 07:24:50 +02:00
|
|
|
$self->userLogger->info("Session $session expired");
|
2019-07-03 23:08:40 +02:00
|
|
|
return $req->error(PE_SESSIONEXPIRED);
|
2019-07-03 06:47:33 +02:00
|
|
|
}
|
2019-06-28 23:53:20 +02:00
|
|
|
|
2019-06-25 19:28:05 +02:00
|
|
|
if ($abort) {
|
2019-07-03 00:09:14 +02:00
|
|
|
$self->userLogger->notice(
|
2019-07-04 07:24:50 +02:00
|
|
|
"Abort ContextSwitching: $spoofId by $realId");
|
2019-07-03 06:47:33 +02:00
|
|
|
if ( my $abortSession = $self->p->getApacheSession( $req->id ) ) {
|
|
|
|
$abortSession->remove;
|
|
|
|
}
|
|
|
|
else {
|
2019-07-04 07:24:50 +02:00
|
|
|
$self->userLogger->info(
|
2019-07-03 23:08:40 +02:00
|
|
|
"ContextSwitching: session " . $req->id . " expired" );
|
2019-07-03 06:47:33 +02:00
|
|
|
}
|
2019-06-25 19:28:05 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-07-03 23:12:15 +02:00
|
|
|
$self->userLogger->notice(
|
2019-07-04 07:24:50 +02:00
|
|
|
"Stop ContextSwitching for $realId with uid $spoofId");
|
2019-06-25 22:01:20 +02:00
|
|
|
$self->p->deleteSession($req);
|
2019-06-25 19:28:05 +02:00
|
|
|
}
|
2019-06-24 23:36:23 +02:00
|
|
|
|
|
|
|
# Restore real session
|
2019-07-03 23:12:15 +02:00
|
|
|
$req->{$type} = { %{ $session->data } };
|
2019-07-03 23:08:40 +02:00
|
|
|
$req->{user} = $session->data->{_user};
|
2019-06-22 23:46:02 +02:00
|
|
|
$req->urldc( $self->conf->{portal} );
|
2019-06-24 23:36:23 +02:00
|
|
|
$req->id($realSessionId);
|
|
|
|
$self->p->buildCookie($req);
|
2019-06-28 23:53:20 +02:00
|
|
|
delete $req->{$type}->{"$self->{conf}->{impersonationPrefix}_session_id"};
|
2019-06-23 22:07:05 +02:00
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
return $req;
|
2019-06-22 23:46:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-24 23:36:23 +02:00
|
|
|
sub displaySwitchContext {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return 'OFF'
|
|
|
|
if $req->userData->{"$self->{conf}->{impersonationPrefix}_session_id"};
|
|
|
|
return 'ON' if $self->rule->( $req, $req->userData );
|
2019-06-22 23:46:02 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 18:13:17 +02:00
|
|
|
1;
|