Commit fa0ae39f authored by Xavier Guimard's avatar Xavier Guimard

Add special handler to be used by PSGI portal (#595)

parent 6865ee99
......@@ -51,7 +51,7 @@ has 'psgi.errors' => ( is => 'rw', reader => 'stderr' );
# Authentication
has REMOTE_USER => (
is => 'ro',
is => 'rw',
reader => 'user',
trigger => sub {
$_[0]->{userData} = { _whatToTrace => $_[0]->{REMOTE_USER}, };
......
......@@ -24,6 +24,7 @@ lib/Lemonldap/NG/Handler/PSGI/API/Server.pm
lib/Lemonldap/NG/Handler/PSGI/Base.pm
lib/Lemonldap/NG/Handler/PSGI/Router.pm
lib/Lemonldap/NG/Handler/PSGI/Server.pm
lib/Lemonldap/NG/Handler/PSGI/Try.pm
lib/Lemonldap/NG/Handler/SecureToken.pm
lib/Lemonldap/NG/Handler/SharedConf.pm
lib/Lemonldap/NG/Handler/Status.pm
......@@ -44,6 +45,7 @@ t/52-Lemonldap-NG-Handler-AuthBasic.t
t/60-Lemonldap-NG-Handler-PSGI.t
t/61-Lemonldap-NG-Handler-PSGI-Server.t
t/62-Lemonldap-NG-Handler-Nginx.t
t/63-Lemonldap-NG-Handler-PSGI-Try.t
t/99-pod.t
t/lmConf-1.js
t/sessions/f5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545
......
package Lemonldap::NG::Handler::PSGI::Try;
use strict;
use Mouse;
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 );
return $self->SUPER::addRoute(@_);
}
sub addUnauthRoute {
my $self = shift;
$self->routes( $self->unAuthRoutes );
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;
$self->rule(1);
return sub {
my $req = Lemonldap::NG::Common::PSGI::Request->new( $_[0] );
my $res = $self->_authAndTrace($req);
if ( $res->[0] == 200 ) {
$self->routes( $self->authRoutes );
$req->userData( $self->api->datas );
}
else {
$self->routes( $self->unAuthRoutes );
}
return $self->handler($req);
};
}
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 ) = @_;
# Will be called only if authorisated
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>
package that maintain 2 REST routers: one for authenticated users and one for
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
Note also that user session datas are available in $req parameter (first argument
received by REST methods):
=over
=item $req->user() returns "whatToTrace" uniq identifier
=item $req->userData() returns a hash reference containing user session datas
=back
=head1 SEE ALSO
See L<Lemonldap::NG::Common::PSGI::Router> for more. You just have to re
=head1 AUTHORS
=over
=item Clement Oudot, E<lt>clem.oudot@gmail.comE<gt>
=item Xavier Guimard, E<lt>x.guimard@free.frE<gt>
=back
=head1 BUG REPORT
Use OW2 system to report bug or ask for features:
L<http://jira.ow2.org>
=head1 DOWNLOAD
Lemonldap::NG is available at
L<http://forge.objectweb.org/project/showfiles.php?group_id=274>
=head1 COPYRIGHT AND LICENSE
=over
=item Copyright (C) 2015-2016 by Xavier Guimard, E<lt>x.guimard@free.frE<gt>
=item Copyright (C) 2015-2016 by Clément Oudot, E<lt>clem.oudot@gmail.comE<gt>
=back
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
#!/usr/bin/env perl -I pl/lib
use Test::More;
use JSON;
use Data::Dumper;
use MIME::Base64;
require 't/test-psgi-lib.pm';
my $app;
use_ok('Lemonldap::NG::Handler::PSGI::Try');
ok( $app = module( Lemonldap::NG::Handler::PSGI::Try->new() ), 'New object' );
init();
ok(
$app->init(
{
configStorage => { type => 'File', dirName => 't' },
logLevel => 'warn',
cookieName => 'lemonldap',
securedCookie => 0,
https => 0,
}
),
'initialization'
);
ok( $app->addAuthRoute( test => sub { [ 200, [], ['Auth'] ] }, ['GET'] ),
'Set auth route' );
ok( $app->addUnauthRoute( test => sub { [ 200, [], ['Unauth'] ] }, ['GET'] ),
'Set auth route' );
count(4);
my $res;
# Unauth tests
ok( $res = $client->_get('/test'), 'Get response' );
ok( $res->[0] == 200, 'Response code is 200' )
or print "Expect 200, got $res->[0]\n";
ok( $res->[2]->[0] eq 'Unauth', 'Get unauth result' )
or print "Expect Unauth, got $res->[2]->[0]\n";
count(3);
# Auth tests
ok(
$res = $client->_get(
'/test',
undef,
undef,
'lemonldap=f5eec18ebb9bc96352595e2d8ce962e8ecf7af7c9a98cb9a43f9cd181cf4b545'
),
'Get response'
);
ok( $res->[0] == 200, 'Response code is 200' )
or print "Expect 200, got $res->[0]\n";
ok( $res->[2]->[0] eq 'Auth', 'Get auth result' )
or print "Expect Auth, got $res->[2]->[0]\n";
count(3);
done_testing( count() );
......@@ -12,8 +12,11 @@ $Data::Dumper::Deparse = 1;
my $module;
sub init {
$module = shift;
use_ok($module);
my $arg = shift;
if($arg) {
$module = $arg;
use_ok($module);
}
ok( $client = Lemonldap::NG::Handler::PSGI::Cli::Lib->new(),
'Client object' );
count(2);
......@@ -23,6 +26,13 @@ sub client {
return $client;
}
sub module {
if(my $arg = shift) {
$module = $arg;
}
return $module;
}
sub count {
my $c = shift;
$count += $c if ($c);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment