Completed
Push — master ( ee280a...529931 )
by John
01:02 queued 57s
created

Authenticator::getUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\JwtBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace KleijnWeb\JwtBundle\Authenticator;
9
10
use Symfony\Component\Security\Core\Authentication\SimplePreAuthenticatorInterface;
11
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
12
use Symfony\Component\Security\Core\Exception\AuthenticationException;
13
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\Security\Core\User\UserProviderInterface;
16
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
17
18
/**
19
 * @author John Kleijn <[email protected]>
20
 */
21
class Authenticator implements SimplePreAuthenticatorInterface
0 ignored issues
show
Deprecated Code introduced by
The interface Symfony\Component\Securi...eAuthenticatorInterface has been deprecated with message: Since version 2.8, to be removed in 3.0. Use the same interface from Security\Http\Authentication instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
22
{
23
    /**
24
     * @var JwtKey[]
25
     */
26
    private $keys = [];
27
28
    /**
29
     * @param JwtKey[] $keys
30
     */
31
    public function __construct(array $keys)
32
    {
33
        foreach ($keys as $key) {
34
            $this->keys[$key->getId()] = $key;
35
        }
36
    }
37
38
    /**
39
     * @param string $id
40
     *
41
     * @return JwtKey
42
     */
43
    public function getKeyById($id)
44
    {
45
        if ($id) {
46
            if (!isset($this->keys[$id])) {
47
                throw new AuthenticationException("Unknown 'kid' $id");
48
            }
49
50
            return $this->keys[$id];
51
        }
52
        if (count($this->keys) > 1) {
53
            throw new AuthenticationException("Missing 'kid'");
54
        }
55
56
        return current($this->keys);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression current($this->keys); of type KleijnWeb\JwtBundle\Authenticator\JwtKey|false adds false to the return on line 56 which is incompatible with the return type documented by KleijnWeb\JwtBundle\Auth...thenticator::getKeyById of type KleijnWeb\JwtBundle\Authenticator\JwtKey. It seems like you forgot to handle an error condition.
Loading history...
57
    }
58
59
    /**
60
     * @param Request $request
61
     * @param string  $providerKey
62
     *
63
     * @return PreAuthenticatedToken
64
     */
65
    public function createToken(Request $request, $providerKey)
66
    {
67
        $tokenString = $request->headers->get('Authorization');
68
69
        if (0 === strpos($tokenString, 'Bearer ')) {
70
            $tokenString = substr($tokenString, 7);
71
        }
72
73
        if (!$tokenString) {
74
            throw new BadCredentialsException('No API key found');
75
        }
76
77
        try {
78
            $token = new JwtToken($tokenString);
79
            $key   = $this->getKeyById($token->getKeyId());
80
            $key->validateToken($token);
81
        } catch (\Exception $e) {
82
            throw new AuthenticationException('Invalid key', 0, $e);
83
        }
84
85
        return new PreAuthenticatedToken('anon.', $token, $providerKey);
86
    }
87
88
    /**
89
     * @param TokenInterface        $token
90
     * @param UserProviderInterface $userProvider
91
     * @param string                $providerKey
92
     *
93
     * @return PreAuthenticatedToken
94
     */
95
    public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
96
    {
97
        /** @var $jwtToken JwtToken */
98
        if (!($jwtToken = $token->getCredentials()) instanceof JwtToken) {
99
            throw new \UnexpectedValueException("Expected credentials to be a JwtToken object");
100
        }
101
102
        $user = $userProvider->loadUserByUsername($jwtToken->getSubject());
0 ignored issues
show
Documentation introduced by
$jwtToken->getSubject() is of type array, 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...
103
104
        return new PreAuthenticatedToken($user, $token, $providerKey, $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...
105
    }
106
107
    /**
108
     * @param TokenInterface $token
109
     * @param string         $providerKey
110
     *
111
     * @return bool
112
     */
113
    public function supportsToken(TokenInterface $token, $providerKey)
114
    {
115
        return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
116
    }
117
}
118