lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/OneTimeToken.pm

67 lines
1.5 KiB
Perl
Raw Normal View History

2017-01-23 12:28:13 +01:00
package Lemonldap::NG::Portal::Lib::OneTimeToken;
use strict;
use Mouse;
2017-02-28 21:53:19 +01:00
our $VERSION = '2.0.0';
2017-01-23 12:28:13 +01:00
extends 'Lemonldap::NG::Common::Module';
has timeout => (
is => 'rw',
default => sub {
$_[0]->{conf}->{timeout};
}
);
sub createToken {
my ( $self, $infos ) = @_;
# Set _utime for session autoremove
# Use default session timeout and register session timeout to compute it
my $time = time();
# Set _utime to remove token after $self->timeout
$infos->{_utime} = $time + ( $self->timeout - $self->conf->{timeout} );
# Store expiration timestamp for further use
$infos->{tokenTimeoutTimestamp} = $time + $self->timeout;
# Store start timestamp for further use
$infos->{tokenSessionStartTimestamp} = $time;
# Store type
$infos->{_type} ||= "token";
# Create a new session
my $tsession = $self->p->getApacheSession( undef, info => $infos );
2017-01-23 12:28:13 +01:00
return $tsession->id;
}
sub getToken {
my ( $self, $id ) = @_;
unless ($id) {
2017-02-15 07:41:50 +01:00
$self->logger->error('getToken called without id');
2017-01-23 12:28:13 +01:00
return undef;
}
# Get token session
my $tsession = $self->p->getApacheSession($id);
unless ($tsession) {
2017-02-15 07:41:50 +01:00
$self->logger->notice("Bad (or expired) token $id");
2017-01-23 12:28:13 +01:00
return undef;
}
my %h = %{ $tsession->{data} };
$tsession->remove;
return \%h;
}
2017-01-26 22:42:42 +01:00
sub setToken {
2017-01-29 14:06:28 +01:00
my ( $self, $req, $info ) = @_;
2017-02-15 07:41:50 +01:00
$self->logger->debug('Prepare token');
2017-01-29 14:06:28 +01:00
$req->token( $self->createToken($info) );
2017-01-26 22:42:42 +01:00
}
2017-01-23 12:28:13 +01:00
1;