olmed/databus-webhook-helpers
Composer 安装命令:
composer require olmed/databus-webhook-helpers
包简介
Secure webhook helper library for encrypting, decrypting and verifying webhook payloads
README 文档
README
A PHP library for secure webhook payload encryption, decryption and verification using AES-256-CBC encryption and HMAC-SHA256 signatures.
Requirements
- PHP >= 7.0
- OpenSSL extension
- JSON extension
Installation
Install via Composer:
composer require olmed/databus-webhook-helpers
Usage
Basic Example
<?php require_once 'vendor/autoload.php'; use Olmed\DataBus\Webhooks\SecureWebhookHelper; // Initialize with your encryption and HMAC keys (both must be 32 bytes) $encryptionKey = 'your-32-byte-encryption-key!!'; $hmacKey = 'your-32-byte-hmac-secret-key!!'; $helper = new SecureWebhookHelper($encryptionKey, $hmacKey); // Webhook data received from request $guid = $_POST['guid']; $webhookType = $_POST['webhookType']; $encryptedPayload = $_POST['payload']; // Base64 encoded with IV prefix $signature = $_POST['signature']; // HMAC-SHA256 signature // Decrypt and verify $decryptedJson = ''; $isValid = $helper->tryDecryptAndVerifyWithIvPrefix( $guid, $webhookType, $encryptedPayload, $signature, $decryptedJson ); if ($isValid) { // Successfully decrypted and verified $webhookData = json_decode($decryptedJson, true); echo "Webhook verified successfully!\n"; print_r($webhookData); } else { // Failed to decrypt or verify signature http_response_code(401); echo "Invalid webhook signature or corrupted payload"; }
Complete Webhook Endpoint Example
<?php require_once 'vendor/autoload.php'; use Olmed\DataBus\Webhooks\SecureWebhookHelper; // Your secret keys (store these securely, e.g., in environment variables) $encryptionKey = getenv('WEBHOOK_ENCRYPTION_KEY'); $hmacKey = getenv('WEBHOOK_HMAC_KEY'); try { $helper = new SecureWebhookHelper($encryptionKey, $hmacKey); // Get POST data $postData = json_decode(file_get_contents('php://input'), true); $guid = $postData['guid'] ?? ''; $webhookType = $postData['webhookType'] ?? ''; $payload = $postData['payload'] ?? ''; $signature = $postData['signature'] ?? ''; $decryptedJson = ''; $isValid = $helper->tryDecryptAndVerifyWithIvPrefix( $guid, $webhookType, $payload, $signature, $decryptedJson ); if ($isValid) { $data = json_decode($decryptedJson, true); // Process your webhook data here processWebhook($webhookType, $data); http_response_code(200); echo json_encode(['status' => 'success']); } else { http_response_code(401); echo json_encode(['status' => 'error', 'message' => 'Invalid signature']); } } catch (InvalidArgumentException $e) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => 'Configuration error']); } catch (Exception $e) { http_response_code(500); echo json_encode(['status' => 'error', 'message' => 'Server error']); } function processWebhook($type, $data) { // Your webhook processing logic here error_log("Processing webhook type: {$type}"); }
How It Works
Encryption Format
The payload is encrypted using AES-256-CBC with the following structure:
- A random 16-byte Initialization Vector (IV) is generated
- The data is encrypted using AES-256-CBC
- The IV is prepended to the encrypted data
- The combined data (IV + encrypted) is base64-encoded
Verification Process
- The base64 payload is decoded
- The IV (first 16 bytes) is extracted
- The remaining data is decrypted using AES-256-CBC
- A JSON structure is reconstructed:
{"guid":"...","webhookType":"...","webhookData":{...}} - An HMAC-SHA256 signature is computed for this JSON
- The computed signature is compared with the provided signature
Security Features
- AES-256-CBC encryption - Industry-standard encryption
- HMAC-SHA256 signatures - Prevents tampering and ensures authenticity
- Timing-attack safe comparison - Uses
hash_equals()for signature verification - 32-byte keys required - Enforces strong key lengths
Key Generation
To generate secure 32-byte keys, you can use:
<?php // Generate encryption key $encryptionKey = bin2hex(random_bytes(16)); // 32 characters echo "Encryption Key: " . $encryptionKey . "\n"; // Generate HMAC key $hmacKey = bin2hex(random_bytes(16)); // 32 characters echo "HMAC Key: " . $hmacKey . "\n";
Or use OpenSSL from command line:
openssl rand -hex 16
API Reference
SecureWebhookHelper
Constructor
public function __construct(string $encryptionKey, string $hmacKey)
Parameters:
$encryptionKey- 32-byte encryption key$hmacKey- 32-byte HMAC key
Throws:
InvalidArgumentException- If keys are not exactly 32 bytes
tryDecryptAndVerifyWithIvPrefix
public function tryDecryptAndVerifyWithIvPrefix( string $guid, string $webhookType, string $base64PayloadWithIv, string $signature, string &$decryptedJson ): bool
Parameters:
$guid- Webhook GUID$webhookType- Type of webhook$base64PayloadWithIv- Base64-encoded encrypted payload with IV prefix$signature- HMAC-SHA256 signature (hex string)$decryptedJson- Output parameter containing decrypted JSON on success
Returns:
trueif decryption and verification succeededfalseif decryption failed or signature is invalid
Local Development (Laravel)
If you want to develop and test this package locally in your Laravel project before publishing to Packagist, you can load it directly via autoload.
- Edit your Laravel project's
composer.jsonand add to theautoload-devsection:
{
"autoload-dev": {
"psr-4": {
"Olmed\\DataBus\\Webhooks\\": "/absolute/path/to/PHP_OlmedDataBus.Webhooks/src/"
}
}
}
- Reload the autoloader:
composer dump-autoload
- Now you can use the class in your Laravel application:
<?php use Olmed\DataBus\Webhooks\SecureWebhookHelper; $helper = new SecureWebhookHelper( config('webhooks.encryption_key'), config('webhooks.hmac_key') );
Any changes you make to the source files in /absolute/path/to/PHP_OlmedDataBus.Webhooks/src/ will be immediately available in your Laravel project.
Note: Remember to remove this entry from autoload-dev and install the package normally via Composer once it's published to Packagist.
olmed/databus-webhook-helpers 适用场景与选型建议
olmed/databus-webhook-helpers 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 12 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「encryption」 「webhook」 「hmac」 「databus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 olmed/databus-webhook-helpers 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 olmed/databus-webhook-helpers 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 olmed/databus-webhook-helpers 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A simple PHP class to encrypt a string and decrypt an encrypted string
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Provide a way to secure accesses to all routes of an symfony application.
Encryption with AES-256 and HMAC-SHA256
Allows installation of Laravel where the PHP Mcrypt extension is not available. Provides encryption using OpenSSL, or by disabling encryption entierly.
High-level cryptographic primitives and security utilities for Maatify systems including password hashing, reversible encryption, HKDF key derivation, and key rotation.
统计信息
- 总下载量: 10
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-03