allejo/bzbb-authentication-bundle
Install the Bundle Add the package via Composer:
composer require allejo/bzbb-authentication-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Allejo\BZBBAuthenticationBundle\AllejoBZBBAuthenticationBundle::class => ['all' => true],
];
Configure the Bundle
Update config/packages/allejo_bzbb_authentication.yaml (or create it if missing):
allejo_bzbb_authentication:
bzflag_url: 'https://www.bzflag.org' # BZFlag forum URL
login_path: '/login' # BZFlag login endpoint
callback_path: '/bzbb-callback' # Symfony callback route
Create a Callback Route
Define a route in config/routes.yaml:
bzbb_callback:
path: /bzbb-callback
controller: Allejo\BZBBAuthenticationBundle\Controller\BZBBCallbackController::callbackAction
First Use Case: Login Button
Add a login link in a Twig template (e.g., templates/base.html.twig):
<a href="{{ path('bzbb_callback') }}">Login with BZFlag</a>
The bundle will handle the OAuth flow automatically.
Trigger Authentication Redirect users to the BZFlag login page via the callback route. The bundle handles:
Callback Handling
After BZFlag authentication, users are redirected to /bzbb-callback. The bundle:
User entity.User Entity Integration
Extend the default User entity to include BZFlag-specific fields (e.g., bzflagId, bzflagUsername):
// src/Entity/User.php
class User implements UserInterface
{
// ...
private ?int $bzflagId;
private ?string $bzflagUsername;
}
Configure the bundle to map BZFlag data to your User entity in config/packages/allejo_bzbb_authentication.yaml:
allejo_bzbb_authentication:
user_entity:
class: App\Entity\User
property_mappings:
bzflag_id: 'bzflagId'
username: 'bzflagUsername'
Event-Driven Customization Listen to bundle events to customize behavior:
bzbb.authenticate: Modify the authentication logic (e.g., validate additional fields).
// src/EventListener/BZBBAuthListener.php
use Allejo\BZBBAuthenticationBundle\Event\AuthenticateEvent;
public function onAuthenticate(AuthenticateEvent $event)
{
$userData = $event->getUserData();
if (!$this->isValidUser($userData)) {
throw new \RuntimeException('Invalid BZFlag user data');
}
}
Register the listener in config/services.yaml:
services:
App\EventListener\BZBBAuthListener:
tags:
- { name: kernel.event_listener, event: bzbb.authenticate, method: onAuthenticate }
Security Integration
Ensure the firewalls section in config/packages/security.yaml includes the callback route:
security:
firewalls:
main:
# ...
form_login:
enable_csrf: true
# Allow the callback route to bypass authentication temporarily
pattern: ^/bzbb-callback
anonymous: true
Deprecated Symfony 3.4 Guard Auth
The bundle was updated for Symfony 3.4’s Guard auth changes, but Symfony 4+ users may need additional adjustments (e.g., Authenticator interfaces). Test thoroughly with your Symfony version.
State Token Validation
state parameter is preserved in redirects and validated in the callback. The bundle uses Symfony’s GuardAuthenticatorHandler for this, but custom routes may bypass it.var/log/dev.log for Invalid CSRF token errors.BZFlag API Changes
BZBBCallbackController may need patches.User Entity Mismatch
User entity doesn’t match the bundle’s expected structure, authentication will fail silently.UserNotFoundException or PropertyAccessException.Session Handling
Testing the Flow
HACCESS or curl to simulate the OAuth flow:
curl -v "https://www.bzflag.org/login?state=YOUR_STATE_TOKEN"
BZBBCallbackController.Customizing the Login Page
AllejoBZBBAuthenticationBundle:Default:login.html.twig in your theme.Logging and Monitoring
config/packages/monolog.yaml:
handlers:
bzbb:
type: stream
path: "%kernel.logs_dir%/bzbb_auth.log"
level: debug
channels: ["bzbb"]
bzbb channel to the bundle’s services.Performance Considerations
Extending the Bundle
Allejo\BZBBAuthenticationBundle\Service\BZBBUserProvider:
// src/Service/CustomBZBBUserProvider.php
use Allejo\BZBBAuthenticationBundle\Service\BZBBUserProvider;
class CustomBZBBUserProvider extends BZBBUserProvider
{
public function fetchUserData($sessionData)
{
// Add custom logic here
return parent::fetchUserData($sessionData);
}
}
config/services.yaml and update the bundle’s configuration to use your provider.Fallback Authentication
ProviderChainAuthenticator. Example:
security:
firewalls:
main:
provider: chain_provider
form_login: ~
providers:
chain_provider:
chain:
providers: [bzbb_provider, custom_provider]
voter_checker: security.user_voter_checker
bzbb_provider:
id: allejo_bzbb_authentication.user_provider
How can I help you explore Laravel packages today?