tourze / json-rpc-encrypt-bundle
JsonRPC加解密实现
Installs: 236
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/json-rpc-encrypt-bundle
Requires
- php: ^8.1
 - ext-hash: *
 - ext-openssl: *
 - doctrine/doctrine-bundle: ^2.13
 - doctrine/orm: ^3.0
 - doctrine/persistence: ^3.1 || ^4
 - psr/log: ^3|^2|^1
 - symfony/config: ^6.4
 - symfony/dependency-injection: ^6.4
 - symfony/doctrine-bridge: ^6.4
 - symfony/event-dispatcher: ^6.4
 - symfony/framework-bundle: ^6.4
 - symfony/http-foundation: ^6.4
 - symfony/http-kernel: ^6.4
 - symfony/polyfill-php83: ^1.31
 - symfony/yaml: ^6.4 || ^7.1
 - tourze/bundle-dependency: 0.0.*
 - tourze/json-rpc-caller-bundle: 0.1.*
 - tourze/json-rpc-core: 0.0.*
 - tourze/json-rpc-endpoint-bundle: 0.1.*
 
Requires (Dev)
- phpstan/phpstan: ^2.1
 - phpunit/phpunit: ^10.0
 
README
A Symfony bundle for encrypting and decrypting JsonRPC requests and responses, ensuring sensitive data security during transmission.
Table of Contents
- Features
 - Installation
 - Configuration
 - Quick Start
 - Advanced Usage
 - Security
 - Dependencies
 - Contributing
 - License
 
Features
- AES-256-CBC symmetric encryption for data transmission
 - Automatic decryption for incoming requests, automatic encryption for outgoing responses
 - Multi-AppID and multi-secret management
 - Compatible with standard JsonRPC workflow
 - Flexible event subscriber mechanism
 - Comprehensive test coverage
 
Installation
Requirements:
- PHP 8.1+
 - Symfony 6.4+
 
Install via Composer:
composer require tourze/json-rpc-encrypt-bundle
Dependencies
This bundle requires the following packages:
tourze/json-rpc-core- Core JsonRPC functionalitytourze/json-rpc-endpoint-bundle- JsonRPC endpoint handlingsymfony/framework-bundle- Symfony frameworkdoctrine/orm- Database ORM support
Configuration
- Register the bundle in your Symfony application:
 
// config/bundles.php return [ // ... Tourze\JsonRPCEncryptBundle\JsonRPCEncryptBundle::class => ['all' => true], ];
- Service Configuration (optional):
 
# config/services.yaml services: Tourze\JsonRPCEncryptBundle\Service\Encryptor: # Custom configuration if needed
Quick Start
Client-side setup:
- Add 
Encrypt-AppIDheader to your JsonRPC requests - Encrypt the payload using AES-256-CBC with your appSecret
 
curl -X POST http://your-server/jsonrpc \ -H "Content-Type: application/json" \ -H "Encrypt-AppID: your-app-id" \ -d "<encrypted-payload>"
Server-side behavior:
- Automatically decrypts incoming requests with 
Encrypt-AppIDheader - Automatically encrypts outgoing responses for encrypted requests
 - Maintains standard JsonRPC error handling
 
Advanced Usage
Custom Encryption Logic
Extend the Encryptor service for custom encryption implementations:
use Tourze\JsonRPCEncryptBundle\Service\Encryptor; class CustomEncryptor extends Encryptor { public function encryptData(string $rawString, string $signSecret, string $signKey): string { // Your custom encryption logic return parent::encryptData($rawString, $signSecret, $signKey); } }
Event Subscribers
The bundle provides EncryptSubscriber that handles:
RequestStartEvent- Decrypts incoming requestsResponseSendingEvent- Encrypts outgoing responses
Error Handling
The bundle throws specific exceptions:
EncryptAppIdMissingException- When Encrypt-AppID header is missingEncryptAppIdNotFoundException- When AppID is not found or invalid
Security
Encryption Details:
- Algorithm: AES-256-CBC
 - Key derivation: SHA-256 hash of appSecret
 - IV generation: MD5 hash of appId (16 bytes)
 - Data encoding: Base64
 
Security Considerations:
- Always use HTTPS in production
 - Rotate appSecret regularly
 - Validate AppID permissions
 - Monitor encryption/decryption failures
 - Keep encryption keys secure
 
Important Security Notes:
- This bundle provides data encryption but does not replace proper authentication
 - Ensure proper key management practices
 - Consider implementing rate limiting for encryption endpoints
 
Documentation
- See 
DataEncryption.pumlfor the encryption process flowchart - Check inline PHPDoc comments for detailed API documentation
 - Review test cases for usage examples
 
Contributing
- Fork the repository
 - Create a feature branch
 - Follow PSR coding standards
 - Ensure new features are fully tested
 - Submit a pull request
 
Development setup:
git clone https://github.com/tourze/json-rpc-encrypt-bundle.git
cd json-rpc-encrypt-bundle
composer install
vendor/bin/phpunit
License
MIT License - see LICENSE file for details.
Copyright © Tourze Team
Changelog
See CHANGELOG.md for release notes and version history.