Add delete function to TOTP & Yubikey - TODO : Verify if user is authorized to delete (#1386)
This commit is contained in:
parent
6b27054603
commit
0637601d8b
|
@ -66,13 +66,11 @@ sub run {
|
|||
my $code = $req->param('code');
|
||||
my $TOTPName = $req->param('TOTPName');
|
||||
|
||||
|
||||
unless ($code) {
|
||||
$self->logger->userInfo('TOTP registration: empty validation form');
|
||||
return $self->p->sendError( $req, 'missingCode', 200 );
|
||||
}
|
||||
|
||||
|
||||
#unless ( $code and $TOTPName ) {
|
||||
#$self->logger->userInfo(
|
||||
#'TOTP registration: empty code or name in validation form');
|
||||
|
@ -111,11 +109,12 @@ sub run {
|
|||
}
|
||||
push @{$list2FDevices},
|
||||
{
|
||||
type => 'totp',
|
||||
type => 'TOTP',
|
||||
name => $TOTPName,
|
||||
_secret => $token->{_totp2fSecret},
|
||||
epoch => time()
|
||||
};
|
||||
|
||||
#$self->logger->debug(
|
||||
#"Append 2F Device : { type => 'totp', name => $TOTPName }");
|
||||
$self->p->updatePersistentSession( $req,
|
||||
|
@ -190,6 +189,33 @@ sub run {
|
|||
}
|
||||
}
|
||||
|
||||
elsif ( $action eq 'delete' ) {
|
||||
my $epoch = $req->param('epoch');
|
||||
|
||||
my $list2FDevices = eval {
|
||||
$self->logger->debug("Loading 2F Devices ...");
|
||||
|
||||
# Read existing 2FDevices
|
||||
from_json( $req->userData->{list2FDevices}, { allow_nonref => 1 } );
|
||||
};
|
||||
|
||||
my @keep = ();
|
||||
while (@$list2FDevices) {
|
||||
my $element = shift @$list2FDevices;
|
||||
$self->logger->debug("Looking for 2F Device to delete ...");
|
||||
push @keep, $element unless ( $element->{epoch} eq $epoch );
|
||||
}
|
||||
|
||||
$self->logger->debug(
|
||||
"Delete 2F Device : { type => 'TOTP', epoch => $epoch }");
|
||||
$self->p->updatePersistentSession( $req,
|
||||
{ list2FDevices => to_json( \@keep ) } );
|
||||
$self->userLogger->notice('TOTP deletion succeed');
|
||||
return [ 200, [ 'Content-Type' => 'application/json' ],
|
||||
['{"result":1}'] ];
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
|
|
|
@ -66,12 +66,14 @@ sub run {
|
|||
|
||||
my $list2FDevices = eval {
|
||||
$self->logger->debug("Looking for 2F Devices ...");
|
||||
|
||||
# Read existing 2FDevices
|
||||
from_json( $req->userData->{list2FDevices},
|
||||
{ allow_nonref => 1 } );
|
||||
};
|
||||
unless ($list2FDevices) {
|
||||
$self->logger->debug("No 2F Device found");
|
||||
|
||||
# Set default value
|
||||
$list2FDevices = [];
|
||||
}
|
||||
|
@ -89,19 +91,15 @@ sub run {
|
|||
foreach (@$list2FDevices) {
|
||||
$self->logger->debug("Reading U2F Keys ...");
|
||||
$SameU2FKeyFound ||= 1 if ( ( $_->{name} eq $keyName ) );
|
||||
};
|
||||
}
|
||||
|
||||
$self->logger->debug("Same 2F Device found ? $SameU2FKeyFound");
|
||||
|
||||
|
||||
if ($SameU2FKeyFound) {
|
||||
$self->userLogger->error("U2F Key already registered !");
|
||||
return $self->p->sendError( $req, 'Bad challenge', 400 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
push @{$list2FDevices},
|
||||
{
|
||||
type => 'U2F',
|
||||
|
@ -221,30 +219,24 @@ sub run {
|
|||
my $epoch = $req->param('epoch');
|
||||
|
||||
my $list2FDevices = eval {
|
||||
$self->logger->debug("Looking for 2F Devices ...");
|
||||
$self->logger->debug("Loading 2F Devices ...");
|
||||
|
||||
# Read existing 2FDevices
|
||||
from_json( $req->userData->{list2FDevices},
|
||||
{ allow_nonref => 1 } );
|
||||
from_json( $req->userData->{list2FDevices}, { allow_nonref => 1 } );
|
||||
};
|
||||
|
||||
my @keep = ();
|
||||
while (@$list2FDevices) {
|
||||
my $element = shift @$list2FDevices;
|
||||
$self->logger->debug("Looking for 2F Device to delete ...");
|
||||
push @keep, $element unless ( $element->{epoch} eq $epoch );
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$self->logger->debug(
|
||||
"Delete 2F Device : { type => 'U2F', epoch => $epoch }");
|
||||
$self->p->updatePersistentSession( $req,
|
||||
{ list2FDevices => to_json( \@keep ) } );
|
||||
|
||||
|
||||
$self->p->updatePersistentSession(
|
||||
$req,
|
||||
{
|
||||
|
|
|
@ -85,6 +85,37 @@ sub run {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
elsif ( $action eq 'delete' ) {
|
||||
my $epoch = $req->param('epoch');
|
||||
|
||||
my $list2FDevices = eval {
|
||||
$self->logger->debug("Loading 2F Devices ...");
|
||||
|
||||
# Read existing 2FDevices
|
||||
from_json( $req->userData->{list2FDevices}, { allow_nonref => 1 } );
|
||||
};
|
||||
|
||||
my @keep = ();
|
||||
while (@$list2FDevices) {
|
||||
my $element = shift @$list2FDevices;
|
||||
$self->logger->debug("Looking for 2F Device to delete ...");
|
||||
push @keep, $element unless ( $element->{epoch} eq $epoch );
|
||||
}
|
||||
|
||||
$self->logger->debug(
|
||||
"Delete 2F Device : { type => 'UBK', epoch => $epoch }");
|
||||
$self->p->updatePersistentSession( $req,
|
||||
{ list2FDevices => to_json( \@keep ) } );
|
||||
|
||||
$self->userLogger->notice('Yubikey deletion succeed');
|
||||
return [
|
||||
200,
|
||||
[ 'Content-Type' => 'application/json', 'Content-Length' => 12, ],
|
||||
['{"result":1}']
|
||||
];
|
||||
|
||||
}
|
||||
else {
|
||||
$self->userLogger->error("Unknown Yubikey action $action");
|
||||
return $self->p->sendHtml(
|
||||
|
|
Loading…
Reference in New Issue
Block a user