2021-08-10 17:02:45 +02:00
|
|
|
package Lemonldap::NG::Portal::Plugins::NewLocationWarning;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use Mouse;
|
2021-08-11 16:26:05 +02:00
|
|
|
use POSIX qw(strftime);
|
|
|
|
use Lemonldap::NG::Portal::Main::Constants qw(PE_OK);
|
2021-08-10 17:02:45 +02:00
|
|
|
|
|
|
|
our $VERSION = '2.0.13';
|
|
|
|
|
2021-08-11 15:13:44 +02:00
|
|
|
extends qw(
|
|
|
|
Lemonldap::NG::Portal::Lib::SMTP
|
|
|
|
Lemonldap::NG::Portal::Main::Plugin
|
|
|
|
);
|
2021-08-10 17:02:45 +02:00
|
|
|
|
|
|
|
# Entrypoint
|
2021-08-18 17:30:30 +02:00
|
|
|
use constant afterData => 'check';
|
2021-08-10 17:02:45 +02:00
|
|
|
|
|
|
|
sub init {
|
|
|
|
my ($self) = @_;
|
|
|
|
|
2021-08-11 16:26:05 +02:00
|
|
|
if ( $self->conf->{disablePersistentStorage} ) {
|
|
|
|
$self->logger->error(
|
|
|
|
'"NewLocationWarning" plugin enabled WITHOUT persistent session storage"'
|
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
unless ( $self->conf->{loginHistoryEnabled} ) {
|
|
|
|
$self->logger->error(
|
|
|
|
'"NewLocationWarning" plugin enabled WITHOUT "History" plugin');
|
|
|
|
return 0;
|
|
|
|
}
|
2021-08-10 17:02:45 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check {
|
|
|
|
my ( $self, $req ) = @_;
|
|
|
|
my $successLogin = $req->sessionInfo->{_loginHistory}->{successLogin};
|
|
|
|
my $failedLogin = $req->sessionInfo->{_loginHistory}->{failedLogin};
|
|
|
|
my $ipSource = $req->env->{ipAddr};
|
2021-08-18 17:30:30 +02:00
|
|
|
$self->logger->debug("Source IP: $ipSource");
|
2021-08-11 16:26:05 +02:00
|
|
|
|
|
|
|
my @successIPs =
|
2021-08-18 17:30:30 +02:00
|
|
|
map { $_->{ipAddr} ne $ipSource ? $_->{ipAddr} : () } @$successLogin;
|
2021-08-11 16:26:05 +02:00
|
|
|
my @failedIPs =
|
2021-08-18 17:30:30 +02:00
|
|
|
map { $_->{ipAddr} ne $ipSource ? $_->{ipAddr} : () } @$failedLogin;
|
2021-08-11 16:26:05 +02:00
|
|
|
my @IPs = ( @successIPs, @failedIPs );
|
2021-08-18 17:30:30 +02:00
|
|
|
if ( scalar @IPs ) {
|
|
|
|
$self->logger->warn("New location found: $ipSource");
|
|
|
|
return $self->_sendMail($req);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$self->logger->debug('Known location or first connection');
|
|
|
|
return PE_OK;
|
|
|
|
}
|
2021-08-10 17:02:45 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 15:13:44 +02:00
|
|
|
sub _sendMail {
|
2021-08-10 17:02:45 +02:00
|
|
|
my ( $self, $req ) = @_;
|
2021-08-11 16:26:05 +02:00
|
|
|
my $date = strftime( '%F %X', localtime );
|
|
|
|
my $ipSource = $req->env->{ipAddr};
|
|
|
|
my $host = $req->env->{HTTP_HOST};
|
2021-08-19 10:41:18 +02:00
|
|
|
my $url = $self->conf->{portal};
|
2021-08-10 17:02:45 +02:00
|
|
|
my $mail =
|
|
|
|
$req->sessionInfo->{ $self->conf->{newLocationWarningMailAttribute}
|
|
|
|
|| 'mail' };
|
2021-08-11 15:13:44 +02:00
|
|
|
|
2021-08-11 16:26:05 +02:00
|
|
|
# Build mail content
|
2021-08-11 15:13:44 +02:00
|
|
|
my $tr = $self->translate($req);
|
|
|
|
my $subject = $self->conf->{newLocationWarningMailSubject};
|
|
|
|
unless ($subject) {
|
2021-08-11 16:26:05 +02:00
|
|
|
$self->logger->debug('Use default warning subject');
|
2021-08-11 15:13:44 +02:00
|
|
|
$subject = 'newLocationWarningMailSubject';
|
|
|
|
$tr->( \$subject );
|
|
|
|
}
|
2021-08-11 16:26:05 +02:00
|
|
|
my ( $body, $html );
|
2021-08-11 15:13:44 +02:00
|
|
|
if ( $self->conf->{newLocationWarningMailBody} ) {
|
|
|
|
|
|
|
|
# We use a specific text message, no html
|
2021-08-11 16:26:05 +02:00
|
|
|
$self->logger->debug('Use specific warning body message');
|
2021-08-11 15:13:44 +02:00
|
|
|
$body = $self->conf->{newLocationWarningMailBody};
|
|
|
|
|
|
|
|
# Replace variables in body
|
|
|
|
$body =~ s/\$newLocationIP/$ipSource/ge;
|
2021-08-11 16:26:05 +02:00
|
|
|
$body =~ s/\$newLocationDate/$date/ge;
|
2021-08-19 10:41:18 +02:00
|
|
|
$body =~ s/\$newLocationUrl/$url/ge;
|
2021-08-11 15:13:44 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
# Use HTML template
|
|
|
|
$body = $self->loadMailTemplate(
|
|
|
|
$req,
|
|
|
|
'mail_new_location_warning',
|
|
|
|
filter => $tr,
|
|
|
|
params => {
|
|
|
|
session_ipAddr => $ipSource,
|
2021-08-11 16:26:05 +02:00
|
|
|
date => $date,
|
|
|
|
host => $host,
|
2021-08-11 15:13:44 +02:00
|
|
|
},
|
|
|
|
);
|
|
|
|
$html = 1;
|
|
|
|
}
|
2021-08-19 10:41:18 +02:00
|
|
|
if ( $mail && $subject && $body ) {
|
|
|
|
$self->logger->warn("User $mail is signing in from a new location");
|
2021-08-11 15:13:44 +02:00
|
|
|
|
2021-08-19 10:41:18 +02:00
|
|
|
# Send mail
|
|
|
|
$self->logger->debug('Unable to send new location warning mail')
|
|
|
|
unless ( $self->send_mail( $mail, $subject, $body, $html ) );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
$self->logger->error('Unable to send new location warning mail: missing parameter(s)');
|
|
|
|
}
|
2021-08-10 17:02:45 +02:00
|
|
|
return PE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|