ejarnutowski/laravel-api-key
Composer 安装命令:
composer require ejarnutowski/laravel-api-key
包简介
Authorize requests to your Laravel application with API keys
README 文档
README
Installation
Run composer require ejarnutowski/laravel-api-key.
In your config/app.php file, add the Laravel API Key service provider to the end of the providers array.
'providers' => [ ... Ejarnutowski\LaravelApiKey\Providers\ApiKeyServiceProvider::class, ],
Publish the migration files
$ php artisan vendor:publish
Run the migrations
$ php artisan migrate
3 new database tables will be created:
- api_keys
- api_key_access_events
- api_key_admin_events
Managing Keys
Generate a new key using php artisan apikey:generate {name}. The name argument is the name of your API key. All new keys are active by default.
$ php artisan apikey:generate app1 // API key created // Name: app1 // Key: 0ZdNlr7LrQocaqz74k6usQsOsqhqSIaUarSTf8mxnHuQVh9CvKAfpUy94VvBmFMq
Deactivate a key using php artisan apikey:deactivate {name}.
$ php artisan apikey:deactivate app1 // Deactivated key: app1
Activate a key using php artisan apikey:activate {name}.
$ php artisan apikey:activate app1 // Activated key: app1
Delete a key. You'll be asked to confirm. Keys are soft-deleted for record keeping.
$ php artisan apikey:delete app1 // Are you sure you want to delete API key 'app1'? (yes/no) [no]: // > yes // Deleted key: app1
List all keys. The -D or --deleted flag includes deleted keys
$ php artisan apikey:list -D // +----------+----+-------------+---------------------+------------------------------------------------------------------+ // | Name | ID | Status | Status Date | Key | // +----------+----+-------------+---------------------+------------------------------------------------------------------+ // | app1 | 5 | deleted | 2017-11-03 13:54:51 | 0ZdNlr7LrQocaqz74k6usQsOsqhqSIaUarSTf8mxnHuQVh9CvKAfpUy94VvBmFMq | // | app2 | 1 | deleted | 2017-11-02 22:34:28 | KuKMQbgZPv0PRC6GqCMlDQ7fgdamsVY75FrQvHfoIbw4gBaG5UX0wfk6dugKxrtW | // | app3 | 3 | deactivated | 2017-11-02 23:12:34 | IrDlc7rSCvUzpZpW8jfhWaH235vJAqFwyzVWpoD0SLGzOimA6hcwqMvy4Nz6Hntn | // | app4 | 2 | active | 2017-11-02 22:48:13 | KZEl4Y2HMuL013xvg6Teaa7zHPJhGy1TDhr2zWzlQCqTxqTzyPTcOV6fIQZVTIU3 | // +----------+----+-------------+---------------------+------------------------------------------------------------------+
Usage
Implementing Authorization
A new auth.apikey route middleware has been registered for you to use in your routes or controllers. Below are examples on how to use middleware, but for detailed information, check out Middleware in the Laravel Docs.
Route example
Route::get('api/user/1', function () { // })->middleware('auth.apikey');
Controller example
class UserController extends Controller { /** * Instantiate a new controller instance. * * @return void */ public function __construct() { $this->middleware('auth.apikey'); } }
Authorizing Requests
In order to pass the auth.apikey middleware, requests must include an X-Authorization header as part of the request, with its value being an active API key.
X-Authorization: KuKMQbgZPv0PRC6GqCMlDQ7fgdamsVY75FrQvHfoIbw4gBaG5UX0wfk6dugKxrtW
Unauthorized Requests
Requests that do not pass authorization will receive an HTTP 401 Status Code with the following response
{
"errors": [
{
"message": "Unauthorized"
}
]
}
Event History
All API requests that pass authorization are logged in the api_key_access_events table. A record is created for each request with the following information:
- api_key_id
- ip_address
- url
- created_at
- updated_at
Any time an API key is generated, activated, deactivated, or deleted, a record is logged in the api_key_admin_events table. Each record contains the following information:
- api_key_id
- ip_address
- event
- created_at
- updated_at
License
The Laravel API Key package is open-sourced software licensed under the MIT license.
ejarnutowski/laravel-api-key 适用场景与选型建议
ejarnutowski/laravel-api-key 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 462.28k 次下载、GitHub Stars 达 157, 最近一次更新时间为 2017 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「api」 「Authentication」 「laravel」 「Api Key」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ejarnutowski/laravel-api-key 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ejarnutowski/laravel-api-key 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ejarnutowski/laravel-api-key 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
A PSR-7 compatible library for making CRUD API endpoints
统计信息
- 总下载量: 462.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 157
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-04