Installation
Add the bundle to your composer.json:
composer require berduj/sso-auth-bundle
Enable it in config/bundles.php:
return [
// ...
Berduj\SsoAuthBundle\BerdujSsoAuthBundle::class => ['all' => true],
];
Configuration
Update config/packages/berduj_sso_auth.yaml (or create it):
berduj_sso_auth:
trusted:
cas:
server_url: "https://your-cas-server.example.com/cas"
service_url: "https://your-app.example.com/login/cas"
validate_url: "https://your-cas-server.example.com/cas/serviceValidate"
First Use Case: CAS Login
Add a route to trigger CAS authentication in config/routes.yaml:
berduj_sso_auth_cas_login:
path: /login/cas
controller: Berduj\SsoAuthBundle\Controller\CasController::login
Redirect users to /login/cas to initiate SSO.
Trusted CAS Authentication
/login/cas.service_url with a ticket.validate_url and creates a Symfony User object.Berduj\SsoAuthBundle\Security\CasUserProvider to map CAS attributes to your User entity.Open SSO (Future)
Extend the bundle by implementing Berduj\SsoAuthBundle\Provider\OpenSsoProviderInterface for protocols like OpenID.
Security Integration
Configure Symfony’s security firewall in config/packages/security.yaml:
firewalls:
main:
form_login:
provider: your_user_provider
cas:
provider: berduj_sso_auth.cas_user_provider
login_path: /login/cas
check_path: /login/cas/check
User Entity Mapping:
Use Doctrine listeners or the CasUserProvider to hydrate your User entity from CAS attributes:
// src/EventListener/CasUserListener.php
public function loadUserByCasAttributes(array $attributes)
{
return UserRepository::findOneBy(['email' => $attributes['email']]);
}
Register the listener in services.yaml:
services:
App\EventListener\CasUserListener:
tags:
- { name: kernel.event_listener, event: berduj_sso_auth.cas_user_load, method: loadUserByCasAttributes }
Attribute Handling:
Extract custom attributes from CAS responses using CasAuthenticationHandler:
$attributes = $event->getAuthenticationToken()->getCredentials();
$user->setFirstName($attributes['givenName'] ?? null);
CORS/Redirect Issues:
service_url in config matches the exact callback URL registered with your CAS server.User Provider Mismatch:
CasUserProvider is correctly wired to your User entity.php bin/console cache:clear) after changing provider configurations.Deprecated Symfony2:
Attribute Parsing:
json_decode($attributes, true) if attributes are JSON-encoded.APP_DEBUG=1 in .env to log CAS validation errors in var/log/dev.log.validate_url endpoint directly with a ticket:
curl "https://your-cas-server.example.com/cas/serviceValidate?ticket=TGT-123&service=https://your-app.example.com/login/cas"
Expected response: XML with <authenticationSuccess> or <authenticationFailure>.Custom Providers:
Implement Berduj\SsoAuthBundle\Provider\SsoProviderInterface for unsupported protocols (e.g., SAML):
class SamlProvider implements SsoProviderInterface {
public function authenticate(array $attributes) { ... }
}
Register in services.yaml:
services:
App\Provider\SamlProvider:
tags:
- { name: berduj_sso_auth.provider, alias: saml }
Event Dispatching:
Listen for berduj_sso_auth.cas_authenticated to post-process authenticated users:
services:
App\EventListener\PostAuthListener:
tags:
- { name: kernel.event_listener, event: berduj_sso_auth.cas_authenticated, method: onCasAuth }
Configuration Overrides: Dynamically override CAS URLs via dependency injection:
// config/services.yaml
Berduj\SsoAuthBundle\Security\CasUserProvider:
arguments:
$casServerUrl: '%env(CAS_SERVER_URL)%'
How can I help you explore Laravel packages today?