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

75 lines
1.8 KiB
Perl
Raw Normal View History

package Lemonldap::NG::Portal::Auth::REST;
use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
2021-02-01 22:30:37 +01:00
PE_OK
PE_ERROR
PE_BADCREDENTIALS
);
2021-02-01 22:30:37 +01:00
our $VERSION = '2.0.12';
2021-02-01 22:30:37 +01:00
extends qw(
Lemonldap::NG::Portal::Auth::_WebForm
Lemonldap::NG::Portal::Lib::REST
);
# INITIALIZATION
sub init {
my $self = shift;
# Add warning in log
unless ( $self->conf->{restAuthUrl} ) {
2021-03-17 14:22:19 +01:00
$self->logger->error('No REST Authentication URL given');
return 0;
}
return $self->Lemonldap::NG::Portal::Auth::_WebForm::init();
}
sub authenticate {
my ( $self, $req ) = @_;
my $res = eval {
$self->restCall( $self->conf->{restAuthUrl},
{ user => $req->user, password => $req->data->{password} } );
};
if ($@) {
$self->logger->error("Auth error: $@");
$self->setSecurity($req);
return PE_ERROR;
}
2019-08-22 15:17:51 +02:00
$self->logger->debug( "REST result:" . ( $res->{result} || 'undef' ) );
if ( $res->{info} ) {
eval {
$self->logger->debug(" $_ => $res->{info}->{$_}")
foreach ( keys %{ $res->{info} } );
};
}
$self->logger->error( 'No "info": ' . $@ ) if ($@);
unless ( $res->{result} ) {
$self->userLogger->warn(
"Bad credentials for " . $req->user . ' (' . $req->address . ')' );
$self->setSecurity($req);
return PE_BADCREDENTIALS;
}
$req->data->{restAuthInfo} = $res->{info} || {};
return PE_OK;
}
sub setAuthSessionInfo {
my ( $self, $req ) = @_;
$self->SUPER::setAuthSessionInfo($req);
$req->sessionInfo->{$_} = $req->data->{restAuthInfo}->{$_}
foreach ( keys %{ $req->data->{restAuthInfo} } );
2019-04-05 22:58:48 +02:00
$req->sessionInfo->{authenticationLevel} = $self->conf->{restAuthnLevel};
return PE_OK;
}
sub authLogout {
2021-02-01 22:30:37 +01:00
return PE_OK;
}
1;