lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/CheckState.pm
2018-05-15 21:20:31 +02:00

67 lines
1.8 KiB
Perl

# Check state plugin
#
# test if portal is well loaded. If user/pasword parameters are set, it tests
# also login process
package Lemonldap::NG::Portal::Plugins::CheckState;
use strict;
use Mouse;
our $VERSION = '2.0.0';
extends 'Lemonldap::NG::Portal::Main::Plugin';
# INITIALIZATION
sub init {
my ($self) = @_;
unless ( $self->conf->{checkStateSecret} ) {
$self->logger->error(
'checkStateSecret is required for "check state" plugin');
return 0;
}
$self->addUnauthRoute( checkstate => 'check', ['GET'] );
return 1;
}
sub check {
my ( $self, $req ) = @_;
my @rep;
unless ($req->param('secret')
and $req->param('secret') eq $self->conf->{checkStateSecret} )
{
return $self->p->sendError( $req, 'Bad secret' );
}
$req->steps( [ 'controlUrl', @{ $self->p->beforeAuth } ] );
my $res = $self->p->process($req);
if ( $res > 0 ) {
push @rep, "Bad result before auth: $res";
}
if ( my $user = $req->param('user') and my $pwd = $req->param('password') )
{
# Note that "extractFormInfo" isn't launched due to "token"
$req->user($user);
$req->datas->{password} = $pwd;
$req->steps(
[
'getUser', 'authenticate',
@{ $self->p->betweenAuthAndDatas }, $self->p->sessionDatas,
@{ $self->p->afterDatas }
]
);
if ( $res = $self->p->process( $req, ) ) {
push @rep, "Bad result during auth: $res";
}
$self->p->deleteSession($req);
}
if (@rep) {
return $self->p->sendError( $req, join( ",\n", @rep ), 500 );
}
else {
return $self->p->sendJSONresponse( $req, { result => 1 } );
}
}
1;