lemonldap-ng/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/ServiceToken.pm

73 lines
2.3 KiB
Perl
Raw Normal View History

2017-03-03 07:29:50 +01:00
package Lemonldap::NG::Handler::Lib::ServiceToken;
use strict;
our $VERSION = '2.0.9';
2017-03-03 07:29:50 +01:00
sub fetchId {
my ( $class, $req ) = @_;
my $token = $req->{env}->{HTTP_X_LLNG_TOKEN};
return $class->Lemonldap::NG::Handler::Main::fetchId($req) unless ($token =~ /\w+/);
$class->logger->debug("Found token: $token");
2018-04-22 16:08:34 +02:00
# Decrypt token
2017-03-03 18:25:03 +01:00
my $s = $class->tsv->{cipher}->decrypt($token);
2018-04-22 16:08:34 +02:00
2019-06-12 22:43:16 +02:00
# Token format:
# time:_session_id:vhost1:vhost2:serviceHeader1=value1:serviceHeader2=value2,...
2017-03-03 18:25:03 +01:00
my ( $t, $_session_id, @vhosts ) = split /:/, $s;
$class->logger->debug("Found epoch: $t");
$class->logger->debug("Found _session_id: $_session_id");
2018-04-22 16:08:34 +02:00
# Looking for service headers
my $vhost = $class->resolveAlias($req);
2019-06-12 22:53:41 +02:00
my %serviceHeaders;
@vhosts = grep {
if (/^([\w\-]+)=(.+)$/) {
$serviceHeaders{$1} = $2;
2019-06-14 17:23:26 +02:00
$class->logger->debug("Found service header: $1 => $2");
2019-06-12 22:53:41 +02:00
0;
}
else { 1 }
} @vhosts;
2019-05-26 22:37:59 +02:00
# $_session_id and at least one vhost
2019-05-27 23:07:18 +02:00
unless ( @vhosts and $_session_id ) {
2018-04-22 16:08:34 +02:00
$class->userLogger->error('Bad service token');
$class->logger->debug(
@vhosts ? 'No _session_id found' : 'No VH found' );
2017-03-03 07:29:50 +01:00
return 0;
}
2018-04-22 16:08:34 +02:00
# Is vhost listed in token ?
unless ( grep { $_ eq $vhost } @vhosts ) {
2017-03-04 13:24:56 +01:00
$class->userLogger->error(
"$vhost not authorized in token (" . join( ', ', @vhosts ) . ')' );
2017-03-04 13:24:56 +01:00
return 0;
}
$class->logger->debug( 'Found VHosts: ' . join ', ', @vhosts );
2019-05-26 21:43:13 +02:00
# Is token in good interval ?
2020-02-20 23:34:02 +01:00
my $ttl =
$class->localConfig->{vhostOptions}->{$vhost}->{vhostServiceTokenTTL}
|| $class->tsv->{serviceTokenTTL}->{$vhost};
$ttl = $class->tsv->{handlerServiceTokenTTL} unless ( $ttl and $ttl > 0 );
my $now = time;
unless ( $t <= $now and $t > $now - $ttl ) {
2019-05-26 21:43:13 +02:00
$class->userLogger->warn('Expired service token');
$class->logger->debug("VH: $vhost with ServiceTokenTTL: $ttl");
$class->logger->debug("TokenTime: $t / Time: $now");
2019-05-26 21:43:13 +02:00
return 0;
}
# Send service headers to protected application if exist
if (%serviceHeaders) {
2019-11-25 22:55:06 +01:00
$class->logger->info("Append service header(s)...");
$class->set_header_in( $req, %serviceHeaders );
}
2017-03-03 07:29:50 +01:00
return $_session_id;
}
1;