tourze / wechat-mini-program-auth-bundle
微信小程序用户授权管理组件,支持 OAuth 登录和权限管理
Installs: 653
Dependents: 6
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/wechat-mini-program-auth-bundle
Requires
- php: ^8.1
- ext-mbstring: *
- doctrine/collections: ^2.3
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- knplabs/knp-menu: ^3.7
- monolog/monolog: ^3.1
- nesbot/carbon: ^2.72 || ^3
- 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/event-dispatcher-contracts: ^2.5 | ^3
- symfony/framework-bundle: ^6.4
- symfony/http-client: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/security-bundle: ^6.4
- symfony/security-core: ^6.4
- symfony/security-http: ^6.4
- symfony/serializer: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/access-token-bundle: 0.0.*
- tourze/biz-user-bundle: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-resolve-target-entity-bundle: 0.0.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-upsert-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.1.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- tourze/http-client-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-endpoint-bundle: 0.1.*
- tourze/json-rpc-lock-bundle: 0.1.*
- tourze/json-rpc-log-bundle: 0.1.*
- tourze/lock-service-bundle: 0.1.*
- tourze/login-protect-bundle: 0.1.*
- tourze/symfony-schedule-entity-clean-bundle: 0.1.*
- tourze/text-manage-bundle: 0.0.*
- tourze/user-event-bundle: 0.0.*
- tourze/user-id-bundle: 0.1.*
- tourze/wechat-helper: 0.0.*
- tourze/wechat-mini-program-appid-contracts: 0.0.*
- tourze/wechat-mini-program-bundle: 0.1.*
- tourze/wechat-mini-program-user-contracts: 0.0.*
- yiisoft/json: ^1.0
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-11-01 19:30:26 UTC
README
WeChat Mini Program Authentication Bundle for Symfony
Table of Contents
- Features
- Installation
- Configuration
- Usage
- Advanced Usage
- Entities
- Events
- Procedures
- Security
- Error Handling
- Requirements
- License
Features
- WeChat Mini Program user authentication
- Code to session conversion
- User profile management
- Phone number binding and verification
- Data encryption/decryption service
- Event-driven architecture for customization
- Comprehensive logging for debugging
Installation
composer require tourze/wechat-mini-program-auth-bundle
Configuration
1. Register the Bundle
Register the bundle in your config/bundles.php:
return [ // ... WechatMiniProgramAuthBundle\WechatMiniProgramAuthBundle::class => ['all' => true], ];
2. Configure Services
The bundle provides auto-configuration for all services. Key services include:
EncryptService: Handles WeChat data decryptionWechatTextFormatter: Formats WeChat-specific textUserService: Manages WeChat Mini Program user creation and persistenceUserTransformService: Transforms between WeChat users and system users
Usage
1. Code to Session
Convert WeChat authorization code to session:
use WechatMiniProgramAuthBundle\Procedure\WechatMiniProgramCodeToSession; // Via JSON-RPC $result = $procedure->execute([ 'code' => 'authorization_code', 'rawData' => '{"nickName":"User",...}', 'signature' => 'signature_string', 'encryptedData' => 'encrypted_data', 'iv' => 'initialization_vector' ]);
2. Get Current User
Get the currently authenticated WeChat Mini Program user:
use WechatMiniProgramAuthBundle\Procedure\GetCurrentWechatMiniProgramUser; $user = $procedure->execute();
3. Upload Phone Number
Upload and bind user phone number:
use WechatMiniProgramAuthBundle\Procedure\UploadWechatMiniProgramPhoneNumber; $result = $procedure->execute([ 'encryptedData' => 'encrypted_phone_data', 'iv' => 'initialization_vector' ]);
Advanced Usage
Custom Event Handlers
Listen to authentication events:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use WechatMiniProgramAuthBundle\Event\CodeToSessionResponseEvent; class AuthenticationSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ CodeToSessionResponseEvent::class => 'onUserAuthenticated', ]; } public function onUserAuthenticated(CodeToSessionResponseEvent $event): void { // Custom logic after user authentication $user = $event->getWechatUser(); // ... } }
Integration with User Management Systems
Extend user repository for custom user creation:
use WechatMiniProgramAuthBundle\Repository\UserRepository; use Tourze\UserServiceContracts\UserManagerInterface; class CustomUserRepository extends UserRepository implements UserManagerInterface { public function createUser(string $identifier, string $nickName, string $avatar): UserInterface { // Custom user creation logic return new CustomUser($identifier, $nickName, $avatar); } }
Entities
The bundle provides the following entities:
User: WeChat Mini Program user entityAuthLog: Authentication log recordsCodeSessionLog: Code to session conversion logsPhoneNumber: User phone number records
Events
The bundle dispatches the following events:
CodeToSessionRequestEvent: Before code to session conversionCodeToSessionResponseEvent: After successful session creationGetPhoneNumberEvent: When retrieving phone numberChangePhoneNumberEvent: When changing phone number
Procedures
Available JSON-RPC procedures:
WechatMiniProgramCodeToSession: Convert authorization code to sessionGetCurrentWechatMiniProgramUser: Get current authenticated userUploadWechatMiniProgramPhoneNumber: Upload and bind phone numberReportWechatMiniProgramAuthorizeResult: Report authorization scope results
Security
Data Protection
- All sensitive data is encrypted using WeChat's encryption standards
- Phone numbers are stored with proper validation and sanitization
- User tokens are managed securely with proper expiration
Best Practices
- Always validate WeChat signatures before processing data
- Use HTTPS for all communications with WeChat APIs
- Implement proper rate limiting for authentication endpoints
- Regularly audit authentication logs for suspicious activity
Security Considerations
- Never store session keys in plain text
- Implement proper session management with appropriate timeouts
- Use environment variables for sensitive configuration
- Regularly update dependencies to patch security vulnerabilities
Error Handling
The bundle provides custom exceptions:
DecryptException: Data decryption failuresUserManagerNotAvailableException: User manager service unavailableSystemUserNotFoundException: System user not foundUserRepositoryException: User repository operation errors
Requirements
- PHP 8.1+
- Symfony 6.4+
- Doctrine ORM 3.0+
License
MIT License