lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Issuer/Get.pm

129 lines
3.3 KiB
Perl
Raw Normal View History

2016-06-12 18:52:37 +02:00
package Lemonldap::NG::Portal::Issuer::Get;
use strict;
use Mouse;
2016-06-12 21:26:14 +02:00
use URI::Escape;
use Lemonldap::NG::Common::FormEncode;
2019-03-07 18:22:16 +01:00
use Lemonldap::NG::Portal::Main::Constants
2021-03-02 08:46:59 +01:00
qw(PE_OK PE_BADURL PE_GET_SERVICE_NOT_ALLOWED URIRE);
2016-06-12 18:52:37 +02:00
2022-06-03 19:17:01 +02:00
our $VERSION = '2.0.15';
2016-06-12 18:52:37 +02:00
extends 'Lemonldap::NG::Portal::Main::Issuer';
2019-02-14 22:12:40 +01:00
has rule => ( is => 'rw' );
# INITIALIZATION
sub init {
my ($self) = @_;
# Parse activation rule
my $hd = $self->p->HANDLER;
2019-03-07 18:22:16 +01:00
$self->logger->debug( "GET rule -> " . $self->conf->{issuerDBGetRule} );
my $rule =
2019-03-07 18:22:16 +01:00
$hd->buildSub( $hd->substitute( $self->conf->{issuerDBGetRule} ) );
unless ($rule) {
my $error = $hd->tsv->{jail}->error || '???';
2020-05-24 00:04:33 +02:00
$self->error("Bad GET activation rule -> $error");
return 0;
}
$self->{rule} = $rule;
2022-06-03 19:17:01 +02:00
return 0 unless $self->SUPER::init();
return 1;
}
2016-06-12 18:52:37 +02:00
# RUNNING METHODS
sub run {
my ( $self, $req ) = @_;
# Check activation rule
unless ( $self->rule->( $req, $req->sessionInfo ) ) {
$self->userLogger->error('GET service not authorized');
return PE_GET_SERVICE_NOT_ALLOWED;
}
2016-06-12 18:52:37 +02:00
# Session ID
2019-12-31 17:14:44 +01:00
my $session_id = $req->{sessionInfo}->{_session_id} || $req->id;
2016-06-12 18:52:37 +02:00
# Session creation timestamp
2017-03-03 13:17:15 +01:00
my $time = $req->{sessionInfo}->{_utime} || time();
2016-06-12 21:26:14 +02:00
$req->path =~ m#^$self->{conf}->{issuerDBGetPath}/(log(?:in|out))#;
2016-06-12 18:52:37 +02:00
my $logInOut = $1 || 'login';
if ( $logInOut eq 'login' ) {
2017-02-15 07:41:50 +01:00
$self->logger->debug("IssuerGet: request for login");
2016-06-12 18:52:37 +02:00
$self->computeGetParams($req);
return PE_OK;
}
elsif ( $logInOut eq 'logout' ) {
2017-02-15 07:41:50 +01:00
$self->logger->debug("IssuerGet: request for logout");
2016-06-12 18:52:37 +02:00
# TODO
# Display a link to the provided URL
return PE_OK;
}
else {
2017-02-15 07:41:50 +01:00
$self->logger->error("IssuerGet: bad url");
2016-06-12 18:52:37 +02:00
return PE_BADURL;
}
}
# Nothing to do here for now
sub logout {
2022-06-03 19:17:01 +02:00
return PE_OK;
2016-06-12 18:52:37 +02:00
}
# INTERNAL METHODS
sub computeGetParams {
my ( $self, $req ) = @_;
# Additional GET variables
my %getPrms;
2016-06-12 18:52:37 +02:00
if ( exists $self->conf->{issuerDBGetParameters} ) {
2021-03-02 08:46:59 +01:00
unless ( $req->urldc =~ URIRE ) {
2017-02-15 07:41:50 +01:00
$self->logger->error("Malformed url $req->urldc");
2016-06-12 18:52:37 +02:00
return;
}
2021-03-02 08:46:59 +01:00
my $vhost = $3 . ( $4 ? ":$4" : '' );
2016-06-12 18:52:37 +02:00
my $prms = $self->conf->{issuerDBGetParameters}->{$vhost};
unless ($prms) {
2017-02-15 07:41:50 +01:00
$self->logger->warn("IssuerGet: $vhost has no configuration");
2016-06-12 18:52:37 +02:00
return '';
}
foreach my $param ( keys %$prms ) {
my $value = $req->{sessionInfo}->{ $prms->{$param} };
2016-06-12 18:52:37 +02:00
$value =~ s/[\r\n\t]//;
$getPrms{$param} = $value;
2016-06-12 18:52:37 +02:00
}
2020-06-18 18:01:33 +02:00
$self->userLogger->notice( 'User '
. $req->sessionInfo->{ $self->conf->{whatToTrace} }
. " is authorized to access to $vhost" );
2016-06-12 18:52:37 +02:00
}
else {
2017-02-15 07:41:50 +01:00
$self->logger->warn("IssuerGet: no configuration");
2016-06-12 18:52:37 +02:00
return;
}
my $getVars = build_urlencoded(%getPrms);
2016-06-12 18:52:37 +02:00
# If there are some GET variables to send
# Add them to URL string
if ( $getVars ne "" ) {
my $urldc = $req->urldc;
$urldc .= ( $urldc =~ /\?\w/ )
?
# there are already get variables
"&" . $getVars
:
# there are no get variables
"?" . $getVars;
$req->urldc($urldc);
}
}
1;