<?php
namespace App\Security\Listener;
use App\Security\CasAuthenticator;
use App\Security\Event\BuildPassport;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
class CasPassportListener
{
public function __invoke(BuildPassport $event): void
{
if (CasAuthenticator::class !== $event->authenticator()) {
return;
}
$event->stopPropagation();
$attributes = $event->attributes();
$uid = $attributes['uid'] ?? '';
if (empty($uid)) {
throw new UserNotFoundException();
}
$event->setPassport(new SelfValidatingPassport(new UserBadge("{$uid}@ku.edu"), [new PreAuthenticatedUserBadge()]));
}
}