lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/ContextSwitching.pm

311 lines
10 KiB
Perl
Raw Normal View History

2019-06-21 14:49:38 +02:00
package Lemonldap::NG::Portal::Plugins::ContextSwitching;
use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_OK
PE_ERROR
2019-11-11 21:51:00 +01:00
PE_NOTOKEN
PE_REDIRECT
2019-11-11 21:51:00 +01:00
PE_TOKENEXPIRED
PE_MALFORMEDUSER
2019-07-04 22:50:46 +02:00
PE_BADCREDENTIALS
PE_SESSIONEXPIRED
2019-07-04 22:50:46 +02:00
PE_IMPERSONATION_SERVICE_NOT_ALLOWED
);
2020-11-17 23:18:05 +01:00
our $VERSION = '2.0.10';
2019-08-03 11:05:12 +02:00
extends qw(
Lemonldap::NG::Portal::Main::Plugin
Lemonldap::NG::Portal::Lib::_tokenRule
Lemonldap::NG::Portal::Lib::OtherSessions
);
# INITIALIZATION
2019-06-25 19:28:05 +02:00
has ott => (
is => 'rw',
lazy => 1,
default => sub {
my $ott =
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
$ott->timeout( $_[0]->{conf}->{formTimeout} );
return $ott;
}
);
2020-05-24 00:04:33 +02:00
has rule => ( is => 'rw', default => sub { 0 } );
has idRule => ( is => 'rw', default => sub { 1 } );
has unrestrictedUsersRule => ( is => 'rw', default => sub { 0 } );
sub init {
my ($self) = @_;
2022-02-16 17:43:29 +01:00
$self->addAuthRoute( switchcontext => 'run', ['POST'] )
2019-07-31 23:38:48 +02:00
->addAuthRoute( switchcontext => 'display', ['GET'] );
2020-04-25 14:41:23 +02:00
# Parse ContextSwitching rules
$self->rule(
2020-04-25 14:48:27 +02:00
$self->p->buildRule(
$self->conf->{contextSwitchingRule},
'contextSwitching'
)
);
2020-04-25 14:41:23 +02:00
return 0 unless $self->rule;
2019-06-23 22:07:05 +02:00
2020-04-25 14:41:23 +02:00
$self->idRule(
2020-04-25 14:48:27 +02:00
$self->p->buildRule(
$self->conf->{contextSwitchingIdRule},
'contextSwitchingId'
)
);
2020-04-25 14:41:23 +02:00
return 0 unless $self->idRule;
2020-04-25 14:48:27 +02:00
$self->unrestrictedUsersRule(
$self->p->buildRule(
$self->conf->{contextSwitchingUnrestrictedUsersRule},
'contextSwitchingUnrestrictedUsers'
)
);
return 0 unless $self->unrestrictedUsersRule;
return 1;
}
# RUNNING METHOD
sub display {
my ( $self, $req ) = @_;
my ( $realSession, $realSessionId );
if ( $realSessionId =
$req->userData->{"$self->{conf}->{contextSwitchingPrefix}_session_id"} )
{
unless ( $realSession = $self->p->getApacheSession($realSessionId) ) {
$self->userLogger->info(
"ContextSwitching: session $realSessionId expired");
return $self->p->do( $req, [ sub { PE_SESSIONEXPIRED } ] );
}
}
2019-06-22 23:46:02 +02:00
# Check access rules
unless ( $self->rule->( $req, $req->userData )
2020-04-04 00:08:20 +02:00
|| $req->userData->{
"$self->{conf}->{contextSwitchingPrefix}_session_id"} )
2019-06-22 23:46:02 +02:00
{
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 } ] );
}
2020-04-04 00:08:20 +02:00
if (
$req->userData->{"$self->{conf}->{contextSwitchingPrefix}_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}");
2019-11-11 21:51:00 +01:00
$self->userLogger->info("Remove real session $realSessionId");
$realSession->remove;
2019-06-23 22:07:05 +02:00
return $self->p->do( $req,
[ @{ $self->p->beforeLogout }, 'authLogout', 'deleteSession' ]
);
2019-06-23 22:07:05 +02:00
}
else {
$req = $self->_abortImpersonation( $req, $req->{user},
$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
}
# Display form
my $params = {
2020-12-21 21:55:51 +01:00
PORTAL => $self->conf->{portal},
MAIN_LOGO => $self->conf->{portalMainLogo},
SKIN => $self->p->getSkin($req),
LANGS => $self->conf->{showLanguages},
MSG => 'contextSwitching_ON',
ALERTE => 'alert-danger',
IMPERSONATION => $self->conf->{contextSwitchingRule},
TOKEN => (
$self->ottRule->( $req, {} )
? $self->ott->createToken()
: ''
)
};
2019-06-21 14:49:38 +02:00
return $self->p->sendHtml( $req, 'contextSwitching', params => $params, );
}
sub run {
my ( $self, $req ) = @_;
2022-02-16 17:43:29 +01:00
my $statut = PE_OK;
my $realId = $req->userData->{ $self->conf->{whatToTrace} };
my $spoofId = $req->param('spoofId') || ''; # ContextSwitching required ?
2022-02-16 17:43:29 +01:00
my $unUser = $self->unrestrictedUsersRule->( $req, $req->userData ) || 0;
2019-11-11 21:51:00 +01:00
# Check token
if ( $self->ottRule->( $req, {} ) ) {
my $token = $req->param('token');
unless ($token) {
$self->userLogger->warn('ContextSwitching called without token');
return $self->p->do( $req, [ sub { PE_NOTOKEN } ] );
}
unless ( $self->ott->getToken($token) ) {
$self->userLogger->warn(
'ContextSwitching called with an expired/bad token');
2019-11-11 21:51:00 +01:00
return $self->p->do( $req, [ sub { PE_TOKENEXPIRED } ] );
}
}
2019-06-21 16:24:50 +02:00
# Check activation rule
unless ( $self->rule->( $req, $req->userData ) ) {
$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 } ] );
}
# ContextSwitching required -> Check user Id
2019-06-22 23:46:02 +02:00
if ( $spoofId && $spoofId ne $req->{user} ) {
2020-05-23 23:02:37 +02:00
$self->logger->debug("Spoofed Id: $spoofId");
2019-06-21 16:24:50 +02:00
unless ( $spoofId =~ /$self->{conf}->{userControl}/o ) {
$self->userLogger->warn('Malformed spoofed Id');
2019-06-21 16:24:50 +02:00
$self->logger->debug(
"ContextSwitching tried with spoofed Id: $spoofId");
2019-06-22 23:46:02 +02:00
return $self->p->do( $req, [ sub { PE_MALFORMEDUSER } ] );
}
}
2019-06-21 16:24:50 +02:00
else {
$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
}
# Create spoofed session
$req = $self->_switchContext( $req, $spoofId, $unUser );
2020-04-04 00:08:20 +02:00
$statut =
( $req->error == PE_BADCREDENTIALS ? PE_MALFORMEDUSER : $req->error )
if $req->error;
# Main session
$self->p->updateSession( $req, $req->sessionInfo );
2019-07-03 00:09:14 +02:00
$self->userLogger->notice(
2020-05-24 00:04:33 +02:00
"ContextSwitching: Update \"$realId\" session with \"$spoofId\" session data"
);
2020-08-28 23:50:57 +02:00
$req->mustRedirect(1);
return $self->p->do( $req, [ sub { $statut } ] );
}
sub _switchContext {
my ( $self, $req, $spoofId, $unUser ) = @_;
my $realSessionId = $req->userData->{_session_id};
my $realAuthLevel = $req->userData->{authenticationLevel};
2020-11-17 23:18:05 +01:00
my $realId = $req->userData->{ $self->conf->{whatToTrace} };
2019-06-25 19:28:05 +02:00
my $raz = 0;
2019-06-24 23:52:39 +02:00
$req->{user} = $spoofId;
# Search user in database & create session
2019-07-03 23:21:19 +02:00
$req->steps( [
'getUser', 'setAuthSessionInfo',
'setSessionInfo', $self->p->groupsAndMacros,
'setPersistentSessionInfo', 'setLocalGroups',
'store', 'buildCookie'
2019-07-03 23:21:19 +02:00
]
);
if ( my $error = $self->p->process($req) ) {
2020-04-04 00:08:20 +02:00
$self->userLogger->warn(
2020-08-28 21:53:19 +02:00
'ContextSwitching requested for an invalid user ('
2020-04-04 00:08:20 +02:00
. $req->{user}
. ")" )
if ( $error == PE_BADCREDENTIALS );
$self->logger->debug("Process returned error: $error");
$req->error($error);
$raz = 1;
}
# Check identities rule if ContextSwitching required
$self->logger->info("\"$realId\" is an unrestricted user!") if $unUser;
unless ( $unUser || $self->idRule->( $req, $req->sessionInfo ) ) {
2019-06-22 23:46:02 +02:00
$self->userLogger->warn(
2020-08-28 21:53:19 +02:00
'ContextSwitching requested for an invalid user ('
2019-06-22 23:46:02 +02:00
. $req->{user}
. ")" );
$self->logger->debug('Identity NOT authorized');
$req->error(PE_MALFORMEDUSER); # Catch error to preserve protected Id
2019-06-22 23:46:02 +02:00
$raz = 1;
}
$req->sessionInfo->{"$self->{conf}->{contextSwitchingPrefix}_session_id"} =
$realSessionId;
2020-04-04 00:08:20 +02:00
return $self->_abortImpersonation( $req, $spoofId, $realId, 1 ) if $raz;
2020-04-04 00:08:20 +02:00
$self->logger->debug(
"Update sessionInfo with real authenticationLevel: $realAuthLevel");
$req->sessionInfo->{authenticationLevel} = $realAuthLevel;
delete $req->sessionInfo->{groups};
2020-04-04 00:08:20 +02:00
# Compute groups & macros again with real authenticationLevel
2020-04-23 14:16:35 +02:00
$req->steps(
[ 'setSessionInfo', $self->p->groupsAndMacros, 'setLocalGroups' ] );
2020-04-04 00:08:20 +02:00
if ( my $error = $self->p->process($req) ) {
$self->logger->debug(
"ContextSwitching: Process returned error: $error");
$req->error($error);
}
2020-04-04 00:08:20 +02:00
$self->userLogger->notice(
2020-05-23 23:02:37 +02:00
"Start ContextSwitching: \"$realId\" becomes \"$spoofId\"");
2020-04-04 00:08:20 +02:00
return $req;
}
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';
my $realSessionId =
$req->{$type}->{"$self->{conf}->{contextSwitchingPrefix}_session_id"};
my $session;
unless ( $session = $self->p->getApacheSession($realSessionId) ) {
2019-07-04 07:24:50 +02:00
$self->userLogger->info("Session $session expired");
return $req->error(PE_SESSIONEXPIRED);
}
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(
2020-05-23 23:02:37 +02:00
"Abort ContextSwitching: \"$spoofId\" by \"$realId\"");
if ( my $abortSession = $self->p->getApacheSession( $req->id ) ) {
$abortSession->remove;
}
else {
2019-07-04 07:24:50 +02:00
$self->userLogger->info(
"ContextSwitching: session " . $req->id . " expired" );
}
2019-06-25 19:28:05 +02:00
}
else {
2019-07-03 23:12:15 +02:00
$self->userLogger->notice(
2020-05-23 23:02:37 +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
}
# Restore real session
2019-07-03 23:12:15 +02:00
$req->{$type} = { %{ $session->data } };
$req->{user} = $session->data->{_user};
2019-06-22 23:46:02 +02:00
$req->urldc( $self->conf->{portal} );
$req->id($realSessionId);
$self->p->buildCookie($req);
2020-04-04 00:08:20 +02:00
delete $req->{$type}
->{"$self->{conf}->{contextSwitchingPrefix}_session_id"};
2019-06-23 22:07:05 +02:00
return $req;
2019-06-22 23:46:02 +02:00
}
2020-02-13 23:27:13 +01:00
sub displayLink {
my ( $self, $req ) = @_;
return 'OFF'
if $req->userData->{"$self->{conf}->{contextSwitchingPrefix}_session_id"};
return 'ON' if $self->rule->( $req, $req->userData );
2019-06-22 23:46:02 +02:00
}
1;