lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Main/Run.pm

121 lines
2.9 KiB
Perl
Raw Normal View History

2016-03-30 21:51:12 +02:00
##@class Lemonldap::NG::Portal::Main::Run
# Serve request part of Lemonldap::NG portal
#
# Methods:
# - handler(): verify that portal configuration is the same that the
# underlying handler configuration before launching
# Lemonldap::NG::Common::PSGI::Router::handler() (which parse
# routes)
2016-03-31 07:27:59 +02:00
#
# Entry points:
# - "/test": * authenticated() for already authenticated users
# + pleaseAuth() for others
2016-03-29 23:09:55 +02:00
package Lemonldap::NG::Portal::Main::Run;
use strict;
use Mouse;
2016-03-31 22:08:43 +02:00
use Lemonldap::NG::Portal::Main::Constants;
2016-03-30 21:51:15 +02:00
use Lemonldap::NG::Portal::Main::Request;
2016-03-29 23:09:55 +02:00
2016-03-30 21:51:12 +02:00
our $VERSION = '2.0.0';
sub handler {
2016-03-31 07:27:59 +02:00
my ( $self, $req ) = shift;
unless ($self->conf->{cfgNum}
and $self->conf->{cfgNum} eq HANDLER->lmConf->{cfgNum} )
{
$self->reloadConf();
2016-03-30 21:51:12 +02:00
}
2016-03-30 21:51:15 +02:00
bless $req, 'Lemonldap::NG::Portal::Main::Request';
2016-03-30 21:51:12 +02:00
return $self->SUPER::handler($req);
}
2016-03-31 07:27:59 +02:00
# CORE REST API
# Methods that handle /test
sub authenticated {
my ( $self, $req ) = @_;
return $self->sendJSONresponse( $req, { status => 1 } );
}
sub pleaseAuth {
my ( $self, $req ) = @_;
return $self->sendJSONresponse( $req, { status => 0 } );
}
# MAIN ENTRY POINTS
2016-03-31 22:08:43 +02:00
# List constants
sub authProcess { qw(extractFormInfo getUser authenticate) }
sub sessionDatas {
qw(setAuthSessionInfo setSessionInfo setMacros setGroups
setPersistentSessionInfo setLocalGroups grantSession store
buildCookie);
}
2016-03-31 07:27:59 +02:00
sub login {
my ( $self, $req ) = @_;
2016-03-31 22:08:43 +02:00
return $req->do($req,
[
'rememberArgs', @{ $self->beforeAuth },
&authProcess, @{ $self->betweenAuthAndDatas },
&sessionDatas, @{ $self->afterdatas },
]
2016-03-31 07:27:59 +02:00
);
}
sub postLogin {
my ( $self, $req ) = @_;
2016-03-31 22:08:43 +02:00
return $req->do($req,
[
'restoreArgs', @{ $self->beforeAuth },
&authProcess, @{ $self->betweenAuthAndDatas },
&sessionDatas, @{ $self->afterdatas },
]
2016-03-31 07:27:59 +02:00
);
}
sub authenticatedRequest {
2016-03-31 22:08:43 +02:00
my ( $self, $req ) = @_;
return $req->do($req, $self->forAuthUser );
}
sub do {
my ($self,$req,$steps) = @_;
$req->steps($steps);
my $err = $self->process($req);
# TODO: updateStatus
if ( !$self->conf->{noAjaxHook} and $req->wantJSON ) {
if ( $err > 0 ) {
return [ 401, ['WWW-Authenticate' => "SSO ".$self->conf->{portal},'Access-Control-Allow-Origin' => '*'],[]];
}
else {
return $self->senfJSONresponse({result=>1,message=>'Authenticated'});
}
}
else {
if($err) {
return $self->sendHtml($req,'login.tpl');
}
else {
return $self->autoRedirect($req);
}
}
}
sub process {
my ( $self, $req ) = @_;
#$req->error(PE_OK);
my $err = PE_OK;
while(my $sub = shift @{$req->steps}) {
last if($err = $self->$sub($req);
}
return $err;
2016-03-31 07:27:59 +02:00
}
2016-03-29 23:09:55 +02:00
# TODO in run
# - mustRedirect
1;