##JASIG CAS Authentication with Laravel Lumen
require {
"jasig/phpcas": "^1.3"
}
CAS_HOST=login.gatech.edu
CAS_CONTEXT=/cas
<?php
namespace App\Http\Middleware;
use Closure;
use phpCAS;
class AuthCASMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$cas_config = array(
'host' => getenv('CAS_HOST'),
'context' => getenv('CAS_CONTEXT'),
);
phpCAS::client(CAS_VERSION_2_0, $cas_config['host'], 443, $cas_config['context']);
phpCAS::setNoCasServerValidation();
if ($request->has('logout')) {
phpCAS::logout();
} else if (phpCAS::isAuthenticated()) {
return $next($request);
} else {
phpCAS::forceAuthentication();
}
}
}
Uncomment the array opening/closing, and insert this line as follows. This will force CAS authentication for all routes. This can be moved to $app->routeMiddleware
if you want to only use it for certain routes. Just specify the middleware as documented in the Lumen docs.
$app->middleware([
...
'App\Http\Middleware\AuthCASMiddleware::class',
...
]);
Put this function in your controller, then access the variable as seen in the example.
/**
* Get current CAS logged-in user
* @return bool|string GT Username if logged in, false if not
*/
public function getCASUser() {
if (phpCAS::isAuthenticated()) {
return phpCAS::getUser();
} else {
return false;
}
}
Add this to your controller's return statement ->with("casUser",$this->getCASUser())
Then you can access it in a view with {{ (isset($casUser)) ? $casUser : "null" }}
or just {{ $casUser }}
Out of curiosity what version of Lumen was this for?