lemonldap-ng/lemonldap-ng-portal/t/76-2F-Ext-with-BruteForce.t

153 lines
3.5 KiB
Perl
Raw Normal View History

2018-10-06 23:10:48 +02:00
use Test::More;
use strict;
use IO::String;
use Data::Dumper;
require 't/test-lib.pm';
use_ok('Lemonldap::NG::Common::FormEncode');
count(1);
2019-02-07 09:27:56 +01:00
my $client = LLNG::Manager::Test->new( {
2018-10-12 10:04:03 +02:00
ini => {
2018-10-06 23:10:48 +02:00
logLevel => 'error',
ext2fActivation => 1,
2019-02-14 22:09:59 +01:00
ext2fCodeActivation => 0,
2018-10-06 23:10:48 +02:00
ext2FSendCommand => 't/sendOTP.pl -uid $uid',
ext2FValidateCommand => 't/vrfyOTP.pl -uid $uid -code $code',
authentication => 'Demo',
userDB => 'Same',
loginHistoryEnabled => 1,
bruteForceProtection => 1,
bruteForceProtectionTempo => 5,
}
}
);
my $res;
## First failed connection
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/',
IO::String->new('user=dwho&password=ohwd'),
length => 23
),
2018-12-13 00:12:49 +01:00
'1st Bad Auth query'
2018-10-06 23:10:48 +02:00
);
count(1);
expectReject($res);
## Second failed connection
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/',
IO::String->new('user=dwho&password=ohwd'),
length => 23
),
2018-12-13 00:12:49 +01:00
'2nd Bad Auth query'
2018-10-06 23:10:48 +02:00
);
count(1);
expectReject($res);
2018-12-13 00:12:49 +01:00
## Third failed connection
ok(
$res = $client->_post(
'/',
IO::String->new('user=dwho&password=ohwd'),
length => 23
),
'3rd Bad Auth query'
);
count(1);
expectReject($res);
## Forth failed connection -> rejected
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/',
IO::String->new('user=dwho&password=ohwd'),
length => 23,
accept => 'text/html',
),
2018-12-13 00:12:49 +01:00
'4th Bad Auth query -> Rejected'
2018-10-06 23:10:48 +02:00
);
2020-12-17 22:40:28 +01:00
ok( $res->[2]->[0] =~ /<span trmsg="86">/, 'Protection enabled' )
or print STDERR Dumper( $res->[2]->[0] );
count(2);
2018-10-06 23:10:48 +02:00
# Count down
Time::Fake->offset("+2s");
2018-10-06 23:10:48 +02:00
# Try to authenticate
# -------------------
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/',
IO::String->new('user=dwho&password=dwho&checkLogins=1'),
length => 37,
accept => 'text/html',
),
'Auth query'
);
2020-12-17 22:40:28 +01:00
ok( $res->[2]->[0] =~ /<span trmsg="86"><\/span>/,
'Rejected -> Protection enabled' )
or print STDERR Dumper( $res->[2]->[0] );
ok( $res->[2]->[0] =~ m%(\d) <span trspan="seconds">seconds</span>%,
"LockTime = $1" );
ok( $1 < 5 && $1 >= 2, 'LockTime in range' )
or print STDERR Dumper( $res->[2]->[0] );
count(4);
2018-10-06 23:10:48 +02:00
# Cool down
Time::Fake->offset("+6s");
2018-10-06 23:10:48 +02:00
# Try to authenticate again
# -------------------------
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/',
IO::String->new('user=dwho&password=dwho&checkLogins=1'),
length => 37,
accept => 'text/html',
),
'Auth query'
);
count(1);
2020-12-17 22:40:28 +01:00
my ( $host, $url, $query ) =
2020-02-20 23:34:02 +01:00
expectForm( $res, undef, '/ext2fcheck?skin=bootstrap', 'token', 'code',
'checkLogins' );
2018-10-06 23:10:48 +02:00
2018-10-12 10:04:03 +02:00
ok(
$res->[2]->[0] =~
2021-08-20 17:20:58 +02:00
qr%<input name="code" value="" type="text" class="form-control" id="extcode" trplaceholder="code" autocomplete="one-time-code" />%,
2018-10-06 23:10:48 +02:00
'Found EXTCODE input'
) or print STDERR Dumper( $res->[2]->[0] );
$query =~ s/code=/code=123456/;
2018-10-12 10:04:03 +02:00
ok(
$res = $client->_post(
2018-10-06 23:10:48 +02:00
'/ext2fcheck',
IO::String->new($query),
length => length($query),
accept => 'text/html',
),
'Post code'
);
count(2);
2018-10-06 23:10:48 +02:00
my $id = expectCookie($res);
ok( $res->[2]->[0] =~ /trspan="lastLogins"/, 'History found' )
2018-10-12 10:04:03 +02:00
or print STDERR Dumper( $res->[2]->[0] );
2018-10-06 23:10:48 +02:00
my @c = ( $res->[2]->[0] =~ /<td>127.0.0.1/gs );
2020-12-17 22:40:28 +01:00
ok( @c == 4, 'Four entries found' )
2018-10-12 10:04:03 +02:00
or print STDERR Dumper( $res->[2]->[0] );
count(2);
2018-10-06 23:10:48 +02:00
$client->logout($id);
clean_sessions();
done_testing( count() );