2020-12-20 17:31:50 +01:00
|
|
|
package Lemonldap::NG::Portal::Plugins::FindUser;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(
|
|
|
|
PE_OK
|
2020-12-23 10:32:06 +01:00
|
|
|
PE_NOTOKEN
|
|
|
|
PE_TOKENEXPIRED
|
2020-12-20 17:31:50 +01:00
|
|
|
PE_FIRSTACCESS
|
|
|
|
);
|
|
|
|
|
|
|
|
our $VERSION = '2.0.11';
|
|
|
|
|
2020-12-23 10:32:06 +01:00
|
|
|
extends qw(
|
|
|
|
Lemonldap::NG::Portal::Main::Plugin
|
|
|
|
Lemonldap::NG::Portal::Lib::_tokenRule
|
|
|
|
);
|
2020-12-20 17:31:50 +01:00
|
|
|
|
|
|
|
# INITIALIZATION
|
2020-12-23 10:32:06 +01:00
|
|
|
has ott => (
|
|
|
|
is => 'rw',
|
|
|
|
lazy => 1,
|
|
|
|
default => sub {
|
|
|
|
my $ott =
|
|
|
|
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
|
|
|
|
$ott->timeout( $_[0]->{conf}->{formTimeout} );
|
|
|
|
return $ott;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-12-20 17:31:50 +01:00
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
2020-12-25 14:54:06 +01:00
|
|
|
( my $imp = grep /::Plugins::Impersonation$/, $self->p->enabledPlugins )
|
|
|
|
? $self->addUnauthRoute( finduser => 'provideUser', ['POST'] )
|
|
|
|
: $self->logger->warn('FindUser plugin enabled without Impersonation');
|
2020-12-26 14:30:27 +01:00
|
|
|
$self->logger->warn('FindUser plugin enabled without searching attribute')
|
|
|
|
unless keys %{ $self->conf->{findUserSearchingAttributes} };
|
2020-12-20 17:31:50 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
# RUNNING METHOD
|
|
|
|
sub provideUser {
|
|
|
|
my ( $self, $req ) = @_;
|
2020-12-23 10:32:06 +01:00
|
|
|
my $error;
|
|
|
|
|
|
|
|
# Check token
|
|
|
|
if ( $self->ottRule->( $req, {} ) ) {
|
|
|
|
my $token = $req->param('token');
|
|
|
|
unless ($token) {
|
2020-12-27 00:45:06 +01:00
|
|
|
$self->userLogger->warn('FindUser called without token');
|
2020-12-23 10:32:06 +01:00
|
|
|
$error = PE_NOTOKEN;
|
|
|
|
}
|
2020-12-26 13:42:30 +01:00
|
|
|
else {
|
|
|
|
unless ( $self->ott->getToken($token) ) {
|
|
|
|
$self->userLogger->warn(
|
|
|
|
'FindUser called with an expired/bad token');
|
|
|
|
$error = PE_TOKENEXPIRED;
|
|
|
|
}
|
2020-12-23 10:32:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($error) {
|
|
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
|
|
return $self->p->do( $req, [ sub { $error } ] );
|
|
|
|
}
|
|
|
|
|
2020-12-20 17:31:50 +01:00
|
|
|
$req->steps( ['findUser'] );
|
2020-12-27 00:45:06 +01:00
|
|
|
$req->data->{findUserChoice} = $self->conf->{authChoiceFindUser};
|
2020-12-23 10:32:06 +01:00
|
|
|
if ( $error = $self->p->process($req) ) {
|
2020-12-20 17:31:50 +01:00
|
|
|
$self->logger->debug("Process returned error: $error");
|
2020-12-27 00:45:06 +01:00
|
|
|
eval { $self->p->_authentication->setSecurity($req) };
|
|
|
|
return $self->p->do( $req, [ sub { $error } ] );
|
2020-12-20 17:31:50 +01:00
|
|
|
}
|
2020-12-27 18:03:08 +01:00
|
|
|
|
2020-12-22 22:51:37 +01:00
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req,
|
|
|
|
{
|
2020-12-27 00:45:06 +01:00
|
|
|
user => ( $req->data->{findUser} ? $req->data->{findUser} : '' ),
|
2020-12-22 22:51:37 +01:00
|
|
|
result => 1
|
|
|
|
}
|
|
|
|
) if $req->wantJSON;
|
2020-12-20 17:31:50 +01:00
|
|
|
return $self->p->do( $req, [ sub { PE_FIRSTACCESS } ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub retreiveFindUserParams {
|
2020-12-22 22:51:37 +01:00
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my ( $searching, $excluding ) = ( [], [] );
|
2020-12-20 17:31:50 +01:00
|
|
|
$self->logger->debug("FindUser: reading parameters...");
|
|
|
|
foreach ( sort keys %{ $self->conf->{findUserSearchingAttributes} } ) {
|
2020-12-22 22:51:37 +01:00
|
|
|
if ( $req->params($_) ) {
|
|
|
|
$self->logger->debug(
|
|
|
|
"Pushing searching parameter: $_ => " . $req->params($_) );
|
|
|
|
push @$searching, { key => $_, value => $req->params($_) };
|
|
|
|
}
|
2020-12-20 23:44:26 +01:00
|
|
|
}
|
2020-12-22 22:51:37 +01:00
|
|
|
|
2020-12-23 22:38:58 +01:00
|
|
|
if ( scalar @$searching
|
|
|
|
&& keys %{ $self->conf->{findUserExcludingAttributes} } )
|
|
|
|
{
|
2020-12-22 22:51:37 +01:00
|
|
|
$self->logger->debug("FindUser: reading excluding parameters...");
|
|
|
|
foreach ( sort keys %{ $self->conf->{findUserExcludingAttributes} } ) {
|
2020-12-25 14:54:06 +01:00
|
|
|
$self->logger->debug( "Pushing excluded parameter: $_ => "
|
|
|
|
. $self->conf->{findUserExcludingAttributes}->{$_} );
|
|
|
|
push @$excluding,
|
|
|
|
{
|
|
|
|
key => $_,
|
|
|
|
value => $self->conf->{findUserExcludingAttributes}->{$_}
|
|
|
|
};
|
2020-12-22 22:51:37 +01:00
|
|
|
}
|
2020-12-20 17:31:50 +01:00
|
|
|
}
|
2020-12-22 22:51:37 +01:00
|
|
|
|
|
|
|
return ( $searching, $excluding );
|
2020-12-20 17:31:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|