kasitaw/api-key
Composer 安装命令:
composer require kasitaw/api-key
包简介
User defined api key(using custom laravel guard) to enable client communicate with server for external integration in general
README 文档
README
This package makes it easy to authenticate users using user defined api key authentication guard with Laravel 6.0+
Installation
API Key can be installed via composer:
composer require "kasitaw/api-key"
The package will automatically register itself.
You can publish the migration with:
php artisan vendor:publish --provider="Kasitaw\ApiKey\ApiKeyServiceProvider" --tag=migrations
After the migration has been published, run the migrations with following command:
php artisan migrate
You can publish the config file with:
php artisan vendor:publish --provider="Kasitaw\ApiKey\ApiKeyServiceProvider" --tag=config
This is the contents of the published config file:
<?php return [ /** * Model use to configure Api Key */ 'model' => [ 'api_key' => Kasitaw\ApiKey\ApiKey::class, // Make sure use Kasitaw\ApiKey\Traits\HasApiKey.php trait if you use your own modal ], /** * Table name that reflected to the above model. */ 'table_name' => [ 'api_keys' => 'api_keys', // Table name to the above model ], /** * Column name being used to store generated api key */ 'columns' => [ 'key' => 'key', ], /** * Field name that being used to fetch the "apiKey". Either passed through query params or as a body. */ 'request_key' => [ 'api_key' => 'api_key', ], /** * Generated key length. */ 'key_length' => 80, ];
Usages
Before started, configure config/auth.php guard as following:
'guards' => [ 'web' => [ // ], 'api' => [ // ], /* * Adding new `api_key` key into guards section */ 'api_key' => [ 'driver' => 'api_key', ] ],
Use HasApiKey.php trait inside App\User.php model or any model that implement \Illuminate\Contracts\Auth\Authenticatable interface:
<?php namespace App; use Kasitaw\ApiKey\Traits\HasApiKey; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { use HasApiKey; }
Call endpoint with middleware as following:
// Using `auth:api` as regular user authentication Route::get('/users', function() { // })->middleware('auth:api'); // Using `auth:api_key` to authenticate user for external api Route::get('/external/intergation/users', function() { dd(request()->user()); // or using Auth::guard('api_key')->user() // or using auth('api_key')->user() })->middleware('auth:api_key');
Finally, lets authenticate. 3 ways to pass in the generated key
- Using query params.
i.e /users?api_key=xxx - Using http body.
i.e api_key = xxx - Using
Authorizationheader.i.e Authorization Bearer xxx
Notes: The request header should be provide
Acceptheader. i.eAccept: application/json
Available Methods to manage the key
Generate new api key that ties up to the authenticate user
$user->generateNewKey(); // By default will activate the key, pass `false` params to make it inactive
Activate all existing keys
$user->activateAllKeys();
Activate the key using key
$user->activateKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC'); // or $user->activateKeyByKey( 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC', '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U' );
Activate the key using uuid
$user->activateKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6'); // or $user->activateKeyByUuid( 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6', '597a67f8-9c19-4c2b-98ff-8020c0f7e360' );
Revoke all existing keys
$user->revokeAllKeys();
Revoke the key using key
$user->revokeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC'); // or $user->revokeKeyByKey( 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC', '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U' );
Revoked the key using uuid
$user->revokeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6'); // or $user->revokeKeyByUuid( 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6', '597a67f8-9c19-4c2b-98ff-8020c0f7e360' );
Delete the key using key
$user->removeKeyByKey('J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC'); // or $user->removeKeyByKey( 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC', '5c9fuEbAny4737an7hXC9VdNmDzd1yE0qn6Am9R8nNzJ0HWROn1daMJ19Lp36XLJlI5QIAkv6xYUkt6U' );
Delete the key using uuid
$user->removeKeyByUuid('e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6'); // or $user->removeKeyByUuid( 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6', '597a67f8-9c19-4c2b-98ff-8020c0f7e360' );
Get all keys
$keys = $user->api_keys; foreach($keys as $key) { // }
Delete all keys
$user->removeAllKeys();
Get all active keys
$keys = $user->api_keys()->active()->get(); foreach($keys as $key) { // }
Get all in-active keys
$keys = $user->api_keys()->inActive()->get(); foreach($keys as $key) { // }
Check whether key is active
$key = $user->api_keys->first(); dd($key->isActive());
Or directly check the key is active
$uuid = 'e0b9ed50-31b4-4ed6-a0f7-71490fa15ad6'; $user->isKeyActive($uuid); // true/false, return null if key not found // or $key = 'J1VFYTgUafp21ljEkanJYYnlY1j4REURXgAKzlwAUxABfCWPw4PBw9HKYbG4GWNvi125WUO0P2e7MmqC'; $user->isKeyActive($key);
Test
Run test with following command
vendor/bin/phpunit --testdox --verbose
License
This package is open-sourced software licensed under the MIT license
kasitaw/api-key 适用场景与选型建议
kasitaw/api-key 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 97 次下载、GitHub Stars 达 2, 最近一次更新时间为 2020 年 03 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「kasitaw」 「api-key」 「api-integration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 kasitaw/api-key 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 kasitaw/api-key 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 kasitaw/api-key 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Secure HTTP transport wrapper for MCP servers with API key auth, IP/Origin allowlisting, and rate limiting
A PSR-7 compatible library for making CRUD API endpoints
Multi-channel application context management for Laravel with JWT and API Key authentication
Detect secrets like API keys, tokens or passwords in PHP projects (e.g. in .env, config, or PHP source). Ideal for CI/CD and pre-commit hooks.
Contoh HTTP client untuk keperluan mengakses API ke aplikasi-aplikasi buatan Clarus IT.
First-party JWT access and rotating refresh token authentication for Laravel apps.
统计信息
- 总下载量: 97
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-23