2017-01-06 10:04:00 +01:00
|
|
|
package Lemonldap::NG::Common::Session::REST;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2017-01-08 09:42:19 +01:00
|
|
|
use Lemonldap::NG::Common::Conf::Constants;
|
|
|
|
|
2017-02-28 21:53:19 +01:00
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
2017-01-08 09:42:19 +01:00
|
|
|
has sessionTypes => ( is => 'rw' );
|
|
|
|
|
|
|
|
sub setTypes {
|
|
|
|
my ( $self, $conf ) = @_;
|
|
|
|
foreach my $type (@sessionTypes) {
|
|
|
|
if ( my $tmp =
|
|
|
|
$self->{ $type . 'Storage' } || $conf->{ $type . 'Storage' } )
|
|
|
|
{
|
|
|
|
$self->{sessionTypes}->{$type}->{module} = $tmp;
|
|
|
|
$self->{sessionTypes}->{$type}->{options} =
|
|
|
|
$self->{ $type . 'StorageOptions' }
|
|
|
|
|| $conf->{ $type . 'StorageOptions' }
|
|
|
|
|| {};
|
|
|
|
$self->{sessionTypes}->{$type}->{kind} =
|
|
|
|
( $type eq 'global' ? 'SSO' : ucfirst($type) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-06 10:04:00 +01:00
|
|
|
|
|
|
|
sub separator {
|
|
|
|
$_[0]->{multiValuesSeparator} || $_[0]->conf->{multiValuesSeparator};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub hAttr {
|
|
|
|
$_[0]->{hiddenAttributes} || $_[0]->conf->{hiddenAttributes};
|
|
|
|
}
|
|
|
|
|
|
|
|
sub delSession {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
return $self->sendJSONresponse( $req, { result => 1 } )
|
|
|
|
if ( $self->{demoMode} );
|
|
|
|
my $mod = $self->getMod($req)
|
|
|
|
or return $self->sendError( $req, undef, 400 );
|
|
|
|
my $id = $req->params('sessionId')
|
|
|
|
or return $self->sendError( $req, 'sessionId is missing', 400 );
|
|
|
|
my $session = $self->getApacheSession( $mod, $id );
|
|
|
|
$session->remove;
|
|
|
|
if ( $session->error ) {
|
|
|
|
return $self->sendError( $req, $session->error, 200 );
|
|
|
|
}
|
|
|
|
return $self->sendJSONresponse( $req, { result => 1 } );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub session {
|
|
|
|
my ( $self, $req, $id, $skey ) = @_;
|
|
|
|
my ( %h, $res );
|
2017-01-08 09:42:19 +01:00
|
|
|
return $self->sendError( $req, 'Bad request', 400 ) unless ($id);
|
2017-01-06 10:04:00 +01:00
|
|
|
my $mod = $self->getMod($req)
|
|
|
|
or return $self->sendError( $req, undef, 400 );
|
|
|
|
|
|
|
|
# Try to read session
|
|
|
|
my $apacheSession = $self->getApacheSession( $mod, $id )
|
|
|
|
or return $self->sendError( $req, undef, 400 );
|
|
|
|
|
|
|
|
my %session = %{ $apacheSession->data };
|
|
|
|
|
|
|
|
foreach my $k ( keys %session ) {
|
|
|
|
$session{$k} = '**********'
|
|
|
|
if ( $self->hAttr =~ /\b$k\b/ );
|
|
|
|
$session{$k} = [ split /$self->separator/o, $session{$k} ]
|
|
|
|
if ( $session{$k} =~ /$self->separator/o );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($skey) {
|
2017-01-10 22:43:34 +01:00
|
|
|
if ( $skey =~ s/^\[(.*)\]$/$1/ ) {
|
|
|
|
my @sk = split /,/, $skey;
|
|
|
|
my $res = {};
|
|
|
|
$res->{$_} = $session{$_} foreach (@sk);
|
|
|
|
return $self->sendJSONresponse( $req, $res );
|
|
|
|
}
|
2017-01-06 10:04:00 +01:00
|
|
|
return $self->sendJSONresponse( $req, $session{$skey} );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->sendJSONresponse( $req, \%session );
|
|
|
|
}
|
|
|
|
|
|
|
|
# TODO: check for utf-8 problems
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getApacheSession {
|
2017-02-27 20:30:43 +01:00
|
|
|
my ( $self, $mod, $id, $info, $force ) = @_;
|
2017-01-06 10:04:00 +01:00
|
|
|
my $apacheSession = Lemonldap::NG::Common::Session->new(
|
|
|
|
{
|
|
|
|
storageModule => $mod->{module},
|
|
|
|
storageModuleOptions => $mod->{options},
|
|
|
|
cacheModule =>
|
2017-02-11 08:47:22 +01:00
|
|
|
Lemonldap::NG::Handler::PSGI::Main->tsv->{sessionCacheModule},
|
2017-01-06 10:04:00 +01:00
|
|
|
cacheModuleOptions =>
|
2017-02-11 08:47:22 +01:00
|
|
|
Lemonldap::NG::Handler::PSGI::Main->tsv->{sessionCacheOptions},
|
2017-02-27 20:30:43 +01:00
|
|
|
id => $id,
|
|
|
|
force => $force,
|
|
|
|
kind => $mod->{kind},
|
2017-02-20 22:00:05 +01:00
|
|
|
( $info ? ( info => $info ) : () ),
|
2017-01-06 10:04:00 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
if ( $apacheSession->error ) {
|
|
|
|
$self->error( $apacheSession->error );
|
|
|
|
return undef;
|
|
|
|
}
|
|
|
|
return $apacheSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub getMod {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my ( $s, $m );
|
|
|
|
unless ( $s = $req->params('sessionType') ) {
|
|
|
|
$self->error('Session type is required');
|
|
|
|
return ();
|
|
|
|
}
|
2017-01-08 09:42:19 +01:00
|
|
|
unless ( $m = $self->sessionTypes->{$s} ) {
|
2017-01-06 10:04:00 +01:00
|
|
|
$self->error('Unknown (or unconfigured) session type');
|
|
|
|
return ();
|
|
|
|
}
|
|
|
|
return $m;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|