Use local cache by default for tokens (#1140)

environments/ppa-mbqj77/deployments/1
Xavier Guimard 8 years ago
parent 435e20491e
commit 81d3729394
  1. 4
      lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Attributes.pm
  2. 5
      lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Attributes.pm
  3. 1
      lemonldap-ng-manager/lib/Lemonldap/NG/Manager/Build/Tree.pm
  4. 1
      lemonldap-ng-manager/site/htdocs/static/languages/en.json
  5. 1
      lemonldap-ng-manager/site/htdocs/static/languages/fr.json
  6. 2
      lemonldap-ng-manager/site/htdocs/static/reverseTree.json
  7. 2
      lemonldap-ng-manager/site/htdocs/static/struct.json
  8. 55
      lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Lib/OneTimeToken.pm
  9. 5
      lemonldap-ng-portal/t/41-Captcha.t
  10. 5
      lemonldap-ng-portal/t/42-Register-Demo-with-captcha.t
  11. 5
      lemonldap-ng-portal/t/43-MailReset-with-captcha.t
  12. 26
      lemonldap-ng-portal/t/test-lib.pm

@ -2890,6 +2890,10 @@ qr/^(?:(?:(?:(?:(?:(?:[a-zA-Z0-9][-a-zA-Z0-9]*)?[a-zA-Z0-9])[.])*(?:[a-zA-Z][-a-
}, },
'type' => 'int' 'type' => 'int'
}, },
'tokenUseGlobalStorage' => {
'default' => 0,
'type' => 'bool'
},
'trustedDomains' => { 'trustedDomains' => {
'type' => 'text' 'type' => 'text'
}, },

@ -430,6 +430,11 @@ sub attributes {
type => 'bool', type => 'bool',
documentation => 'Enable token for forms', documentation => 'Enable token for forms',
}, },
tokenUseGlobalStorage => {
default => 0,
type => 'bool',
documentation => 'Enable global token storage',
},
cda => { cda => {
default => 0, default => 0,
type => 'bool', type => 'bool',

@ -638,6 +638,7 @@ sub tree {
}, },
'requireToken', 'requireToken',
'formTimeout', 'formTimeout',
'tokenUseGlobalStorage',
] ]
}, },
{ {

@ -646,6 +646,7 @@
"timeout": "Sessions timeout", "timeout": "Sessions timeout",
"timeoutActivity": "Sessions activity timeout", "timeoutActivity": "Sessions activity timeout",
"timeoutActivityInterval": "Sessions update interval", "timeoutActivityInterval": "Sessions update interval",
"tokenUseGlobalStorage": "Use global storage",
"trustedDomains": "Trusted domains", "trustedDomains": "Trusted domains",
"trustedProxies": "Trusted proxies IP", "trustedProxies": "Trusted proxies IP",
"twitterAppName": "Application name", "twitterAppName": "Application name",

@ -646,6 +646,7 @@
"timeout": "Durée de vie maximale des sessions", "timeout": "Durée de vie maximale des sessions",
"timeoutActivity": "Délai d'expiration des sessions", "timeoutActivity": "Délai d'expiration des sessions",
"timeoutActivityInterval": "Intervalle de mise à jour des sessions", "timeoutActivityInterval": "Intervalle de mise à jour des sessions",
"tokenUseGlobalStorage": "Utiliser le cache global",
"trustedDomains": "Domaines approuvés", "trustedDomains": "Domaines approuvés",
"trustedProxies": "IP des proxys de confiance", "trustedProxies": "IP des proxys de confiance",
"twitterAppName": "Nom de l'application", "twitterAppName": "Nom de l'application",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -2,6 +2,7 @@ package Lemonldap::NG::Portal::Lib::OneTimeToken;
use strict; use strict;
use Mouse; use Mouse;
use JSON qw(from_json to_json);
our $VERSION = '2.0.0'; 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 { sub createToken {
my ( $self, $infos ) = @_; my ( $self, $infos ) = @_;
@ -33,10 +58,20 @@ sub createToken {
# Store type # Store type
$infos->{_type} ||= "token"; $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 # Create a new session
my $tsession = $self->p->getApacheSession( undef, info => $infos ); my $tsession = $self->p->getApacheSession( undef, info => $infos );
return $tsession->id; return $tsession->id;
}
} }
sub getToken { sub getToken {
@ -46,6 +81,23 @@ sub getToken {
return undef; 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 # Get token session
my $tsession = $self->p->getApacheSession($id); my $tsession = $self->p->getApacheSession($id);
unless ($tsession) { unless ($tsession) {
@ -55,6 +107,7 @@ sub getToken {
my %h = %{ $tsession->{data} }; my %h = %{ $tsession->{data} };
$tsession->remove; $tsession->remove;
return \%h; return \%h;
}
} }
sub setToken { sub setToken {

@ -39,8 +39,9 @@ SKIP: {
# Try to get captcha value # Try to get captcha value
my ( $ts, $captcha ); my ( $ts, $captcha );
ok( $ts = $client->p->getApacheSession($token), ' Found token session' ); ok( $ts = getCache()->get($token), ' Found token session' );
ok( $captcha = $ts->data->{captcha}, ' Found captcha value' ); $ts = eval { JSON::from_json($ts) };
ok( $captcha = $ts->{captcha}, ' Found captcha value' );
# Try to authenticate # Try to authenticate
$query .= "&user=dwho&password=dwho&captcha=$captcha"; $query .= "&user=dwho&password=dwho&captcha=$captcha";

@ -49,8 +49,9 @@ s/^.*token=([^&]+).*$/token=$1&firstname=foo&lastname=bar&mail=foobar%40badwolf.
# Try to get captcha value # Try to get captcha value
my ( $ts, $captcha ); my ( $ts, $captcha );
ok( $ts = $client->p->getApacheSession($token), ' Found token session' ); ok( $ts = getCache()->get($token), ' Found token session' );
ok( $captcha = $ts->data->{captcha}, ' Found captcha value' ); $ts = eval { JSON::from_json($ts) };
ok( $captcha = $ts->{captcha}, ' Found captcha value' );
$query .= "&captcha=$captcha"; $query .= "&captcha=$captcha";

@ -51,8 +51,9 @@ SKIP: {
# Try to get captcha value # Try to get captcha value
my ( $ts, $captcha ); my ( $ts, $captcha );
ok( $ts = $client->p->getApacheSession($token), ' Found token session' ); ok( $ts = getCache()->get($token), ' Found token session' );
ok( $captcha = $ts->data->{captcha}, ' Found captcha value' ); $ts = eval { JSON::from_json($ts) };
ok( $captcha = $ts->{captcha}, ' Found captcha value' );
$query .= "&captcha=$captcha"; $query .= "&captcha=$captcha";

@ -42,6 +42,18 @@ sub clean_sessions {
} }
} }
} }
my $cache = getCache();
$cache->clear;
}
sub getCache {
return Cache::FileCache->new(
{
namespace => 'lemonldap-ng-session',
cache_root => 't/',
cache_depth => 0,
}
);
} }
sub expectRedirection { sub expectRedirection {
@ -211,8 +223,16 @@ use Mouse;
extends 'Lemonldap::NG::Common::PSGI::Cli::Lib'; extends 'Lemonldap::NG::Common::PSGI::Cli::Lib';
our $defaultIni = { our $defaultIni = {
configStorage => { type => 'File', dirName => 't' }, configStorage => {
localSessionStorage => '', type => 'File',
dirName => 't',
},
localSessionStorage => 'Cache::FileCache',
localSessionStorageOptions => {
namespace => 'lemonldap-ng-session',
cache_root => 't/',
cache_depth => 0,
},
logLevel => 'error', logLevel => 'error',
cookieName => 'lemonldap', cookieName => 'lemonldap',
domain => 'example.com', domain => 'example.com',
@ -273,7 +293,7 @@ sub logout {
'Logout request' 'Logout request'
); );
main::ok( $res->[0] == 200, ' Response is 200' ) main::ok( $res->[0] == 200, ' Response is 200' )
or explain( $res->[0], 200 ); or main::explain( $res->[0], 200 );
my $c; my $c;
main::ok( main::ok(
( defined( $c = main::getCookies($res)->{lemonldap} ) and not $c ), ( defined( $c = main::getCookies($res)->{lemonldap} ) and not $c ),

Loading…
Cancel
Save