1 | <?php |
||
21 | class ApiKeyAuthenticationHandler implements AuthenticationHandlerInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var ObjectManager |
||
25 | */ |
||
26 | private $om; |
||
27 | |||
28 | /** |
||
29 | * @var PasswordHasherInterface |
||
30 | */ |
||
31 | private $passwordHasher; |
||
32 | |||
33 | /** |
||
34 | * @var KeyFactoryInterface |
||
35 | */ |
||
36 | private $keyFactory; |
||
37 | |||
38 | /** |
||
39 | * @var EventDispatcherInterface |
||
40 | */ |
||
41 | private $eventDispatcher; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $modelName; |
||
47 | |||
48 | /** |
||
49 | * @var ClassMetadata |
||
50 | */ |
||
51 | private $classMetadata; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * @param ObjectManager $om |
||
57 | * @param PasswordHasherInterface $passwordHasher |
||
58 | * @param KeyFactoryInterface $keyFactory |
||
59 | * @param EventDispatcherInterface $dispatcher |
||
60 | * @param string $modelName |
||
61 | * @param ClassMetadata $metadata |
||
62 | */ |
||
63 | public function __construct( |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function authenticate(array $credentials) |
||
83 | { |
||
84 | 11 | $loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY); |
|
85 | 11 | $passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY); |
|
86 | 11 | ||
87 | 11 | if (!isset($credentials[$passwordProperty])) { |
|
88 | 11 | throw new \InvalidArgumentException( |
|
89 | 11 | sprintf('Unable to find password property "%s" in credential set!', $passwordProperty) |
|
90 | 11 | ); |
|
91 | 11 | } |
|
92 | 11 | ||
93 | if (!isset($credentials[$loginProperty])) { |
||
94 | throw new \InvalidArgumentException( |
||
95 | sprintf('Unable to find login property "%s" in credential set!', $loginProperty) |
||
96 | ); |
||
97 | 9 | } |
|
98 | |||
99 | 9 | $objectRepository = $this->om->getRepository($this->modelName); |
|
100 | 1 | $object = $objectRepository->findOneBy(array($loginProperty => $credentials[$loginProperty])); |
|
101 | |||
102 | if (null === $object || !$this->passwordHasher->compareWith($object->getPassword(), $credentials[$passwordProperty])) { |
||
103 | 8 | $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, new OnInvalidCredentialsEvent($object)); |
|
104 | 8 | ||
105 | 5 | throw new CredentialException(); |
|
106 | 1 | } |
|
107 | 1 | ||
108 | 1 | $this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object)); |
|
109 | |||
110 | $this->classMetadata->modifyProperty($object, $this->keyFactory->getKey(), ClassMetadata::API_KEY_PROPERTY); |
||
111 | 4 | $this->om->persist($object); |
|
112 | 4 | ||
113 | $this->om->flush(); |
||
114 | 7 | ||
115 | 3 | return $object; |
|
116 | 1 | } |
|
117 | 1 | ||
118 | 1 | /** |
|
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | 2 | public function removeSession($user, $purgeJob = false) |
|
139 | |||
140 | 3 | /** |
|
141 | * Getter for the object manager. |
||
142 | 3 | * |
|
143 | 3 | * @return ObjectManager |
|
144 | */ |
||
145 | 3 | protected function getOm() |
|
149 | |||
150 | /** |
||
151 | * Getter for the password hasher. |
||
152 | * |
||
153 | 3 | * @return PasswordHasherInterface |
|
154 | */ |
||
155 | 3 | protected function getPasswordHasher() |
|
159 | 1 | ||
160 | 1 | /** |
|
161 | * Getter for the key factory. |
||
162 | 3 | * |
|
163 | * @return KeyFactoryInterface |
||
164 | 3 | */ |
|
165 | protected function getKeyFactory() |
||
169 | 2 | ||
170 | 3 | /** |
|
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 | public function getClassMetadata() |
||
197 | } |
||
198 |