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

117 lines
2.7 KiB
Perl
Raw Normal View History

2016-05-01 09:30:21 +02:00
package Lemonldap::NG::Portal::Auth::LDAP;
use strict;
use Mouse;
use Lemonldap::NG::Portal::Main::Constants qw(
PE_OK
PE_DONE
PE_ERROR
PE_LDAPCONNECTFAILED
PE_PP_ACCOUNT_LOCKED
PE_PP_PASSWORD_EXPIRED
PE_PP_CHANGE_AFTER_RESET
);
2016-05-01 09:30:21 +02:00
2022-02-03 11:20:47 +01:00
our $VERSION = '2.0.14';
2016-05-01 09:30:21 +02:00
2016-05-02 12:30:23 +02:00
# Inheritance: UserDB::LDAP provides all needed ldap functions
extends qw(
Lemonldap::NG::Portal::Lib::LDAP
Lemonldap::NG::Portal::Auth::_WebForm
);
2017-01-15 14:18:01 +01:00
sub init {
my ($self) = @_;
2017-01-27 23:40:17 +01:00
return ( $self->Lemonldap::NG::Portal::Auth::_WebForm::init
and $self->Lemonldap::NG::Portal::Lib::LDAP::init );
2017-01-15 14:18:01 +01:00
}
2016-05-01 09:30:21 +02:00
has authnLevel => (
is => 'rw',
lazy => 1,
default => sub {
$_[0]->conf->{ldapAuthnLevel};
}
);
2016-06-09 20:40:20 +02:00
# RUNNING METHODS
2016-05-01 09:30:21 +02:00
sub authenticate {
2016-05-04 13:38:49 +02:00
my ( $self, $req ) = @_;
2016-05-01 09:30:21 +02:00
# Set the dn unless done before
unless ( $req->data->{dn} ) {
if ( my $tmp = $self->getUser($req) ) {
eval { $self->setSecurity($req) };
$self->logger->warn($@) if ($@);
2016-05-01 09:30:21 +02:00
return $tmp;
}
}
unless ( $req->data->{password} ) {
$self->p->{user} = $req->userData->{_dn} = $req->data->{dn};
2019-03-07 18:22:16 +01:00
unless ( $self->p->{_passwordDB} ) {
$self->logger->error('No password database configured, aborting');
return PE_ERROR;
}
my $res = $self->p->{_passwordDB}->_modifyPassword( $req, 1 );
# Refresh entry
2019-03-29 11:53:52 +01:00
if ( $self->p->{_userDB}->getUser($req) != PE_OK ) {
$self->logger->error(
"Unable to refresh entry for " . $self->p->{user} );
}
$req->data->{noerror} = 1;
$self->setSecurity($req);
# Security: never create session here
return $res || PE_DONE;
}
$self->validateLdap;
unless ( $self->ldap ) {
return PE_LDAPCONNECTFAILED;
}
2016-05-01 09:30:21 +02:00
my $res =
$self->ldap->userBind( $req, $req->data->{dn},
password => $req->data->{password} );
2019-06-05 15:25:50 +02:00
$self->setSecurity($req) if ( $res > PE_OK );
2016-05-01 09:30:21 +02:00
# Remember password if password reset needed
if (
$res == PE_PP_CHANGE_AFTER_RESET
or ( $res == PE_PP_PASSWORD_EXPIRED
and $self->conf->{ldapAllowResetExpiredPassword} )
)
{
$req->data->{oldpassword} = $req->data->{password}; # Fix 2377
$req->data->{noerror} = 1;
$self->setSecurity($req);
}
2016-05-01 09:30:21 +02:00
return $res;
}
sub authLogout {
return PE_OK;
2016-05-01 09:30:21 +02:00
}
# Define which error codes will stop Combination process
# @param res error code
# @return result 1 if stop is needed
sub stop {
my ( $self, $res ) = @_;
return 1
if ( $res == PE_PP_PASSWORD_EXPIRED
or $res == PE_PP_ACCOUNT_LOCKED
or $res == PE_PP_CHANGE_AFTER_RESET );
return 0;
}
2016-05-01 09:30:21 +02:00
1;