|
1
|
|
|
<?php declare(strict_types = 1); |
|
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 KleijnWeb\JwtBundle\User\JwtUserProvider; |
|
11
|
|
|
use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface; |
|
12
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
13
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
14
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use KleijnWeb\JwtBundle\User\UnsafeGroupsUserInterface; |
|
17
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
18
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @author John Kleijn <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Authenticator implements SimplePreAuthenticatorInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var JwtKey[] |
|
27
|
|
|
*/ |
|
28
|
|
|
private $keys = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param JwtKey[] $keys |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(array $keys) |
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($keys as $key) { |
|
36
|
|
|
$this->keys[$key->getId()] = $key; |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param string|null $id |
|
42
|
|
|
* |
|
43
|
|
|
* @return JwtKey |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getKeyById(string $id = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($id) { |
|
|
|
|
|
|
48
|
|
|
if (!isset($this->keys[$id])) { |
|
49
|
|
|
throw new AuthenticationException("Unknown 'kid' $id"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $this->keys[$id]; |
|
53
|
|
|
} |
|
54
|
|
|
if (count($this->keys) > 1) { |
|
55
|
|
|
throw new AuthenticationException("Missing 'kid'"); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return current($this->keys); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Request $request |
|
63
|
|
|
* @param string $providerKey |
|
64
|
|
|
* |
|
65
|
|
|
* @return PreAuthenticatedToken |
|
66
|
|
|
*/ |
|
67
|
|
|
public function createToken(Request $request, $providerKey) |
|
68
|
|
|
{ |
|
69
|
|
|
$tokenString = $request->headers->get('Authorization'); |
|
70
|
|
|
|
|
71
|
|
|
if (0 === strpos((string)$tokenString, 'Bearer ')) { |
|
72
|
|
|
$tokenString = substr($tokenString, 7); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (!$tokenString) { |
|
76
|
|
|
throw new BadCredentialsException('No API key found'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
try { |
|
80
|
|
|
$token = new JwtToken($tokenString); |
|
81
|
|
|
$key = $this->getKeyById($token->getKeyId()); |
|
82
|
|
|
$key->validateToken($token); |
|
83
|
|
|
} catch (\Exception $e) { |
|
84
|
|
|
throw new AuthenticationException('Invalid key', 0, $e); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return new PreAuthenticatedToken('anon.', $token, $providerKey); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param TokenInterface $token |
|
92
|
|
|
* @param UserProviderInterface $userProvider |
|
93
|
|
|
* @param string $providerKey |
|
94
|
|
|
* |
|
95
|
|
|
* @return PreAuthenticatedToken |
|
96
|
|
|
*/ |
|
97
|
|
|
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
|
98
|
|
|
{ |
|
99
|
|
|
/** @var $jwtToken JwtToken */ |
|
100
|
|
|
if (!($jwtToken = $token->getCredentials()) instanceof JwtToken) { |
|
101
|
|
|
throw new \UnexpectedValueException("Expected credentials to be a JwtToken object"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$username = $jwtToken->getSubject(); |
|
105
|
|
|
|
|
106
|
|
|
if ($userProvider instanceof JwtUserProvider) { |
|
107
|
|
|
// Not ideal, sequential coupling |
|
108
|
|
|
$userProvider->setClaimsUsingToken($jwtToken); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$user = $userProvider->loadUserByUsername($username); |
|
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
if (!$userProvider instanceof JwtUserProvider && $user instanceof UnsafeGroupsUserInterface) { |
|
114
|
|
|
$user = $this->setUserRolesFromAudienceClaims($user, $jwtToken->getClaims()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return new PreAuthenticatedToken($user, $token, $providerKey, $user->getRoles()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param TokenInterface $token |
|
122
|
|
|
* @param string $providerKey |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function supportsToken(TokenInterface $token, $providerKey) |
|
127
|
|
|
{ |
|
128
|
|
|
return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param UnsafeGroupsUserInterface $user |
|
133
|
|
|
* @param array $claims |
|
134
|
|
|
* |
|
135
|
|
|
* @return UnsafeGroupsUserInterface |
|
136
|
|
|
*/ |
|
137
|
|
|
private function setUserRolesFromAudienceClaims(UnsafeGroupsUserInterface $user, array $claims) |
|
138
|
|
|
{ |
|
139
|
|
|
foreach (JwtUserProvider::getRolesFromAudienceClaims($claims) as $role) { |
|
|
|
|
|
|
140
|
|
|
$user->addRole($role); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $user; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: