tourze / sina-weibo-oauth2-bundle
Sina Weibo
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/tourze/sina-weibo-oauth2-bundle
Requires
- ext-filter: *
- ext-json: *
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/orm: ^3.0
- doctrine/persistence: ^4.1
- easycorp/easyadmin-bundle: ^4
- knplabs/knp-menu: ^3.7
- monolog/monolog: ^3.1
- psr/container: ^1.1|^2.0
- psr/log: ^3|^2|^1
- symfony/cache-contracts: ^3
- symfony/config: ^7.3
- symfony/console: ^7.3
- symfony/dependency-injection: ^7.3
- symfony/doctrine-bridge: ^7.3
- symfony/form: ^7.3
- symfony/framework-bundle: ^7.4
- symfony/http-client: ^7.3
- symfony/http-client-contracts: ^3.6
- symfony/http-foundation: ^7.4
- symfony/http-kernel: ^7.3
- symfony/property-access: ^7.3
- symfony/routing: ^7.3
- symfony/security-bundle: ^7.3
- symfony/security-core: ^7.3
- symfony/yaml: ^7.3
- tourze/bundle-dependency: 1.*
- tourze/doctrine-indexed-bundle: 1.0.*
- tourze/doctrine-timestamp-bundle: 1.1.*
- tourze/easy-admin-menu-bundle: 1.0.*
- tourze/symfony-dependency-service-loader: 1.0.*
- tourze/symfony-routing-auto-loader-bundle: 1.0.*
Requires (Dev)
- doctrine/data-fixtures: ^2.0
- doctrine/doctrine-fixtures-bundle: ^4.0
- fakerphp/faker: ^1.23
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- tourze/phpunit-base: 1.*
- tourze/phpunit-doctrine-entity: 1.*
- tourze/phpunit-symfony-kernel-test: 2.0.*
- tourze/phpunit-symfony-unit-test: 2.0.*
- tourze/phpunit-symfony-web-test: 2.0.*
This package is auto-updated.
Last update: 2025-12-20 18:22:50 UTC
README
A comprehensive Symfony bundle that provides seamless integration with Sina Weibo OAuth2 authentication, including user management, token handling, and administrative tools.
Table of Contents
- Features
- Installation
- Quick Start
- Advanced Usage
- Console Commands
- System Requirements
- Security
- License
Features
- β Complete OAuth2 Flow: Full implementation of Sina Weibo OAuth2 authentication
- ποΈ Doctrine Integration: Pre-built entities for configurations, users, and states
- ποΈ Management Commands: Console commands for easy configuration and maintenance
- π§Ή Auto Cleanup: Automatic cleanup of expired tokens and authentication states
- π Ready-to-use Controllers: Built-in login and callback controllers
- π Repository Pattern: Clean data access layer with optimized queries
- π Security First: CSRF protection via state validation and secure token handling
- π§ͺ 100% Test Coverage: Comprehensive unit and integration tests
- π Bilingual Documentation: Complete documentation in English and Chinese
Installation
composer require tourze/sina-weibo-oauth2-bundle
Quick Start
1. Configure OAuth2 Application
Use the configuration command to set up your Sina Weibo OAuth2 application:
php bin/console sina-weibo-oauth2:config create --app-id=YOUR_APP_ID --app-secret=YOUR_APP_SECRET --scope=email
2. Basic Usage
The bundle provides two pre-built controllers for OAuth2 flow:
- Login URL:
/sina-weibo-oauth2/login- Redirects to Sina Weibo for authentication - Callback URL:
/sina-weibo-oauth2/callback- Handles the OAuth2 callback
Using Built-in Controllers
Simply redirect users to the login URL:
<?php use Symfony\Component\HttpFoundation\RedirectResponse; class YourController { public function login(): RedirectResponse { // Redirect to the built-in login controller return new RedirectResponse('/sina-weibo-oauth2/login'); } }
Custom Implementation
For custom OAuth2 flow:
<?php use Tourze\SinaWeiboOAuth2Bundle\Service\SinaWeiboOAuth2Service; use Symfony\Component\HttpFoundation\Request; class YourController { public function __construct( private SinaWeiboOAuth2Service $oauth2Service, ) { } public function login(Request $request): RedirectResponse { $sessionId = $request->getSession()->getId(); $authUrl = $this->oauth2Service->generateAuthorizationUrl($sessionId); return new RedirectResponse($authUrl); } public function callback(Request $request): Response { $code = $request->query->get('code'); $state = $request->query->get('state'); $user = $this->oauth2Service->handleCallback($code, $state); // Process authenticated user return new Response('User authenticated: ' . $user->getUid()); } }
Advanced Usage
Custom Configuration
You can programmatically manage OAuth2 configurations:
use Tourze\SinaWeiboOAuth2Bundle\Entity\SinaWeiboOAuth2Config; use Tourze\SinaWeiboOAuth2Bundle\Repository\SinaWeiboOAuth2ConfigRepository; public function createConfig(SinaWeiboOAuth2ConfigRepository $configRepo): void { $config = new SinaWeiboOAuth2Config(); $config->setAppId('your-app-id') ->setAppSecret('your-app-secret') ->setScope('email,profile') ->setValid(true); $configRepo->save($config); }
Error Handling
use Tourze\SinaWeiboOAuth2Bundle\Exception\SinaWeiboOAuth2Exception; use Tourze\SinaWeiboOAuth2Bundle\Exception\SinaWeiboOAuth2ConfigurationException; try { $user = $this->oauth2Service->handleCallback($request); } catch (SinaWeiboOAuth2ConfigurationException $e) { // Handle configuration errors $this->logger->error('OAuth2 configuration error: ' . $e->getMessage()); } catch (SinaWeiboOAuth2Exception $e) { // Handle OAuth2 API errors $this->logger->error('OAuth2 API error: ' . $e->getMessage()); }
Console Commands
Configuration Management
sina-weibo-oauth2:config
Manage Sina Weibo OAuth2 configuration.
# List all configurations php bin/console sina-weibo-oauth2:config list # Create a new configuration php bin/console sina-weibo-oauth2:config create --app-id=YOUR_APP_ID --app-secret=YOUR_APP_SECRET --scope=email --active=true # Update an existing configuration php bin/console sina-weibo-oauth2:config update --id=1 --app-id=NEW_APP_ID --scope=profile,email # Delete a configuration php bin/console sina-weibo-oauth2:config delete --id=1
Available Options:
--app-id: Sina Weibo App ID--app-secret: Sina Weibo App Secret--scope: OAuth2 scope (default: email)--active: Configuration status (true/false, default: true)--id: Configuration ID for update/delete operations
Data Maintenance
sina-weibo-oauth2:cleanup
Clean up expired OAuth2 states and tokens.
# Clean up expired data php bin/console sina-weibo-oauth2:cleanup # Dry run to see what would be cleaned up php bin/console sina-weibo-oauth2:cleanup --dry-run
Available Options:
--dry-run: Show what would be cleaned up without actually doing it
Token Refresh
sina-weibo-oauth2:refresh-tokens
Refresh expired OAuth2 access tokens.
# Attempt to refresh expired tokens php bin/console sina-weibo-oauth2:refresh-tokens # Dry run to see what would be refreshed php bin/console sina-weibo-oauth2:refresh-tokens --dry-run
Available Options:
--dry-run: Show what would be refreshed without actually doing it
Note: Sina Weibo API does not support refresh tokens like other OAuth2 providers. This command is provided for interface compatibility but will always report 0 refreshed tokens. Users must re-authenticate when their tokens expire.
System Requirements
- PHP: 8.1 or higher
- Symfony: 7.3 or higher
- Doctrine ORM: 3.0 or higher
- Extensions:
ext-filter,ext-json - HTTP Client: Any PSR-18 compliant HTTP client
Included Dependencies
- Symfony Framework Bundle
- Symfony Security Bundle
- Symfony HTTP Client
- Doctrine Bundle
- Doctrine ORM
- PSR Log
Security
Important Security Considerations
- Store secrets securely: Never hardcode your app secret in source code. Use environment variables or Symfony's secrets management.
- Validate states: The bundle automatically validates OAuth2 states to prevent CSRF attacks.
- Token expiration: Monitor token expiration and implement proper re-authentication flows.
- HTTPS only: Always use HTTPS in production for OAuth2 flows.
Reporting Security Issues
If you discover a security vulnerability, please create a security advisory on GitHub or contact the maintainers directly instead of creating a public issue.
Contributing
Contributions are welcome! Please read our contributing guidelines before submitting pull requests.
Development Setup
# Clone the repository git clone https://github.com/tourze/php-monorepo.git cd php-monorepo # Install dependencies composer install # Run tests ./vendor/bin/phpunit packages/sina-weibo-oauth2-bundle/tests # Run static analysis php -d memory_limit=2G ./vendor/bin/phpstan analyse packages/sina-weibo-oauth2-bundle
Code Quality
- Follow PSR-12 coding standards
- Maintain 100% test coverage
- Pass PHPStan level 5 analysis
- Document all public APIs
Changelog
See CHANGELOG.md for version history and breaking changes.
License
This bundle is released under the MIT License. See LICENSE for details.
Credits
- Built for the Tourze PHP Monorepo project
- Sina Weibo OAuth2 API integration
- Community contributions welcomed