1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Service\Auth; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
6
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnAuthenticationEvent; |
7
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnInvalidCredentialsEvent; |
8
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Event\OnLogoutEvent; |
9
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Exception\CredentialException; |
10
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Ma27ApiKeyAuthenticationEvents; |
11
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Key\KeyFactoryInterface; |
12
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Password\PasswordHasherInterface; |
13
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Service\Mapping\ClassMetadata; |
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Concrete handler for api key authorization. |
18
|
|
|
*/ |
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
|
21 |
|
public function __construct( |
62
|
|
|
ObjectManager $om, |
63
|
|
|
PasswordHasherInterface $passwordHasher, |
64
|
|
|
KeyFactoryInterface $keyFactory, |
65
|
|
|
EventDispatcherInterface $dispatcher, |
66
|
|
|
$modelName, |
67
|
|
|
ClassMetadata $metadata |
68
|
|
|
) { |
69
|
21 |
|
$this->om = $om; |
70
|
21 |
|
$this->passwordHasher = $passwordHasher; |
71
|
21 |
|
$this->keyFactory = $keyFactory; |
72
|
21 |
|
$this->eventDispatcher = $dispatcher; |
73
|
21 |
|
$this->modelName = (string) $modelName; |
74
|
21 |
|
$this->classMetadata = $metadata; |
75
|
21 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritdoc} |
79
|
|
|
*/ |
80
|
17 |
|
public function authenticate(array $credentials) |
81
|
|
|
{ |
82
|
17 |
|
$loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY); |
83
|
17 |
|
$passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY); |
84
|
|
|
|
85
|
17 |
|
if (!array_key_exists($passwordProperty, $credentials)) { |
86
|
1 |
|
throw new \InvalidArgumentException( |
87
|
1 |
|
sprintf('Unable to find password property "%s" in credential set!', $passwordProperty) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
16 |
|
if (!array_key_exists($loginProperty, $credentials)) { |
92
|
1 |
|
throw new \InvalidArgumentException( |
93
|
1 |
|
sprintf('Unable to find login property "%s" in credential set!', $loginProperty) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
15 |
|
$objectRepository = $this->om->getRepository($this->modelName); |
98
|
15 |
|
$object = $objectRepository->findOneBy(array($loginProperty => $credentials[$loginProperty])); |
99
|
|
|
|
100
|
15 |
|
if (null === $object || !$this->passwordHasher->compareWith($object->getPassword(), $credentials[$passwordProperty])) { |
101
|
5 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, new OnInvalidCredentialsEvent($object)); |
102
|
|
|
|
103
|
5 |
|
throw new CredentialException(); |
104
|
|
|
} |
105
|
|
|
|
106
|
10 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object)); |
107
|
|
|
|
108
|
10 |
|
$key = $this->classMetadata->getPropertyValue($object, ClassMetadata::API_KEY_PROPERTY); |
109
|
10 |
|
if (empty($key)) { |
110
|
7 |
|
$this->classMetadata->modifyProperty($object, $this->keyFactory->getKey(), ClassMetadata::API_KEY_PROPERTY); |
111
|
|
|
} |
112
|
|
|
|
113
|
10 |
|
$this->om->persist($object); |
114
|
|
|
|
115
|
10 |
|
$this->om->flush(); |
116
|
|
|
|
117
|
10 |
|
return $object; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
4 |
|
public function removeSession($user, $purgeJob = false) |
124
|
|
|
{ |
125
|
4 |
|
$this->classMetadata->modifyProperty($user, null, ClassMetadata::API_KEY_PROPERTY); |
126
|
|
|
|
127
|
4 |
|
$event = new OnLogoutEvent($user); |
128
|
4 |
|
if ($purgeJob) { |
129
|
1 |
|
$event->markAsPurgeJob(); |
130
|
|
|
} |
131
|
|
|
|
132
|
4 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::LOGOUT, $event); |
133
|
|
|
|
134
|
4 |
|
$this->om->persist($user); |
135
|
|
|
|
136
|
|
|
// on purge jobs one big flush will be commited to the db after the whole action |
137
|
4 |
|
if (!$purgeJob) { |
138
|
3 |
|
$this->om->flush(); |
139
|
|
|
} |
140
|
4 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Getter for the object manager. |
144
|
|
|
* |
145
|
|
|
* @return ObjectManager |
146
|
|
|
*/ |
147
|
|
|
protected function getOm() |
148
|
|
|
{ |
149
|
|
|
return $this->om; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Getter for the password hasher. |
154
|
|
|
* |
155
|
|
|
* @return PasswordHasherInterface |
156
|
|
|
*/ |
157
|
|
|
protected function getPasswordHasher() |
158
|
|
|
{ |
159
|
|
|
return $this->passwordHasher; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Getter for the key factory. |
164
|
|
|
* |
165
|
|
|
* @return KeyFactoryInterface |
166
|
|
|
*/ |
167
|
|
|
protected function getKeyFactory() |
168
|
|
|
{ |
169
|
|
|
return $this->keyFactory; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Getter for the dispatcher. |
174
|
|
|
* |
175
|
|
|
* @return EventDispatcherInterface |
176
|
|
|
*/ |
177
|
|
|
protected function getEventDispatcher() |
178
|
|
|
{ |
179
|
|
|
return $this->eventDispatcher; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Getter for the model name. |
184
|
|
|
* |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function getModelName() |
188
|
|
|
{ |
189
|
|
|
return $this->modelName; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return ClassMetadata |
194
|
|
|
*/ |
195
|
|
|
public function getClassMetadata() |
196
|
|
|
{ |
197
|
|
|
return $this->classMetadata; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|