Completed
Pull Request — master (#144)
by
unknown
12:31
created

ApiKeyAuthenticator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
B authenticateToken() 0 31 3
A __construct() 0 4 1
A createToken() 0 13 2
A supportsToken() 0 4 2
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
    /**
17
     * @var ManagerRegistry
18
     */
19
    private $registry;
20
21
    /**
22
     * @param ManagerRegistry $registry
23
     */
24
    public function __construct(ManagerRegistry $registry)
25
    {
26
        $this->registry = $registry;
27
    }
28
29
    /**
30
     * @param Request $request
31
     * @param string $providerKey
32
     * @return PreAuthenticatedToken
33
     */
34
    public function createToken(Request $request, $providerKey)
35
    {
36
        $apiKey = $request->headers->get('API-Key-Token');
37
        if (!$apiKey) {
38
            throw new BadCredentialsException();
39
        }
40
41
        return new PreAuthenticatedToken(
42
            'customer',
43
            $apiKey,
44
            $providerKey
45
        );
46
    }
47
48
    /**
49
     * @param TokenInterface $token
50
     * @param string $providerKey
51
     * @return bool
52
     */
53
    public function supportsToken(TokenInterface $token, $providerKey)
54
    {
55
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
56
    }
57
58
    /**
59
     * @param TokenInterface $token
60
     * @param UserProviderInterface $userProvider
61
     * @param string $providerKey
62
     * @return PreAuthenticatedToken
63
     */
64
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
65
    {
66
        if (!$userProvider instanceof ApiKeyUserProvider) {
67
            throw new \InvalidArgumentException(
68
                sprintf(
69
                    'The user provider must be an instance of ApiKeyUserProvider (%s was given).',
70
                    get_class($userProvider)
71
                )
72
            );
73
        }
74
75
        $apiKey = $token->getCredentials();
76
        $username = $userProvider->getUsernameByApiKey($apiKey);
77
78
        if (!$username) {
79
            // CAUTION: this message will be returned to the client
80
            // (so don't put any un-trusted messages / error strings here)
81
            throw new InvalidApiKeyException(
82
                sprintf('API Key "%s" does not exist.', $apiKey)
83
            );
84
        }
85
86
        $user = $userProvider->loadUserByUsername($username);
0 ignored issues
show
Documentation introduced by
$username is of type object, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
87
88
        return new PreAuthenticatedToken(
89
            $user,
90
            $apiKey,
91
            $providerKey,
92
            $user->getRoles()
93
        );
94
    }
95
}
96