|
|
|
@ -2,6 +2,7 @@ package Lemonldap::NG::Portal::Lib::OneTimeToken; |
|
|
|
|
|
|
|
|
|
use strict; |
|
|
|
|
use Mouse; |
|
|
|
|
use JSON qw(from_json to_json); |
|
|
|
|
|
|
|
|
|
our $VERSION = '2.0.0'; |
|
|
|
|
|
|
|
|
@ -14,6 +15,30 @@ has timeout => ( |
|
|
|
|
} |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
has cache => ( |
|
|
|
|
is => 'rw', |
|
|
|
|
default => sub { |
|
|
|
|
my $c = $_[0]->{conf}; |
|
|
|
|
if ( !$c->{tokenUseGlobalStorage} ) { |
|
|
|
|
if ( $c->{localSessionStorage} ) { |
|
|
|
|
eval "use $c->{localSessionStorage}"; |
|
|
|
|
if ($@) { |
|
|
|
|
$_[0]->{p}->logger->error($@); |
|
|
|
|
return undef; |
|
|
|
|
} |
|
|
|
|
return $c->{localSessionStorage} |
|
|
|
|
->new( $c->{localSessionStorageOptions} ); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
$_[0]->{p}->logger->error( |
|
|
|
|
'Local storage not defined, token will be store in global storage' |
|
|
|
|
); |
|
|
|
|
return undef; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
sub createToken { |
|
|
|
|
my ( $self, $infos ) = @_; |
|
|
|
|
|
|
|
|
@ -33,10 +58,20 @@ sub createToken { |
|
|
|
|
# Store type |
|
|
|
|
$infos->{_type} ||= "token"; |
|
|
|
|
|
|
|
|
|
if ( $self->cache ) { |
|
|
|
|
my $id = $infos->{_utime} . '_' . int( rand(10000) ); |
|
|
|
|
|
|
|
|
|
# Dereference $infos |
|
|
|
|
my %h = %$infos; |
|
|
|
|
$self->cache->set( $id, to_json( \%h ) ); |
|
|
|
|
return $id; |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
|
|
|
|
|
# Create a new session |
|
|
|
|
my $tsession = $self->p->getApacheSession( undef, info => $infos ); |
|
|
|
|
|
|
|
|
|
return $tsession->id; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub getToken { |
|
|
|
@ -46,6 +81,23 @@ sub getToken { |
|
|
|
|
return undef; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if ( $self->cache ) { |
|
|
|
|
my $data; |
|
|
|
|
my @t = split /_/, $id; |
|
|
|
|
if ( $t[0] > time ) { |
|
|
|
|
$self->logger->notice("Expired token $id"); |
|
|
|
|
$self->cache->remove($id); |
|
|
|
|
return undef; |
|
|
|
|
} |
|
|
|
|
unless ( $data = $self->cache->get($id) ) { |
|
|
|
|
$self->logger->notice("Bad (or expired) token $id"); |
|
|
|
|
return undef; |
|
|
|
|
} |
|
|
|
|
$self->cache->remove($id); |
|
|
|
|
return from_json($data); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
|
|
|
|
|
# Get token session |
|
|
|
|
my $tsession = $self->p->getApacheSession($id); |
|
|
|
|
unless ($tsession) { |
|
|
|
@ -55,6 +107,7 @@ sub getToken { |
|
|
|
|
my %h = %{ $tsession->{data} }; |
|
|
|
|
$tsession->remove; |
|
|
|
|
return \%h; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
sub setToken { |
|
|
|
|