blib/lib/Lemonldap/NG/Portal/Plugins/BruteForceProtection.pm untested

filter: Statementstmt 0.0% 0 / 94 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 150 CC 17 SCAR 43.4
CC
17
Cov
0%
run
110.0
init
72.0
BEGIN
2.0
CRAP 76.5
1 package Lemonldap::NG::Portal::Plugins::BruteForceProtection;
2  
3 use strict;
4 use Mouse;
5 use Lemonldap::NG::Portal::Main::Constants qw(
6 PE_OK
7 PE_WAIT
8 );
9  
10 our $VERSION = '2.19.0';
11  
12 extends 'Lemonldap::NG::Portal::Main::Plugin';
13  
14 # INITIALIZATION
15 use constant afterSub => { setPersistentSessionInfo => 'run' };
16  
17 has lockTimes => (
18 is => 'rw',
19 isa => 'ArrayRef',
20 default => sub { [] }
21 );
22 has maxAge => (
23 is => 'rw',
24 isa => 'Int'
25 );
26 has maxFailed => (
27 is => 'rw',
28 isa => 'Int'
29 );
30  
31 sub init {
32 my ($self) = @_;
33 $self->maxFailed( abs $self->conf->{bruteForceProtectionMaxFailed} );
34  
35 if ( $self->conf->{disablePersistentStorage} ) {
36 $self->logger->error(
37 '"BruteForceProtection" plugin enabled WITHOUT persistent session storage"'
38 );
39 return 0;
40 }
41  
42 unless ( $self->conf->{loginHistoryEnabled} ) {
43 $self->logger->error(
44 '"BruteForceProtection" plugin enabled WITHOUT "History" plugin');
45 return 0;
46 }
47  
48 unless ( $self->conf->{failedLoginNumber} >= $self->maxFailed ) {
49 $self->logger->error( 'Number of failed logins history ('
50 . $self->conf->{failedLoginNumber}
51 . ') must be higher than allowed failed logins attempt ('
52 . $self->maxFailed
53 . ')' );
54 return 0;
55 }
56  
57 my $maxAge = $self->conf->{bruteForceProtectionMaxAge} || 300;
58 if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) {
59 my $lockTimes = @{ $self->lockTimes } =
60 sort { $a <=> $b }
61 map {
62 my $time = s/\D//r;
63 abs $time < $self->conf->{bruteForceProtectionMaxLockTime}
64 ? abs $time
65 : ()
66 }
67 grep /\d+/,
68 split /\s*,\s*/, $self->conf->{bruteForceProtectionLockTimes};
69  
70 unless ($lockTimes) {
71 @{ $self->lockTimes } = ( 15, 30, 60, 300, 600 );
72 $lockTimes = 5;
73 }
74  
75 for ( my $i = 1 ; $i < $self->maxFailed ; $i++ ) {
76 unshift @{ $self->lockTimes }, 0;
77 $lockTimes++;
78 }
79  
80 unless ( $lockTimes <= $self->conf->{failedLoginNumber} ) {
81 $self->logger->warn( 'Number failed logins history ('
82 . $self->conf->{failedLoginNumber}
83 . ') must be higher than incremental lock time values plus allowed failed logins attempt ('
84 . "$lockTimes)" );
85 splice @{ $self->lockTimes }, $self->conf->{failedLoginNumber};
86 $lockTimes = $self->conf->{failedLoginNumber};
87 }
88  
89 my $sum =
90 $maxAge * ( 1 + $self->conf->{failedLoginNumber} - $lockTimes );
91 $sum += $_ foreach @{ $self->lockTimes };
92 $self->maxAge($sum);
93 }
94 else {
95 $self->maxAge( $maxAge * ( 1 + $self->maxFailed ) );
96 }
97  
98 return 1;
99 }
100  
101 # RUNNING METHOD
102 sub run {
103 my ( $self, $req ) = @_;
104 my $now = time;
105 my $countFailed = my @failedLogins =
106 map { ( $now - $_->{_utime} ) <= $self->maxAge ? $_ : () }
107 @{ $req->sessionInfo->{_loginHistory}->{failedLogin} };
108 $self->logger->debug( ' -> Failed login maxAge = ' . $self->maxAge );
109 $self->logger->debug(
110 "Number of failed login(s) to take into account = $countFailed");
111 my $lastFailedLoginEpoch = $failedLogins[0]->{_utime} || undef;
112  
113 if ( $self->conf->{bruteForceProtectionIncrementalTempo} ) {
114 return PE_OK unless $lastFailedLoginEpoch;
115  
116 # Delta between current attempt and last failed login
117 my $delta = $now - $lastFailedLoginEpoch;
118 $self->logger->debug(" -> Delta = $delta");
119  
120 # Time to wait
121 my $waitingTime = $self->lockTimes->[ $countFailed - 1 ]
122 // $self->conf->{bruteForceProtectionMaxLockTime};
123  
124 # Reach last tempo. Stop to increase waiting time
125 if ( $countFailed >= scalar @{ $self->lockTimes } ) {
126 $self->userLogger->warn(
127 "BruteForceProtection: Last lock time has been reached");
128 $self->logger->debug("Force waitingTime to last value");
129 $waitingTime =
130 $self->lockTimes->[ scalar @{ $self->lockTimes } - 1 ];
131 }
132 $self->logger->debug(" -> Waiting time = $waitingTime");
133  
134 # Delta < waitingTime => wait
135 if ( $waitingTime && $delta < $waitingTime ) {
136 $self->userLogger->warn("BruteForceProtection enabled");
137 $req->authResult(PE_WAIT);
138  
139 # Do not store failed login if last tempo or max tempo is reached
140 $self->p->registerLogin( $req, $req->{user} )
141 if ( $waitingTime < $self->conf->{bruteForceProtectionMaxLockTime}
142 && $waitingTime <
143 $self->lockTimes->[ scalar @{ $self->lockTimes } - 1 ] );
144 $req->lockTime( $waitingTime - $delta );
145 return PE_WAIT;
146 }
147 return PE_OK;
148 }
149  
150 return PE_OK
151 if ( $countFailed < $self->maxFailed );
152  
153 # Delta between current attempt and last failed login
154 my $delta = $lastFailedLoginEpoch ? $now - $lastFailedLoginEpoch : 0;
155 $self->logger->debug(" -> Delta = $delta");
156  
157 # Delta < Tempo => wait
158 return PE_OK
159 unless ( $delta < $self->conf->{bruteForceProtectionTempo}
160 && $countFailed );
161  
162 # Account locked
163 $self->userLogger->warn("BruteForceProtection enabled");
164 $self->logger->debug(
165 " -> Waiting time = $self->{conf}->{bruteForceProtectionTempo}");
166 $req->lockTime( $self->conf->{bruteForceProtectionTempo} - $delta );
167 return PE_WAIT;
168 }
169  
170 1;