Authenticator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Bundle\ApiAuthBundle\Security\Jwt;
6
7
use Damax\Bundle\ApiAuthBundle\Extractor\Extractor;
8
use Damax\Bundle\ApiAuthBundle\Jwt\Claims;
9
use Damax\Bundle\ApiAuthBundle\Jwt\TokenParser;
10
use Damax\Bundle\ApiAuthBundle\Security\AbstractAuthenticator;
11
use Damax\Bundle\ApiAuthBundle\Security\ResponseFactory;
12
use Symfony\Component\Security\Core\Exception\AuthenticationException;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
16
final class Authenticator extends AbstractAuthenticator
17
{
18
    private $tokenParser;
19
    private $identityClaim;
20
21
    public function __construct(Extractor $extractor, ResponseFactory $response, TokenParser $tokenParser, string $identityClaim = null)
22
    {
23
        parent::__construct($extractor, $response);
24
25
        $this->tokenParser = $tokenParser;
26
        $this->identityClaim = $identityClaim;
27
    }
28
29
    public function checkCredentials($credentials, UserInterface $user): bool
30
    {
31
        return $this->tokenParser->isValid($credentials);
32
    }
33
34
    public function getUser($credentials, UserProviderInterface $userProvider): UserInterface
35
    {
36
        $jwtToken = $this->tokenParser->parse($credentials);
37
38
        if (null === $username = $jwtToken->get($this->identityClaim ?? Claims::SUBJECT)) {
39
            throw new AuthenticationException('Username could not be identified.');
40
        }
41
42
        return $userProvider->loadUserByUsername($username);
43
    }
44
}
45