2018-03-11 23:33:55 +01:00
|
|
|
package Lemonldap::NG::Manager::2ndFA;
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
use 5.10.0;
|
|
|
|
use utf8;
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
|
|
|
use MIME::Base64 qw(encode_base64 decode_base64);
|
|
|
|
|
|
|
|
use Lemonldap::NG::Common::Session;
|
|
|
|
use Lemonldap::NG::Common::Conf::Constants;
|
|
|
|
use Lemonldap::NG::Common::PSGI::Constants;
|
|
|
|
use Lemonldap::NG::Common::Conf::ReConstants;
|
|
|
|
|
|
|
|
use feature 'state';
|
|
|
|
|
|
|
|
extends 'Lemonldap::NG::Common::Conf::AccessLib',
|
|
|
|
'Lemonldap::NG::Common::Session::REST';
|
|
|
|
|
2019-01-23 22:47:42 +01:00
|
|
|
our $VERSION = '2.0.2';
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
#############################
|
|
|
|
# I. INITIALIZATION METHODS #
|
|
|
|
#############################
|
|
|
|
|
2018-06-28 18:17:41 +02:00
|
|
|
use constant defaultRoute => '2ndfa.html';
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
sub addRoutes {
|
|
|
|
my ( $self, $conf ) = @_;
|
|
|
|
|
2018-04-16 17:07:38 +02:00
|
|
|
# Remote Procedure are defined in Lemonldap::NG::Common::Session::REST
|
2018-03-10 23:32:03 +01:00
|
|
|
# HTML template
|
2018-03-20 21:58:43 +01:00
|
|
|
$self->addRoute( '2ndfa.html', undef, ['GET'] )
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
->addRoute(
|
|
|
|
sfa => { ':sessionType' => 'sfa' },
|
|
|
|
['GET']
|
|
|
|
)
|
|
|
|
|
2018-04-15 11:35:42 +02:00
|
|
|
# DELETE 2FA DEVICE
|
2018-03-10 23:32:03 +01:00
|
|
|
->addRoute(
|
2018-07-21 22:11:38 +02:00
|
|
|
sfa => { ':sessionType' => { ':sessionId' => 'del2F' } },
|
2018-03-10 23:32:03 +01:00
|
|
|
['DELETE']
|
2018-04-15 11:35:42 +02:00
|
|
|
);
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
$self->setTypes($conf);
|
|
|
|
$self->{multiValuesSeparator} ||= '; ';
|
|
|
|
$self->{hiddenAttributes} //= "_password";
|
2019-01-23 22:47:42 +01:00
|
|
|
$self->{TOTPCheck} = $self->{U2FCheck} = $self->{UBKCheck} = '1';
|
2018-03-10 23:32:03 +01:00
|
|
|
}
|
|
|
|
|
2018-03-11 23:33:55 +01:00
|
|
|
###################
|
|
|
|
# II. 2FA METHODS #
|
|
|
|
###################
|
2018-03-10 23:32:03 +01:00
|
|
|
|
2018-07-21 22:11:38 +02:00
|
|
|
sub del2F {
|
2018-03-11 23:33:55 +01:00
|
|
|
|
2018-03-18 13:22:21 +01:00
|
|
|
my ( $self, $req, $session, $skey ) = @_;
|
|
|
|
|
|
|
|
my $mod = $self->getMod($req)
|
2018-08-26 12:17:54 +02:00
|
|
|
or return $self->sendError( $req, 'Bad mode', 400 );
|
2018-03-18 13:22:21 +01:00
|
|
|
|
2018-04-19 13:36:04 +02:00
|
|
|
my $params = $req->parameters();
|
2018-09-02 17:31:58 +02:00
|
|
|
my $type = $params->{type}
|
|
|
|
or return $self->sendError( $req, 'Missing "type" parameter', 400 );
|
|
|
|
my $epoch = $params->{epoch}
|
|
|
|
or return $self->sendError( $req, 'Missing "epoch" parameter', 400 );
|
2018-08-26 12:17:54 +02:00
|
|
|
|
|
|
|
if ( $type =~ /\b(?:U2F|TOTP|UBK)\b/ ) {
|
2018-04-19 13:36:04 +02:00
|
|
|
$self->logger->debug(
|
|
|
|
"Call procedure delete2F with type=$type and epoch=$epoch");
|
2018-04-16 17:07:38 +02:00
|
|
|
return $self->delete2F( $req, $session, $skey );
|
2018-03-18 13:22:21 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-08-26 12:17:54 +02:00
|
|
|
return $self->sendError( $req, 'Bad value "type" parameter', 400 );
|
2018-03-18 13:22:21 +01:00
|
|
|
}
|
2018-03-11 23:33:55 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 23:32:03 +01:00
|
|
|
########################
|
|
|
|
# III. DISPLAY METHODS #
|
|
|
|
########################
|
|
|
|
|
|
|
|
sub sfa {
|
|
|
|
my ( $self, $req, $session, $skey ) = @_;
|
|
|
|
|
|
|
|
# Case 1: only one session is required
|
2018-03-18 13:22:21 +01:00
|
|
|
if ($session) {
|
2018-03-10 23:32:03 +01:00
|
|
|
return $self->session( $req, $session, $skey );
|
|
|
|
}
|
2018-03-18 13:22:21 +01:00
|
|
|
|
2018-03-10 23:32:03 +01:00
|
|
|
my $mod = $self->getMod($req)
|
2018-08-26 12:17:54 +02:00
|
|
|
or return $self->sendError( $req, 'Bad mode', 400 );
|
2018-03-10 23:32:03 +01:00
|
|
|
my $params = $req->parameters();
|
|
|
|
my $type = delete $params->{sessionType};
|
|
|
|
$type = ucfirst($type);
|
|
|
|
my $res;
|
2018-03-13 07:14:01 +01:00
|
|
|
|
2018-03-10 23:32:03 +01:00
|
|
|
# Case 2: list of sessions
|
|
|
|
|
|
|
|
my $whatToTrace = Lemonldap::NG::Handler::PSGI::Main->tsv->{whatToTrace};
|
|
|
|
|
|
|
|
# 2.1 Get fields to require
|
2018-08-26 12:17:54 +02:00
|
|
|
my @fields = ( '_httpSessionType', $whatToTrace, '_2fDevices' );
|
2018-03-10 23:32:03 +01:00
|
|
|
if ( my $groupBy = $params->{groupBy} ) {
|
|
|
|
$groupBy =~ s/^substr\((\w+)(?:,\d+(?:,\d+)?)?\)$/$1/;
|
|
|
|
$groupBy =~ s/^_whatToTrace$/$whatToTrace/o
|
|
|
|
or push @fields, $groupBy;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
push @fields, '_utime';
|
|
|
|
}
|
|
|
|
|
|
|
|
# 2.2 Restrict query if possible: search for filters (any query arg that is
|
|
|
|
# not a keyword)
|
|
|
|
my $moduleOptions = $mod->{options};
|
|
|
|
$moduleOptions->{backend} = $mod->{module};
|
2018-03-18 13:22:21 +01:00
|
|
|
|
2018-03-18 23:44:14 +01:00
|
|
|
# Select 2FA sessions to display
|
2018-08-26 12:17:54 +02:00
|
|
|
foreach (qw(U2F TOTP UBK)) {
|
2018-08-22 15:20:16 +02:00
|
|
|
$self->{ $_ . 'Check' } = delete $params->{ $_ . 'Check' }
|
|
|
|
if ( defined $params->{ $_ . 'Check' } );
|
2018-03-18 13:22:21 +01:00
|
|
|
}
|
|
|
|
|
2018-03-10 23:32:03 +01:00
|
|
|
my %filters = map {
|
|
|
|
my $s = $_;
|
|
|
|
$s =~ s/\b_whatToTrace\b/$whatToTrace/o;
|
2018-03-14 21:35:51 +01:00
|
|
|
/^groupBy$/
|
2018-03-10 23:32:03 +01:00
|
|
|
? ()
|
|
|
|
: ( $s => $params->{$_} );
|
|
|
|
} keys %$params;
|
|
|
|
$filters{_session_kind} = $type;
|
|
|
|
|
|
|
|
push @fields, keys(%filters);
|
|
|
|
{
|
|
|
|
my %seen;
|
|
|
|
@fields = grep { !$seen{$_}++ } @fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
# For now, only one argument can be passed to
|
|
|
|
# Lemonldap::NG::Common::Apache::Session so just the first filter is
|
|
|
|
# used
|
|
|
|
my ($firstFilter) = sort {
|
|
|
|
$filters{$a} =~ m#^[\w:]+/\d+\*?$# ? 1
|
|
|
|
: $filters{$b} =~ m#^[\w:]+/\d+\*?$# ? -1
|
|
|
|
: $a eq '_session_kind' ? 1
|
|
|
|
: $b eq '_session_kind' ? -1
|
|
|
|
: $a cmp $b
|
|
|
|
} keys %filters;
|
|
|
|
|
|
|
|
# Check if a '*' is required
|
|
|
|
my $function = 'searchOn';
|
|
|
|
$function = 'searchOnExpr'
|
|
|
|
if ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# }
|
|
|
|
( $filters{$firstFilter} ) );
|
|
|
|
$self->logger->debug(
|
|
|
|
"First filter: $firstFilter = $filters{$firstFilter} ($function)");
|
|
|
|
|
|
|
|
$res =
|
|
|
|
Lemonldap::NG::Common::Apache::Session->$function( $moduleOptions,
|
|
|
|
$firstFilter, $filters{$firstFilter}, @fields );
|
|
|
|
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req,
|
|
|
|
{
|
|
|
|
result => 1,
|
|
|
|
count => 0,
|
|
|
|
total => 0,
|
|
|
|
values => []
|
|
|
|
}
|
|
|
|
) unless ( $res and %$res );
|
|
|
|
|
|
|
|
delete $filters{$firstFilter}
|
|
|
|
unless ( grep { /\*/ and not m#^[\w:]+/\d+\*?$# }
|
|
|
|
( $filters{$firstFilter} ) );
|
|
|
|
foreach my $k ( keys %filters ) {
|
|
|
|
$self->logger->debug("Removing unless $k =~ /^$filters{$k}\$/");
|
2018-08-22 15:20:16 +02:00
|
|
|
$filters{$k} =~ s/\./\\./g;
|
|
|
|
$filters{$k} =~ s/\*/\.\*/g;
|
|
|
|
foreach my $session ( keys %$res ) {
|
|
|
|
if ( $res->{$session}->{$k} ) {
|
2018-03-10 23:32:03 +01:00
|
|
|
delete $res->{$session}
|
2018-08-22 15:20:16 +02:00
|
|
|
unless ( $res->{$session}->{$k} =~ /^$filters{$k}$/ );
|
2018-03-10 23:32:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-30 22:27:15 +02:00
|
|
|
# Remove sessions without at least one 2F device(s)
|
|
|
|
$self->logger->debug(
|
|
|
|
"Removing sessions without at least one 2F device(s)...");
|
|
|
|
foreach my $session ( keys %$res ) {
|
|
|
|
delete $res->{$session}
|
|
|
|
unless ( defined $res->{$session}->{_2fDevices}
|
|
|
|
and $res->{$session}->{_2fDevices} =~
|
|
|
|
/"type":\s*"(?:U2F|TOTP|UBK)"/s );
|
|
|
|
}
|
2018-06-30 18:19:31 +02:00
|
|
|
|
2018-03-18 13:22:21 +01:00
|
|
|
# Filter 2FA sessions if needed
|
2018-05-04 22:21:22 +02:00
|
|
|
$self->logger->debug("Filtering 2F sessions...");
|
|
|
|
my $all = ( keys %$res );
|
2018-08-26 12:17:54 +02:00
|
|
|
foreach (qw(U2F TOTP UBK)) {
|
2018-08-22 15:20:16 +02:00
|
|
|
if ( $self->{ $_ . 'Check' } eq '2' ) {
|
|
|
|
foreach my $session ( keys %$res ) {
|
|
|
|
delete $res->{$session}
|
|
|
|
unless ( defined $res->{$session}->{_2fDevices}
|
|
|
|
and $res->{$session}->{_2fDevices} =~ /"type":\s*"$_"/s );
|
|
|
|
}
|
|
|
|
$self->logger->debug(
|
|
|
|
"Removing sessions unless a $_ device is registered");
|
2018-03-18 13:22:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 23:32:03 +01:00
|
|
|
my $total = ( keys %$res );
|
2018-06-30 18:19:31 +02:00
|
|
|
$self->logger->debug("2FA session(s) left : $total / $all");
|
2018-03-10 23:32:03 +01:00
|
|
|
|
|
|
|
if ( my $group = $req->params('groupBy') ) {
|
|
|
|
my $r;
|
|
|
|
$group =~ s/\b_whatToTrace\b/$whatToTrace/o;
|
|
|
|
|
|
|
|
# Substrings
|
|
|
|
if ( $group =~ /^substr\((\w+)(?:,(\d+)(?:,(\d+))?)?\)$/ ) {
|
|
|
|
my ( $field, $length, $start ) = ( $1, $2, $3 );
|
|
|
|
$start ||= 0;
|
|
|
|
$length = 1 if ( $length < 1 );
|
|
|
|
foreach my $k ( keys %$res ) {
|
|
|
|
$r->{ substr $res->{$k}->{$field}, $start, $length }++
|
|
|
|
if ( $res->{$k}->{$field} );
|
|
|
|
}
|
|
|
|
$group = $field;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Simple field groupBy query
|
|
|
|
elsif ( $group =~ /^\w+$/ ) {
|
|
|
|
eval {
|
|
|
|
foreach my $k ( keys %$res ) {
|
|
|
|
$r->{ $res->{$k}->{$group} }++;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return $self->sendError(
|
|
|
|
$req,
|
|
|
|
qq{Use of an uninitialized attribute "$group" to group sessions},
|
|
|
|
400
|
|
|
|
) if ($@);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return $self->sendError( $req, 'Syntax error in groupBy', 400 );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Build result
|
|
|
|
$res = [
|
|
|
|
sort {
|
|
|
|
my @a = ( $a->{value} =~ /^(\d+)(?:\.(\d+))*$/ );
|
|
|
|
my @b = ( $b->{value} =~ /^(\d+)(?:\.(\d+))*$/ );
|
|
|
|
( @a and @b )
|
|
|
|
? ( $a[0] <=> $b[0]
|
|
|
|
or $a[1] <=> $b[1]
|
|
|
|
or $a[2] <=> $b[2]
|
|
|
|
or $a[3] <=> $b[3] )
|
|
|
|
: $a->{value} cmp $b->{value}
|
|
|
|
}
|
|
|
|
map { { value => $_, count => $r->{$_} } } keys %$r
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
# Else, $res elements will be like:
|
2018-04-19 13:36:04 +02:00
|
|
|
# { session => <sessionId>, userId => <_session_uid> }
|
2018-03-10 23:32:03 +01:00
|
|
|
else {
|
|
|
|
$res = [
|
|
|
|
sort { $a->{date} <=> $b->{date} }
|
2018-04-19 11:35:42 +02:00
|
|
|
map { { session => $_, userId => $res->{$_}->{_session_uid} } }
|
2018-03-10 23:32:03 +01:00
|
|
|
keys %$res
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $self->sendJSONresponse(
|
|
|
|
$req,
|
|
|
|
{
|
|
|
|
result => 1,
|
|
|
|
count => scalar(@$res),
|
|
|
|
total => $total,
|
|
|
|
values => $res
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|