src/Security/Listener/CasPassportListener.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Listener;
  3. use App\Security\CasAuthenticator;
  4. use App\Security\Event\BuildPassport;
  5. use Symfony\Component\Security\Core\Exception\UserNotFoundException;
  6. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\PreAuthenticatedUserBadge;
  7. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  8. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  9. class CasPassportListener
  10. {
  11.     public function __invoke(BuildPassport $event): void
  12.     {
  13.         if (CasAuthenticator::class !== $event->authenticator()) {
  14.             return;
  15.         }
  16.         $event->stopPropagation();
  17.         $attributes $event->attributes();
  18.         $uid $attributes['uid'] ?? '';
  19.         if (empty($uid)) {
  20.             throw new UserNotFoundException();
  21.         }
  22.         $event->setPassport(new SelfValidatingPassport(new UserBadge("{$uid}@ku.edu"), [new PreAuthenticatedUserBadge()]));
  23.     }
  24. }