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

147 lines
3.8 KiB
Perl
Raw Normal View History

2018-02-23 09:11:19 +01:00
package Lemonldap::NG::Portal::2F::REST;
use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_BADOTP
2018-02-23 09:11:19 +01:00
PE_ERROR
PE_FORMEMPTY
PE_OK
PE_SENDRESPONSE
);
2022-07-28 12:42:15 +02:00
our $VERSION = '2.0.15';
2018-02-23 09:11:19 +01:00
2020-10-31 23:43:08 +01:00
extends qw(
Lemonldap::NG::Portal::Lib::Code2F
2020-10-31 23:43:08 +01:00
Lemonldap::NG::Portal::Lib::REST
);
2018-02-23 09:11:19 +01:00
# INITIALIZATION
# Prefix can overriden by sfExtra and is used for routes
has prefix => ( is => 'rw', default => 'rest' );
# Type is used to lookup config
has type => ( is => 'ro', default => 'rest' );
has legend => ( is => 'rw', default => 'enterRest2fCode' );
2018-03-19 16:58:26 +01:00
has initAttrs => ( is => 'rw', default => sub { {} } );
has vrfyAttrs => ( is => 'rw', default => sub { {} } );
2018-02-23 09:11:19 +01:00
sub init {
my ($self) = @_;
if ( $self->code_activation ) {
unless ( $self->conf->{rest2fInitUrl} ) {
$self->logger->error('Missing REST intialization URL');
return 0;
}
}
else {
unless ( $self->conf->{rest2fVerifyUrl} ) {
$self->logger->error('Missing REST verification URL');
return 0;
}
2018-02-23 09:11:19 +01:00
}
2018-02-23 09:11:19 +01:00
foreach my $k ( keys %{ $self->conf->{rest2fInitArgs} } ) {
my $attr = $self->conf->{rest2fInitArgs}->{$k};
$attr =~ s/^$//;
unless ( $attr =~ /^\w+$/ ) {
$self->logger->error(
"2F REST: $k key must point to a single attribute or macro");
return 0;
}
$self->initAttrs->{$k} = $attr;
}
foreach my $k ( keys %{ $self->conf->{rest2fVerifyArgs} } ) {
my $attr = $self->conf->{rest2fVerifyArgs}->{$k};
$attr =~ s/^$//;
unless ( $attr =~ /^\w+$/ ) {
$self->logger->error(
"2F REST: $k key must point to a single attribute or macro");
return 0;
}
$self->vrfyAttrs->{$k} = $attr;
}
$self->prefix( $self->conf->{sfPrefix} ) if ( $self->conf->{sfPrefix} );
2018-03-19 17:34:21 +01:00
return $self->SUPER::init();
2018-02-23 09:11:19 +01:00
}
sub sendCode {
my ( $self, $req, $sessionInfo, $code ) = @_;
2018-02-23 09:11:19 +01:00
if ( $self->conf->{rest2fInitUrl} ) {
# Prepare args
my $args = {
user => $sessionInfo->{ $self->conf->{whatToTrace} },
( $code ? ( code => $code ) : () ),
};
2018-02-23 09:11:19 +01:00
foreach my $k ( keys %{ $self->{initAttrs} } ) {
$args->{$k} = $sessionInfo->{ $self->{initAttrs}->{$k} };
2018-02-23 09:11:19 +01:00
}
# Launch REST request
$self->logger->debug('Call REST init URL');
my $res =
eval { $self->restCall( $self->conf->{rest2fInitUrl}, $args ); };
if ($@) {
$self->logger->error("REST 2F error: $@");
return PE_ERROR;
}
unless ( $res->{result} ) {
$self->logger->error("REST 2F initialization has failed");
return PE_ERROR;
}
}
else {
$self->logger->debug('No init URL, skipping initialization');
}
return 1;
2018-02-23 09:11:19 +01:00
}
sub verify_external {
my ( $self, $req, $session, $code ) = @_;
2018-02-23 09:11:19 +01:00
# Prepare args
my $args = {
user => $session->{ $self->conf->{whatToTrace} },
code => $code,
};
2018-02-23 09:11:19 +01:00
foreach my $k ( keys %{ $self->{vrfyAttrs} } ) {
# in older versions, code was not automatically defined
# if admins defined it explicitely, do not treat it as a session
# attribute
2018-03-13 07:14:01 +01:00
$args->{$k} = (
$k eq 'code'
2018-02-23 09:51:14 +01:00
? $code
: $session->{ $self->{vrfyAttrs}->{$k} }
2018-03-13 07:14:01 +01:00
);
2018-02-23 09:11:19 +01:00
}
# Launch REST request
$self->logger->debug('Call REST vrfy URL');
my $res =
2018-02-23 09:38:33 +01:00
eval { $self->restCall( $self->conf->{rest2fVerifyUrl}, $args ); };
2018-02-23 09:11:19 +01:00
if ($@) {
$self->logger->error("REST 2F error: $@");
return PE_ERROR;
}
# Result
unless ( $res->{result} ) {
$self->userLogger->warn( 'REST Second factor failed for '
. $session->{ $self->conf->{whatToTrace} } );
return PE_BADOTP;
2018-02-23 09:11:19 +01:00
}
2020-02-17 23:22:31 +01:00
return PE_OK;
2018-02-23 09:11:19 +01:00
}
1;