| 1 | package Lemonldap::NG::Portal::Plugins::NewLocationWarning; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Mouse; | ||
| 5 | use POSIX qw(strftime); | ||
| 6 | use Lemonldap::NG::Portal::Main::Constants qw(PE_OK); | ||
| 7 | use List::MoreUtils qw/uniq/; | ||
| 8 | |||
| 9 | our $VERSION = '2.23.0'; | ||
| 10 | |||
| 11 | has locationAttribute => ( is => 'rw' ); | ||
| 12 | has locationDisplayAttribute => ( is => 'rw' ); | ||
| 13 | has locationMaxValues => ( is => 'rw' ); | ||
| 14 | has mailSessionKey => ( | ||
| 15 | is => 'ro', | ||
| 16 | lazy => 1, | ||
| 17 | default => sub { | ||
| 18 | return | ||
| 19 | $_[0]->{conf}->{newLocationWarningMailAttribute} | ||
| 20 | || $_[0]->{conf}->{mailSessionKey} | ||
| 21 | || 'mail'; | ||
| 22 | } | ||
| 23 | ); | ||
| 24 | |||
| 25 | extends qw( | ||
| 26 | Lemonldap::NG::Portal::Lib::SMTP | ||
| 27 | Lemonldap::NG::Portal::Main::Plugin | ||
| 28 | ); | ||
| 29 | |||
| 30 | # Entrypoint | ||
| 31 | use constant afterSub => { setLocalGroups => 'checkNewLocation' }; | ||
| 32 | use constant endAuth => 'sendWarningEmail'; | ||
| 33 | |||
| 34 | sub init { | ||
| 35 | my ($self) = @_; | ||
| 36 | |||
| 37 | if ( $self->conf->{disablePersistentStorage} ) { | ||
| 38 | $self->logger->error( | ||
| 39 | '"NewLocationWarning" plugin enabled WITHOUT persistent session storage"' | ||
| 40 | ); | ||
| 41 | return 0; | ||
| 42 | } | ||
| 43 | unless ( $self->conf->{loginHistoryEnabled} ) { | ||
| 44 | $self->logger->error( | ||
| 45 | '"NewLocationWarning" plugin enabled WITHOUT "History" plugin'); | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | $self->locationAttribute( $self->conf->{newLocationWarningLocationAttribute} | ||
| 50 | || 'ipAddr' ); | ||
| 51 | $self->locationDisplayAttribute( | ||
| 52 | $self->conf->{newLocationWarningLocationDisplayAttribute} | ||
| 53 | || $self->locationAttribute ); | ||
| 54 | $self->locationMaxValues( $self->conf->{newLocationWarningMaxValues} || 0 ); | ||
| 55 | |||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub checkNewLocation { | ||
| 60 | my ( $self, $req ) = @_; | ||
| 61 | my $successLogin = $req->sessionInfo->{_loginHistory}->{successLogin} || []; | ||
| 62 | my $location = $req->sessionInfo->{ $self->locationAttribute }; | ||
| 63 | my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} }; | ||
| 64 | |||
| 65 | $self->logger->debug( "Could not find location of user " . $user ) | ||
| 66 | unless $location; | ||
| 67 | |||
| 68 | # Get all non-empty, unique values of location attribute through list of | ||
| 69 | # successful logins | ||
| 70 | my @envHistory = | ||
| 71 | grep { $_ // "" } | ||
| 72 | uniq( map { $_->{ $self->locationAttribute } // "" } @{$successLogin} ); | ||
| 73 | |||
| 74 | # Only consider some of the past unique locations | ||
| 75 | my $maxLocations = $self->locationMaxValues; | ||
| 76 | splice @envHistory, $maxLocations | ||
| 77 | if ( $maxLocations and ( scalar @envHistory > $maxLocations ) ); | ||
| 78 | |||
| 79 | if ( grep { $_ eq $location } @envHistory ) { | ||
| 80 | $self->userLogger->debug( | ||
| 81 | "User $user logged in from known location $location"); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | # Not the first location in history, warn if new location | ||
| 85 | if (@envHistory) { | ||
| 86 | $self->userLogger->info( | ||
| 87 | "User $user logged in from unknown location $location"); | ||
| 88 | my $riskLevel = ( $req->sessionInfo->{_riskLevel} || 0 ) + 1; | ||
| 89 | $req->sessionInfo->{_riskLevel} = $riskLevel; | ||
| 90 | $req->sessionInfo->{_riskDetails}->{newLocation} = | ||
| 91 | $req->sessionInfo->{ $self->locationDisplayAttribute }; | ||
| 92 | } | ||
| 93 | else { | ||
| 94 | $self->userLogger->info( | ||
| 95 | "User $user logged with empty location history " | ||
| 96 | . "from location $location" ); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | return PE_OK; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub sendWarningEmail { | ||
| 103 | my ( $self, $req ) = @_; | ||
| 104 | |||
| 105 | if ( $req->sessionInfo->{_riskDetails}->{newLocation} ) { | ||
| 106 | my $mail = $req->sessionInfo->{ $self->mailSessionKey }; | ||
| 107 | my $user = $req->sessionInfo->{ $self->conf->{whatToTrace} }; | ||
| 108 | if ($mail) { | ||
| 109 | $self->userLogger->info( | ||
| 110 | "User $user is signing in from a new location"); | ||
| 111 | return $self->_sendMail( $req, $mail ); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $self->logger->warn( "User $user is signing in from a new location" | ||
| 115 | . " but has no configured email" ); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | return PE_OK; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub _sendMail { | ||
| 123 | my ( $self, $req, $mail ) = @_; | ||
| 124 | my $date = strftime( '%F %X (UTC%z)', localtime ); | ||
| 125 | my $location = $req->sessionInfo->{_riskDetails}->{newLocation}; | ||
| 126 | my $ua = $req->env->{HTTP_USER_AGENT}; | ||
| 127 | |||
| 128 | $self->sendEmail( | ||
| 129 | $req, | ||
| 130 | subject => $self->conf->{newLocationWarningMailSubject}, | ||
| 131 | subject_trmsg => 'newLocationWarningMailSubject', | ||
| 132 | body => $self->conf->{newLocationWarningMailBody}, | ||
| 133 | body_template => 'mail_new_location_warning', | ||
| 134 | dest => $mail, | ||
| 135 | params => { | ||
| 136 | location => $location, | ||
| 137 | date => $date, | ||
| 138 | ua => $ua | ||
| 139 | }, | ||
| 140 | ); | ||
| 141 | |||
| 142 | return PE_OK; | ||
| 143 | } | ||
| 144 | |||
| 145 | 1; |