Skip to content

Commit a916caa

Browse files
authored
Merge pull request #102 from rappasoft/develop
Develop
2 parents ab9c521 + b4322c9 commit a916caa

12 files changed

+113
-11
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
All notable changes to `Laravel Authentication Log` will be documented in this file.
44

5+
### 4.0.0 - 2024-03-28
6+
7+
- Laravel 11 Support (https://github.com/rappasoft/laravel-authentication-log/pull/100)
8+
- Add config listeners (https://github.com/rappasoft/laravel-authentication-log/pull/92)
9+
- Use real user IP behind Cloudflare
10+
- Check for AuthenticationLoggable trait on event (https://github.com/rappasoft/laravel-authentication-log/pull/94)
11+
- Added PHPDocs to allow autocompletion in IDE (https://github.com/rappasoft/laravel-authentication-log/pull/80)
12+
- Fixes the down method for php artisan migrate:rollback (https://github.com/rappasoft/laravel-authentication-log/pull/93)
13+
514
### 3.0.0 - 2023-02-23
615

716
- Laravel 10 Support - https://github.com/rappasoft/laravel-authentication-log/pull/70

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
![Package Logo](https://banners.beyondco.de/Laravel%20Authentication%20Log.png?theme=dark&packageManager=composer+require&packageName=rappasoft%2Flaravel-authentication-log&pattern=hideout&style=style_1&description=Log+user+authentication+details+and+send+new+device+notifications.&md=1&showWatermark=0&fontSize=100px&images=lock-closed)
22

33
[![Latest Version on Packagist](https://img.shields.io/packagist/v/rappasoft/laravel-authentication-log.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-authentication-log)
4-
[![GitHub Tests Action Status](https://img.shields.io/github/workflow/status/rappasoft/laravel-authentication-log/run-tests?label=tests)](https://github.com/rappasoft/laravel-authentication-log/actions?query=workflow%3Arun-tests+branch%3Amain)
5-
[![GitHub Code Style Action Status](https://img.shields.io/github/workflow/status/rappasoft/laravel-authentication-log/Check%20&%20fix%20styling?label=code%20style)](https://github.com/rappasoft/laravel-authentication-log/actions?query=workflow%3A"Check+%26+fix+styling"+branch%3Amain)
64
[![Total Downloads](https://img.shields.io/packagist/dt/rappasoft/laravel-authentication-log.svg?style=flat-square)](https://packagist.org/packages/rappasoft/laravel-authentication-log)
75

86
Laravel Authentication Log is a package which tracks your user's authentication information such as login/logout time, IP, Browser, Location, etc. as well as sends out notifications via mail, slack, or sms for new devices and failed logins.
@@ -18,6 +16,7 @@ See the [documentation](https://rappasoft.com/docs/laravel-authentication-log) f
1816
8.x | 1.x
1917
9.x | 2.x
2018
10.x | 3.x
19+
11.x | 4.x
2120

2221
## Installation
2322

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^8.1",
20-
"illuminate/contracts": "^10.0",
20+
"illuminate/contracts": "^10.0|^11.0",
2121
"spatie/laravel-package-tools": "^1.4.3"
2222
},
2323
"require-dev": {

config/authentication-log.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
1717
],
1818

19+
'listeners' => [
20+
'login' => \Rappasoft\LaravelAuthenticationLog\Listeners\LoginListener::class,
21+
'failed' => \Rappasoft\LaravelAuthenticationLog\Listeners\FailedLoginListener::class,
22+
'logout' => \Rappasoft\LaravelAuthenticationLog\Listeners\LogoutListener::class,
23+
'logout-other-devices' => \Rappasoft\LaravelAuthenticationLog\Listeners\OtherDeviceLogoutListener::class,
24+
],
25+
1926
'notifications' => [
2027
'new-device' => [
2128
// Send the NewDevice notification
@@ -42,4 +49,13 @@
4249
// When the clean-up command is run, delete old logs greater than `purge` days
4350
// Don't schedule the clean-up command if you want to keep logs forever.
4451
'purge' => 365,
52+
53+
// If you are behind an CDN proxy, set 'behind_cdn.http_header_field' to the corresponding http header field of your cdn
54+
// For cloudflare you can have look at: https://developers.cloudflare.com/fundamentals/get-started/reference/http-request-headers/
55+
// 'behind_cdn' => [
56+
// 'http_header_field' => 'HTTP_CF_CONNECTING_IP' // used by Cloudflare
57+
// ],
58+
59+
// If you are not a cdn user, use false
60+
'behind_cdn' => false,
4561
];

database/migrations/create_authentication_log_table.php.stub

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ return new class extends Migration
2020
$table->json('location')->nullable();
2121
});
2222
}
23+
24+
public function down(): void
25+
{
26+
Schema::dropIfExists(config('authentication-log.table_name'));
27+
}
2328
};

resources/lang/pt_BR.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"A failed login to your account": "Um login com falha em sua conta",
3+
"Account:": "Conta",
4+
"Browser:": "Navegador:",
5+
"If this was you, you can ignore this alert. If you suspect any suspicious activity on your account, please change your password.": "Se foi você, pode ignorar este alerta. Se você suspeitar de qualquer atividade suspeita em sua conta, altere sua senha.",
6+
"IP Address:": "Endereço de IP:",
7+
"Location:": "Localização:",
8+
"Regards,": "Obrigado,",
9+
"There has been a failed login attempt to your :app account.": "Houve uma falha na tentativa de login em seu :app conta.",
10+
"Time:": "Data:",
11+
"Unknown City": "Cidade Desconhecida",
12+
"Unknown State": "Estado Desconhecido",
13+
"Your :app account logged in from a new device.": "Sua :app foi acessada em um novo dispositivo"
14+
}

src/LaravelAuthenticationLogServiceProvider.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function configurePackage(Package $package): void
2828
->hasCommand(PurgeAuthenticationLogCommand::class);
2929

3030
$events = $this->app->make(Dispatcher::class);
31-
$events->listen(config('authentication-log.events.login', Login::class), LoginListener::class);
32-
$events->listen(config('authentication-log.events.failed', Failed::class), FailedLoginListener::class);
33-
$events->listen(config('authentication-log.events.logout', Logout::class), LogoutListener::class);
34-
$events->listen(config('authentication-log.events.other-device-logout', OtherDeviceLogout::class), OtherDeviceLogoutListener::class);
31+
$events->listen(config('authentication-log.events.login', Login::class), config('authentication-log.listeners.login', LoginListener::class));
32+
$events->listen(config('authentication-log.events.failed', Failed::class), config('authentication-log.listeners.failed', FailedLoginListener::class));
33+
$events->listen(config('authentication-log.events.logout', Logout::class), config('authentication-log.listeners.logout', LogoutListener::class));
34+
$events->listen(config('authentication-log.events.other-device-logout', OtherDeviceLogout::class), config('authentication-log.listeners.other-device-logout', OtherDeviceLogoutListener::class));
3535
}
3636
}

src/Listeners/FailedLoginListener.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Auth\Events\Failed;
66
use Illuminate\Http\Request;
77
use Rappasoft\LaravelAuthenticationLog\Notifications\FailedLogin;
8+
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
89

910
class FailedLoginListener
1011
{
@@ -18,13 +19,24 @@ public function __construct(Request $request)
1819
public function handle($event): void
1920
{
2021
$listener = config('authentication-log.events.failed', Failed::class);
22+
2123
if (! $event instanceof $listener) {
2224
return;
2325
}
2426

2527
if ($event->user) {
28+
if(! in_array(AuthenticationLoggable::class, class_uses_recursive(get_class($event->user)))) {
29+
return;
30+
}
31+
32+
if (config('authentication-log.behind_cdn')) {
33+
$ip = $this->request->server(config('authentication-log.behind_cdn.http_header_field'));
34+
} else {
35+
$ip = $this->request->ip();
36+
}
37+
2638
$log = $event->user->authentications()->create([
27-
'ip_address' => $ip = $this->request->ip(),
39+
'ip_address' => $ip,
2840
'user_agent' => $this->request->userAgent(),
2941
'login_at' => now(),
3042
'login_successful' => false,

src/Listeners/LoginListener.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Support\Carbon;
88
use Rappasoft\LaravelAuthenticationLog\Notifications\NewDevice;
9+
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
910

1011
class LoginListener
1112
{
@@ -19,13 +20,23 @@ public function __construct(Request $request)
1920
public function handle($event): void
2021
{
2122
$listener = config('authentication-log.events.login', Login::class);
23+
2224
if (! $event instanceof $listener) {
2325
return;
2426
}
2527

2628
if ($event->user) {
29+
if(! in_array(AuthenticationLoggable::class, class_uses_recursive(get_class($event->user)))) {
30+
return;
31+
}
32+
33+
if (config('authentication-log.behind_cdn')) {
34+
$ip = $this->request->server(config('authentication-log.behind_cdn.http_header_field'));
35+
} else {
36+
$ip = $this->request->ip();
37+
}
38+
2739
$user = $event->user;
28-
$ip = $this->request->ip();
2940
$userAgent = $this->request->userAgent();
3041
$known = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->whereLoginSuccessful(true)->first();
3142
$newUser = Carbon::parse($user->{$user->getCreatedAtColumn()})->diffInMinutes(Carbon::now()) < 1;

src/Listeners/LogoutListener.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Auth\Events\Logout;
66
use Illuminate\Http\Request;
77
use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
8+
use Rappasoft\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
89

910
class LogoutListener
1011
{
@@ -18,13 +19,24 @@ public function __construct(Request $request)
1819
public function handle($event): void
1920
{
2021
$listener = config('authentication-log.events.logout', Logout::class);
22+
2123
if (! $event instanceof $listener) {
2224
return;
2325
}
2426

2527
if ($event->user) {
28+
if(! in_array(AuthenticationLoggable::class, class_uses_recursive(get_class($event->user)))) {
29+
return;
30+
}
31+
2632
$user = $event->user;
27-
$ip = $this->request->ip();
33+
34+
if (config('authentication-log.behind_cdn')) {
35+
$ip = $this->request->server(config('authentication-log.behind_cdn.http_header_field'));
36+
} else {
37+
$ip = $this->request->ip();
38+
}
39+
2840
$userAgent = $this->request->userAgent();
2941
$log = $user->authentications()->whereIpAddress($ip)->whereUserAgent($userAgent)->orderByDesc('login_at')->first();
3042

0 commit comments

Comments
 (0)