corgspace/hmac-http-client 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

corgspace/hmac-http-client

Composer 安装命令:

composer require corgspace/hmac-http-client

包简介

HMAC-signed HTTP client middleware for Laravel server-to-server APIs

README 文档

README

Latest Version on Packagist Tests Total Downloads

Laravel package that adds HMAC-signed HTTP requests to the built-in HTTP client via an Http::hmac('service_name') macro. Each outgoing request gets X-Key-Id, X-Timestamp, X-Nonce, and X-Signature headers added automatically; the caller supplies X-Idempotency-Key and the body.

Install

composer require corgspace/hmac-http-client
php artisan vendor:publish --tag=hmac-http-client-config

Configure

Add a service to config/hmac-http-client.php:

'services' => [
    'example_api' => [
        'base_url'        => env('EXAMPLE_API_URL'),
        'key_id'          => env('EXAMPLE_API_KEY_ID'),
        'secret'          => env('EXAMPLE_API_SECRET'),
        'secret_encoding' => env('EXAMPLE_API_SECRET_ENCODING', 'base64'),
    ],
],

.env:

EXAMPLE_API_URL=https://api.example.com
EXAMPLE_API_KEY_ID=my-app-prod
EXAMPLE_API_SECRET=<base64- or hex-encoded secret>

secret_encoding is base64 (default), hex, or raw. Decoded secret must be at least 32 bytes.

Use

use Illuminate\Support\Facades\Http;

$response = Http::hmac('example_api')
    ->withHeaders(['X-Idempotency-Key' => $operationId])
    ->post('/v1/resource', [
        'external_ref' => $externalId,
        'source'       => 'direct',
    ]);

if ($response->successful()) {
    $data = $response->json();
}

The macro returns a PendingRequest with acceptJson()->asJson() already applied. Chain any normal HTTP client method after Http::hmac(...).

The caller must set X-Idempotency-Key. It is part of the signed canonical and should be meaningful to the upstream (a webhook event ID, a logical operation ID, etc.). For read-only calls with no natural key, generate a fresh UUID per call.

Retries

Laravel's built-in retry re-signs on every attempt — fresh nonce and timestamp, same idempotency key:

Http::hmac('example_api')
    ->withHeaders(['X-Idempotency-Key' => $operationId])
    ->retry(3, 100)
    ->post('/v1/resource', $payload);

Canonical string format

For implementers of the verifier side, or anyone debugging a signature mismatch:

{METHOD}\n{REQUEST_TARGET}\n{TIMESTAMP}\n{NONCE}\n{IDEMPOTENCY_KEY}\n{hex(sha256(BODY))}
  • No trailing newline. Separators are single \n (0x0A), never \r\n.
  • METHOD uppercased.
  • REQUEST_TARGET is the full request target as it appears on the wire — path plus query string, in the exact order the client sends it. Matches PSR-7's RequestInterface::getRequestTarget(). Examples: /v1/users, /v1/search?q=widget&sort=asc. Empty targets are normalized to / by PSR-7.
  • BODY is the raw request body bytes. Empty body hashes to e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.
  • Signature is base64(hmac_sha256(canonical, secret_bytes)).

Verifier must read the request target from the same source (e.g. the raw request line) and apply the same method-case rules. Any reordering or reformatting of query parameters on either side will invalidate the signature.

Testing

composer test         # phpunit
composer analyse      # phpstan (level max, larastan)
composer format-test  # pint --test

composer format applies pint fixes in place.

Changelog

See CHANGELOG for a list of recent changes.

Contributing

Contributions are welcome. Please open an issue or pull request at github.com/CorgSpace/hmac-http-client. Run composer test, composer analyse, and composer format-test before submitting.

Security

If you discover a security vulnerability, please report it privately via GitHub's private vulnerability reporting rather than opening a public issue. See SECURITY.md for details.

License

MIT — see LICENSE.

corgspace/hmac-http-client 适用场景与选型建议

corgspace/hmac-http-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 18 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「Guzzle」 「middleware」 「signing」 「laravel」 「hmac」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 corgspace/hmac-http-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 corgspace/hmac-http-client 我们能提供哪些服务?
定制开发 / 二次开发

基于 corgspace/hmac-http-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 40
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-18