aporat/laravel-auth-signature
Composer 安装命令:
composer require aporat/laravel-auth-signature
包简介
A Laravel package providing a middleware for validating API requests with HMAC-SHA256 signatures
关键字:
README 文档
README
A robust Laravel package providing a middleware for validating API requests with HMAC-SHA256 signatures. It features configurable signature templates, version-specific authentication settings, and a secure, time-based validation to protect your endpoints.
✨ Features
- HMAC-SHA256 Validation: Securely validates incoming API requests.
- Configurable Signature Templates: Easily define the exact order and components of the string-to-be-signed.
- Version-Specific Rules: Apply different secrets, states, and signature templates based on an
X-Auth-Versionheader. - Timestamp Validation: Protects against replay attacks by ensuring requests are recent.
- Simple Middleware Integration: Secure your routes with a single middleware alias:
auth.signature. - Clean and Modern Codebase: Fully typed, tested, and built on modern PHP and Laravel features.
📋 Requirements
- PHP: ^8.4
- Laravel: 12.x or 13.x
🚀 Installation
-
Install the package via Composer:
composer require aporat/laravel-auth-signature
-
Publish the configuration file. The service provider is auto-discovered.
php artisan vendor:publish --provider="Aporat\AuthSignature\AuthSignatureServiceProvider" --tag="config"
This will create a new configuration file at
config/auth-signature.php.
🔧 Configuration
Edit config/auth-signature.php to define your clients, authentication versions, and settings.
<?php return [ /* | Defines the time window, in seconds, for which a signature is valid. | This helps prevent replay attacks. Default is 300 seconds (5 minutes). */ 'timestamp_tolerance_seconds' => 300, /* | Define each client that can make signed requests. | The key is the Client ID sent in the `X-Auth-Client-ID` header. */ 'clients' => [ 'your-client-id' => [ // The secret key used to sign requests for this client. 'client_secret' => env('CLIENT_SECRET_KEY'), // The bundle ID or unique identifier for the client application. 'bundle_id' => 'com.yourcompany.yourapp', // (Optional) The minimum auth version this client must use. 'min_auth_level' => 10, ], ], /* | Define rules for different signature versions. | This allows you to evolve your signature algorithm over time. */ 'auth_versions' => [ 10 => [ // (Optional) A version-specific secret appended to the client's secret. 'secret' => env('AUTH_V10_SECRET'), // (Optional) A static string included in the signature for this version. 'state' => 'some_static_string_for_v10', // (Optional) The exact order of components for the string-to-be-signed. 'signature_template' => [ 'bundle_id', 'timestamp', 'client_id', 'state', 'auth_version', 'method', 'signature', 'path', ], ], ], ];
Remember to add the corresponding keys to your .env file for security.
🛠️ Usage
Applying the Middleware
Apply the auth.signature middleware to any route or route group that requires signature validation.
// in routes/api.php Route::middleware('auth.signature')->group(function () { Route::get('/orders', [OrderController::class, 'index']); Route::post('/orders', [OrderController::class, 'store']); });
The middleware will automatically validate incoming requests and throw a SignatureException (resulting in a 4xx HTTP response) if validation fails.
Generating a Signature
You can use the SignatureGenerator class to create a valid signature, which is useful for testing or for client-side implementations.
use Aporat\AuthSignature\SignatureGenerator; // Resolve the generator from the container $generator = app(SignatureGenerator::class); $signature = $generator->generate( clientId: 'your-client-id', authVersion: 10, timestamp: time(), method: 'GET', path: '/api/orders', params: ['page' => 1] ); // The output will be a 64-character HMAC-SHA256 hash // e.g., "b5f0029b48b61a9151528c11e74f115340f666d44a141b279d633036e88c0353"
Example Client Request
A client would then make a request including the generated signature and other required headers.
# Store timestamp and generate signature first TIMESTAMP=$(date +%s) SIGNATURE="..." # Generate signature using the same timestamp curl -X GET "[http://yourapp.test/api/orders?page=1](http://yourapp.test/api/orders?page=1)" \ -H "Content-Type: application/json" \ -H "X-Auth-Client-ID: your-client-id" \ -H "X-Auth-Version: 10" \ -H "X-Auth-Timestamp: $TIMESTAMP" \ -H "X-Auth-Signature: $SIGNATURE"
🧪 Testing
The package is fully tested. To run the test suite:
# Run all tests composer test # Run tests with code coverage composer test-ci
🤝 Contributing
Contributions are welcome! Please feel free to fork the repository, create a feature branch, and open a pull request.
📜 License
This package is open-source software licensed under the MIT License.
💬 Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
aporat/laravel-auth-signature 适用场景与选型建议
aporat/laravel-auth-signature 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.28k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 04 月 08 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「auth」 「signature」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 aporat/laravel-auth-signature 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 aporat/laravel-auth-signature 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 aporat/laravel-auth-signature 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
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.
接口签名验证
A PSR-7 compatible library for making CRUD API endpoints
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
Digital signature Nova field.
统计信息
- 总下载量: 1.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-08