ecphp/cas-lib
Laravel-oriented PHP CAS (Central Authentication Service) library for integrating SSO into your app. Provides CAS client features like login/logout handling, ticket validation, and user attribute retrieval, aiming for straightforward setup and compatibility with common CAS servers.
Installation
composer require ecphp/cas-lib
Add to config/app.php under providers:
Ecphp\CasLib\CasLibServiceProvider::class,
Basic Configuration Publish the config file:
php artisan vendor:publish --provider="Ecphp\CasLib\CasLibServiceProvider"
Update config/cas.php with your CAS server details (e.g., server_url, client_id, client_secret).
First Use Case: Authenticate a User
use Ecphp\CasLib\Facades\CasLib;
// Redirect user to CAS login
$loginUrl = CasLib::getLoginUrl('/return-to-app');
return redirect()->to($loginUrl);
// Handle CAS callback (e.g., in a route)
$ticket = request()->get('ticket');
$user = CasLib::validateTicket($ticket);
if ($user) {
// Authenticate user in your app (e.g., Laravel session)
auth()->loginUsingId($user->getId());
}
User Authentication Flow
$loginUrl = CasLib::getLoginUrl(route('cas.callback'));
public function handleCasCallback()
{
$ticket = request()->get('ticket');
$user = CasLib::validateTicket($ticket);
if ($user) {
auth()->login($user); // Customize based on your user model
}
return redirect()->intended('/dashboard');
}
$logoutUrl = CasLib::getLogoutUrl();
return redirect()->to($logoutUrl);
Service Integration
public function handle($request, Closure $next)
{
if (!auth()->check()) {
return redirect()->route('cas.login');
}
return $next($request);
}
public function apiAuth(Request $request)
{
$ticket = $request->bearerToken();
$user = CasLib::validateTicket($ticket);
if (!$user) {
return response()->json(['error' => 'Invalid ticket'], 401);
}
return $next($request);
}
Proxy Authentication
CasLib::proxyValidateTicket() for proxy tickets (e.g., SAML-like flows):
$proxyTicket = request()->get('proxyTicket');
$user = CasLib::proxyValidateTicket($proxyTicket, $serviceUrl);
CasLibUserProvider for seamless integration:
use Ecphp\CasLib\CasLibUserProvider;
auth()->provider('cas', function ($app) {
return new CasLibUserProvider($app['config']['cas']);
});
session(['cas_attributes' => $user->getAttributes()]);
$user->setAttribute('email', $user->getAttribute('mail'));
Ticket Validation Timeouts
Ecphp\CasLib\CasLib to handle stale tickets gracefully.CSRF in Callback Routes
POST requests with the ticket. Ensure your route handles both GET and POST:
Route::post('/cas/callback', [CasController::class, 'handleCallback']);
Attribute Parsing Issues
$cleanAttributes = array_map('trim', $user->getAttributes());
Proxy Ticket Limitations
Enable Logging
Configure config/cas.php:
'debug' => env('CAS_DEBUG', false),
Logs will appear in storage/logs/laravel.log.
Validate URLs
Ensure service_url in config/cas.php matches the exact callback URL (including http/https and port).
Test with curl
Manually test CAS flows:
curl -v "https://your-cas-server/login?service=YOUR_CALLBACK_URL"
Custom User Model
Override CasLibUser to map attributes to your model:
use Ecphp\CasLib\CasLibUser;
class AppCasUser extends CasLibUser
{
public function getLaravelUser()
{
return User::firstOrCreate(
['email' => $this->getAttribute('mail')],
['name' => $this->getAttribute('cn')]
);
}
}
Custom Attribute Handlers
Extend Ecphp\CasLib\AttributeHandler to process attributes:
class CustomAttributeHandler extends AttributeHandler
{
public function handleAttributes(array $attributes)
{
$attributes['normalized_email'] = strtolower($attributes['mail']);
return $attributes;
}
}
Register in config/cas.php:
'attribute_handler' => \App\Services\CustomAttributeHandler::class,
Multi-CAS Server Support Use dynamic configuration for multiple CAS servers:
CasLib::setConfig(['server_url' => env('CAS_SERVER_URL')]);
How can I help you explore Laravel packages today?