1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ma27\ApiKeyAuthenticationBundle\Model\Login\ApiToken; |
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\Model\Key\KeyFactoryInterface; |
12
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\Login\AuthenticationHandlerInterface; |
13
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\Password\PasswordHasherInterface; |
14
|
|
|
use Ma27\ApiKeyAuthenticationBundle\Model\User\ClassMetadata; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Concrete handler for api key authorization. |
19
|
|
|
*/ |
20
|
|
|
class ApiKeyAuthenticationHandler implements AuthenticationHandlerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ObjectManager |
24
|
|
|
*/ |
25
|
|
|
private $om; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var PasswordHasherInterface |
29
|
|
|
*/ |
30
|
|
|
private $passwordHasher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var KeyFactoryInterface |
34
|
|
|
*/ |
35
|
|
|
private $keyFactory; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var EventDispatcherInterface |
39
|
|
|
*/ |
40
|
|
|
private $eventDispatcher; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $modelName; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ClassMetadata |
49
|
|
|
*/ |
50
|
|
|
private $classMetadata; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Constructor. |
54
|
|
|
* |
55
|
|
|
* @param ObjectManager $om |
56
|
|
|
* @param PasswordHasherInterface $passwordHasher |
57
|
|
|
* @param KeyFactoryInterface $keyFactory |
58
|
|
|
* @param EventDispatcherInterface $dispatcher |
59
|
|
|
* @param string $modelName |
60
|
|
|
* @param ClassMetadata $metadata |
61
|
|
|
*/ |
62
|
10 |
|
public function __construct( |
63
|
|
|
ObjectManager $om, |
64
|
|
|
PasswordHasherInterface $passwordHasher, |
65
|
|
|
KeyFactoryInterface $keyFactory, |
66
|
|
|
EventDispatcherInterface $dispatcher, |
67
|
|
|
$modelName, |
68
|
|
|
ClassMetadata $metadata |
69
|
|
|
) { |
70
|
10 |
|
$this->om = $om; |
71
|
10 |
|
$this->passwordHasher = $passwordHasher; |
72
|
10 |
|
$this->keyFactory = $keyFactory; |
73
|
10 |
|
$this->eventDispatcher = $dispatcher; |
74
|
10 |
|
$this->modelName = (string) $modelName; |
75
|
10 |
|
$this->classMetadata = $metadata; |
76
|
10 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
7 |
|
public function authenticate(array $credentials) |
82
|
|
|
{ |
83
|
7 |
|
$loginProperty = $this->classMetadata->getPropertyName(ClassMetadata::LOGIN_PROPERTY); |
84
|
7 |
|
$passwordProperty = $this->classMetadata->getPropertyName(ClassMetadata::PASSWORD_PROPERTY); |
85
|
|
|
|
86
|
7 |
|
if (!isset($credentials[$passwordProperty])) { |
87
|
1 |
|
throw new \InvalidArgumentException( |
88
|
1 |
|
sprintf('Unable to find password property "%s" in credential set!', $passwordProperty) |
89
|
1 |
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
if (!isset($credentials[$loginProperty])) { |
93
|
1 |
|
throw new \InvalidArgumentException( |
94
|
1 |
|
sprintf('Unable to find login property "%s" in credential set!', $loginProperty) |
95
|
1 |
|
); |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
$objectRepository = $this->om->getRepository($this->modelName); |
99
|
5 |
|
$object = $objectRepository->findOneBy(array($loginProperty => $credentials[$loginProperty])); |
100
|
|
|
|
101
|
5 |
|
if (null === $object || !$this->passwordHasher->compareWith($object->getPassword(), $credentials[$passwordProperty])) { |
102
|
2 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::CREDENTIAL_FAILURE, new OnInvalidCredentialsEvent($object)); |
103
|
|
|
|
104
|
2 |
|
throw new CredentialException(); |
105
|
|
|
} |
106
|
|
|
|
107
|
3 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::AUTHENTICATION, new OnAuthenticationEvent($object)); |
108
|
|
|
|
109
|
3 |
|
$this->classMetadata->modifyProperty($object, $this->keyFactory->getKey(), ClassMetadata::API_KEY_PROPERTY); |
110
|
3 |
|
$this->om->persist($object); |
111
|
|
|
|
112
|
3 |
|
$this->om->flush(); |
113
|
|
|
|
114
|
3 |
|
return $object; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
3 |
|
public function removeSession($user, $purgeJob = false) |
121
|
|
|
{ |
122
|
3 |
|
$this->classMetadata->modifyProperty($user, null, ClassMetadata::API_KEY_PROPERTY); |
123
|
|
|
|
124
|
3 |
|
$event = new OnLogoutEvent($user); |
125
|
3 |
|
if ($purgeJob) { |
126
|
1 |
|
$event->markAsPurgeJob(); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
3 |
|
$this->eventDispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::LOGOUT, $event); |
130
|
|
|
|
131
|
3 |
|
$this->om->persist($user); |
132
|
|
|
|
133
|
|
|
// on purge jobs one big flush will be commited to the db after the whole action |
134
|
3 |
|
if (!$purgeJob) { |
135
|
2 |
|
$this->om->flush(); |
136
|
2 |
|
} |
137
|
3 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Getter for the object manager. |
141
|
|
|
* |
142
|
|
|
* @return ObjectManager |
143
|
|
|
*/ |
144
|
|
|
protected function getOm() |
145
|
|
|
{ |
146
|
|
|
return $this->om; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Getter for the password hasher. |
151
|
|
|
* |
152
|
|
|
* @return PasswordHasherInterface |
153
|
|
|
*/ |
154
|
|
|
protected function getPasswordHasher() |
155
|
|
|
{ |
156
|
|
|
return $this->passwordHasher; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Getter for the key factory. |
161
|
|
|
* |
162
|
|
|
* @return KeyFactoryInterface |
163
|
|
|
*/ |
164
|
|
|
protected function getKeyFactory() |
165
|
|
|
{ |
166
|
|
|
return $this->keyFactory; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Getter for the dispatcher. |
171
|
|
|
* |
172
|
|
|
* @return EventDispatcherInterface |
173
|
|
|
*/ |
174
|
|
|
protected function getEventDispatcher() |
175
|
|
|
{ |
176
|
|
|
return $this->eventDispatcher; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Getter for the model name. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function getModelName() |
185
|
|
|
{ |
186
|
|
|
return $this->modelName; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return ClassMetadata |
191
|
|
|
*/ |
192
|
|
|
public function getClassMetadata() |
193
|
|
|
{ |
194
|
|
|
return $this->classMetadata; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|