siberfx / authentication-logger
记录用户认证详情并发送新设备通知。
该软件包的官方仓库似乎已不存在,因此该软件包已被冻结。
Requires
- php: ^8.1|^8.2
- illuminate/contracts: ^10.0
Requires (Dev)
- nunomaduro/collision: ^6.4
- orchestra/testbench: ^6.6
README
Authentication Logger 是一个软件包,它跟踪用户的认证信息,如登录/注销时间、IP、浏览器、位置等,并可以通过邮件、slack 或短信发送新设备通知和失败的登录通知。
安装
您可以通过 composer 安装此软件包
composer require siberfx/authentication-log
如果您想使用位置功能,也必须安装 torann/geoip
composer require torann/geoip
您可以使用以下命令发布和运行迁移
php artisan vendor:publish --provider="Siberfx\AuthenticationLogger\AuthenticationLogServiceProviderger" --tag="authentication-log-migrations" php artisan migrate
您可以使用以下命令发布视图/邮件文件
php artisan vendor:publish --provider="Siberfx\AuthenticationLogger\AuthenticationLogServiceProviderger" --tag="authentication-log-views"
您可以使用以下命令发布配置文件
php artisan vendor:publish --provider="Siberfx\AuthenticationLogger\AuthenticationLogServiceProviderger" --tag="authentication-log-config"
这是已发布配置文件的内容
return [ // The database table name // You can change this if the database keys get too long for your driver 'table_name' => 'authentication_log', 'notifications' => [ 'new-device' => [ // Send the NewDevice notification 'enabled' => env('NEW_DEVICE_NOTIFICATION', true), // Use torann/geoip to attempt to get a location 'location' => true, // The Notification class to send 'template' => \Siberfx\AuthenticationLogger\Notifications\NewDevice::class, ], 'failed-login' => [ // Send the FailedLogin notification 'enabled' => env('FAILED_LOGIN_NOTIFICATION', false), // Use torann/geoip to attempt to get a location 'location' => true, // The Notification class to send 'template' => \Siberfx\AuthenticationLogger\Notifications\FailedLogin::class, ], ], // When the clean-up command is run, delete old logs greater than `purge` days // Don't schedule the clean-up command if you want to keep logs forever. 'purge' => 60, ];
如果您安装了 torann/geoip
,您也应该发布该配置文件以设置默认值
php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config
配置
您必须将 AuthenticationLoggable
和 Notifiable
特性添加到您想跟踪的模型中。
use Illuminate\Notifications\Notifiable; use Siberfx\AuthenticationLogger\Traits\AuthenticationLoggable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Notifiable, AuthenticationLoggable; }
该软件包将监听 Laravel 的登录、注销、失败和其他设备注销事件。
用法
获取用户的全部认证日志
User::find(1)->authentications;
获取用户的最后登录信息
User::find(1)->lastLoginAt(); User::find(1)->lastSuccessfulLoginAt(); User::find(1)->lastLoginIp(); User::find(1)->lastSuccessfulLoginIp();
获取用户的先前登录时间 & IP 地址(忽略当前登录)
auth()->user()->previousLoginAt(); auth()->user()->previousLoginIp();
通知
通知可以在 mail
、nexmo
和 slack
通道发送,但默认通过电子邮件发送。
您可以在可认证模型上定义一个 notifyAuthenticationLogVia
方法来决定通知应该发送到哪些通道
public function notifyAuthenticationLogVia() { return ['nexmo', 'mail', 'slack']; }
您必须安装 Slack 和 Nexmo 驱动程序来使用这些路由,并遵循它们针对特定可认证模型设置的文档。
新设备通知
默认启用,它们使用 \Siberfx\AuthenticationLogger\Notifications\NewDevice
类,该类可以在配置文件中重写。
失败的登录通知
默认禁用,它们使用 \Siberfx\AuthenticationLogger\Notifications\FailedLogin
类,该类可以在配置文件中重写。
位置
如果已安装 torann/geoip
软件包,它将默认尝试将位置信息包含在通知中。
您可以在每个模板的配置中关闭此功能。
注意:默认情况下,在本地工作时,不会记录任何位置,因为它会从 geoip
配置文件发送回 默认地址
。您可以在电子邮件模板中覆盖此行为。
清除旧日志
在您的 config/authentication-log.php
文件中指定的天数之前记录的记录将被删除。
'purge' => 60,
您还可以使用 Laravel 8.50 版本中提供的 Prunnable
调度此命令。
$schedule->command('model:prune')->daily();
显示日志
注意:此示例使用 jenssegers/agent
软件包,该软件包默认包含在 Laravel Jetstream 中,以及 jamesmills/laravel-timezone
以在用户的本地时区显示时区。两者都是可选的,请根据您的需求修改表格。
use Illuminate\Support\ServiceProvider; use Illuminate\Auth\Events\Failed; use Illuminate\Auth\Events\Login; use Illuminate\Auth\Events\Logout; use Siberfx\AuthenticationLogger\Listeners\FailedLoginListener; use Siberfx\AuthenticationLogger\Listeners\LoginListener; use Siberfx\AuthenticationLogger\Listeners\LogoutListener; use Siberfx\AuthenticationLogger\Listeners\OtherDeviceLogoutListener; $events->listen(Login::class, LoginListener::class); $events->listen(Failed::class, FailedLoginListener::class); $events->listen(Logout::class, LogoutListener::class); $events->listen(OtherDeviceLogout::class, OtherDeviceLogoutListener::class);