2009-03-08 09:50:58 +01:00
|
|
|
## @file
|
|
|
|
# Authentication and UserDB chaining mechanism
|
|
|
|
|
|
|
|
## @class
|
|
|
|
# Authentication and UserDB chaining mechanism.
|
|
|
|
# To use it set your authentication module like this :
|
|
|
|
# authentication => 'Multi CAS;LDAP'
|
|
|
|
#
|
|
|
|
# If CAS failed, LDAP will be used. You can also add a condition. Example:
|
|
|
|
# authentication => 'Multi Remote $ENV{REMOTE_ADDR}=~/^192/;LDAP $ENV{REMOTE_ADDR}!~/^192/'
|
|
|
|
package Lemonldap::NG::Portal::_Multi;
|
|
|
|
|
|
|
|
use Lemonldap::NG::Portal::Simple;
|
2014-06-05 20:05:55 +02:00
|
|
|
use Scalar::Util 'weaken';
|
2009-03-08 09:50:58 +01:00
|
|
|
|
2015-09-24 08:58:21 +02:00
|
|
|
our $VERSION = '1.4.6';
|
2009-06-08 18:29:13 +02:00
|
|
|
|
2009-03-08 09:50:58 +01:00
|
|
|
## @cmethod Lemonldap::NG::Portal::_Multi new(Lemonldap::NG::Portal::Simple portal)
|
|
|
|
# Constructor
|
|
|
|
# @param $portal Lemonldap::NG::Portal::Simple object
|
|
|
|
# @return new Lemonldap::NG::Portal::_Multi object
|
|
|
|
sub new {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $class, $portal ) = @_;
|
2009-03-08 09:50:58 +01:00
|
|
|
my $self = bless { p => $portal, res => PE_NOSCHEME }, $class;
|
2014-06-05 20:05:55 +02:00
|
|
|
weaken $self->{p};
|
2011-05-16 15:12:44 +02:00
|
|
|
|
|
|
|
# Browse authentication and userDB configuration
|
2015-12-10 21:05:29 +01:00
|
|
|
my @stack = ( $portal->{multiAuthStack}, $portal->{multiUserDBStack} );
|
2009-03-08 09:50:58 +01:00
|
|
|
for ( my $i = 0 ; $i < 2 ; $i++ ) {
|
|
|
|
$stack[$i] =~ s/^Multi\s*//;
|
|
|
|
foreach my $l ( split /;/, $stack[$i] ) {
|
2015-03-10 16:07:33 +01:00
|
|
|
$l =~ s/^\s+//; # Remove first space
|
2009-03-08 09:50:58 +01:00
|
|
|
$l =~ /^([\w#]+)(?:\s+(.*))?$/
|
|
|
|
or $portal->abort( 'Bad configuration', "Unable to read $l" );
|
|
|
|
my ( $mod, $cond ) = ( $1, $2 );
|
|
|
|
my $name = $mod;
|
|
|
|
$mod =~ s/#(.*)$//;
|
2015-09-24 08:58:21 +02:00
|
|
|
my $shortname = $mod;
|
2009-03-08 09:50:58 +01:00
|
|
|
$cond = 1 unless ( defined $cond );
|
|
|
|
$mod = "Lemonldap::NG::Portal::" . [ 'Auth', 'UserDB' ]->[$i] . $mod
|
|
|
|
unless ( $mod =~ /::/ );
|
2010-01-28 15:47:51 +01:00
|
|
|
|
|
|
|
$portal->abort( 'Bad configuration', "Unable to load $mod" )
|
|
|
|
unless $self->{p}->loadModule($mod);
|
2009-03-08 09:50:58 +01:00
|
|
|
push @{ $self->{stack}->[$i] },
|
2015-09-24 08:58:21 +02:00
|
|
|
{ m => $mod, c => $cond, n => $name, s => $shortname };
|
2009-03-08 09:50:58 +01:00
|
|
|
}
|
2011-05-16 15:12:44 +02:00
|
|
|
|
|
|
|
# Override portal settings
|
|
|
|
%{ $self->{p} } = (
|
|
|
|
%{ $self->{p} },
|
|
|
|
%{ $self->{p}->{multi}->{ $self->{stack}->[$i]->[0]->{n} } }
|
|
|
|
) if ( $self->{p}->{multi}->{ $self->{stack}->[$i]->[0]->{n} } );
|
|
|
|
|
2009-03-08 09:50:58 +01:00
|
|
|
}
|
2011-05-16 15:12:44 +02:00
|
|
|
|
|
|
|
# Return _Multi object
|
2009-03-08 09:50:58 +01:00
|
|
|
return $self;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @method int try(string sub,int type)
|
|
|
|
# Main method: try to call $sub method in the current authentication or
|
|
|
|
# userDB module. If it fails, call next() and replay()
|
|
|
|
# @param sub name of the method to launch
|
|
|
|
# @param type 0 for authentication, 1 for userDB
|
|
|
|
# @return Lemonldap::NG::Portal error code returned by method $sub
|
|
|
|
sub try {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $sub, $type ) = @_;
|
2009-03-08 09:50:58 +01:00
|
|
|
my $res;
|
|
|
|
my $s = $self->{stack}->[$type]->[0]->{m} . "::$sub";
|
|
|
|
my $old = $self->{stack}->[$type]->[0]->{n};
|
|
|
|
my $ci;
|
2010-01-28 15:47:51 +01:00
|
|
|
|
2012-09-09 23:37:49 +02:00
|
|
|
# Store last module used
|
2013-04-19 10:27:29 +02:00
|
|
|
$self->{last}->[$type] = $self->{stack}->[$type]->[0]->{m};
|
2012-09-09 23:37:49 +02:00
|
|
|
|
2009-03-08 09:50:58 +01:00
|
|
|
if ( $ci = $self->{p}->safe->reval( $self->{stack}->[$type]->[0]->{c} ) ) {
|
2010-01-28 15:47:51 +01:00
|
|
|
|
|
|
|
# Log used module
|
2012-09-08 19:20:08 +02:00
|
|
|
$self->{p}
|
|
|
|
->lmLog( "Multi (type $type): trying $sub for module $old", 'debug' );
|
2010-01-28 15:47:51 +01:00
|
|
|
|
|
|
|
# Run subroutine
|
2009-03-08 09:50:58 +01:00
|
|
|
$res = $self->{p}->$s();
|
2010-01-28 15:47:51 +01:00
|
|
|
|
|
|
|
# Stop if no error, or if confirmation needed, or if form not filled
|
|
|
|
return $res
|
|
|
|
if ( $res <= 0
|
|
|
|
or $res == PE_CONFIRM
|
|
|
|
or $res == PE_FIRSTACCESS
|
|
|
|
or $res == PE_FORMEMPTY );
|
2009-03-08 09:50:58 +01:00
|
|
|
}
|
|
|
|
unless ( $self->next($type) ) {
|
|
|
|
return ( $ci ? $res : $self->{res} );
|
|
|
|
}
|
|
|
|
$self->{res} = $res if ( defined($res) );
|
|
|
|
$self->{p}->lmLog(
|
|
|
|
[ 'Authentication', 'Retriving user' ]->[$type]
|
|
|
|
. " with $old failed, trying next",
|
|
|
|
'info'
|
|
|
|
) if ($ci);
|
|
|
|
$res = $self->replay( $sub, $type );
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @method protected boolean next(int type)
|
|
|
|
# Set the next authentication or userDB module as current. If both
|
|
|
|
# authentication and userDB module have the same name, both are changed if
|
|
|
|
# possible.
|
|
|
|
# @param type 0 for authentication, 1 for userDB
|
|
|
|
# return true if an other module is available
|
|
|
|
sub next {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $type ) = @_;
|
2012-09-08 19:20:08 +02:00
|
|
|
|
2009-03-08 18:37:31 +01:00
|
|
|
if ( $self->{stack}->[$type]->[0]->{n} eq
|
|
|
|
$self->{stack}->[ 1 - $type ]->[0]->{n}
|
|
|
|
and $self->{stack}->[ 1 - $type ]->[1] )
|
|
|
|
{
|
|
|
|
shift @{ $self->{stack}->[ 1 - $type ] };
|
2009-03-08 09:50:58 +01:00
|
|
|
}
|
|
|
|
shift @{ $self->{stack}->[$type] };
|
2012-09-09 23:37:49 +02:00
|
|
|
|
|
|
|
# Manage end of the stack
|
2009-03-08 09:50:58 +01:00
|
|
|
return 0 unless ( @{ $self->{stack}->[$type] } );
|
2012-09-09 23:37:49 +02:00
|
|
|
|
2009-03-08 09:50:58 +01:00
|
|
|
%{ $self->{p} } = (
|
|
|
|
%{ $self->{p} },
|
2009-03-08 18:37:31 +01:00
|
|
|
%{ $self->{p}->{multi}->{ $self->{stack}->[$type]->[0]->{n} } }
|
|
|
|
) if ( $self->{p}->{multi}->{ $self->{stack}->[$type]->[0]->{n} } );
|
2009-03-08 09:50:58 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
## @method protected int replay(string sub)
|
|
|
|
# replay all methods since authInit() until method $sub with the new module.
|
|
|
|
# @param $sub name of the method who has failed
|
|
|
|
# @return Lemonldap::NG::Portal error code
|
|
|
|
sub replay {
|
2016-01-02 10:29:05 +01:00
|
|
|
my ( $self, $sub ) = @_;
|
2009-03-08 09:50:58 +01:00
|
|
|
my @subs = ();
|
2012-09-08 19:20:08 +02:00
|
|
|
$self->{p}->lmLog( "Replay all methods until sub $sub", 'debug' );
|
|
|
|
|
2009-03-08 09:50:58 +01:00
|
|
|
foreach (
|
2010-09-10 21:52:44 +02:00
|
|
|
qw(authInit extractFormInfo userDBInit getUser setAuthSessionInfo
|
2012-09-08 19:20:08 +02:00
|
|
|
setSessionInfo setGroups authenticate authFinish)
|
2009-03-08 09:50:58 +01:00
|
|
|
)
|
|
|
|
{
|
|
|
|
push @subs, $_;
|
|
|
|
last if ( $_ eq $sub );
|
|
|
|
}
|
|
|
|
return $self->{p}->_subProcess(@subs);
|
|
|
|
}
|
|
|
|
|
|
|
|
package Lemonldap::NG::Portal::Simple;
|
|
|
|
|
|
|
|
## @method private Lemonldap::NG::Portal::_Multi _multi()
|
|
|
|
# Return Lemonldap::NG::Portal::_Multi object and builds it if it was not build
|
|
|
|
# before. This method is used if authentication is set to "Multi".
|
|
|
|
# @return Lemonldap::NG::Portal::_Multi object
|
|
|
|
sub _multi {
|
|
|
|
my $self = shift;
|
|
|
|
return $self->{_multi} if ( $self->{_multi} );
|
|
|
|
return $self->{_multi} = Lemonldap::NG::Portal::_Multi->new($self);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|
|
|
|
|