pearldrift/laravel-authentication-log

记录用户认证详情并发送新设备通知。

v1.0 2022-01-07 11:57 UTC

This package is auto-updated.

Last update: 2024-09-11 01:59:27 UTC


README

Package Logo

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel Authentication Log是一个跟踪用户认证信息(如登录/登出时间、IP、浏览器、位置等)的包,并通过邮件、Slack或短信为新的设备和失败的登录发送通知。

文档、安装和用法说明

安装

Laravel Authentication Log需要Laravel 5.5或更高版本,以及PHP 7.0+。

您可以使用Composer将Laravel Authentication Log安装到您的Laravel项目中

  composer require pearldrift/laravel-authentication-log
  
  composer require torann/geoip

配置

安装Laravel Authentication Log后,使用vendor:publish Artisan命令发布其配置、迁移和视图

  php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-migrations"

接下来,您需要迁移您的数据库。Laravel Authentication Log迁移将创建应用程序存储认证日志所需的表

   php artisan migrate

您可以使用以下命令发布视图/电子邮件文件

  php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --tag="authentication-log-views"

最后,将AuthenticationLogableNotifiable特质添加到您的认证模型中(默认为App\User模型)。这些特质提供了各种方法,允许您获取常见的认证日志数据,例如最后登录时间、最后登录IP地址,并设置当用户从新设备登录时通知用户的通道

您可以使用以下命令发布配置文件

     php artisan vendor:publish --provider="Pearldrift\LaravelAuthenticationLog\LaravelAuthenticationLogServiceProvider" --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',

      // The database connection where the authentication_log table resides. Leave empty to use the default
      'db_connection' => null,

      // The events the package listens for to log (as of v1.3)
      'events' => [
          'login' => \Illuminate\Auth\Events\Login::class,
          'failed' => \Illuminate\Auth\Events\Failed::class,
          'logout' => \Illuminate\Auth\Events\Logout::class,
          'logout-other-devices' => \Illuminate\Auth\Events\OtherDeviceLogout::class,
      ],

      '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' => \Pearldrift\LaravelAuthenticationLog\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' => \Pearldrift\LaravelAuthenticationLog\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' => 365,
  ];

如果您安装了torann/geoip,还应该发布该配置文件以设置默认值

php artisan vendor:publish --provider="Torann\GeoIP\GeoIPServiceProvider" --tag=config

设置您的模型

您必须将AuthenticationLoggableNotifiable特质添加到您要跟踪的模型中。

use Illuminate\Notifications\Notifiable;
use Pearldrift\LaravelAuthenticationLog\Traits\AuthenticationLoggable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable, AuthenticationLoggable;
}

该包将监听Laravel的登录、登出、失败和其他设备登出事件。

覆盖默认Laravel事件

如果您想监听您自己的事件,可以在包配置中覆盖它们(自v1.3版起)。

示例事件覆盖

您可能会注意到,如果用户在登录时点击了“记住我”,Laravel将在会话更新时触发登录事件。这将每次产生空的登录行,这不是我们想要的。解决办法是触发您自己的登录事件而不是监听Laravel的。

您可以创建一个接受用户的登录事件

<?php

namespace App\Domains\Auth\Events;

use Illuminate\Queue\SerializesModels;

class Login
{
    use SerializesModels;

    public $user;

    public function __construct($user)
    {
        $this->user = $user;
    }
}

然后在包配置中覆盖它

// The events the package listens for to log
'events' => [
    'login' => \App\Domains\Auth\Events\Login::class,
    ...
],

然后在您登录用户的地方调用它

event(new Login($user));

现在,该包将仅注册实际登录事件,而不是会话重新认证。

在Fortify中覆盖

如果您正在使用Fortify并希望注册自己的登录事件,您可以将一个类附加到认证堆栈中

在FortifyServiceProvider中

    Fortify::authenticateThrough(function () {
    return array_filter([
        ...
        FireLoginEvent::class,
    ]);
});

FireLoginEvent只是一个触发事件的类

<?php

  namespace App\Domains\Auth\Actions;

  use App\Domains\Auth\Events\Login;

  class FireLoginEvent
  {
      public function handle($request, $next)
      {
          if ($request->user()) {
              event(new Login($request->user()));
          }

          return $next($request);
      }
  }

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

贡献

请参阅CONTRIBUTING以获取详细信息。

安全漏洞

请参阅我们的安全策略以了解如何报告安全漏洞。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅许可证文件