1 | <?php |
||
19 | abstract class AbstractAccessTokenAuthenticator |
||
20 | implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface |
||
21 | { |
||
22 | const DEFAULT_TOKEN_QUERY_PARAMETER_NAME = 'token'; |
||
23 | const DEFAULT_TOKEN_HEADER_NAME = 'X-Access-Token'; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $tokenQueryParameterName = self::DEFAULT_TOKEN_QUERY_PARAMETER_NAME; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $tokenHeaderName = self::DEFAULT_TOKEN_HEADER_NAME; |
||
34 | |||
35 | /** |
||
36 | * If set to true an AuthenticationException will be thrown if no Access Token was found. Otherwise if will simply |
||
37 | * continue with other authentication methods. |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $tokenRequired = false; |
||
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 48 | public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
|
47 | { |
||
48 | 48 | $tokenString = $token->getCredentials(); |
|
49 | |||
50 | 48 | $user = $this->findUserByToken($tokenString); |
|
51 | |||
52 | 48 | if (null === $user || !$user instanceof UserInterface) { |
|
53 | 2 | throw new AuthenticationException('Invalid Access Token'); |
|
54 | } |
||
55 | |||
56 | 48 | $userRoles = $user->getRoles(); |
|
57 | 48 | $userRoles[] = 'ROLE_REST_API'; |
|
58 | |||
59 | 48 | return new PreAuthenticatedToken( |
|
60 | 48 | $user, |
|
61 | 48 | $tokenString, |
|
62 | 48 | $providerKey, |
|
63 | 48 | $userRoles |
|
|
|||
64 | ); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | 88 | public function supportsToken(TokenInterface $token, $providerKey) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 88 | public function createToken(Request $request, $providerKey) |
|
79 | { |
||
80 | 88 | $token = $request->query->get($this->getTokenQueryParameterName()); |
|
81 | 88 | if (null === $token) { |
|
82 | 88 | $token = $request->headers->get($this->getTokenHeaderName()); |
|
83 | } |
||
84 | |||
85 | 88 | if (null === $token || 'null' === $token) { |
|
86 | 44 | if ($this->tokenRequired) { |
|
87 | throw $this->createTokenMissingException(); |
||
88 | } else { |
||
89 | 44 | return null; |
|
90 | } |
||
91 | } |
||
92 | |||
93 | 48 | return new PreAuthenticatedToken( |
|
94 | 48 | 'anon.', |
|
95 | 48 | $token, |
|
96 | 48 | $providerKey |
|
97 | ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | 2 | public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | 88 | public function getTokenQueryParameterName() |
|
115 | |||
116 | /** |
||
117 | * @param $tokenQueryName |
||
118 | */ |
||
119 | public function setTokenQueryParameterName($tokenQueryName) |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | 88 | public function getTokenHeaderName() |
|
131 | |||
132 | /** |
||
133 | * @param $tokenHeaderName |
||
134 | */ |
||
135 | public function setTokenHeaderName($tokenHeaderName) |
||
139 | |||
140 | /** |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function isTokenRequired() |
||
147 | |||
148 | /** |
||
149 | * @param bool $tokenRequired |
||
150 | */ |
||
151 | 91 | public function setTokenRequired($tokenRequired) |
|
155 | |||
156 | /** |
||
157 | * @return AuthenticationException |
||
158 | */ |
||
159 | protected function createTokenMissingException() |
||
163 | |||
164 | /** |
||
165 | * @param $token |
||
166 | * |
||
167 | * @return UserInterface|null |
||
168 | */ |
||
169 | protected abstract function findUserByToken($token); |
||
170 | } |
||
171 |
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: