lemonldap-ng/lemonldap-ng-portal/lib/Lemonldap/NG/Portal/2F/Ext2F.pm

95 lines
2.2 KiB
Perl
Raw Normal View History

2018-03-09 07:17:25 +01:00
package Lemonldap::NG::Portal::2F::Ext2F;
2017-03-22 23:18:28 +01:00
use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_BADCREDENTIALS
PE_ERROR
PE_FORMEMPTY
PE_OK
PE_SENDRESPONSE
);
our $VERSION = '2.0.0';
2017-03-23 12:17:01 +01:00
extends 'Lemonldap::NG::Portal::Main::SecondFactor';
2017-03-22 23:18:28 +01:00
# INITIALIZATION
2017-03-23 12:17:01 +01:00
has prefix => ( is => 'ro', default => 'ext' );
2017-03-23 07:20:06 +01:00
2017-03-22 23:18:28 +01:00
sub init {
my ($self) = @_;
foreach (qw(ext2FSendCommand ext2FValidateCommand)) {
unless ( $self->conf->{$_} ) {
$self->error("Missing $_ parameter, aborting");
return 0;
}
}
2018-03-15 22:22:20 +01:00
$self->logo( $self->conf->{ext2fLogo} ) if ( $self->conf->{ext2fLogo} );
2017-03-23 12:17:01 +01:00
return $self->SUPER::init();
2017-03-22 23:18:28 +01:00
}
2017-03-23 12:17:01 +01:00
# RUNNING METHODS
2017-03-23 07:20:06 +01:00
2017-03-23 12:17:01 +01:00
sub run {
my ( $self, $req, $token ) = @_;
2017-03-22 23:18:28 +01:00
# Prepare command and launch it
2017-03-23 07:20:06 +01:00
if ( my $c =
$self->launch( $req->sessionInfo, $self->conf->{ext2FSendCommand} ) )
{
$self->logger->error("External send command failed (code $c)");
2017-03-22 23:18:28 +01:00
return $self->p->do( $req, [ sub { PE_ERROR } ] );
}
# Prepare form
my $tmp = $self->p->sendHtml(
$req,
'ext2fcheck',
params => {
SKIN => $self->conf->{portalSkin},
TOKEN => $token
}
);
$self->logger->debug("Prepare external 2F verification");
2017-03-22 23:18:28 +01:00
$req->response($tmp);
return PE_SENDRESPONSE;
}
sub verify {
2017-03-23 12:17:01 +01:00
my ( $self, $req, $session ) = @_;
2017-03-22 23:18:28 +01:00
my $code;
unless ( $code = $req->param('code') ) {
$self->userLogger->error('External 2F: no code');
2017-03-23 12:17:01 +01:00
return PE_FORMEMPTY;
2017-03-22 23:18:28 +01:00
}
# Prepare command and launch it
2017-03-23 07:20:06 +01:00
if ( my $c =
$self->launch( $session, $self->conf->{ext2FValidateCommand}, $code ) )
{
$self->userLogger->warn( 'Second factor failed for '
. $session->{ $self->conf->{whatToTrace} } );
2017-03-23 12:17:01 +01:00
return PE_BADCREDENTIALS;
2017-03-22 23:18:28 +01:00
}
PE_OK;
2017-03-22 23:18:28 +01:00
}
# system() is used with an array to avoid shell injection
sub launch {
my ( $self, $session, $command, $code ) = @_;
my @args;
foreach ( split( /\s+/, $command ) ) {
if ( defined $code ) {
s#\$code\b#$code#g;
}
s#\$(\w+)#$session->{$1} // ''#ge;
push @args, $_;
}
return system @args;
}
2017-03-22 23:18:28 +01:00
1;