Combination in progress (#1151)

This commit is contained in:
Xavier Guimard 2017-02-05 17:05:33 +00:00
parent 193ac7c260
commit afcb39a355
4 changed files with 75 additions and 35 deletions

View File

@ -82,10 +82,10 @@ sub parseAnd {
foreach my $type (@mod) {
push @res, sub {
foreach my $obj (@$type) {
my $r = $obj->(@_);
return $r unless ( $r == PE_OK );
my ( $r, $name ) = $obj->(@_);
return ( $r, $name ) unless ( $r == PE_OK );
}
return PE_OK;
return ( PE_OK, $expr );
};
}
return \@res;
@ -115,25 +115,26 @@ sub parseBlock {
# Return sub
sub parseMod {
my ( $self, $moduleList, $type, $expr ) = @_;
my @list = split( /\s+and\s+/, $expr );
my @mods = map {
die "Undeclared module $_"
unless ( $moduleList->{$_}->[$type] );
$moduleList->{$_}->[$type]
} split( /\s+and\s+/, $expr );
} @list;
if ( @mods == 1 ) {
my ($m) = @mods;
return sub {
my ( $sub, $req ) = @_;
return $m->$sub($req);
return ( $m->$sub($req), $expr );
};
}
return sub {
my ( $sub, $req ) = @_;
foreach my $obj (@mods) {
my $res = $obj->$sub($req);
return $res unless ( $res == PE_OK );
for ( my $i = 0 ; $i < @list ; $i++ ) {
my $res = $mods[$i]->$sub($req);
return ( $res, $list[$i] ) unless ( $res == PE_OK );
}
return PE_OK;
return ( PE_OK, $expr );
};
}

View File

@ -59,7 +59,7 @@ ok(
'if(0) then [A,B] else [A,B] and [B,C]' );
while ( my $expr = shift @tests ) {
ok( getok($expr) == 0, qq{"$expr" returns PE_OK as auth result} )
ok( [getok($expr)]->[0] == 0, qq{"$expr" returns PE_OK as auth result} )
or print STDERR "Expect 0, get " . getok($expr) . "\n";
}

View File

@ -55,47 +55,54 @@ sub init {
return 1;
}
sub name {
my ( $self, $req, $type ) = @_;
return $req->sessionInfo->{ ( $type eq 'auth' ? '_auth' : '_userDB' ) }
|| 'Combination';
}
sub extractFormInfo {
my ( $self, $req ) = @_;
# Get available authentication schemes for this user
$self->getStack($req) or return PE_ERROR;
return $self->try->( 0, 'extractFormInfo', $req );
$self->getStack( $req, 'extractFormInfo' ) or return PE_ERROR;
return $self->try( 0, 'extractFormInfo', $req );
}
sub getUser {
return $_[0]->try->( 1, 'getUser', $_[1] );
return $_[0]->try( 1, 'getUser', $_[1] );
}
sub authenticate {
return $_[0]->try->( 0, 'authenticate', $_[1] );
return $_[0]->try( 0, 'authenticate', $_[1] );
}
sub setAuthSessionInfo {
return $_[0]->try->( 0, 'authenticate', $_[1] );
return $_[0]->try( 0, 'setAuthSessionInfo', $_[1] );
}
sub setSessionInfo {
return $_[0]->try->( 1, 'authenticate', $_[1] );
return $_[0]->try( 1, 'setSessionInfo', $_[1] );
}
sub setGroups {
return $_[0]->try->( 1, 'authenticate', $_[1] );
return $_[0]->try( 1, 'setGroups', $_[1] );
}
sub getDisplayType {
return $_[0]->try->( 0, 'getDisplayType', {} );
return $_[0]->try( 0, 'getDisplayType', {} );
}
# TODO: authLogout
sub getStack {
my ( $self, $req ) = @_;
my $stack = $req->datas->{multiStack} = $self->stackSub($req);
my ( $self, $req, @steps ) = @_;
return $req->datas->{multiStack} if ( $req->datas->{multiStack} );
my $stack = $req->datas->{multiStack} = $self->stackSub->($req);
unless ($stack) {
$self->lmLog( 'No authentication scheme for this user', 'error' );
}
@{ $req->datas->{multiSteps} } = @{ $req->steps };
@{ $req->datas->{multiSteps} } = ( @steps, @{ $req->steps } );
$req->datas->{multiTry} = 0;
return $stack;
}
@ -105,20 +112,26 @@ sub try {
my ( $nb, $stack ) = ( $req->datas->{multiTry}, $req->datas->{multiStack} );
# If more than 1 scheme is available
my ( $res, $name );
if ( $nb < @$stack ) {
# TODO: change logLevel for userLog()
my $res = $stack->[$nb]->[$type]->$subname($req);
( $res, $name ) = $stack->[$nb]->[$type]->( $subname, $req );
# On error, restart authentication with next scheme
if ( $res > PE_OK ) {
$self->lmLog( qq'Scheme "$name" has return $res, trying next',
'info' );
$req->datas->{multiTry}++;
$req->steps( [ @{ $req->datas->{multiSteps} } ] );
return PE_OK;
}
return $res;
}
return $stack->[$nb]->[$type]->$subname($req);
else {
( $res, $name ) = $stack->[$nb]->[$type]->$subname($req);
}
$req->sessionInfo->{ [ '_auth', '_userDB' ]->[$type] } = $name;
return $res;
}
1;

View File

@ -6,6 +6,7 @@ require 't/test-lib.pm';
my $res;
my $mainTests = 0;
my $client;
eval { unlink 't/userdb.db' };
@ -16,9 +17,36 @@ SKIP: {
}
my $dbh = DBI->connect("dbi:SQLite:dbname=t/userdb.db");
$dbh->do('CREATE TABLE users (user text,password text,name text)');
$dbh->do("INSERT INTO users VALUES ('test','test','Test user')");
$dbh->do("INSERT INTO users VALUES ('test1','test1','Test user 1')");
my $client = LLNG::Manager::Test->new(
$client = iniCmb('[Dm] or [DB]');
my $res = try('dwho');
expectCookie($res);
$res = try('test1');
expectCookie($res);
}
count($mainTests);
clean_sessions();
eval { unlink 't/userdb.db' };
done_testing( count() );
sub try {
my $user = shift;
my $s = "user=$user&password=$user";
my $res;
ok(
$res = $client->_post( '/', IO::String->new($s), length => length($s) ),
"Test $user"
);
count(1);
return $res;
}
sub iniCmb {
my $expr = shift;
my $res;
return LLNG::Manager::Test->new(
{
ini => {
logLevel => 'debug',
@ -26,16 +54,16 @@ SKIP: {
authentication => 'Combination',
userDB => 'Same',
combination => '[DBI] or [Demo]',
combination => $expr,
combModules => [
{
for => 0,
name => 'DBI',
for => 0,
name => 'DB',
type => 'DBI',
},
{
for => 0,
name => 'Demo',
for => 0,
name => 'Dm',
type => 'Demo',
},
],
@ -47,11 +75,9 @@ SKIP: {
dbiAuthLoginCol => 'user',
dbiAuthPasswordCol => 'password',
dbiAuthPasswordHash => '',
dbiExportedVars => {},
dbiExportedVars => {},
demoExportedVars => {},
}
}
);
}
count($mainTests);
eval { unlink 't/userdb.db' };
done_testing( count() );