Authenticator   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getUser() 0 9 2
A checkCredentials() 0 3 1
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