alexandret/phpcas-guard-bundle
composer require alexandret/phpcas-guard-bundle:~1
config/bundles.php:
AlexandreT\Bundle\CasGuardBundle\CasGuardBundle::class => ['all' => true],
config/packages/security.yaml:
firewalls:
main:
guard:
authenticators:
- phpcasguard.cas_authenticator
logout:
path: /logout
success_handler: phpcasguard.cas_authenticator
config/packages/cas_guard.yaml:
cas_guard:
hostname: '%env(CAS_HOSTNAME)%'
# Add other required settings (e.g., `cas_server_url`, `service_validate_url`)
/login (default route). The bundle handles the CAS flow automatically.$this->getUser() in controllers to confirm a user is logged in./logout.User Authentication Flow
/login → redirected to CAS server → CAS validates credentials → redirects back to your app.phpcasguard.cas_authenticator processes the response and creates a Symfony User object.User Provider Integration
User class and implement UserInterface (e.g., App\Entity\User).user_provider in security.yaml to map CAS attributes to your user entity:
security:
providers:
cas_provider:
entity:
class: App\Entity\User
property: username # CAS attribute to match
Attribute Mapping
Use cas_guard.yaml to map CAS attributes to Symfony roles or user properties:
cas_guard:
attribute_callback:
- AlexandreT\Bundle\CasGuardBundle\Attribute\Callback\RoleAttributeCallback
role_attributes:
- 'CN' # CAS attribute to map to Symfony roles
Custom Authenticators
Extend CasAuthenticator for custom logic (e.g., post-authentication redirects):
use AlexandreT\Bundle\CasGuardBundle\Security\CasAuthenticator;
class CustomCasAuthenticator extends CasAuthenticator {
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) {
// Custom logic (e.g., flash messages, redirects)
return new RedirectResponse('/dashboard');
}
}
Register in security.yaml:
guard:
authenticators:
- App\Security\CustomCasAuthenticator
Logout Handling
Override the default logout behavior by extending the authenticator or using a custom LogoutSuccessHandler:
logout:
path: /logout
success_handler: App\Security\CustomLogoutHandler
CAS Server Configuration
cas_server_url and service_validate_url in cas_guard.yaml point to your CAS server’s endpoints.CAS_HOSTNAME set to a valid CAS server (e.g., https://cas.example.com).Attribute Mismatches
CN, uid) match your user_provider configuration.phpcasguard.cas_authenticator.debug: true in cas_guard.yaml.Symfony Guard Deprecation
authenticator system or a fork of this bundle.HTTPS Requirements
service_validate_url uses https://.Session Issues
framework.session in config/packages/framework.yaml).Enable Debug Mode
Add to cas_guard.yaml:
debug: true
This logs CAS requests/responses to var/log/dev.log.
Validate CAS Response
Use a tool like Postman to manually test the CAS validate endpoint with your service URL.
Check PHP-CAS Dependencies
Ensure jasig/phpcas is compatible with your PHP version (e.g., PHP 7.4+ may need ~1.3.5).
Custom Attribute Callbacks
Implement AttributeCallbackInterface to transform CAS attributes:
use AlexandreT\Bundle\CasGuardBundle\Attribute\AttributeCallbackInterface;
class CustomAttributeCallback implements AttributeCallbackInterface {
public function transform(array $attributes) {
$attributes['custom_role'] = $attributes['memberOf'][0];
return $attributes;
}
}
Register in cas_guard.yaml:
attribute_callback:
- App\Attribute\CustomAttributeCallback
Override Authenticator Logic
Extend CasAuthenticator to modify:
Proxy Support
If behind a proxy, configure trusted_proxies in security.yaml and ensure CAS requests include the correct REMOTE_ADDR.
Multi-Tenant CAS For multiple CAS servers, create a dynamic authenticator that switches configurations based on tenant-specific routes.
How can I help you explore Laravel packages today?