lemonldap-ng/lemonldap-ng-portal/t/70-2F-TOTP-with-TTL.t

167 lines
4.5 KiB
Perl
Raw Normal View History

2018-02-25 09:49:54 +01:00
use Test::More;
use strict;
use IO::String;
require 't/test-lib.pm';
2020-04-30 22:26:41 +02:00
my $maintests = 22;
2018-02-25 09:49:54 +01:00
SKIP: {
eval { require Convert::Base32 };
if ($@) {
skip 'Convert::Base32 is missing', $maintests;
}
require Lemonldap::NG::Common::TOTP;
2019-02-07 09:27:56 +01:00
my $client = LLNG::Manager::Test->new( {
2018-11-26 14:40:21 +01:00
ini => {
logLevel => 'error',
totp2fSelfRegistration => 1,
totp2fActivation => 1,
totp2fTTL => 120,
2020-04-30 22:26:41 +02:00
sfManagerRule => 0,
2020-04-03 11:06:37 +02:00
totp2fIssuer => 'LLNG_Demo',
portalMainLogo => 'common/logos/logo_llng_old.png',
2018-02-25 09:49:54 +01:00
}
}
);
my $res;
# Try to authenticate
# -------------------
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2018-02-25 09:49:54 +01:00
'/',
IO::String->new('user=dwho&password=dwho'),
length => 23
),
'Auth query'
);
my $id = expectCookie($res);
2020-04-30 22:26:41 +02:00
ok(
$res = $client->_get(
'/',
cookie => "lemonldap=$id",
accept => 'text/html'
),
'Get Menu'
);
ok( $res->[2]->[0] !~ m%<span trspan="sfaManager">sfaManager</span>%,
'sfaManager link not found' )
2020-04-30 22:26:41 +02:00
or print STDERR Dumper( $res->[2]->[0] );
2018-02-25 09:49:54 +01:00
# TOTP form
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_get(
2018-03-15 07:14:47 +01:00
'/2fregisters',
cookie => "lemonldap=$id",
accept => 'text/html',
),
'Form registration'
);
expectRedirection( $res, qr#/2fregisters/totp$# );
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_get(
2018-03-15 07:14:47 +01:00
'/2fregisters/totp',
2018-02-25 09:49:54 +01:00
cookie => "lemonldap=$id",
accept => 'text/html',
),
'Form registration'
);
ok( $res->[2]->[0] =~ /totpregistration\.(?:min\.)?js/, 'Found TOTP js' );
2018-11-26 14:40:21 +01:00
ok(
$res->[2]->[0] =~ qr%<img src="/static/common/logos/logo_llng_old.png"%,
'Found custom Main Logo'
) or print STDERR Dumper( $res->[2]->[0] );
2018-02-25 09:49:54 +01:00
# JS query
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2018-03-15 07:14:47 +01:00
'/2fregisters/totp/getkey', IO::String->new(''),
2018-02-25 09:49:54 +01:00
cookie => "lemonldap=$id",
length => 0,
),
'Get new key'
);
eval { $res = JSON::from_json( $res->[2]->[0] ) };
ok( not($@), 'Content is JSON' )
2018-11-26 14:40:21 +01:00
or explain( $res->[2]->[0], 'JSON content' );
2018-02-25 09:49:54 +01:00
my ( $key, $token );
2020-04-03 11:06:37 +02:00
ok( $key = $res->{secret}, 'Found secret' ) or print STDERR Dumper($res);
ok( $token = $res->{token}, 'Found token' ) or print STDERR Dumper($res);
ok( $res->{portal} eq 'LLNG_Demo', 'Found issuer' )
or print STDERR Dumper($res);
ok( $res->{user} eq 'dwho', 'Found user' )
or print STDERR Dumper($res);
2018-02-25 09:49:54 +01:00
$key = Convert::Base32::decode_base32($key);
# Post code
my $code;
ok( $code = Lemonldap::NG::Common::TOTP::_code( undef, $key, 0, 30, 6 ),
'Code' );
ok( $code =~ /^\d{6}$/, 'Code contains 6 digits' );
2019-06-07 19:50:19 +02:00
my $s = "code=$code&token=$token&TOTPName=myTOTP";
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2018-03-15 07:14:47 +01:00
'/2fregisters/totp/verify',
2018-02-25 09:49:54 +01:00
IO::String->new($s),
length => length($s),
cookie => "lemonldap=$id",
),
'Post code'
);
eval { $res = JSON::from_json( $res->[2]->[0] ) };
ok( not($@), 'Content is JSON' )
2018-11-26 14:40:21 +01:00
or explain( $res->[2]->[0], 'JSON content' );
ok( $res->{result} == 1, 'TOTP is registered' );
2018-02-25 09:49:54 +01:00
2018-06-13 21:51:01 +02:00
# Try to sign-in
2018-02-25 09:49:54 +01:00
$client->logout($id);
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2018-02-25 09:49:54 +01:00
'/',
IO::String->new('user=dwho&password=dwho'),
length => 23,
accept => 'text/html',
),
'Auth query'
);
2018-11-26 14:40:21 +01:00
my ( $host, $url, $query ) =
expectForm( $res, undef, '/totp2fcheck', 'token' );
2018-02-25 09:49:54 +01:00
ok( $code = Lemonldap::NG::Common::TOTP::_code( undef, $key, 0, 30, 6 ),
'Code' );
$query =~ s/code=/code=$code/;
2018-11-26 14:40:21 +01:00
ok(
$res = $client->_post(
2018-02-25 09:49:54 +01:00
'/totp2fcheck', IO::String->new($query),
length => length($query),
),
'Post code'
);
$id = expectCookie($res);
$client->logout($id);
2019-06-07 19:50:19 +02:00
# Skipping time until TOTP expiration
Time::Fake->offset("+5m");
2019-06-07 19:50:19 +02:00
# Try to sign-in
ok(
$res = $client->_post(
'/',
IO::String->new('user=dwho&password=dwho'),
length => 23,
accept => 'text/html',
),
'Auth query'
);
$id = expectCookie($res);
expectRedirection( $res, 'http://auth.example.com/' );
2019-06-14 23:03:38 +02:00
$client->logout($id);
2018-02-25 09:49:54 +01:00
}
count($maintests);
clean_sessions();
done_testing( count() );