ohffs/simple-api-key-middleware
Composer 安装命令:
composer require ohffs/simple-api-key-middleware
包简介
Very basic API key middleware for Laravel
README 文档
README
This is a very basic middleware to check a bearer token on an incoming request.
Installation
composer require ohffs/simple-api-key-middleware php artisan migrate
The migration will create a new database table called simple_api_keys. You can have a look in the packages migrations/ directory to see the details.
Usage
The package registers a middleware alias simple-api-key (mapped to \Ohffs\SimpleApiKeyMiddleware\ApiKeyMiddleware). To protect a given route you could do :
Route::get('/secret-stuff', [SecretStuffController::class, 'show'])->middleware('simple-api-key');
See the Laravel docs for more examples such as route groups or default middlewares.
To generate an API key from the CLI you can use the artisan command :
php artisan api-key:generate "Key for secret stuff"
Which will create the key with that description and show you the resulting token. You can remove a key by doing :
php artisan api-key:remove 14-2398adshh2349addasd7213402
(where the 14-2398... is the token you generated). You can also programmatically create a token :
$apikey = \Ohffs\SimpleApiKeyMiddleware\SimpleApiKey::generate('My new Token'); echo $apikey->plaintext_token;
The plaintext_token is only available on freshly created tokens.
To make a request using the token, you pass it as a regular bearer-token. Eg, with curl :
curl -H "Authorization: Bearer 12-sd8623hsdfi9823nsdf9sdf" https://example.com/secret-stuff
Or using Laravel's HTTP client :
$response = Http::withToken('12-sd8623hsdfi9823nsdf9sdf')->get('https://example.com/secret-stuff');
Configuration
The default configuration is as follows :
// default length of generated api keys 'token_length' => env('API_KEY_LENGTH', 64), // enable caching of token lookups 'cache_enabled' => env('API_KEY_CACHE_ENABLED', true), // time to cache token lookup results 'cache_ttl_seconds' => env('API_KEY_CACHE_TTL', 60),
You can set those environment variables, or if you run php artisan vendor:publish and pick the config from this package you can edit the resulting config/simple_api_keys.php file.
By default the middleware will cache token lookups to save hitting the database for every one. If you are not using a cache such as Redis then you might want to disable the caching.
ohffs/simple-api-key-middleware 适用场景与选型建议
ohffs/simple-api-key-middleware 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 5 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 ohffs/simple-api-key-middleware 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ohffs/simple-api-key-middleware 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 5
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-02