craftcms/http-message-signatures 问题修复 & 功能扩展

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

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

craftcms/http-message-signatures

Composer 安装命令:

composer require craftcms/http-message-signatures

包简介

PSR-7 compliant PHP 8.1+ implementation of HTTP Message Signatures (RFC 9421)

README 文档

README

A PHP 8.1+ implementation of HTTP Message Signatures as specified in RFC 9421.

Features

  • ✅ RFC 9421 signing and verification support
  • PSR-7 compliant - Works with any PSR-7 HTTP message implementation
  • ✅ Support for multiple signature algorithms:
    • HMAC-SHA256
    • RSA-SHA256
    • Ed25519
  • ✅ Signature creation and verification
  • ✅ Structured fields parsing for signature-input and signature headers
  • ✅ Component derivation (headers, query parameters, request target, etc.)
  • ✅ Immutable message handling (respects PSR-7 immutability)

Installation

composer require craftcms/http-message-signatures

Requirements

  • PHP 8.1 or higher
  • PSR-7 HTTP message implementation (e.g., guzzlehttp/psr7, nyholm/psr7, slim/psr7)

Dependencies

This package uses well-maintained, industry-standard libraries:

Usage

Creating a Signature

use HttpMessageSignatures\Signer;
use HttpMessageSignatures\Algorithm\HmacSha256;
use GuzzleHttp\Psr7\Request;

// Create a PSR-7 request
$request = new Request(
    'POST',
    'https://api.example.com/resource',
    [
        'Host' => 'api.example.com',
        'Content-Type' => 'application/json',
        'Date' => gmdate('D, d M Y H:i:s \G\M\T'),
    ],
    '{"data":"value"}'
);

// Create signer with HMAC-SHA256 algorithm
$signer = new Signer(new HmacSha256('your-secret-key'));

// Sign the request (returns a new immutable PSR-7 message)
$signedRequest = $signer->sign(
    $request,
    ['@method', '@path', '@authority', 'content-type', 'date'],
    [
        'keyid' => 'my-key-id',
        'signatureId' => 'sig1',
        'created' => time(),
        'expires' => time() + 300, // Optional: 5 minutes
    ]
);

// The original request is unchanged (PSR-7 immutability)
// $signedRequest is a new instance with Signature and Signature-Input headers

Verifying a Signature

use HttpMessageSignatures\Verifier;
use HttpMessageSignatures\Algorithm\HmacSha256;
use HttpMessageSignatures\Exception\VerificationException;

$verifier = new Verifier(new HmacSha256('your-secret-key'));

try {
    // Verify the signature (returns true if valid)
    $isValid = $verifier->verify($signedRequest);
    
    if ($isValid) {
        echo "Signature is valid!\n";
    }
} catch (VerificationException $e) {
    echo "Verification failed: " . $e->getMessage() . "\n";
}

Signing URLs

Use UrlSigner and UrlVerifier when the signature needs to live in the URL instead of HTTP headers.

RFC 9421 does not define a signed URL format. This package provides URL signing as a convenience API that applies the same component derivation, signature base, parameters, and algorithms to a URL-carried signature.

use Http\Factory\Guzzle\RequestFactory;
use HttpMessageSignatures\Algorithm\HmacSha256;
use HttpMessageSignatures\Url\UrlSigner;
use HttpMessageSignatures\Url\UrlSigningConfig;
use HttpMessageSignatures\Url\UrlVerifier;

$algorithm = new HmacSha256('your-secret-key');
$requestFactory = new RequestFactory();

$config = new UrlSigningConfig(
    components: ['@target-uri'],
    signatureParam: 'signature',
);

$signer = new UrlSigner($algorithm, $requestFactory, $config);
$verifier = new UrlVerifier($algorithm, $requestFactory, $config);

$signedUrl = $signer->sign('https://example.com/image.jpg?w=800');

if ($verifier->verify($signedUrl)) {
    echo "URL signature is valid!\n";
}

Signed URLs only include the configured signature query parameter. The component list and signature parameters are verifier policy, so the signer and verifier must be configured with the same UrlSigningConfig.

By default, URL signatures cover @target-uri, append a signature query parameter, and omit created/expires. Configure components, signatureParam, created, expiresAfter, keyid, nonce, or tag when those values are part of your URL signing policy.

$config = UrlSigningConfig::withCurrentTime(
    components: ['@path', '@query'],
    signatureParam: 'sig',
    expiresAfter: 300,
);

When signing or verifying non-GET URLs, pass a PSR-7 RequestInterface so the request method can be included with @method.

Using RSA-SHA256

use HttpMessageSignatures\Signer;
use HttpMessageSignatures\Algorithm\RsaSha256;

// Load your private key (for signing)
$privateKey = file_get_contents('/path/to/private-key.pem');

// Optionally provide public key (for verification)
$publicKey = file_get_contents('/path/to/public-key.pem');

$signer = new Signer(new RsaSha256($privateKey, $publicKey));

$signedRequest = $signer->sign(
    $request,
    ['@method', '@path', '@authority', 'content-type'],
    ['keyid' => 'rsa-key-1']
);

Using Ed25519

use HttpMessageSignatures\Signer;
use HttpMessageSignatures\Algorithm\Ed25519;

// Ed25519 requires the sodium extension
$privateKey = sodium_crypto_sign_seed_keypair(...);
$publicKey = sodium_crypto_sign_publickey($privateKey);

$signer = new Signer(new Ed25519($privateKey, $publicKey));

$signedRequest = $signer->sign(
    $request,
    ['@method', '@path', '@authority'],
    ['keyid' => 'ed25519-key-1']
);

Available Components

The following components can be included in signatures:

Derived Components:

  • @method - HTTP method
  • @path - Request path
  • @query - Query string
  • @authority - Host and port
  • @scheme - URI scheme
  • @target-uri - Full URI
  • @request-target - Request target
  • @status - Response status code (for responses)

Headers:

  • Any header name (e.g., content-type, date, authorization)

Query Parameters:

  • @query-param;name="paramname" - Specific query parameter

PSR-7 Compliance

This package is fully PSR-7 compliant:

  • Works with any PSR-7 implementation (guzzlehttp/psr7, nyholm/psr7, slim/psr7, etc.)
  • Respects PSR-7 immutability - all methods return new message instances
  • Uses only PSR-7 interfaces (MessageInterface, RequestInterface, ResponseInterface)
  • No direct dependencies on specific PSR-7 implementations

Testing

composer test

Code Quality

# Run all checks (lint + PHPStan + tests)
composer check

# Lint code
composer lint

# Auto-fix lint issues
composer fix

# Static analysis
composer phpstan

License

MIT

craftcms/http-message-signatures 适用场景与选型建议

craftcms/http-message-signatures 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 134 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 craftcms/http-message-signatures 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-06