2016-03-23 22:55:47 +01:00
|
|
|
package Lemonldap::NG::Handler::PSGI::Try;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
|
2017-02-28 21:53:19 +01:00
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
2016-03-23 22:55:47 +01:00
|
|
|
extends 'Lemonldap::NG::Handler::PSGI::Router';
|
|
|
|
|
|
|
|
has 'authRoutes' => (
|
|
|
|
is => 'rw',
|
|
|
|
isa => 'HashRef',
|
|
|
|
default => sub { { GET => {}, POST => {}, PUT => {}, DELETE => {} } }
|
|
|
|
);
|
|
|
|
|
|
|
|
has 'unAuthRoutes' => (
|
|
|
|
is => 'rw',
|
|
|
|
isa => 'HashRef',
|
|
|
|
default => sub { { GET => {}, POST => {}, PUT => {}, DELETE => {} } }
|
|
|
|
);
|
|
|
|
|
|
|
|
sub addRoute {
|
|
|
|
die;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub addAuthRoute {
|
|
|
|
my $self = shift;
|
|
|
|
$self->routes( $self->authRoutes );
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('Declaring auth route');
|
2016-03-23 22:55:47 +01:00
|
|
|
return $self->SUPER::addRoute(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub addUnauthRoute {
|
|
|
|
my $self = shift;
|
|
|
|
$self->routes( $self->unAuthRoutes );
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug('Declaring unauth route');
|
2016-03-23 22:55:47 +01:00
|
|
|
return $self->SUPER::addRoute(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub defaultAuthRoute {
|
|
|
|
my $self = shift;
|
|
|
|
$self->routes( $self->authRoutes );
|
|
|
|
return $self->SUPER::defaultRoute(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub defaultUnauthRoute {
|
|
|
|
my $self = shift;
|
|
|
|
$self->routes( $self->unAuthRoutes );
|
|
|
|
return $self->SUPER::defaultRoute(@_);
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _run {
|
|
|
|
my $self = shift;
|
|
|
|
|
|
|
|
return sub {
|
|
|
|
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
|
2016-04-05 13:12:53 +02:00
|
|
|
my $res = $self->_authAndTrace( $req, 1 );
|
|
|
|
if ( $res->[0] < 300 ) {
|
2016-03-23 22:55:47 +01:00
|
|
|
$self->routes( $self->authRoutes );
|
2018-07-05 22:56:16 +02:00
|
|
|
$req->userData( $self->api->data );
|
2016-03-23 22:55:47 +01:00
|
|
|
}
|
2019-02-25 23:48:20 +01:00
|
|
|
elsif ( $res->[0] != 403 ) {
|
2019-03-07 18:22:16 +01:00
|
|
|
|
2016-04-04 22:39:22 +02:00
|
|
|
# Unset headers (handler adds a Location header)
|
2017-02-15 07:41:50 +01:00
|
|
|
$self->logger->debug(
|
|
|
|
"User not authenticated, Try in use, cancel redirection");
|
2017-01-04 17:36:54 +01:00
|
|
|
$req->userData( {} );
|
2016-04-04 22:39:22 +02:00
|
|
|
$req->respHeaders( [] );
|
2016-03-23 22:55:47 +01:00
|
|
|
$self->routes( $self->unAuthRoutes );
|
|
|
|
}
|
2019-02-25 23:48:20 +01:00
|
|
|
else {
|
|
|
|
return $res;
|
|
|
|
}
|
2018-05-22 12:37:05 +02:00
|
|
|
$res = $self->handler($req);
|
|
|
|
|
|
|
|
# Insert respHeaders in response only if not already set
|
|
|
|
my %hdr1 = @{ $res->[1] };
|
|
|
|
my %hdr2 = @{ $req->{respHeaders} };
|
|
|
|
foreach ( keys %hdr2 ) {
|
|
|
|
unless ( $hdr1{$_} and $hdr2{$_} eq $hdr1{$_} ) {
|
|
|
|
push @{ $res->[1] }, ( $_ => $hdr2{$_} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $res;
|
2016-03-23 22:55:47 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
__END__
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
=encoding utf8
|
|
|
|
|
|
|
|
Lemonldap::NG::Handler::PSGI::Try - Special handler for Lemonldap::NG Portal
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
package My::PSGI;
|
|
|
|
|
|
|
|
use base Lemonldap::NG::Handler::PSGI::Try;
|
|
|
|
|
|
|
|
sub init {
|
|
|
|
my ($self,$args) = @_;
|
|
|
|
|
|
|
|
# Declare REST routes for authenticated users (could be HTML templates or
|
|
|
|
# methods)
|
|
|
|
$self->addAuthRoute ( 'index.html', undef, ['GET'] )
|
|
|
|
->addAuthRoute ( books => { ':book' => 'booksMethod' }, ['GET', 'POST'] );
|
|
|
|
|
|
|
|
# Default route (ie: PATH_INFO == '/')
|
|
|
|
$self->defaultAuthRoute('index.html');
|
|
|
|
|
|
|
|
# Same for unauthenticated users
|
|
|
|
$self->addUnauthRoute ( 'login.html', undef, ['GET'] )
|
|
|
|
->addUnauthRoute ( 'login', undef, ['POST'] );
|
|
|
|
$self->defaultUnauthRoute('login.html');
|
|
|
|
|
|
|
|
# Return a boolean. If false, then error message has to be stored in
|
|
|
|
# $self->error
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub booksMethod {
|
|
|
|
my ( $self, $req, @otherPathInfo ) = @_;
|
|
|
|
|
2018-08-13 14:00:21 +02:00
|
|
|
# Will be called only if authorized
|
2016-03-23 22:55:47 +01:00
|
|
|
my $userId = $self->userId;
|
|
|
|
my $book = $req->params('book');
|
|
|
|
my $method = $req->method;
|
|
|
|
...
|
|
|
|
$self->sendJSONresponse(...);
|
|
|
|
}
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
Lemonldap::NG::Handler::PSGI::Try is a L<Lemonldap::NG::Handler::PSGI::Router>
|
2018-08-13 14:00:21 +02:00
|
|
|
package that provides 2 REST routers: one for authenticated users and one for
|
2016-03-23 22:55:47 +01:00
|
|
|
unauthenticated users.
|
|
|
|
|
|
|
|
=head1 METHODS
|
|
|
|
|
|
|
|
Same as L<Lemonldap::NG::Handler::PSGI::Router> (inherits from
|
|
|
|
L<Lemonldap::NG::Common::PSGI::Router>) except that:
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
|
|
|
=item addRoute() must be replaced by addAuthRoute() or addUnauthRoute()
|
|
|
|
|
|
|
|
=item defaultRoute() must be replaced by defaultAuthRoute() or defaultUnauthRoute()
|
|
|
|
|
|
|
|
=back
|
|
|
|
|
2018-08-13 14:00:21 +02:00
|
|
|
Note also that user session datas are available in $req parameter (first argument
|
2016-03-23 22:55:47 +01:00
|
|
|
received by REST methods):
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
2018-07-05 22:56:16 +02:00
|
|
|
=item $req->userData() returns a hash reference containing user session data
|
2016-03-23 22:55:47 +01:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 SEE ALSO
|
|
|
|
|
2017-01-04 21:31:36 +01:00
|
|
|
See L<Lemonldap::NG::Common::PSGI::Router> for more.
|
2016-03-23 22:55:47 +01:00
|
|
|
|
|
|
|
=head1 AUTHORS
|
|
|
|
|
|
|
|
=over
|
|
|
|
|
2017-01-04 21:31:36 +01:00
|
|
|
=item LemonLDAP::NG team L<http://lemonldap-ng.org/team>
|
2016-03-23 22:55:47 +01:00
|
|
|
|
|
|
|
=back
|
|
|
|
|
|
|
|
=head1 BUG REPORT
|
|
|
|
|
|
|
|
Use OW2 system to report bug or ask for features:
|
2017-11-11 14:06:23 +01:00
|
|
|
L<https://gitlab.ow2.org/lemonldap-ng/lemonldap-ng/issues>
|
2016-03-23 22:55:47 +01:00
|
|
|
|
|
|
|
=head1 DOWNLOAD
|
|
|
|
|
|
|
|
Lemonldap::NG is available at
|
|
|
|
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
|
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE
|
|
|
|
|
2017-01-04 21:31:36 +01:00
|
|
|
See COPYING file for details.
|
2016-03-23 22:55:47 +01:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see L<http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
=cut
|