lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/Plugins/AutoSignin.pm

73 lines
1.7 KiB
Perl
Raw Normal View History

2017-10-27 07:17:32 +02:00
package Lemonldap::NG::Portal::Plugins::AutoSignin;
2017-10-27 07:17:30 +02:00
use strict;
use Mouse;
use Safe;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_OK
);
our $VERSION = '2.0.8';
2017-10-27 07:17:30 +02:00
extends 'Lemonldap::NG::Portal::Main::Plugin';
# INTERFACE
use constant beforeAuth => 'check';
# INITIALIZATION
has rules => (
is => 'rw',
isa => 'ArrayRef',
default => sub { [] }
);
2017-10-27 07:17:30 +02:00
sub init {
my ($self) = @_;
2017-10-27 15:11:30 +02:00
if ( my $rules = $self->conf->{autoSigninRules} ) {
2017-10-27 07:17:30 +02:00
my $safe = Safe->new;
2017-10-27 15:11:30 +02:00
foreach my $id ( sort keys %$rules ) {
2018-03-13 07:14:01 +01:00
my $sub =
$safe->reval( 'sub{my($env)=@_;return (' . $rules->{$id} . ')}' );
2017-10-27 07:17:30 +02:00
if ($@) {
$self->logger->error(
'Bad Autologin rule "' . $rules->{$id} . ": $@" );
$self->logger->debug(
"Skipping Autologin rule for user \"$id\"");
next;
2017-10-27 07:17:30 +02:00
}
$self->logger->debug("Autologin rule for user \"$id\" appended");
2017-10-27 15:11:30 +02:00
$id =~ s/^\s*([\w\-\@]+)\s*/$1/;
push @{ $self->rules }, [ $sub, $id ];
2017-10-27 07:17:30 +02:00
}
}
2022-02-16 17:43:29 +01:00
2017-10-27 07:17:30 +02:00
return 1;
}
# RUNNING METHODS
sub check {
my ( $self, $req ) = @_;
foreach ( @{ $self->rules } ) {
my ( $test, $name ) = @$_;
2017-10-27 15:11:30 +02:00
$self->logger->debug("Autosignin: testing user $name");
2017-10-27 07:17:30 +02:00
if ( $test->( $req->env ) ) {
$req->user($name);
my @steps =
2018-03-21 20:48:31 +01:00
grep {
!ref $_
and $_ !~ /^(?:extractFormInfo|authenticate|secondFactor)$/
} @{ $req->steps };
2017-10-27 07:17:30 +02:00
$req->steps( \@steps );
2017-10-27 15:11:30 +02:00
$self->userLogger->notice("Autosignin for $name");
2017-10-27 07:17:30 +02:00
return PE_OK;
}
}
return PE_OK;
}
1;