Completed
Pull Request — master (#137)
by
unknown
12:51
created

ApiKeyAuthenticator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 60
rs 10
wmc 8
lcom 0
cbo 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A createToken() 0 13 2
A supportsToken() 0 4 2
B authenticateToken() 0 31 3
1
<?php
2
3
namespace AppBundle\Security;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
7
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
8
use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
9
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
10
use Symfony\Component\Security\Core\User\UserProviderInterface;
11
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
12
use Doctrine\Common\Persistence\ManagerRegistry;
13
14
class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface
15
{
16
    private $registry;
17
18
    public function __construct(ManagerRegistry $registry)
19
    {
20
        $this->registry = $registry;
21
    }
22
23
    public function createToken(Request $request, $providerKey)
24
    {
25
        $apiKey = $request->headers->get('API-Key-Token');
26
        if (!$apiKey) {
27
            throw new BadCredentialsException();
28
        }
29
30
        return new PreAuthenticatedToken(
31
                 'anon.',
32
                 $apiKey,
33
                $providerKey
34
            );
35
    }
36
37
    public function supportsToken(TokenInterface $token, $providerKey)
38
    {
39
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
40
    }
41
42
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
43
    {
44
        if (!$userProvider instanceof ApiKeyUserProvider) {
45
            throw new \InvalidArgumentException(
46
                sprintf(
47
                    'The user provider must be an instance of ApiKeyUserProvider (%s was given).',
48
                    get_class($userProvider)
49
                )
50
            );
51
        }
52
53
        $apiKey = $token->getCredentials();
54
        $username = $userProvider->getUsernameForApiKey($apiKey);
55
56
        if (!$username) {
57
            // CAUTION: this message will be returned to the client
58
            // (so don't put any un-trusted messages / error strings here)
59
            throw new CustomUserMessageAuthenticationException(
60
                sprintf('API Key "%s" does not exist.', $apiKey)
61
            );
62
        }
63
64
        $user = $userProvider->loadUserByUsername($username);
65
66
        return new PreAuthenticatedToken(
67
            $user,
68
            $apiKey,
69
            $providerKey,
70
            $user->getRoles()
71
        );
72
    }
73
}
74