|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Auth\Adapter; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Zend\Authentication\Adapter\AbstractAdapter; |
|
7
|
|
|
use Zend\Authentication\Result; |
|
8
|
|
|
use Auth\Entity\Filter\CredentialFilter; |
|
9
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This class allows an external application to authenticate via a pre-shared application key and to |
|
13
|
|
|
* push job openings via Rest Calls |
|
14
|
|
|
* |
|
15
|
|
|
* Class ExternalApplication |
|
16
|
|
|
* @package Auth\Adapter |
|
17
|
|
|
*/ |
|
18
|
|
|
class ExternalApplication extends AbstractAdapter |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
protected $applicationKey; |
|
22
|
|
|
|
|
23
|
|
|
/* @var $repository \Auth\Repository\User */ |
|
|
|
|
|
|
24
|
|
|
protected $repository; |
|
25
|
|
|
|
|
26
|
|
|
protected $applicationKeys = array(); |
|
27
|
|
|
|
|
28
|
|
|
/* @var $serviceManager ServiceLocatorInterface */ |
|
|
|
|
|
|
29
|
|
|
protected $serviceManager; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param $repository |
|
33
|
|
|
* @param null $identity |
|
|
|
|
|
|
34
|
|
|
* @param null $credential |
|
|
|
|
|
|
35
|
|
|
* @param null $applicationKey |
|
|
|
|
|
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct($repository, $identity = null, $credential = null, $applicationKey = null) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->repository = $repository; |
|
40
|
2 |
|
$this->setIdentity($identity); |
|
41
|
2 |
|
$this->setCredential($credential); |
|
42
|
2 |
|
$this->setApplicationKey($applicationKey); |
|
43
|
2 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param ContainerInterface $serviceManager |
|
47
|
|
|
*/ |
|
48
|
2 |
|
public function setServiceLocator(ContainerInterface $serviceManager) |
|
49
|
|
|
{ |
|
50
|
2 |
|
$this->serviceManager = $serviceManager; |
|
51
|
2 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return \Auth\Repository\User |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getRepository() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->repository; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param $applicationKey |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public function setApplicationKey($applicationKey) |
|
66
|
|
|
{ |
|
67
|
2 |
|
$this->applicationKey = $applicationKey; |
|
68
|
2 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getApplicationKey() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->applicationKey; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getApplicationIdentifier() |
|
83
|
|
|
{ |
|
84
|
|
|
$keys = $this->getApplicationKeys(); |
|
85
|
|
|
$ids = array_flip($keys); |
|
86
|
|
|
$key = $this->getApplicationKey(); |
|
87
|
|
|
|
|
88
|
|
|
return isset($ids[$key]) ? $ids[$key] : null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param array $applicationKeys |
|
93
|
|
|
* @return $this |
|
94
|
|
|
*/ |
|
95
|
2 |
|
public function setApplicationKeys(array $applicationKeys) |
|
96
|
|
|
{ |
|
97
|
2 |
|
$this->applicationKeys = $applicationKeys; |
|
98
|
2 |
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getApplicationKeys() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->applicationKeys; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return Result |
|
111
|
|
|
*/ |
|
112
|
|
|
public function authenticate() |
|
113
|
|
|
{ |
|
114
|
|
|
if (!in_array($this->getApplicationKey(), $this->getApplicationKeys())) { |
|
115
|
|
|
return new Result(Result::FAILURE, $this->getIdentity(), array('Invalid application key')); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$identity = $this->getIdentity(); |
|
119
|
|
|
$applicationId = '@' . $this->getApplicationIdentifier(); |
|
|
|
|
|
|
120
|
|
|
$applicationIdIndex = strrpos($identity, $applicationId); |
|
121
|
|
|
//$login = (0 < $applicationIdIndex && strlen($identity) - strlen($applicationId) == $applicationIdIndex)?substr($identity, 0, $applicationIdIndex):$identity; |
|
122
|
|
|
$login = $identity; |
|
123
|
|
|
$users = $this->getRepository(); |
|
124
|
|
|
/* @var \Auth\Entity\User $user */ |
|
125
|
|
|
$user = $users->findByLogin($login, ['allowDeactivated' => true]); |
|
126
|
|
|
$filter = new CredentialFilter(); |
|
127
|
|
|
$credential = $this->getCredential(); |
|
128
|
|
|
|
|
129
|
|
|
$loginSuccess = false; |
|
130
|
|
|
$loginResult = array(); |
|
131
|
|
|
|
|
132
|
|
|
if (0 < $applicationIdIndex && strlen($identity) - strlen($applicationId) == $applicationIdIndex) { |
|
133
|
|
|
$this->serviceManager->get('Core/Log')->debug('User ' . $login . ', login with correct suffix: '); |
|
134
|
|
|
// the login ends with the applicationID, therefore use the secret key |
|
135
|
|
|
// the external login must be the form 'xxxxx@yyyy' where yyyy is the matching suffix to the external application key |
|
136
|
|
|
if (isset($user)) { |
|
137
|
|
|
if ($user->getSecret() == $filter->filter($credential)) { |
|
138
|
|
|
$loginSuccess = true; |
|
139
|
|
|
} else { |
|
140
|
|
|
$loginSuccess = false; |
|
141
|
|
|
$this->serviceManager->get('Core/Log')->info('User ' . $login . ', secret: ' . $user->getSecret() . ' != loginPassword: ' . $filter->filter($credential) . ' (' . $credential . ')'); |
|
142
|
|
|
} |
|
143
|
|
|
} else { |
|
144
|
|
|
$user = $users->create( |
|
145
|
|
|
array( |
|
146
|
|
|
'login' => $login, |
|
147
|
|
|
'password' => $credential, |
|
148
|
|
|
'secret' => $filter->filter($credential), |
|
149
|
|
|
'role' => 'recruiter' |
|
150
|
|
|
) |
|
151
|
|
|
); |
|
152
|
|
|
$users->store($user); |
|
153
|
|
|
$loginSuccess = true; |
|
154
|
|
|
$loginResult = array('firstLogin' => true); |
|
155
|
|
|
} |
|
156
|
|
|
} elseif (isset($user)) { |
|
157
|
|
|
$this->serviceManager->get('Core/Log')->debug('User ' . $login . ', login with incorrect suffix: '); |
|
158
|
|
|
if ($user->getCredential() == $filter->filter($credential)) { |
|
159
|
|
|
$this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials are equal'); |
|
160
|
|
|
$loginSuccess = true; |
|
161
|
|
|
} elseif (!empty($applicationId)) { |
|
162
|
|
|
$this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials are not equal'); |
|
163
|
|
|
// TODO: remove this code as soon as the secret key has been fully established |
|
164
|
|
|
// basically this does allow an external login with an applicationIndex match against the User-Password |
|
165
|
|
|
// the way it had been used in the start |
|
166
|
|
|
if ($user->getCredential() == $filter->filter($credential)) { |
|
167
|
|
|
$this->serviceManager->get('Core/Log')->debug('User ' . $login . ', credentials2 test'); |
|
168
|
|
|
$loginSuccess = true; |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if (!$loginSuccess) { |
|
174
|
|
|
return new Result(Result::FAILURE_CREDENTIAL_INVALID, $identity, array('User not known or invalid credential')); |
|
175
|
|
|
} |
|
176
|
|
|
return new Result(Result::SUCCESS, $user->getId(), $loginResult); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|