2018-03-30 21:24:34 +02:00
|
|
|
# Self TOTP registration
|
2018-02-20 13:19:52 +01:00
|
|
|
package Lemonldap::NG::Portal::2F::Register::TOTP;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2018-03-29 21:27:35 +02:00
|
|
|
use JSON qw(from_json to_json);
|
2018-02-20 13:19:52 +01:00
|
|
|
|
|
|
|
our $VERSION = '2.0.0';
|
|
|
|
|
2018-02-20 18:36:34 +01:00
|
|
|
extends 'Lemonldap::NG::Portal::Main::Plugin', 'Lemonldap::NG::Common::TOTP';
|
2018-02-20 13:19:52 +01:00
|
|
|
|
|
|
|
# INITIALIZATION
|
|
|
|
|
2018-03-08 20:36:32 +01:00
|
|
|
has prefix => ( is => 'rw', default => 'totp' );
|
|
|
|
|
2018-04-01 16:39:02 +02:00
|
|
|
has template => ( is => 'ro', default => 'totp2fregister' );
|
2018-03-15 07:04:52 +01:00
|
|
|
|
2018-03-15 22:35:59 +01:00
|
|
|
has logo => ( is => 'rw', default => 'totp.png' );
|
|
|
|
|
2018-02-20 13:19:52 +01:00
|
|
|
has ott => (
|
|
|
|
is => 'rw',
|
|
|
|
lazy => 1,
|
|
|
|
default => sub {
|
|
|
|
my $ott =
|
|
|
|
$_[0]->{p}->loadModule('Lemonldap::NG::Portal::Lib::OneTimeToken');
|
|
|
|
$ott->timeout( $_[0]->conf->{formTimeout} );
|
|
|
|
return $ott;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
sub init {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-03-15 07:04:52 +01:00
|
|
|
sub run {
|
|
|
|
my ( $self, $req, $action ) = @_;
|
2018-03-18 14:26:45 +01:00
|
|
|
my $user = $req->userData->{ $self->conf->{whatToTrace} };
|
2018-02-20 13:19:52 +01:00
|
|
|
unless ($user) {
|
|
|
|
return $self->p->sendError( $req,
|
2018-02-20 18:36:34 +01:00
|
|
|
'No ' . $self->conf->{whatToTrace} . ' found in user data', 500 );
|
2018-02-20 13:19:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# Verification that user has a valid TOTP app
|
|
|
|
if ( $action eq 'verify' ) {
|
|
|
|
|
|
|
|
# Get form token
|
|
|
|
my $token = $req->param('token');
|
|
|
|
unless ($token) {
|
|
|
|
$self->userLogger->warn(
|
|
|
|
"TOTP registration: register try without token for $user");
|
|
|
|
return $self->p->sendError( $req, 'Go away', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Verify that token exists in DB (note that "keep" flag is set to
|
|
|
|
# permit more than 1 try during token life
|
|
|
|
unless ( $token = $self->ott->getToken( $token, 1 ) ) {
|
|
|
|
$self->userLogger->notice(
|
|
|
|
"TOTP registration: token expired for $user");
|
|
|
|
return $self->p->sendError( $req, 'PE82', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Token is valid, so we have the master key proposed
|
|
|
|
# ($token->{_totp2fSecret})
|
|
|
|
|
|
|
|
# Now check TOTP code to verify that user has a valid TOTP app
|
2018-03-29 23:11:46 +02:00
|
|
|
my $code = $req->param('code');
|
|
|
|
my $TOTPName = $req->param('TOTPName');
|
2018-04-03 21:17:15 +02:00
|
|
|
my $epoch = time();
|
|
|
|
|
|
|
|
# Set default name if empty
|
|
|
|
$TOTPName ||= $epoch;
|
2018-03-31 00:16:36 +02:00
|
|
|
|
|
|
|
unless ($code) {
|
|
|
|
$self->logger->userInfo('TOTP registration: empty validation form');
|
2018-02-20 22:58:20 +01:00
|
|
|
return $self->p->sendError( $req, 'missingCode', 200 );
|
2018-02-20 13:19:52 +01:00
|
|
|
}
|
2018-04-03 00:01:01 +02:00
|
|
|
|
2018-02-20 13:19:52 +01:00
|
|
|
my $r = $self->verifyCode(
|
|
|
|
$self->conf->{totp2fInterval},
|
|
|
|
$self->conf->{totp2fRange},
|
2018-02-21 22:07:12 +01:00
|
|
|
$self->conf->{totp2fDigits},
|
2018-02-20 13:19:52 +01:00
|
|
|
$token->{_totp2fSecret}, $code
|
|
|
|
);
|
|
|
|
if ( $r == -1 ) {
|
|
|
|
return $self->p->sendError( 'serverError', 500 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Invalid try is returned with a 200 code. Javascript will read error
|
|
|
|
# and propose to retry
|
|
|
|
elsif ( $r == 0 ) {
|
|
|
|
$self->userLogger->notice(
|
|
|
|
"TOTP registration: invalid TOTP for $user");
|
|
|
|
return $self->p->sendError( $req, 'badCode', 200 );
|
|
|
|
}
|
|
|
|
$self->logger->debug('TOTP code verified');
|
|
|
|
|
2018-02-20 18:36:34 +01:00
|
|
|
# Now code is verified, let's store the master key in persistent data
|
2018-02-20 13:19:52 +01:00
|
|
|
$self->p->updatePersistentSession( $req,
|
|
|
|
{ _totp2fSecret => $token->{_totp2fSecret} } );
|
2018-03-29 23:11:46 +02:00
|
|
|
|
2018-04-04 23:05:27 +02:00
|
|
|
my $_2FDevices = eval {
|
2018-03-30 21:24:34 +02:00
|
|
|
$self->logger->debug("Looking for 2F Devices ...");
|
2018-04-04 23:05:27 +02:00
|
|
|
from_json( $req->userData->{_2FDevices}, { allow_nonref => 1 } );
|
2018-03-29 23:11:46 +02:00
|
|
|
};
|
2018-04-04 23:05:27 +02:00
|
|
|
unless ($_2FDevices) {
|
2018-03-29 23:11:46 +02:00
|
|
|
$self->logger->debug("No 2F Device found");
|
2018-04-04 23:05:27 +02:00
|
|
|
$_2FDevices = [];
|
2018-03-29 23:11:46 +02:00
|
|
|
}
|
2018-04-04 23:50:33 +02:00
|
|
|
|
|
|
|
# Check if user can register one more device
|
2018-04-05 17:58:42 +02:00
|
|
|
my $size = @$_2FDevices;
|
|
|
|
my $maxSize = $self->conf->{max2FDevices};
|
|
|
|
$self->logger->debug(
|
|
|
|
"Nbr 2FDevices = $size / $maxSize");
|
|
|
|
if ( $size > $maxSize ) {
|
|
|
|
$self->userLogger->error(
|
|
|
|
"Max number of 2F devices is reached !!!");
|
|
|
|
return $self->p->sendError( $req, 'MaxNumberof2FDevicesReached', 200 );
|
2018-04-04 23:50:33 +02:00
|
|
|
}
|
|
|
|
|
2018-04-04 23:05:27 +02:00
|
|
|
push @{$_2FDevices},
|
2018-03-29 23:11:46 +02:00
|
|
|
{
|
2018-04-03 00:01:01 +02:00
|
|
|
type => 'TOTP',
|
2018-03-29 23:11:46 +02:00
|
|
|
name => $TOTPName,
|
|
|
|
_secret => $token->{_totp2fSecret},
|
2018-04-03 21:17:15 +02:00
|
|
|
epoch => $epoch
|
2018-03-29 23:11:46 +02:00
|
|
|
};
|
2018-04-03 00:01:01 +02:00
|
|
|
|
2018-04-03 21:17:15 +02:00
|
|
|
$self->logger->debug(
|
|
|
|
"Append 2F Device : { type => 'totp', name => $TOTPName }");
|
2018-03-29 21:27:35 +02:00
|
|
|
$self->p->updatePersistentSession( $req,
|
2018-04-04 23:05:27 +02:00
|
|
|
{ _2FDevices => to_json($_2FDevices) } );
|
2018-03-29 23:11:46 +02:00
|
|
|
|
2018-02-20 22:58:20 +01:00
|
|
|
$self->userLogger->notice('TOTP registration succeed');
|
2018-02-20 13:19:52 +01:00
|
|
|
return [ 200, [ 'Content-Type' => 'application/json' ],
|
|
|
|
['{"result":1}'] ];
|
|
|
|
}
|
2018-02-20 18:36:34 +01:00
|
|
|
|
2018-02-20 13:19:52 +01:00
|
|
|
# Get or generate master key
|
2018-02-20 18:36:34 +01:00
|
|
|
elsif ( $action eq 'getkey' ) {
|
|
|
|
my $nk = 0;
|
|
|
|
my $secret;
|
2018-03-18 14:26:45 +01:00
|
|
|
if ( ( $req->param('newkey') and $self->conf->{totp2fUserCanChangeKey} )
|
|
|
|
or not $req->userData->{_totp2fSecret} )
|
|
|
|
{
|
2018-02-20 18:36:34 +01:00
|
|
|
$secret = $self->newSecret;
|
|
|
|
$nk = 1;
|
|
|
|
}
|
2018-03-18 14:26:45 +01:00
|
|
|
elsif ( $req->param('newkey') ) {
|
2018-04-03 23:03:29 +02:00
|
|
|
return $self->p->sendError( $req, 'notAuthorized', 200 );
|
2018-03-18 14:26:45 +01:00
|
|
|
}
|
|
|
|
elsif ( $self->conf->{totp2fDisplayExistingSecret} ) {
|
2018-02-20 18:36:34 +01:00
|
|
|
$secret = $req->userData->{_totp2fSecret};
|
|
|
|
}
|
2018-03-18 14:26:45 +01:00
|
|
|
else {
|
|
|
|
return $self->p->sendError( $req, 'totpExistingKey', 200 );
|
|
|
|
}
|
2018-02-20 18:36:34 +01:00
|
|
|
|
|
|
|
# Secret is stored in a token: we choose to not accept secret returned
|
|
|
|
# by Ajax request to avoid some attacks
|
|
|
|
my $token = $self->ott->createToken(
|
|
|
|
{
|
|
|
|
_totp2fSecret => $secret,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2018-02-21 22:07:12 +01:00
|
|
|
my $issuer;
|
|
|
|
unless ( $issuer = $self->conf->{totp2fIssuer} ) {
|
|
|
|
$issuer = $self->conf->{portal};
|
|
|
|
$issuer =~ s#^https?://([^/:]+).*$#$1#;
|
|
|
|
}
|
2018-02-20 18:36:34 +01:00
|
|
|
|
|
|
|
# QR-code will be generated by a javascript, here we just send data
|
|
|
|
return $self->p->sendJSONresponse(
|
|
|
|
$req,
|
|
|
|
{
|
2018-02-21 22:07:12 +01:00
|
|
|
secret => $secret,
|
|
|
|
token => $token,
|
|
|
|
portal => $issuer,
|
|
|
|
user => $user,
|
|
|
|
newkey => $nk,
|
|
|
|
digits => $self->conf->{totp2fDigits},
|
|
|
|
interval => $self->conf->{totp2fInterval}
|
2018-02-20 18:36:34 +01:00
|
|
|
}
|
|
|
|
);
|
2018-02-20 13:19:52 +01:00
|
|
|
}
|
2018-04-03 21:17:15 +02:00
|
|
|
|
|
|
|
# Check if unregistration is allowed
|
2018-04-03 23:03:29 +02:00
|
|
|
unless ( $self->conf->{TOTP2fUserCanChangeKey} ) {
|
|
|
|
return $self->p->sendError( $req, 'notAuthorized', 400 );
|
2018-04-03 21:17:15 +02:00
|
|
|
}
|
2018-03-13 07:14:01 +01:00
|
|
|
|
|
|
|
# Get or generate master key
|
2018-04-03 17:15:38 +02:00
|
|
|
if ( $action eq 'unregister' ) {
|
2018-04-03 21:17:15 +02:00
|
|
|
$self->p->updatePersistentSession( $req, { _totp2fSecret => '' } );
|
|
|
|
$self->userLogger->notice('TOTP unregistration succeed');
|
|
|
|
return [ 200, [ 'Content-Type' => 'application/json' ],
|
|
|
|
['{"result":1}'] ];
|
|
|
|
}
|
2018-04-03 00:01:01 +02:00
|
|
|
elsif ( $action eq 'delete' ) {
|
2018-04-04 23:16:36 +02:00
|
|
|
my $epoch = $req->param('epoch');
|
2018-04-04 23:05:27 +02:00
|
|
|
my $_2FDevices = eval {
|
2018-04-03 00:01:01 +02:00
|
|
|
$self->logger->debug("Loading 2F Devices ...");
|
|
|
|
|
|
|
|
# Read existing 2FDevices
|
2018-04-04 23:05:27 +02:00
|
|
|
from_json( $req->userData->{_2FDevices}, { allow_nonref => 1 } );
|
2018-04-03 00:01:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
my @keep = ();
|
2018-04-04 23:05:27 +02:00
|
|
|
while (@$_2FDevices) {
|
|
|
|
my $element = shift @$_2FDevices;
|
2018-04-03 21:17:15 +02:00
|
|
|
$self->logger->debug("Looking for 2F device to delete ...");
|
2018-04-03 00:01:01 +02:00
|
|
|
push @keep, $element unless ( $element->{epoch} eq $epoch );
|
|
|
|
}
|
|
|
|
|
|
|
|
$self->logger->debug(
|
|
|
|
"Delete 2F Device : { type => 'TOTP', epoch => $epoch }");
|
|
|
|
$self->p->updatePersistentSession( $req,
|
2018-04-04 23:05:27 +02:00
|
|
|
{ _2FDevices => to_json( \@keep ) } );
|
2018-04-03 00:01:01 +02:00
|
|
|
$self->userLogger->notice('TOTP deletion succeed');
|
|
|
|
return [ 200, [ 'Content-Type' => 'application/json' ],
|
|
|
|
['{"result":1}'] ];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-20 13:19:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
1;
|