« blib/lib/Lemonldap/NG/Portal/2F/Register/Password.pm
↑ Summary
blib/lib/Lemonldap/NG/Portal/Lib/CAS.pm »
| 1 | package Lemonldap::NG::Portal::Plugins::AdaptativeAuthenticationLevel; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use Lemonldap::NG::Portal::Main::Constants 'PE_OK'; | ||
| 6 | |||
| 7 | our $VERSION = '2.21.0'; | ||
| 8 | |||
| 9 | extends 'Lemonldap::NG::Portal::Main::Plugin'; | ||
| 10 | |||
| 11 | use constant aroundSub => { 'store' => 'adaptAuthenticationLevel' }; | ||
| 12 | |||
| 13 | has rules => ( is => 'rw', default => sub { {} } ); | ||
| 14 | |||
| 15 | sub init { | ||
| 16 | my ($self) = @_; | ||
| 17 | $self->logger->debug('Init AdaptativeAuthenticationLevel plugin'); | ||
| 18 | |||
| 19 | foreach ( | ||
| 20 | keys %{ $self->conf->{adaptativeAuthenticationLevelRules} // {} } ) | ||
| 21 | { | ||
| 22 | $self->logger->debug("adaptativeAuthenticationLevelRules key -> $_"); | ||
| 23 | $self->logger->debug( "adaptativeAuthenticationLevelRules value -> " | ||
| 24 | . $self->conf->{adaptativeAuthenticationLevelRules}->{$_} ); | ||
| 25 | |||
| 26 | my $rule = | ||
| 27 | $self->p->buildRule( $_, 'adaptativeAuthenticationLevelRules' ); | ||
| 28 | next unless $rule; | ||
| 29 | $self->rules->{$_} = $rule; | ||
| 30 | } | ||
| 31 | |||
| 32 | return 1; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub adaptAuthenticationLevel { | ||
| 36 | my ( $self, $sub, $req ) = @_; | ||
| 37 | |||
| 38 | return $sub->($req) if $req->refresh; | ||
| 39 | |||
| 40 | my $userid = $req->sessionInfo->{ $self->conf->{whatToTrace} } | ||
| 41 | || $req->sessionInfo->{uid}; | ||
| 42 | $self->logger->debug("Check adaptative authentication rules for $userid"); | ||
| 43 | my $authenticationLevel = $req->sessionInfo->{authenticationLevel}; | ||
| 44 | $self->logger->debug( | ||
| 45 | "Current authentication level for $userid is $authenticationLevel"); | ||
| 46 | |||
| 47 | my $updatedAuthenticationLevel = $authenticationLevel; | ||
| 48 | |||
| 49 | foreach ( keys %{ $self->rules } ) { | ||
| 50 | my $rule = $_; | ||
| 51 | $self->logger->debug( | ||
| 52 | "Check adaptativeAuthenticationLevelRules -> $rule"); | ||
| 53 | if ( $self->rules->{$_}->( $req, $req->sessionInfo ) ) { | ||
| 54 | my $levelOperation = | ||
| 55 | $self->conf->{adaptativeAuthenticationLevelRules}->{$_}; | ||
| 56 | $self->logger->debug( | ||
| 57 | "User $userid match rule, apply $levelOperation on authentication level" | ||
| 58 | ); | ||
| 59 | |||
| 60 | my ( $op, $level ) = ( $levelOperation =~ /([=+-])?(\d+)/ ); | ||
| 61 | $updatedAuthenticationLevel = $level if ( !$op or $op eq '=' ); | ||
| 62 | $updatedAuthenticationLevel += $level if ( $op and $op eq '+' ); | ||
| 63 | $updatedAuthenticationLevel -= $level if ( $op and $op eq '-' ); | ||
| 64 | $self->logger->debug( | ||
| 65 | "Authentication level for $userid is now $updatedAuthenticationLevel" | ||
| 66 | ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | if ( $authenticationLevel ne $updatedAuthenticationLevel ) { | ||
| 71 | $self->logger->debug("Authentication level has changed for $userid"); | ||
| 72 | $req->sessionInfo->{authenticationLevel} = $updatedAuthenticationLevel; | ||
| 73 | } | ||
| 74 | |||
| 75 | return $sub->($req); | ||
| 76 | } | ||
| 77 | |||
| 78 | 1; |