jurager / tracker
跟踪Laravel中的Sanctum令牌。
Requires
- php: ^8.0|^8.1
- guzzlehttp/guzzle: ^7.4
- illuminate/database: ^8.0|^9.0|^10.0
- illuminate/queue: ^8.0|^9.0|^10.0
- illuminate/support: ^8.0|^9.0|^10.0
- laravel/sanctum: ^2.0|^3.0|^10.0
- nesbot/carbon: ^2.55.2
Requires (Dev)
- jenssegers/agent: ^2.6
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
- whichbrowser/parser: ^2.1
README
此包允许您通过读取请求并记录IP地址和其他元数据来跟踪 sanctum令牌。
通过IP地址查找,您还可以通过使用支持的服务提供商或建立自己的自定义提供商来检索更多信息,例如地理位置。
它还包含一个特质,引入了三个对用户模型有用的方法:'logout'、'logoutOthers' 和 'logoutAll'。
要求
PHP => 8.0
和 Laravel => 8.x
与 Sanctum => 2.0
安装
composer require jurager/tracker
使用以下命令发布配置
php artisan vendor:publish --provider="Jurager\Tracker\TrackerServiceProvider" --tag="config"
运行迁移以更新表
php artisan migrate
覆盖Sanctum模型
此包包含一个自定义模型(Jurager\Tracker\Models\PersonalAccessToken
),该模型扩展了默认的Sanctum模型。
通过Sanctum提供的usePersonalAccessTokenModel
方法指示Sanctum使用此自定义模型。通常,您应该在应用程序的某个服务提供者的boot
方法中调用此方法。
use Jurager\Tracker\Models\PersonalAccessToken; use Laravel\Sanctum\Sanctum; /** * Bootstrap any application services. * * @return void */ public function boot() { Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class); }
删除过时记录
此包允许您删除过时的身份验证记录。
记录过期的时间段在配置tracker.expires
中描述。
将调度命令添加到您的Kernel.php
,您可以使用所需的时间段
$schedule->command('model:prune')->everyMinute();
安装用户代理解析器
此包依赖于用户代理解析器来提取信息。
当前支持的解析器
在使用此包之前,您需要选择一个支持的解析器。
使用特质
此包提供了一个Jurager\Tracker\Traits\Tracked
特质,可用于您的用户模型以快速撤销令牌。
它引入了方便的方法
logout
:通过传递参数ID撤销当前令牌或特定令牌logoutAll
:撤销所有令牌,包括当前令牌logoutOthers
:撤销所有令牌,除了当前令牌
use Jurager\Tracker\Traits\Tracked; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use Tracked; // ... }
使用方法
像平常一样使用Sanctum。
此包提供的PersonalAccessToken
模型将自动填充额外信息。
IP查找
默认情况下,包收集IP地址和User-Agent头提供的信息。
但您还可以收集其他数据,例如地理位置。
要这样做,您首先必须在配置文件中启用IP查找功能。
此包包含两个官方支持的IP地址查找提供商(请参阅config/tracker.php
配置文件中的IP地址查找部分)。
IP2Location Lite
此包官方支持使用IP2Location DB3进行IP地址地理位置。
以下是启用和使用它的步骤
-
下载数据库,并按照文档中的说明导入它
-
将
lookup.provider
选项的名称在config/tracker.php
中设置为ip2location-lite
-
在
config/tracker.php
中设置用于IPv4和IPv6的数据库表名称
自定义提供商
您可以通过创建一个实现Jurager\Tracker\Interfaces\IpProvider
接口的类并使用Jurager\Tracker\Traits\MakesApiCalls
特质来添加自己的提供商。
您必须将自定义类注册到配置文件的custom_providers
数组中。
让我们看看一个带有内置 IpApi
提供者的 IP 查询提供商的例子。
use Jurager\Tracker\Interfaces\IpProvider; use Jurager\Tracker\Traits\MakesApiCalls; use GuzzleHttp\Psr7\Request as GuzzleRequest; use Illuminate\Support\Facades\Request; class IpApi implements IpProvider { use MakesApiCalls; /** * Get the Guzzle request. * * @return GuzzleRequest */ public function getRequest() { return new GuzzleRequest('GET', 'http://ip-api.com/json/'.Request::ip().'?fields=25'); } /** * Get the country name. * * @return string */ public function getCountry() { return $this->result->get('country'); } /** * Get the region name. * * @return string */ public function getRegion() { return $this->result->get('regionName'); } /** * Get the city name. * * @return string */ public function getCity() { return $this->result->get('city'); } }
如您所见,该类有一个 getRequest
方法,必须返回一个 GuzzleHttp\Psr7\Request
实例。
Guzzle 使用 PSR-7 作为 HTTP 消息接口。请查看其文档:[http://docs.guzzlephp.org/en/stable/psr7.html](http://docs.guzzlephp.org/en/stable/psr7.html)
IpProvider
接口包含与地理位置相关的必需方法。API 响应的所有键都可通过 $this->result
在您的提供者中访问,它是一个 Laravel 集合。
如果您想收集其他信息,您可以在自定义提供者中添加一个 getCustomData
方法。这些自定义数据将被保存在 logins
表的 ip_data
JSON 列中。让我们看看附加数据的例子
public function getCustomData() { return [ 'country_code' => $this->result->get('countryCode'), 'latitude' => $this->result->get('lat'), 'longitude' => $this->result->get('lon'), 'timezone' => $this->result->get('timezone'), 'isp_name' => $this->result->get('isp'), ]; }
处理错误
如果您的 IP 地址查询提供商在 API 请求期间触发异常,此包将触发 FailedApiCall
事件。
此事件包含一个异常属性,包含 GuzzleHttp\Exception\TransferException
(请参阅Guzzle 文档)。
您可以通过监听此事件来添加自己的逻辑。
事件
PersonalAccessTokenCreated
您可以在新登录时监听 Jurager/Tracker/Events/PersonalAccessTokenCreated
事件。
它有一个 personalAccessToken
属性,包含新创建的 Jurager/Tracker/Models/PersonalAccessToken
,还有一个 context
属性,接收一个包含请求上收集的所有数据的 Jurager/Tracker/RequestContext
。
可用属性
$this->context->userAgent; // The full, unparsed, User-Agent header $this->context->ip; // The IP address
可用方法
$this->context->parser(); // Returns the parser used to parse the User-Agent header $this->context->ip(); // Returns the IP address lookup provider
解析器中的可用方法
$this->context->parser()->getDevice(); // The name of the device (MacBook...) $this->context->parser()->getDeviceType(); // The type of the device (desktop, mobile, tablet, phone...) $this->context->parser()->getPlatform(); // The name of the platform (macOS...) $this->context->parser()->getBrowser(); // The name of the browser (Chrome...)
IP 地址查询提供商中的可用方法
$this->context->ip()->getCountry(); // The name of the country $this->context->ip()->getRegion(); // The name of the region $this->context->ip()->getCity(); // The name of the city $this->context->ip()->getResult(); // The entire result of the API call as a Laravel collection // And all your custom methods in the case of a custom provider
许可证
开源软件,采用MIT 许可证。