2017-02-09 07:24:12 +01:00
|
|
|
# Main running methods file
|
2016-03-17 23:19:44 +01:00
|
|
|
package Lemonldap::NG::Handler::Main::Run;
|
|
|
|
|
2019-10-15 17:36:09 +02:00
|
|
|
our $VERSION = '2.0.7';
|
2017-02-08 23:18:52 +01:00
|
|
|
|
|
|
|
package Lemonldap::NG::Handler::Main;
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
use strict;
|
|
|
|
|
|
|
|
#use AutoLoader 'AUTOLOAD';
|
|
|
|
use MIME::Base64;
|
|
|
|
use URI::Escape;
|
|
|
|
use Lemonldap::NG::Common::Session;
|
|
|
|
|
2017-02-09 07:24:12 +01:00
|
|
|
# Methods that must be overloaded
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
sub handler {
|
|
|
|
die "Must be overloaded" unless ($#_);
|
2016-08-03 10:02:22 +02:00
|
|
|
my ($res) = $_[0]->run( $_[1] );
|
|
|
|
return $res;
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub logout {
|
|
|
|
my $class;
|
|
|
|
$class = $#_ ? shift : __PACKAGE__;
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->unlog(@_);
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
sub status {
|
|
|
|
my $class;
|
|
|
|
$class = $#_ ? shift : __PACKAGE__;
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->getStatus(@_);
|
2017-02-09 07:24:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Public methods
|
|
|
|
|
2017-02-11 08:47:22 +01:00
|
|
|
# Return Handler::Lib::Status output
|
2017-02-09 07:24:12 +01:00
|
|
|
sub getStatus {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Request for status");
|
2016-03-17 23:19:44 +01:00
|
|
|
my $statusPipe = $class->tsv->{statusPipe};
|
|
|
|
my $statusOut = $class->tsv->{statusOut};
|
2018-06-12 21:00:10 +02:00
|
|
|
my $args = '';
|
|
|
|
if ( $ENV{LLNGSTATUSHOST} ) {
|
|
|
|
require IO::Socket::INET;
|
|
|
|
foreach ( 64322 .. 64331 ) {
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $statusOut =
|
|
|
|
IO::Socket::INET->new( Proto => 'udp', LocalPort => $_ ) )
|
2018-06-12 21:00:10 +02:00
|
|
|
{
|
2019-03-07 18:22:16 +01:00
|
|
|
$args =
|
|
|
|
' host=' . ( $ENV{LLNGSTATUSCLIENT} || 'localhost' ) . ":$_";
|
2018-06-12 21:00:10 +02:00
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $class->abort( $req,
|
2019-03-07 18:22:16 +01:00
|
|
|
"$class: status page can not be displayed, unable to open socket" )
|
|
|
|
unless ($statusOut);
|
2018-06-12 21:00:10 +02:00
|
|
|
}
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->abort( $req, "$class: status page can not be displayed" )
|
2019-03-07 18:22:16 +01:00
|
|
|
unless ( $statusPipe and $statusOut );
|
2018-06-13 06:15:57 +02:00
|
|
|
my $q = $req->{env}->{QUERY_STRING} || '';
|
|
|
|
if ( $q =~ /\s/ ) {
|
|
|
|
$class->logger->error("Bad characters in query");
|
|
|
|
return $class->FORBIDDEN;
|
|
|
|
}
|
2018-06-12 11:45:21 +02:00
|
|
|
$statusPipe->print(
|
2018-06-12 21:00:10 +02:00
|
|
|
"STATUS " . ( $req->{env}->{QUERY_STRING} || '' ) . "$args\n" );
|
2016-03-17 23:19:44 +01:00
|
|
|
my $buf;
|
|
|
|
|
2018-06-12 12:17:17 +02:00
|
|
|
while ( $_ = $statusOut->getline ) {
|
2016-03-17 23:19:44 +01:00
|
|
|
last if (/^END$/);
|
|
|
|
$buf .= $_;
|
|
|
|
}
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->set_header_out( $req,
|
|
|
|
"Content-Type" => "text/html; charset=UTF-8" );
|
|
|
|
$class->print( $req, $buf );
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->OK;
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:24:12 +01:00
|
|
|
# Method that must be called by base packages (Handler::ApacheMP2,...) to get
|
|
|
|
# type of handler to call (Main, AuthBasic,...)
|
2017-02-08 23:18:52 +01:00
|
|
|
sub checkType {
|
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
|
|
|
|
if ( time() - $class->lastCheck > $class->checkTime ) {
|
2019-09-02 23:00:51 +02:00
|
|
|
unless ( $class->checkConf ) {
|
|
|
|
$class->logger->error("$class: No configuration found");
|
|
|
|
$req->data->{noTry} = 1;
|
|
|
|
return 'Fail';
|
|
|
|
}
|
2017-02-08 23:18:52 +01:00
|
|
|
}
|
2017-03-28 23:07:49 +02:00
|
|
|
my $vhost = $class->resolveAlias($req);
|
2017-02-09 07:24:12 +01:00
|
|
|
return ( defined $class->tsv->{type}->{$vhost} )
|
2019-03-07 18:22:16 +01:00
|
|
|
? $class->tsv->{type}->{$vhost}
|
|
|
|
: 'Main';
|
2017-02-08 23:18:52 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
## @rmethod int run
|
|
|
|
# Check configuration and launch Lemonldap::NG::Handler::Main::run().
|
2019-10-15 17:36:09 +02:00
|
|
|
# Each $checkTime, server child verifies if its configuration is the same
|
2016-03-17 23:19:44 +01:00
|
|
|
# as the configuration stored in the local storage.
|
|
|
|
# @param $rule optional Perl expression to grant access
|
2019-10-15 17:36:09 +02:00
|
|
|
# @return constant
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
sub run {
|
|
|
|
my ( $class, $req, $rule, $protection ) = @_;
|
2017-02-02 15:23:45 +01:00
|
|
|
my ( $id, $session );
|
|
|
|
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->DECLINED unless ( $class->is_initial_req($req) );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2018-07-04 22:49:51 +02:00
|
|
|
# Direct return if maintenance mode is enabled
|
2017-03-28 23:07:49 +02:00
|
|
|
if ( $class->checkMaintenanceMode($req) ) {
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
if ( $class->tsv->{useRedirectOnError} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Go to portal with maintenance error code");
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->goToError( $req, '/', $class->MAINTENANCE );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Return maintenance error code");
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->MAINTENANCE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-15 17:36:09 +02:00
|
|
|
# Authentication process
|
2017-03-28 23:07:49 +02:00
|
|
|
my $uri = $req->{env}->{REQUEST_URI};
|
2016-03-17 23:19:44 +01:00
|
|
|
my ($cond);
|
2019-10-15 17:36:09 +02:00
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
( $cond, $protection ) = $class->conditionSub($rule) if ($rule);
|
2017-03-28 23:07:49 +02:00
|
|
|
$protection = $class->isUnprotected( $req, $uri ) || 0
|
2019-03-07 18:22:16 +01:00
|
|
|
unless ( defined $protection );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
if ( $protection == $class->SKIP ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Access control skipped");
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, 'SKIP' );
|
|
|
|
$class->hideCookie($req);
|
|
|
|
$class->cleanHeaders($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Try to recover cookie and user session
|
2017-03-28 23:07:49 +02:00
|
|
|
if ( $id = $class->fetchId($req)
|
|
|
|
and $session = $class->retrieveSession( $req, $id ) )
|
2017-02-02 15:23:45 +01:00
|
|
|
{
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# AUTHENTICATION done
|
|
|
|
|
|
|
|
# Local macros
|
2016-07-30 22:48:03 +02:00
|
|
|
my $kc = keys %{$session}; # in order to detect new local macro
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# ACCOUNTING (1. Inform web server)
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->set_user( $req, $session->{ $class->tsv->{whatToTrace} } );
|
2019-08-27 10:10:11 +02:00
|
|
|
$class->set_custom( $req, $session->{ $class->tsv->{customToTrace} } )
|
|
|
|
if $class->tsv->{customToTrace}
|
|
|
|
and $session->{ $class->tsv->{customToTrace} };
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# AUTHORIZATION
|
2017-03-28 23:07:49 +02:00
|
|
|
return ( $class->forbidden( $req, $session ), $session )
|
2019-03-07 18:22:16 +01:00
|
|
|
unless ( $class->grant( $req, $session, $uri, $cond ) );
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, 'OK',
|
|
|
|
$session->{ $class->tsv->{whatToTrace} } );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# ACCOUNTING (2. Inform remote application)
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->sendHeaders( $req, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Store local macros
|
2016-07-30 22:48:03 +02:00
|
|
|
if ( keys %$session > $kc ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Update local cache");
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->data->{session}->update( $session, { updateCache => 2 } );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Hide Lemonldap::NG cookie
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->hideCookie($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Log access granted
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug( "User "
|
2019-03-07 18:22:16 +01:00
|
|
|
. $session->{ $class->tsv->{whatToTrace} }
|
|
|
|
. " was granted to access to $uri" );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Catch POST rules
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->postOutputFilter( $req, $session, $uri );
|
|
|
|
$class->postInputFilter( $req, $session, $uri );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2016-08-02 15:52:29 +02:00
|
|
|
return ( $class->OK, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
elsif ( $protection == $class->UNPROTECT ) {
|
|
|
|
|
|
|
|
# Ignore unprotected URIs
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("No valid session but unprotected access");
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, 'UNPROTECT' );
|
|
|
|
$class->hideCookie($req);
|
|
|
|
$class->cleanHeaders($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Redirect user to the portal
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->info("No cookie found")
|
2019-03-07 18:22:16 +01:00
|
|
|
unless ($id);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# if the cookie was fetched, a log is sent by retrieveSession()
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, $id ? 'EXPIRED' : 'REDIRECT' );
|
|
|
|
return $class->goToPortal( $req, $req->{env}->{REQUEST_URI} );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:24:12 +01:00
|
|
|
## @rmethod protected int unlog()
|
|
|
|
# Call localUnlog() then goToPortal() to unlog the current user.
|
|
|
|
# @return Constant value returned by goToPortal()
|
|
|
|
sub unlog {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
$class->localUnlog( $req, @_ );
|
|
|
|
$class->updateStatus( $req, 'LOGOUT' );
|
|
|
|
return $class->goToPortal( $req, '/', 'logout=1' );
|
2017-02-09 07:24:12 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
# INTERNAL METHODS
|
|
|
|
|
|
|
|
## @rmethod protected void updateStatus(string action,string user,string url)
|
|
|
|
# Inform the status process of the result of the request if it is available
|
|
|
|
# @param action string Result of access control (as $class->OK, $class->SKIP, LOGOUT...)
|
|
|
|
# @param optional user string Username to log, if undefined defaults to remote IP
|
|
|
|
# @param optional url string URL to log, if undefined defaults to request URI
|
|
|
|
sub updateStatus {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $action, $user, $url ) = @_;
|
2016-03-17 23:19:44 +01:00
|
|
|
my $statusPipe = $class->tsv->{statusPipe} or return;
|
2017-03-28 23:07:49 +02:00
|
|
|
$user ||= $req->{env}->{REMOTE_ADDR};
|
|
|
|
$url ||= $req->{env}->{REQUEST_URI};
|
2016-03-17 23:19:44 +01:00
|
|
|
eval {
|
2018-06-12 11:45:21 +02:00
|
|
|
$statusPipe->print(
|
|
|
|
"$user => " . $req->{env}->{HTTP_HOST} . "$url $action\n" );
|
2016-03-17 23:19:44 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod void lmLog(string msg, string level)
|
|
|
|
# Wrapper for Apache log system
|
|
|
|
# @param $msg message to log
|
|
|
|
# @param $level string (emerg|alert|crit|error|warn|notice|info|debug)
|
|
|
|
sub lmLog {
|
|
|
|
my ( $class, $msg, $level ) = @_;
|
2017-02-13 12:58:39 +01:00
|
|
|
return $class->logger->$level($msg);
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected boolean checkMaintenanceMode
|
|
|
|
# Check if we are in maintenance mode
|
2018-07-04 22:49:51 +02:00
|
|
|
# @return true if maintenance mode is enabled
|
2016-03-17 23:19:44 +01:00
|
|
|
sub checkMaintenanceMode {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2019-03-07 18:22:16 +01:00
|
|
|
my $_maintenance =
|
|
|
|
( defined $class->tsv->{maintenance}->{$vhost} )
|
|
|
|
? $class->tsv->{maintenance}->{$vhost}
|
|
|
|
: $class->tsv->{maintenance}->{_};
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
if ($_maintenance) {
|
2018-07-04 22:49:51 +02:00
|
|
|
$class->logger->debug("Maintenance mode enabled");
|
2016-03-17 23:19:44 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod boolean grant(string uri, string cond)
|
|
|
|
# Grant or refuse client using compiled regexp and functions
|
|
|
|
# @param $uri URI
|
|
|
|
# @param $cond optional Function granting access
|
|
|
|
# @return True if the user is granted to access to the current URL
|
|
|
|
sub grant {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $session, $uri, $cond, $vhost ) = @_;
|
2019-10-27 17:47:29 +01:00
|
|
|
my $level;
|
|
|
|
|
2017-03-28 23:07:49 +02:00
|
|
|
return $cond->( $req, $session ) if ($cond);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2017-03-28 23:07:49 +02:00
|
|
|
$vhost ||= $class->resolveAlias($req);
|
2019-10-29 20:09:11 +01:00
|
|
|
|
|
|
|
# Using URL authentification level if exists
|
2019-10-27 17:47:29 +01:00
|
|
|
for (
|
|
|
|
my $i = 0 ;
|
|
|
|
$i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
|
|
|
|
$i++
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ( $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] ) {
|
|
|
|
$level = $class->tsv->{locationAuthnLevel}->{$vhost}->[$i];
|
|
|
|
last;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 20:09:11 +01:00
|
|
|
$level
|
|
|
|
? $class->logger->debug(
|
|
|
|
'Found AuthnLevel=' . $level . ' for "' . "$vhost$uri" . '"' )
|
|
|
|
: $class->logger->debug("No URL authentication level found...");
|
2019-10-27 17:47:29 +01:00
|
|
|
|
2019-10-29 20:09:11 +01:00
|
|
|
# Using VH authentification level if exists
|
2019-10-27 17:47:29 +01:00
|
|
|
if ( $level ||= $class->tsv->{authnLevel}->{$vhost} ) {
|
2017-03-23 19:22:44 +01:00
|
|
|
if ( $session->{authenticationLevel} < $level ) {
|
2019-10-15 17:36:09 +02:00
|
|
|
$class->logger->debug(
|
|
|
|
"User authentication level = $session->{authenticationLevel}");
|
|
|
|
$class->logger->debug("Required authentication level = $level");
|
|
|
|
$class->logger->warn(
|
|
|
|
"User rejected due to insufficient authentication level -> Session upgrade enabled"
|
|
|
|
);
|
2017-03-23 19:22:44 +01:00
|
|
|
$session->{_upgrade} = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2017-03-06 17:43:06 +01:00
|
|
|
for (
|
2019-03-07 18:22:16 +01:00
|
|
|
my $i = 0 ;
|
|
|
|
$i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
|
2017-03-06 17:43:06 +01:00
|
|
|
$i++
|
2019-03-07 18:22:16 +01:00
|
|
|
)
|
2017-03-06 17:43:06 +01:00
|
|
|
{
|
2016-03-17 23:19:44 +01:00
|
|
|
if ( $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug( 'Regexp "'
|
2019-03-07 18:22:16 +01:00
|
|
|
. $class->tsv->{locationConditionText}->{$vhost}->[$i]
|
|
|
|
. '" match' );
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->tsv->{locationCondition}->{$vhost}->[$i]
|
2019-03-07 18:22:16 +01:00
|
|
|
->( $req, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
unless ( $class->tsv->{defaultCondition}->{$vhost} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->warn(
|
|
|
|
"User rejected because VirtualHost \"$vhost\" has no configuration"
|
2016-03-17 23:19:44 +01:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("$vhost: Apply default rule");
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->tsv->{defaultCondition}->{$vhost}->( $req, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected int forbidden(string uri)
|
|
|
|
# Used to reject non authorized requests.
|
2018-07-04 22:49:51 +02:00
|
|
|
# Inform the status process and call logForbidden().
|
2016-03-17 23:19:44 +01:00
|
|
|
# @param $uri URI
|
|
|
|
# @return Constant $class->FORBIDDEN
|
|
|
|
sub forbidden {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $session, $vhost ) = @_;
|
|
|
|
my $uri = $req->{env}->{REQUEST_URI};
|
|
|
|
$vhost ||= $class->resolveAlias($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2016-07-30 22:48:03 +02:00
|
|
|
if ( $session->{_logout} ) {
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, 'LOGOUT',
|
2016-07-30 22:48:03 +02:00
|
|
|
$session->{ $class->tsv->{whatToTrace} } );
|
|
|
|
my $u = $session->{_logout};
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->localUnlog($req);
|
|
|
|
return $class->goToPortal( $req, $u, 'logout=1' );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
2017-03-23 19:22:44 +01:00
|
|
|
if ( $session->{_upgrade} ) {
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->goToPortal( $req, $uri, undef, '/upgradesession' );
|
2017-03-23 19:22:44 +01:00
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
# Log forbidding
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->userLogger->notice( "User "
|
2019-03-07 18:22:16 +01:00
|
|
|
. $session->{ $class->tsv->{whatToTrace} }
|
|
|
|
. " was forbidden to access to $vhost$uri" );
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->updateStatus( $req, 'REJECT',
|
|
|
|
$session->{ $class->tsv->{whatToTrace} } );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Redirect or Forbidden?
|
|
|
|
if ( $class->tsv->{useRedirectOnForbidden} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Use redirect for forbidden access");
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->goToError( $req, $uri, 403 );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Return forbidden access");
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->FORBIDDEN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected void hideCookie()
|
|
|
|
# Hide Lemonldap::NG cookie to the protected application.
|
|
|
|
sub hideCookie {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("removing cookie");
|
2017-03-28 23:07:49 +02:00
|
|
|
my $cookie = $req->env->{HTTP_COOKIE};
|
2019-01-30 19:36:36 +01:00
|
|
|
$class->logger->debug("Cookies -> $cookie");
|
|
|
|
my $cn = $class->tsv->{cookieName};
|
|
|
|
$class->logger->debug("CookieName -> $cn");
|
|
|
|
$cookie =~ s/\b$cn(http)?=[^,;]*[,;\s]*//og;
|
|
|
|
$class->logger->debug("newCookies -> $cookie");
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
if ($cookie) {
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->set_header_in( $req, 'Cookie' => $cookie );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->unset_header_in( $req, 'Cookie' );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected string encodeUrl(string url)
|
2018-07-04 22:49:51 +02:00
|
|
|
# Encode URL in the format used by Lemonldap::NG::Portal for redirections.
|
2016-03-17 23:19:44 +01:00
|
|
|
# @return Base64 encoded string
|
|
|
|
sub encodeUrl {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $url ) = @_;
|
|
|
|
$url = $class->_buildUrl( $req, $url ) if ( $url !~ m#^https?://# );
|
2016-03-17 23:19:44 +01:00
|
|
|
return encode_base64( $url, '' );
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected int goToPortal(string url, string arg)
|
|
|
|
# Redirect non-authenticated users to the portal by setting "Location:" header.
|
|
|
|
# @param $url Url requested
|
|
|
|
# @param $arg optionnal GET parameters
|
|
|
|
# @return Constant $class->REDIRECT
|
|
|
|
sub goToPortal {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $url, $arg, $path ) = @_;
|
2017-03-23 19:22:44 +01:00
|
|
|
$path ||= '';
|
2016-03-17 23:19:44 +01:00
|
|
|
my ( $ret, $msg );
|
2017-03-28 23:07:49 +02:00
|
|
|
my $urlc_init = $class->encodeUrl( $req, $url );
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug(
|
2017-03-28 23:07:49 +02:00
|
|
|
"Redirect $req->{env}->{REMOTE_ADDR} to portal (url was $url)");
|
|
|
|
$class->set_header_out( $req,
|
2019-03-07 18:22:16 +01:00
|
|
|
'Location' => $class->tsv->{portal}->()
|
|
|
|
. "$path?url=$urlc_init"
|
|
|
|
. ( $arg ? "&$arg" : "" ) );
|
2016-03-17 23:19:44 +01:00
|
|
|
return $class->REDIRECT;
|
|
|
|
}
|
|
|
|
|
2017-03-16 22:33:13 +01:00
|
|
|
sub goToError {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $url, $code ) = @_;
|
|
|
|
my $urlc_init = $class->encodeUrl( $req, $url );
|
2017-03-16 22:33:13 +01:00
|
|
|
$class->logger->debug(
|
2017-03-28 23:07:49 +02:00
|
|
|
"Redirect $req->{env}->{REMOTE_ADDR} to lmError (url was $url)");
|
|
|
|
$class->set_header_out( $req,
|
2019-03-07 18:22:16 +01:00
|
|
|
'Location' => $class->tsv->{portal}->()
|
|
|
|
. "/lmerror/$code"
|
|
|
|
. "?url=$urlc_init" );
|
2017-03-16 22:33:13 +01:00
|
|
|
return $class->REDIRECT;
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
## @rmethod protected fetchId()
|
|
|
|
# Get user cookies and search for Lemonldap::NG cookie.
|
|
|
|
# @return Value of the cookie if found, 0 else
|
|
|
|
sub fetchId {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
my $t = $req->{env}->{HTTP_COOKIE} or return 0;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2019-01-25 11:37:14 +01:00
|
|
|
my $lookForHttpCookie = ( $class->tsv->{securedCookie} =~ /^(2|3)$/
|
2019-03-07 18:22:16 +01:00
|
|
|
and not $class->_isHttps( $req, $vhost ) );
|
2016-03-17 23:19:44 +01:00
|
|
|
my $cn = $class->tsv->{cookieName};
|
2019-03-07 18:22:16 +01:00
|
|
|
my $value =
|
|
|
|
$lookForHttpCookie
|
|
|
|
? ( $t =~ /${cn}http=([^,; ]+)/o ? $1 : 0 )
|
2019-10-15 17:36:09 +02:00
|
|
|
: ( $t =~ /$cn=([^,; ]+)/o ? $1 : 0 );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2017-02-02 15:23:45 +01:00
|
|
|
if ( $value && $lookForHttpCookie && $class->tsv->{securedCookie} == 3 ) {
|
|
|
|
$value = $class->tsv->{cipher}->decryptHex( $value, "http" );
|
|
|
|
}
|
|
|
|
elsif ( $value =~ s/^c:// ) {
|
|
|
|
$value = $class->tsv->{cipher}->decrypt($value);
|
|
|
|
unless ( $value =~ s/^(.*)? (.*)$/$1/ and $2 eq $vhost ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->userLogger->error(
|
2019-09-11 22:59:36 +02:00
|
|
|
"Bad CDA cookie: available for $2 instead of $vhost");
|
2017-02-02 15:23:45 +01:00
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
}
|
2016-03-17 23:19:44 +01:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected boolean retrieveSession(id)
|
|
|
|
# Tries to retrieve the session whose index is id
|
|
|
|
# @return true if the session was found, false else
|
|
|
|
sub retrieveSession {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $id ) = @_;
|
2016-03-17 23:19:44 +01:00
|
|
|
my $now = time();
|
|
|
|
|
|
|
|
# 1. Search if the user was the same as previous (very efficient in
|
|
|
|
# persistent connection).
|
2018-04-17 16:19:23 +02:00
|
|
|
# NB: timout is here the same value as current HTTP/1.1 Keep-Alive timeout
|
|
|
|
# (15 seconds)
|
2018-07-05 22:56:16 +02:00
|
|
|
if ( defined $class->data->{_session_id}
|
|
|
|
and $id eq $class->data->{_session_id}
|
2019-01-30 19:36:36 +01:00
|
|
|
and
|
|
|
|
( $now - $class->dataUpdate < $class->tsv->{handlerInternalCache} ) )
|
2016-03-17 23:19:44 +01:00
|
|
|
{
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Get session $id from Handler internal cache");
|
2018-07-05 22:56:16 +02:00
|
|
|
return $class->data;
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# 2. Get the session from cache or backend
|
2018-07-05 22:56:16 +02:00
|
|
|
my $session = $req->data->{session} = (
|
2019-03-07 18:22:16 +01:00
|
|
|
Lemonldap::NG::Common::Session->new( {
|
|
|
|
storageModule => $class->tsv->{sessionStorageModule},
|
2016-03-17 23:19:44 +01:00
|
|
|
storageModuleOptions => $class->tsv->{sessionStorageOptions},
|
|
|
|
cacheModule => $class->tsv->{sessionCacheModule},
|
|
|
|
cacheModuleOptions => $class->tsv->{sessionCacheOptions},
|
|
|
|
id => $id,
|
|
|
|
kind => "SSO",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-07-30 22:48:03 +02:00
|
|
|
unless ( $session->error ) {
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2018-07-05 22:56:16 +02:00
|
|
|
$class->data( $session->data );
|
2018-10-22 22:28:37 +02:00
|
|
|
$class->logger->debug("Get session $id from Handler::Main::Run");
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2017-07-07 17:14:06 +02:00
|
|
|
# Verify that session is valid
|
2018-02-12 13:33:56 +01:00
|
|
|
$class->logger->error(
|
2019-03-07 18:22:16 +01:00
|
|
|
"_utime is not defined. This should not happen. Check if it is well transmitted to handler"
|
2018-02-12 13:33:56 +01:00
|
|
|
) unless $session->data->{_utime};
|
2018-10-22 22:40:20 +02:00
|
|
|
|
2018-10-23 21:35:44 +02:00
|
|
|
$class->logger->debug("Check session validity from Handler");
|
2019-03-07 18:22:16 +01:00
|
|
|
$class->logger->debug( "Session timeout -> " . $class->tsv->{timeout} );
|
2018-10-23 21:35:44 +02:00
|
|
|
$class->logger->debug( "Session timeoutActivity -> "
|
2019-03-07 18:22:16 +01:00
|
|
|
. $class->tsv->{timeoutActivity}
|
|
|
|
. "s" )
|
|
|
|
if ( $class->tsv->{timeoutActivity} );
|
2018-10-23 21:35:44 +02:00
|
|
|
$class->logger->debug(
|
|
|
|
"Session _utime -> " . $session->data->{_utime} );
|
|
|
|
$class->logger->debug( "now -> " . $now );
|
|
|
|
$class->logger->debug( "_lastSeen -> " . $session->data->{_lastSeen} )
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $session->data->{_lastSeen} );
|
2018-11-26 14:40:21 +01:00
|
|
|
my $delta = $now - $session->data->{_lastSeen}
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $session->data->{_lastSeen} );
|
2018-10-23 21:35:44 +02:00
|
|
|
$class->logger->debug( "now - _lastSeen = " . $delta )
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $session->data->{_lastSeen} );
|
2018-10-23 21:35:44 +02:00
|
|
|
$class->logger->debug( "Session timeoutActivityInterval -> "
|
2019-03-07 18:22:16 +01:00
|
|
|
. $class->tsv->{timeoutActivityInterval} )
|
|
|
|
if ( $class->tsv->{timeoutActivityInterval} );
|
2018-10-23 21:35:44 +02:00
|
|
|
my $ttl = $class->tsv->{timeout} - $now + $session->data->{_utime};
|
|
|
|
$class->logger->debug( "Session TTL = " . $ttl );
|
|
|
|
|
2019-03-07 18:22:16 +01:00
|
|
|
if (
|
|
|
|
$now - $session->data->{_utime} > $class->tsv->{timeout}
|
2017-07-07 17:14:06 +02:00
|
|
|
or ( $class->tsv->{timeoutActivity}
|
|
|
|
and $session->data->{_lastSeen}
|
2018-10-23 21:35:44 +02:00
|
|
|
and $delta > $class->tsv->{timeoutActivity} )
|
2019-03-07 18:22:16 +01:00
|
|
|
)
|
2017-07-07 17:14:06 +02:00
|
|
|
{
|
|
|
|
$class->logger->info("Session $id expired");
|
|
|
|
|
|
|
|
# Clean cached data
|
2018-07-05 22:56:16 +02:00
|
|
|
$class->data( {} );
|
2017-07-07 17:14:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-23 21:35:44 +02:00
|
|
|
# Update the session to notify activity, if necessary
|
2019-03-07 18:22:16 +01:00
|
|
|
if (
|
|
|
|
$class->tsv->{timeoutActivity}
|
|
|
|
and ( $now - $session->data->{_lastSeen} >
|
|
|
|
$class->tsv->{timeoutActivityInterval} )
|
|
|
|
)
|
2018-10-23 21:35:44 +02:00
|
|
|
{
|
|
|
|
$req->data->{session}->update( { '_lastSeen' => $now } );
|
|
|
|
$class->data( $session->data );
|
|
|
|
|
|
|
|
if ( $session->error ) {
|
|
|
|
$class->logger->error("Cannot update session $id");
|
|
|
|
$class->logger->error( $req->data->{session}->error );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$class->logger->debug("Update _lastSeen with $now");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 22:56:16 +02:00
|
|
|
$class->dataUpdate($now);
|
2016-07-30 22:48:03 +02:00
|
|
|
return $session->data;
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->info("Session $id can't be retrieved");
|
|
|
|
$class->logger->info( $session->error );
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:37:14 +01:00
|
|
|
## @cmethod private int _getPort(string s)
|
|
|
|
# Returns the port on which this vhost is accessed
|
|
|
|
# @param $s VHost name
|
|
|
|
# @return PORT
|
|
|
|
|
|
|
|
sub _getPort {
|
|
|
|
|
|
|
|
my ( $class, $req, $vhost ) = @_;
|
|
|
|
|
|
|
|
if ( defined $class->tsv->{port}->{$vhost}
|
|
|
|
and ( $class->tsv->{port}->{$vhost} > 0 ) )
|
|
|
|
{
|
|
|
|
return $class->tsv->{port}->{$vhost};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( defined $class->tsv->{port}->{_}
|
|
|
|
and ( $class->tsv->{port}->{_} > 0 ) )
|
|
|
|
{
|
|
|
|
return $class->tsv->{port}->{_};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $req->{env}->{SERVER_PORT};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
## @cmethod private boot _isHttps(string s)
|
|
|
|
# Returns whether this VHost should he accessed
|
|
|
|
# via HTTPS
|
|
|
|
# @param $s VHost name
|
|
|
|
# @return RUE if the vhost should be accessed over HTTPS
|
|
|
|
sub _isHttps {
|
|
|
|
|
|
|
|
my ( $class, $req, $vhost ) = @_;
|
|
|
|
|
|
|
|
if ( defined $class->tsv->{https}->{$vhost}
|
|
|
|
and ( $class->tsv->{https}->{$vhost} > -1 ) )
|
|
|
|
{
|
|
|
|
return $class->tsv->{https}->{$vhost};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ( defined $class->tsv->{https}->{_}
|
|
|
|
and ( $class->tsv->{https}->{_} > -1 ) )
|
|
|
|
{
|
|
|
|
return $class->tsv->{https}->{_};
|
|
|
|
}
|
|
|
|
else {
|
2019-02-05 11:21:28 +01:00
|
|
|
return ( ( uc( $req->{env}->{HTTPS} ) || "OFF" ) eq "ON" );
|
2019-01-25 11:37:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
## @cmethod private string _buildUrl(string s)
|
|
|
|
# Transform /<s> into http(s?)://<host>:<port>/s
|
|
|
|
# @param $s path
|
|
|
|
# @return URL
|
|
|
|
sub _buildUrl {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $s ) = @_;
|
2019-01-25 11:37:14 +01:00
|
|
|
my $realvhost = $req->{env}->{HTTP_HOST};
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
|
|
|
my $_https = $class->_isHttps( $req, $vhost );
|
|
|
|
my $portString = $class->_getPort( $req, $vhost );
|
2016-03-17 23:19:44 +01:00
|
|
|
$portString = (
|
2019-03-07 18:22:16 +01:00
|
|
|
( $realvhost =~ /:\d+/ )
|
|
|
|
or ( $_https && $portString == 443 )
|
|
|
|
or ( !$_https && $portString == 80 )
|
2016-03-17 23:19:44 +01:00
|
|
|
) ? '' : ":$portString";
|
2018-10-05 11:03:58 +02:00
|
|
|
my $url = "http" . ( $_https ? "s" : "" ) . "://$realvhost$portString$s";
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Build URL $url");
|
2016-03-17 23:19:44 +01:00
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected int isUnprotected()
|
|
|
|
# @param $uri URI
|
|
|
|
# @return 0 if URI is protected,
|
|
|
|
# $class->UNPROTECT if it is unprotected by "unprotect",
|
2018-07-04 22:49:51 +02:00
|
|
|
# SKIP if unprotected by "skip"
|
2016-03-17 23:19:44 +01:00
|
|
|
sub isUnprotected {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $uri ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2017-02-22 07:35:19 +01:00
|
|
|
for (
|
2019-03-07 18:22:16 +01:00
|
|
|
my $i = 0 ;
|
|
|
|
$i < ( $class->tsv->{locationCount}->{$vhost} || 0 ) ;
|
2017-02-22 07:35:19 +01:00
|
|
|
$i++
|
2019-03-07 18:22:16 +01:00
|
|
|
)
|
2017-02-22 07:35:19 +01:00
|
|
|
{
|
2016-03-17 23:19:44 +01:00
|
|
|
if ( $uri =~ $class->tsv->{locationRegexp}->{$vhost}->[$i] ) {
|
|
|
|
return $class->tsv->{locationProtection}->{$vhost}->[$i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $class->tsv->{defaultProtection}->{$vhost};
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod void sendHeaders()
|
2019-04-09 14:36:20 +02:00
|
|
|
# Launch function compiled by headersInit() for the current virtual host
|
2016-03-17 23:19:44 +01:00
|
|
|
sub sendHeaders {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $session ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2016-04-28 07:59:34 +02:00
|
|
|
if ( defined $class->tsv->{forgeHeaders}->{$vhost} ) {
|
2016-04-27 11:46:33 +02:00
|
|
|
|
|
|
|
# Log headers in debug mode
|
2019-03-07 18:22:16 +01:00
|
|
|
my %headers =
|
|
|
|
$class->tsv->{forgeHeaders}->{$vhost}->( $req, $session );
|
2016-04-28 07:52:46 +02:00
|
|
|
foreach my $h ( sort keys %headers ) {
|
|
|
|
if ( defined( my $v = $headers{$h} ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Send header $h with value $v");
|
2016-04-27 11:46:33 +02:00
|
|
|
}
|
|
|
|
else {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Send header $h with empty value");
|
2016-04-27 11:46:33 +02:00
|
|
|
}
|
|
|
|
}
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->set_header_in( $req, %headers );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 21:55:03 +01:00
|
|
|
## @rfunction array ref checkHeaders()
|
2019-04-09 14:36:20 +02:00
|
|
|
# Return computed headers by headersInit() for the current virtual host
|
2019-02-26 22:01:15 +01:00
|
|
|
# [ { key => 'header1', value => 'value1' }, { key => 'header2', value => 'value2' }, ...]
|
2019-02-24 22:07:55 +01:00
|
|
|
sub checkHeaders {
|
|
|
|
my ( $class, $req, $session ) = @_;
|
2019-02-26 21:55:03 +01:00
|
|
|
my $vhost = $class->resolveAlias($req);
|
2019-02-24 22:07:55 +01:00
|
|
|
my $array_headers = [];
|
|
|
|
if ( defined $class->tsv->{forgeHeaders}->{$vhost} ) {
|
|
|
|
|
|
|
|
# Create array of hashes with headers
|
2019-03-07 18:22:16 +01:00
|
|
|
my %headers =
|
|
|
|
$class->tsv->{forgeHeaders}->{$vhost}->( $req, $session );
|
2019-02-24 22:07:55 +01:00
|
|
|
foreach my $h ( sort keys %headers ) {
|
2019-02-26 22:01:15 +01:00
|
|
|
defined $headers{$h}
|
2019-03-07 18:22:16 +01:00
|
|
|
? push @$array_headers, { key => $h, value => $headers{$h} }
|
|
|
|
: push @$array_headers, { key => $h, value => '' };
|
2019-02-24 22:07:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $array_headers;
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:19:44 +01:00
|
|
|
## @rmethod void cleanHeaders()
|
|
|
|
# Unset HTTP headers, when sendHeaders is skipped
|
|
|
|
sub cleanHeaders {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
if ( defined( $class->tsv->{headerList}->{$vhost} ) ) {
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->unset_header_in( $req,
|
|
|
|
@{ $class->tsv->{headerList}->{$vhost} } );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod string resolveAlias
|
|
|
|
# returns vhost whose current hostname is an alias
|
|
|
|
sub resolveAlias {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req ) = @_;
|
2017-10-24 12:56:21 +02:00
|
|
|
my $vhost = ref $req ? $req->{env}->{HTTP_HOST} : $req;
|
2017-03-29 07:17:35 +02:00
|
|
|
|
|
|
|
$vhost =~ s/:\d+//;
|
2017-10-23 21:59:43 +02:00
|
|
|
return $class->tsv->{vhostAlias}->{$vhost}
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $class->tsv->{vhostAlias}->{$vhost} );
|
2017-10-23 21:59:43 +02:00
|
|
|
return $vhost if ( $class->tsv->{defaultCondition}->{$vhost} );
|
|
|
|
my $v = $vhost;
|
|
|
|
while ( $v =~ s/[\w\-]+/\*/ ) {
|
|
|
|
return $v if ( $class->tsv->{defaultCondition}->{$v} );
|
|
|
|
$v =~ s/^\*\.*//;
|
|
|
|
}
|
|
|
|
return $vhost;
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#__END__
|
|
|
|
|
|
|
|
## @rmethod int abort(string msg)
|
|
|
|
# Logs message and exit or redirect to the portal if "useRedirectOnError" is
|
|
|
|
# set to true.
|
|
|
|
# @param $msg Message to log
|
|
|
|
# @return Constant ($class->REDIRECT, $class->SERVER_ERROR)
|
|
|
|
sub abort {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $msg ) = @_;
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# If abort is called without a valid request, fall to die
|
|
|
|
eval {
|
2017-03-28 23:07:49 +02:00
|
|
|
my $uri = $req->{env}->{REQUEST_URI};
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->error($msg);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Redirect or die
|
|
|
|
if ( $class->tsv->{useRedirectOnError} ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Use redirect for error");
|
2017-03-28 23:07:49 +02:00
|
|
|
return $class->goToError( $req, $uri, 500 );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $class->SERVER_ERROR;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
die $msg if ($@);
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected void localUnlog()
|
|
|
|
# Delete current user from local cache entry.
|
|
|
|
sub localUnlog {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $id ) = @_;
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug('Local handler logout');
|
2017-03-28 23:07:49 +02:00
|
|
|
if ( $id //= $class->fetchId($req) ) {
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2018-07-05 22:56:16 +02:00
|
|
|
# Delete thread data
|
|
|
|
if ( $class->data->{_session_id}
|
|
|
|
and $id eq $class->data->{_session_id} )
|
2016-12-23 17:03:33 +01:00
|
|
|
{
|
2018-07-05 22:56:16 +02:00
|
|
|
$class->data( {} );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
2018-07-05 22:56:16 +02:00
|
|
|
delete $req->data->{session};
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
# Delete local cache
|
|
|
|
if ( $class->tsv->{refLocalStorage}
|
|
|
|
and $class->tsv->{refLocalStorage}->get($id) )
|
|
|
|
{
|
|
|
|
$class->tsv->{refLocalStorage}->remove($id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected postOutputFilter(string uri)
|
|
|
|
# Add a javascript to html page in order to fill html form with fake data
|
|
|
|
# @param uri URI to catch
|
|
|
|
sub postOutputFilter {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $session, $uri ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
if ( defined( $class->tsv->{outputPostData}->{$vhost}->{$uri} ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Filling a html form with fake data");
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->unset_header_in( $req, "Accept-Encoding" );
|
2019-03-07 18:22:16 +01:00
|
|
|
my %postdata =
|
|
|
|
$class->tsv->{outputPostData}->{$vhost}->{$uri}->( $req, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
my $formParams = $class->tsv->{postFormParams}->{$vhost}->{$uri};
|
2017-03-28 23:07:49 +02:00
|
|
|
my $js = $class->postJavascript( $req, \%postdata, $formParams );
|
|
|
|
$class->addToHtmlHead( $req, $js );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected postInputFilter(string uri)
|
2018-07-05 22:56:16 +02:00
|
|
|
# Replace request body with form data defined in configuration
|
2016-03-17 23:19:44 +01:00
|
|
|
# @param uri URI to catch
|
|
|
|
sub postInputFilter {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $session, $uri ) = @_;
|
|
|
|
my $vhost = $class->resolveAlias($req);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
if ( defined( $class->tsv->{inputPostData}->{$vhost}->{$uri} ) ) {
|
2017-02-15 07:41:50 +01:00
|
|
|
$class->logger->debug("Replacing fake data with real form data");
|
2016-03-17 23:19:44 +01:00
|
|
|
|
2019-03-07 18:22:16 +01:00
|
|
|
my %data =
|
|
|
|
$class->tsv->{inputPostData}->{$vhost}->{$uri}->( $req, $session );
|
2016-03-17 23:19:44 +01:00
|
|
|
foreach ( keys %data ) {
|
2019-03-25 14:23:27 +01:00
|
|
|
my $post_key = uri_escape($_);
|
|
|
|
my $post_value = uri_escape( $data{$_} );
|
|
|
|
delete $data{$_};
|
|
|
|
$data{$post_key} = $post_value;
|
|
|
|
$class->logger->debug("Send key $post_key with value $post_value");
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
2017-03-28 23:07:49 +02:00
|
|
|
$class->setPostParams( $req, \%data );
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
## @rmethod protected postJavascript(hashref data)
|
|
|
|
# build a javascript to fill a html form with fake data
|
|
|
|
# @param data hashref containing input => value
|
|
|
|
sub postJavascript {
|
2017-03-28 23:07:49 +02:00
|
|
|
my ( $class, $req, $data, $formParams ) = @_;
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
my $form = $formParams->{formSelector} || "form";
|
|
|
|
|
|
|
|
my $filler;
|
|
|
|
foreach my $name ( keys %$data ) {
|
|
|
|
use bytes;
|
|
|
|
my $value = "x" x bytes::length( $data->{$name} );
|
2019-03-07 18:22:16 +01:00
|
|
|
$filler .=
|
|
|
|
"form.find('input[name=\"$name\"], select[name=\"$name\"], textarea[name=\"$name\"]').val('$value')\n";
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 18:22:16 +01:00
|
|
|
my $submitter =
|
|
|
|
$formParams->{buttonSelector} eq "none" ? ""
|
|
|
|
: $formParams->{buttonSelector}
|
|
|
|
? "form.find('$formParams->{buttonSelector}').click();\n"
|
|
|
|
: "form.submit();\n";
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
my $jqueryUrl = $formParams->{jqueryUrl} || "";
|
2019-03-25 13:36:51 +01:00
|
|
|
$jqueryUrl =
|
|
|
|
&{ $class->tsv->{portal} } . "static/bwr/jquery/dist/jquery.min.js"
|
2019-03-07 18:22:16 +01:00
|
|
|
if ( $jqueryUrl eq "default" );
|
2016-03-17 23:19:44 +01:00
|
|
|
$jqueryUrl = "<script type='text/javascript' src='$jqueryUrl'></script>\n"
|
2019-03-07 18:22:16 +01:00
|
|
|
if ($jqueryUrl);
|
2016-03-17 23:19:44 +01:00
|
|
|
|
|
|
|
return
|
2019-03-07 18:22:16 +01:00
|
|
|
$jqueryUrl
|
|
|
|
. "<script type='text/javascript'>\n"
|
|
|
|
. "/* script added by Lemonldap::NG */\n"
|
|
|
|
. "jQuery(window).on('load', function() {\n"
|
|
|
|
. "var form = jQuery('$form');\n"
|
|
|
|
. "form.attr('autocomplete', 'off');\n"
|
|
|
|
. $filler
|
|
|
|
. $submitter . "})\n"
|
|
|
|
. "</script>\n";
|
2016-03-17 23:19:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|