SchulIT /
idp
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Security; |
||
| 4 | |||
| 5 | use App\Repository\KioskUserRepositoryInterface; |
||
| 6 | use App\Security\Badge\ClientIpAddressBadge; |
||
| 7 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||
| 8 | use Symfony\Component\HttpFoundation\Request; |
||
| 9 | use Symfony\Component\HttpFoundation\Response; |
||
| 10 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
||
| 11 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||
| 12 | use Symfony\Component\Security\Core\Exception\UserNotFoundException; |
||
| 13 | use Symfony\Component\Security\Http\Authenticator\AbstractLoginFormAuthenticator; |
||
| 14 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
||
| 15 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
||
| 16 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
||
| 17 | |||
| 18 | class KioskUserAuthenticator extends AbstractLoginFormAuthenticator { |
||
| 19 | |||
| 20 | public function __construct(private KioskUserRepositoryInterface $repository, private UrlGeneratorInterface $urlGenerator) |
||
| 21 | { |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @inheritDoc |
||
| 26 | */ |
||
| 27 | public function supports(Request $request): bool { |
||
| 28 | return $request->query->has('token'); |
||
| 29 | } |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @inheritDoc |
||
| 33 | */ |
||
| 34 | public function onAuthenticationSuccess(Request $request, TokenInterface $token, $firewallName): ?Response { |
||
| 35 | return new RedirectResponse($this->urlGenerator->generate('dashboard')); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @inheritDoc |
||
| 40 | */ |
||
| 41 | protected function getLoginUrl(Request $request): string { |
||
| 42 | return $this->urlGenerator->generate('login'); |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @inheritDoc |
||
| 47 | */ |
||
| 48 | public function authenticate(Request $request): Passport { |
||
| 49 | $token = $request->query->get('token'); |
||
| 50 | |||
| 51 | $user = $this->repository->findOneByToken($token); |
||
| 52 | |||
| 53 | if($user === null) { |
||
| 54 | throw new UserNotFoundException(); |
||
| 55 | } |
||
| 56 | |||
| 57 | return new SelfValidatingPassport( |
||
| 58 | new UserBadge($user->getUser()->getUserIdentifier()), |
||
| 59 | [ |
||
| 60 | new ClientIpAddressBadge(explode(',', $user->getIpAddresses())) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 61 | ] |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | } |