KioskUserAuthenticator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 2
rs 10
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
It seems like $user->getIpAddresses() can also be of type null; however, parameter $string of explode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
                new ClientIpAddressBadge(explode(',', /** @scrutinizer ignore-type */ $user->getIpAddresses()))
Loading history...
61
            ]
62
        );
63
    }
64
}