1 | <?php |
||
19 | class ApiKeyAuthenticationHandler implements AuthenticationHandlerInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var ObjectManager |
||
23 | */ |
||
24 | private $om; |
||
25 | |||
26 | /** |
||
27 | * @var PasswordHasherInterface |
||
28 | */ |
||
29 | private $passwordHasher; |
||
30 | |||
31 | /** |
||
32 | * @var KeyFactoryInterface |
||
33 | */ |
||
34 | private $keyFactory; |
||
35 | |||
36 | /** |
||
37 | * @var EventDispatcherInterface |
||
38 | */ |
||
39 | private $eventDispatcher; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $modelName; |
||
45 | |||
46 | /** |
||
47 | * @var ClassMetadata |
||
48 | */ |
||
49 | private $classMetadata; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param ObjectManager $om |
||
55 | * @param PasswordHasherInterface $passwordHasher |
||
56 | * @param KeyFactoryInterface $keyFactory |
||
57 | * @param EventDispatcherInterface $dispatcher |
||
58 | * @param string $modelName |
||
59 | * @param ClassMetadata $metadata |
||
60 | */ |
||
61 | 19 | public function __construct( |
|
76 | |||
77 | /** |
||
78 | * {@inheritdoc} |
||
79 | * |
||
80 | * @throws \InvalidArgumentException If the `login` or `password` property is missing. |
||
81 | * @throws CredentialException If the credentials couldn't be validated. |
||
82 | */ |
||
83 | 17 | public function authenticate(array $credentials) |
|
84 | { |
||
85 | 17 | $loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY); |
|
86 | 17 | $passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY); |
|
87 | |||
88 | 17 | if (!array_key_exists($passwordProperty, $credentials)) { |
|
89 | 1 | throw new \InvalidArgumentException(sprintf( |
|
90 | 1 | 'Unable to find password property "%s" in credential set!', |
|
91 | 1 | $passwordProperty |
|
92 | )); |
||
93 | } |
||
94 | |||
95 | 16 | if (!array_key_exists($loginProperty, $credentials)) { |
|
96 | 1 | throw new \InvalidArgumentException(sprintf( |
|
97 | 1 | 'Unable to find login property "%s" in credential set!', |
|
98 | 1 | $loginProperty |
|
99 | )); |
||
100 | } |
||
101 | |||
102 | 15 | $object = $this->resolveObject($loginProperty, $credentials); |
|
|
|||
103 | |||
104 | 15 | if (!$this->validateCredentials($object, $credentials[$passwordProperty])) { |
|
105 | 5 | $this->eventDispatcher->dispatch( |
|
106 | 5 | Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, |
|
107 | 5 | new OnInvalidCredentialsEvent($object) |
|
108 | ); |
||
109 | |||
110 | 5 | throw new CredentialException(); |
|
111 | } |
||
112 | |||
113 | 10 | $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object)); |
|
114 | 10 | $this->buildKey($object); |
|
115 | |||
116 | 10 | $this->om->persist($object); |
|
117 | 10 | $this->om->flush(); |
|
118 | |||
119 | 10 | return $object; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | 4 | public function removeSession($user, $purgeJob = false) |
|
139 | |||
140 | /** |
||
141 | * Getter for the object manager. |
||
142 | * |
||
143 | * @return ObjectManager |
||
144 | */ |
||
145 | protected function getOm() |
||
149 | |||
150 | /** |
||
151 | * Getter for the password hasher. |
||
152 | * |
||
153 | * @return PasswordHasherInterface |
||
154 | */ |
||
155 | protected function getPasswordHasher() |
||
159 | |||
160 | /** |
||
161 | * Getter for the key factory. |
||
162 | * |
||
163 | * @return KeyFactoryInterface |
||
164 | */ |
||
165 | protected function getKeyFactory() |
||
169 | |||
170 | /** |
||
171 | * Getter for the dispatcher. |
||
172 | * |
||
173 | * @return EventDispatcherInterface |
||
174 | */ |
||
175 | protected function getEventDispatcher() |
||
179 | |||
180 | /** |
||
181 | * Getter for the model name. |
||
182 | * |
||
183 | * @return string |
||
184 | */ |
||
185 | protected function getModelName() |
||
189 | |||
190 | /** |
||
191 | * @return ClassMetadata |
||
192 | */ |
||
193 | protected function getClassMetadata() |
||
197 | |||
198 | /** |
||
199 | * Simple helper which builds the API key and stores it in the user. |
||
200 | * |
||
201 | * @param object $userObject |
||
202 | */ |
||
203 | 10 | private function buildKey($userObject) |
|
204 | { |
||
205 | 10 | $key = $this->classMetadata->getPropertyValue($userObject, ClassMetadata::API_KEY_PROPERTY); |
|
206 | |||
207 | 10 | if (empty($key)) { |
|
208 | 7 | $this->classMetadata->modifyProperty( |
|
209 | 7 | $userObject, |
|
210 | 7 | $this->keyFactory->getKey(), |
|
211 | 7 | ClassMetadata::API_KEY_PROPERTY |
|
212 | ); |
||
213 | } |
||
214 | 10 | } |
|
215 | |||
216 | /** |
||
217 | * Simple helper which searches the ObjectManager by the given login parameter. |
||
218 | * |
||
219 | * @param string $loginProperty |
||
220 | * @param array $credentials |
||
221 | * |
||
222 | * @return object |
||
223 | */ |
||
224 | 15 | private function resolveObject($loginProperty, array $credentials) |
|
230 | |||
231 | /** |
||
232 | * Validates the existance of the object and ensures that a valid password is given. |
||
233 | * |
||
234 | * @param object $object |
||
235 | * @param string $password |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | 15 | private function validateCredentials($object, $password) |
|
246 | |||
247 | /** |
||
248 | * Builds the `OnLogoutEvent`. |
||
249 | * |
||
250 | * @param object $user |
||
251 | * @param bool $purgeJob |
||
252 | * |
||
253 | * @return OnLogoutEvent |
||
254 | */ |
||
255 | 4 | private function buildEventObject($user, $purgeJob = false) |
|
264 | } |
||
265 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.