alajusticia / laravel-sanctum-tracker
跟踪Laravel中的Sanctum令牌。
Requires
- php: ^7.4|^8.0
- alajusticia/laravel-expirable: ^1.5
- guzzlehttp/guzzle: ^7.3
- illuminate/database: ^7.0|^8.0
- illuminate/queue: ^7.0|^8.0
- illuminate/support: ^7.0|^8.0
- laravel/sanctum: ^2.9
- nesbot/carbon: ^2.53
Requires (Dev)
- jenssegers/agent: ^2.6
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.5
- whichbrowser/parser: ^2.1
README
⚠️ 此包现已归档。请使用 Laravel Logins 代替。
跟踪Laravel中的Sanctum令牌。
此包允许您跟踪Sanctum令牌,通过解析User-Agent并保存IP地址来附加信息。
使用支持的提供者或创建自己的自定义提供者,您可以使用IP地址查找收集更多信息,例如获取地理位置。
它还提供了一个引入便捷方法的特质:为您的用户模型提供logout
、logoutOthers
和logoutAll
。
兼容性
对于最新版本的Laravel,请使用 Laravel Logins。
此包已在 Laravel 8.x 和 Laravel Sanctum (v2) 上进行测试。
安装
使用Composer安装此包
composer require alajusticia/laravel-sanctum-tracker
使用以下命令发布配置文件(config/sanctum_tracker.php
):
php artisan vendor:publish --provider="ALajusticia\SanctumTracker\SanctumTrackerServiceProvider" --tag="config"
运行迁移以更新personal_access_tokens
表
php artisan migrate
覆盖默认模型
此包包含一个自定义模型(ALajusticia\SanctumTracker\Models\PersonalAccessToken
),它扩展了默认的Sanctum模型。
通过Sanctum提供的usePersonalAccessTokenModel
方法指示Sanctum使用此自定义模型。通常,您应该在应用程序的一个服务提供者的boot
方法中调用此方法。
use ALajusticia\SanctumTracker\Models\PersonalAccessToken; use Laravel\Sanctum\Sanctum; /** * Bootstrap any application services. * * @return void */ public function boot() { Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class); }
选择并安装一个用户代理解析器
此包依赖于用户代理解析器来提取信息。
目前,它支持两种最受欢迎的解析器:
- WhichBrowser (https://github.com/WhichBrowser/Parser-PHP)
- Agent (https://github.com/jenssegers/agent)
在使用Sanctum Tracker之前,您需要选择一个支持的解析器,安装它,并在配置文件中指定您想要使用哪个。
将特质添加到您的用户模型(可选)
此包提供了一个ALajusticia\SanctumTracker\Traits\SanctumTracked
特质,可用于您的用户模型以快速吊销Sanctum令牌。
它引入了便捷方法
logout
:通过传递参数ID吊销当前令牌或特定令牌logoutOthers
:吊销除当前令牌之外的所有令牌logoutAll
:吊销所有令牌,包括当前令牌
use ALajusticia\SanctumTracker\Traits\SanctumTracked; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use SanctumTracked; // ... }
用法
像通常一样颁发Sanctum令牌。此包提供的PersonalAccessToken
模型将自动填充额外信息。
IP地址查找
默认情况下,Sanctum Tracker收集IP地址和User-Agent头中提供的信息。
但您还可以进一步收集有关IP地址的其他信息,例如地理位置。
要这样做,您首先需要在配置文件中启用IP查找功能。
此包包含两个官方支持的IP地址查找提供者(请参阅配置文件config/sanctum_tracker.php
中的IP地址查找部分)。
Ip2Location Lite DB3
此包官方支持使用Ip2Location Lite DB3进行IP地址地理定位。
以下是启用和使用它的步骤
-
下载当前版本的数据库,并按照文档说明将其导入到您的数据库中:https://lite.ip2location.com/database/ip-country-region-city
-
在配置文件
config/sanctum_tracker.php
中将ip_lookup.provider
选项的名称设置为ip2location-lite
-
在
config/sanctum_tracker.php
配置文件中指定您数据库中用于 IPv4 和 IPv6 的表名(默认使用与文档相同的名称:ip2location_db3
和ip2location_db3_ipv6
)
自定义提供者
您可以通过创建一个实现了 ALajusticia\SanctumTracker\Interfaces\IpProvider
接口的类来添加自己的提供者,并使用 ALajusticia\SanctumTracker\Traits\MakesApiCalls
特性。
您自定义的类必须在配置文件的 custom_providers
数组中进行注册。
让我们看看一个使用内置的 IpApi
提供者的 IP 查询提供者示例
use ALajusticia\SanctumTracker\Interfaces\IpProvider; use ALajusticia\SanctumTracker\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
IpProvider
接口包含与地理位置相关的必需方法。API 响应的所有键都可通过 $this->result
在您的提供者中访问,这是一个 Laravel 集合。
如果您想收集其他信息,可以在自定义提供者中添加一个 getCustomData
方法。这些自定义数据将被保存在登录表中的 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'), ]; }
处理API错误
如果在您的 IP 地址查询提供者的 API 调用期间抛出异常,此包将触发 FailedApiCall 事件。
此事件包含一个异常属性,该属性包含 GuzzleHttp\Exception\TransferException(请参阅 Guzzle 文档)。
您可以监听此事件以添加自己的逻辑。
事件
PersonalAccessTokenCreated
在新的登录过程中,您可以监听 ALajusticia\SanctumTracker\Events\PersonalAccessTokenCreated
事件。它有一个包含新创建的 ALajusticia\SanctumTracker\Models\PersonalAccessToken
的 personalAccessToken
属性,以及一个接收包含在请求上收集的所有信息的 ALajusticia\SanctumTracker\RequestContext
对象的 context
属性。
可用属性
$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 许可。