lemonldap-ng/lemonldap-ng-handler/lib/Lemonldap/NG/Handler/Lib/ServiceToken.pm
2019-05-27 12:03:45 +02:00

47 lines
1.2 KiB
Perl

package Lemonldap::NG::Handler::Lib::ServiceToken;
use strict;
use Data::Dumper;
our $VERSION = '2.1.0';
sub fetchId {
my ( $class, $req ) = @_;
my $token = $req->{env}->{HTTP_X_LLNG_TOKEN};
return $class->Lemonldap::NG::Handler::Main::fetchId($req) unless ($token);
$class->logger->debug('Found token header');
# Decrypt token
my $s = $class->tsv->{cipher}->decrypt($token);
# Token format:
# time:_session_id:vhost1:vhost2,...
my ( $t, $_session_id, @vhosts ) = split /:/, $s;
# At least one vhost
unless (@vhosts) {
$class->userLogger->error('Bad service token');
return 0;
}
# Is vhost listed in token ?
my $vh = $class->resolveAlias($req);
unless ( grep { $_ eq $vh } @vhosts ) {
$class->userLogger->error(
"$vh not authorized in token (" . join( ', ', @vhosts ) . ')' );
return 0;
}
# Is token in good interval ?
my $localConfig = $class->localConfig;
my $ttl = $localConfig->{vhostOptions}->{$vh}->{vhostServiceTokenTTL} || $class->tsv->{handlerServiceTokenTTL};
unless ( $t <= time and $t > time - $ttl ) {
$class->userLogger->warn('Expired service token');
return 0;
}
return $_session_id;
}
1;