SimplePreAuthenticator::supportsToken()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the OsLabSecurityApiBundle package.
5
 *
6
 * (c) OsLab <https://github.com/OsLab>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace OsLab\SecurityApiBundle\Security\Authentication;
13
14
use OsLab\SecurityApiBundle\Security\User\InMemoryApiUserProvider;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
17
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
19
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
20
use Symfony\Component\Security\Core\User\UserProviderInterface;
21
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
22
23
/**
24
 * Pre-authentication mechanism.
25
 *
26
 * @author Michael COULLERET <[email protected]>
27
 * @author Florent DESPIERRES <[email protected]>
28
 */
29
class SimplePreAuthenticator implements SimplePreAuthenticatorInterface
30
{
31
    /**
32
     * @var string
33
     */
34
    protected $keyName;
35
36
    /**
37
     * @var string
38
     */
39
    protected $method;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param string $keyName the key name
45
     * @param string $method  the method http
46
     */
47 24
    public function __construct($keyName, $method)
48
    {
49 24
        $this->keyName = $keyName;
50 24
        $this->method = $method;
51 24
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 9
    public function createToken(Request $request, $providerKey)
57
    {
58 9
        $apiKey = null;
59
60 9
        if ('header' === $this->method) {
61 3
            $apiKey = $request->headers->get($this->keyName);
62 6
        } elseif ('query' === $this->method) {
63 3
            $apiKey = $request->query->get($this->keyName);
64
        }
65
66 9
        if (false === isset($apiKey)) {
67 3
            throw new AccessDeniedException(sprintf('The key "%s" is not provided', $this->keyName));
68
        }
69
70 6
        return new PreAuthenticatedToken(
71 6
            'anon.',
72
            $apiKey,
73
            $providerKey
74
        );
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 6
    public function supportsToken(TokenInterface $token, $providerKey)
81
    {
82 6
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 9
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
89
    {
90 9
        if (!$userProvider instanceof InMemoryApiUserProvider) {
91 3
            throw new \InvalidArgumentException(
92
                sprintf(
93 3
                    'The user provider must be an instance of %s; (%s was given).',
94 3
                    InMemoryApiUserProvider::class,
95
                    get_class($userProvider)
96
                )
97
            );
98
        }
99
100 6
        $apiKey = $token->getCredentials();
101 6
        $username = $userProvider->getUsernameByApiKey($apiKey);
102
103 6
        if (false === isset($username) && strlen($username) <= 0) {
104 3
            $exception = new UsernameNotFoundException(sprintf('API Key "%s" does not exist.', $apiKey));
105 3
            $exception->setUsername($username);
106
107 3
            throw $exception;
108
        }
109
110 3
        $user = $userProvider->loadUserByUsername($username);
111
112 3
        return new PreAuthenticatedToken(
113
            $user,
114
            $apiKey,
115
            $providerKey,
116 3
            $user->getRoles()
0 ignored issues
show
Documentation introduced by
$user->getRoles() is of type array<integer,object<Sym...Core\Role\Role>|string>, but the function expects a array<integer,object<Sym...\RoleInterface>|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...
117
        );
118
    }
119
}
120