ecphp/ecas
ECAS is a PHP library for working with CAS (Central Authentication Service) authentication. It provides a clean API, strong type coverage, and maintained CI. See full guides and usage examples in the dedicated documentation at ecpHP-ecas.readthedocs.io.
Installation
composer require ecphp/ecas
Add to config/app.php under providers:
Ecphp\Ecas\EcasServiceProvider::class,
Configuration Publish the config file:
php artisan vendor:publish --provider="Ecphp\Ecas\EcasServiceProvider"
Update config/ecas.php with your eCAS server URL and credentials.
First Use Case: Authentication
use Ecphp\Ecas\Ecas;
$ecas = app(Ecas::class);
$loginUrl = $ecas->getLoginUrl('/post-login'); // Redirect user to eCAS
return redirect()->to($loginUrl);
Handling Callback Add a route to handle the eCAS callback:
Route::get('/post-login', function () {
$ecas = app(Ecas::class);
$user = $ecas->handleCallback(); // Validates and retrieves user data
auth()->loginUsingId($user['id']); // Integrate with Laravel auth
return redirect()->intended('/dashboard');
});
Single Sign-On (SSO) Flow
getLoginUrl() to redirect users to eCAS.handleCallback().User Data Retrieval
$userInfo = $ecas->getUserInfo(); // After successful login
// Example: $userInfo['username'], $userInfo['email']
Logout Handling
$logoutUrl = $ecas->getLogoutUrl('/post-logout');
return redirect()->to($logoutUrl);
Custom Attributes
Extend the User model to include eCAS-specific fields (e.g., cas_attributes).
Laravel Auth Integration
Create a custom EcasGuard to extend Laravel’s auth system:
use Ecphp\Ecas\Ecas;
class EcasGuard extends Guard {
public function validate(array $credentials = []) {
$ecas = app(Ecas::class);
$user = $ecas->handleCallback();
return $user !== null;
}
}
Middleware for Protected Routes
class EcasAuthenticate extends Middleware {
public function handle($request, Closure $next) {
if (!auth()->check()) {
$ecas = app(Ecas::class);
return redirect()->to($ecas->getLoginUrl());
}
return $next($request);
}
}
Session Management
Use ecas:session middleware to validate active sessions:
Route::middleware(['ecas:session'])->group(function () {
// Protected routes
});
Callback Validation
handleCallback() response. Malformed responses may indicate:
$response = $ecas->handleCallback();
dd($response->getErrors()); // Check for errors
Session Expiry
last_active timestamp in your users table and clear expired sessions:
if (auth()->user()->last_active < now()->subHours(2)) {
auth()->logout();
return redirect()->route('login');
}
Attribute Mapping
uid, mail). Map them to your Laravel User model:
$user = User::updateOrCreate(
['email' => $userInfo['mail']],
['name' => $userInfo['cn'] ?? $userInfo['uid']]
);
HTTPS Requirements
APP_URL=https://your-app.com
Enable Logging
Add to config/ecas.php:
'debug' => env('ECAS_DEBUG', false),
Check logs in storage/logs/laravel.log for errors.
Test Locally Use a local eCAS test server (e.g., eCAS Docker) to avoid production issues.
Custom User Provider
Extend Ecphp\Ecas\UserProvider to fetch users from your database:
class CustomUserProvider extends UserProvider {
public function retrieveByCredentials(array $credentials) {
// Custom logic (e.g., LDAP, API)
}
}
Attribute Filters Filter attributes before storing them:
$ecas->setAttributeFilter(function ($attributes) {
return array_filter($attributes, fn($key) => str_starts_with($key, 'eduPerson'), ARRAY_FILTER_USE_KEY);
});
Event Listeners Listen for eCAS events (e.g., login/logout) via Laravel’s event system:
Event::listen(EcasEvents::LOGIN, function ($user) {
// Trigger analytics, notifications, etc.
});
Proxy Support
Configure proxy settings in config/ecas.php:
'proxy' => [
'host' => env('ECAS_PROXY_HOST'),
'port' => env('ECAS_PROXY_PORT'),
],
How can I help you explore Laravel packages today?