Installation Add the bundle via Composer:
composer require db4y/cas-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Db4y\CasBundle\Db4yCasBundle::class => ['all' => true],
];
Configuration Publish the default config:
php artisan vendor:publish --tag=cas-bundle-config
Update config/cas.php with your CAS server details (e.g., server_url, client_id, client_secret).
First Use Case Secure a route with CAS authentication:
use Symfony\Component\HttpFoundation\Response;
use Db4y\CasBundle\Security\Authenticator\CasAuthenticator;
protected function configure()
{
$this->formLogin(CasAuthenticator::class);
}
Redirect users to CAS login via:
return $this->authenticator->authenticateRequest($request);
Login Flow
CasAuthenticator in a Firewall (e.g., main in config/security.yaml).CasCallbackController (auto-registered).security.yaml:
firewalls:
main:
pattern: ^/
anonymous: lazy
provider: cas
form_login:
authenticator: Db4y\CasBundle\Security\Authenticator\CasAuthenticator
User Sync
CasUserProvider to map CAS attributes to Laravel users:
class CustomCasUserProvider extends CasUserProvider
{
public function loadUserByUsername($username)
{
return User::where('cas_username', $username)->firstOrFail();
}
}
config/cas.php:
'user_provider' => \App\Providers\CustomCasUserProvider::class,
Logout
use Db4y\CasBundle\Security\Logout\CasLogoutHandler;
$logoutHandler = new CasLogoutHandler();
$logoutHandler->logout($request, $response, $token);
CasAuthenticator in middleware for API routes:
public function handle($request, Closure $next)
{
if (!$request->user()) {
return $this->authenticator->authenticateRequest($request);
}
return $next($request);
}
CasAuthenticator in PHPUnit:
$this->mock(CasAuthenticator::class)
->shouldReceive('authenticateRequest')
->andReturn($this->createMock(User::class));
Callback URL Mismatch
cas.callback.url in config/cas.php matches your CAS server’s expected redirect URI.php artisan cas:debug
Attribute Mapping
urn:oid:...). Normalize in CasUserProvider:
$attributes = $this->normalizeAttributes($user->getAttributes());
Session Fixation
$request->getSession()->regenerate();
config/cas.php:
'debug' => env('APP_DEBUG', false),
dd($this->casService->getServiceResponse());
Custom Attributes
Override CasUserProvider::mapAttributes() to handle non-standard fields:
protected function mapAttributes(array $attributes): array
{
return [
'email' => $attributes['mail'] ?? $attributes['email'] ?? null,
'name' => $attributes['cn'] ?? null,
];
}
Multi-CAS Support Use environment-based config:
'server_url' => env('CAS_SERVER_URL', 'https://default-cas.example.com'),
Proxy Integration
For reverse proxies, set trust_proxy in config/cas.php:
'trust_proxy' => true,
How can I help you explore Laravel packages today?