1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Firesphere\YubiAuth\Authenticators; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Firesphere\BootstrapMFA\Authenticators\BootstrapMFAAuthenticator; |
7
|
|
|
use Firesphere\BootstrapMFA\Handlers\BootstrapMFALoginHandler; |
|
|
|
|
8
|
|
|
use Firesphere\YubiAuth\Handlers\YubikeyLoginHandler; |
9
|
|
|
use Firesphere\YubiAuth\Helpers\QwertyConvertor; |
10
|
|
|
use Firesphere\YubiAuth\Providers\YubikeyAuthProvider; |
11
|
|
|
use SilverStripe\Control\HTTPRequest; |
12
|
|
|
use SilverStripe\Core\Config\Config; |
13
|
|
|
use SilverStripe\Core\Environment; |
14
|
|
|
use SilverStripe\Core\Injector\Injector; |
15
|
|
|
use SilverStripe\ORM\ValidationException; |
16
|
|
|
use SilverStripe\ORM\ValidationResult; |
17
|
|
|
use SilverStripe\Security\Authenticator; |
18
|
|
|
use SilverStripe\Security\Member; |
19
|
|
|
use SilverStripe\Security\MemberAuthenticator\LoginHandler; |
20
|
|
|
use Yubikey\Response; |
21
|
|
|
use Yubikey\Validate; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class YubikeyAuthenticator |
25
|
|
|
* |
26
|
|
|
* Enable Yubikey Authentication for SilverStripe CMS and member-protected pages. |
27
|
|
|
*/ |
28
|
|
|
class YubikeyMemberAuthenticator extends BootstrapMFAAuthenticator |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Validate |
33
|
|
|
*/ |
34
|
|
|
protected $yubiService; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var YubikeyAuthProvider |
38
|
|
|
*/ |
39
|
|
|
protected $provider; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
private $authenticatorName = 'yubiauth'; |
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Set the provider to a YubikeyAuthProvider instance |
48
|
|
|
* |
49
|
|
|
* YubikeyMemberAuthenticator constructor. |
50
|
|
|
*/ |
51
|
|
|
public function __construct() |
52
|
|
|
{ |
53
|
|
|
if (!$this->provider) { |
54
|
|
|
$this->provider = Injector::inst()->get(YubikeyAuthProvider::class); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Name of this authenticator |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public static function get_name() |
64
|
|
|
{ |
65
|
|
|
return _t('YubikeyAuthenticator.TITLE', 'Yubikey 2 factor login'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return YubikeyAuthProvider |
70
|
|
|
*/ |
71
|
|
|
public function getProvider() |
72
|
|
|
{ |
73
|
|
|
return $this->provider; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param YubikeyAuthProvider $provider |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
public function setProvider($provider) |
81
|
|
|
{ |
82
|
|
|
$this->provider = $provider; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
|
|
public function supportedServices() |
91
|
|
|
{ |
92
|
|
|
// Bitwise-OR of all the supported services in this Authenticator, to make a bitmask |
93
|
|
|
return Authenticator::LOGIN | Authenticator::LOGOUT | Authenticator::CHANGE_PASSWORD |
94
|
|
|
| Authenticator::RESET_PASSWORD | Authenticator::CHECK_PASSWORD; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritdoc |
99
|
|
|
* |
100
|
|
|
* @param array $data |
101
|
|
|
* @param HTTPRequest $request |
102
|
|
|
* @param ValidationResult $validationResult |
103
|
|
|
* |
104
|
|
|
* @return ValidationResult|Member |
105
|
|
|
* @throws ValidationException |
106
|
|
|
*/ |
107
|
|
|
public function validateToken($data, $request, &$validationResult = null) |
108
|
|
|
{ |
109
|
|
|
if (!$validationResult instanceof ValidationResult) { |
110
|
|
|
$validationResult = ValidationResult::create(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$memberID = $request->getSession()->get(BootstrapMFALoginHandler::SESSION_KEY . '.MemberID'); |
114
|
|
|
// First, let's see if we know the member |
115
|
|
|
/** @var Member|null $member */ |
116
|
|
|
$member = Member::get()->filter(['ID' => $memberID])->first(); |
117
|
|
|
|
118
|
|
|
// Continue if we have a valid member |
119
|
|
|
if ($member instanceof Member) { |
120
|
|
|
|
121
|
|
|
// We do not have to check the YubiAuth for this situation. |
122
|
|
|
if (!$member->MFAEnabled && empty($data['yubiauth'])) { |
123
|
|
|
return $this->authenticateNoYubikey($member); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// If we know the member, and it's YubiAuth enabled, continue. |
127
|
|
|
return $this->checkYubikey($data, $member); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$validationResult->addError(_t(__CLASS__ . '.MEMBERNOTFOUND', 'Could not identify member')); |
131
|
|
|
|
132
|
|
|
return $validationResult; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Handle login if the user did not enter a Yubikey string. |
137
|
|
|
* Will break out and return NULL if the member should use their Yubikey |
138
|
|
|
* |
139
|
|
|
* @param Member $member |
140
|
|
|
* @return ValidationResult|Member |
141
|
|
|
* @throws ValidationException |
142
|
|
|
*/ |
143
|
|
|
private function authenticateNoYubikey($member) |
144
|
|
|
{ |
145
|
|
|
++$member->NoYubikeyCount; |
146
|
|
|
$member->write(); |
147
|
|
|
$yubiAuthNoYubi = $this->provider->checkNoYubiAttempts($member); |
148
|
|
|
if ($yubiAuthNoYubi instanceof ValidationResult) { |
149
|
|
|
return $yubiAuthNoYubi; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $member; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @param $data |
157
|
|
|
* @param $member |
158
|
|
|
* @return ValidationResult|Member |
159
|
|
|
* @throws ValidationException |
160
|
|
|
*/ |
161
|
|
|
protected function checkYubikey($data, $member) |
162
|
|
|
{ |
163
|
|
|
/** @var Validate $service */ |
164
|
|
|
$this->yubiService = Injector::inst()->createWithArgs( |
165
|
|
|
Validate::class, |
166
|
|
|
[ |
167
|
|
|
Environment::getEnv('YUBIAUTH_APIKEY'), |
168
|
|
|
Environment::getEnv('YUBIAUTH_CLIENTID'), |
169
|
|
|
] |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
return $this->authenticateYubikey($data, $member); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Validate a member plus it's yubikey login. It compares the fingerprintt and after that, |
177
|
|
|
* tries to validate the Yubikey string |
178
|
|
|
* |
179
|
|
|
* @todo improve this, it's a bit overly complicated |
180
|
|
|
* @todo use the ValidationResult as e reference instead of returning |
181
|
|
|
* |
182
|
|
|
* @param array $data |
183
|
|
|
* @param Member $member |
184
|
|
|
* @return ValidationResult|Member |
185
|
|
|
* @throws ValidationException |
186
|
|
|
*/ |
187
|
|
|
private function authenticateYubikey($data, $member) |
188
|
|
|
{ |
189
|
|
|
if ($url = Config::inst()->get(self::class, 'AuthURL')) { |
190
|
|
|
$this->yubiService->setHost($url); |
191
|
|
|
} |
192
|
|
|
$yubiCode = QwertyConvertor::convertString($data['yubiauth']); |
193
|
|
|
$yubiFingerprint = substr($yubiCode, 0, -32); |
194
|
|
|
$validationResult = ValidationResult::create(); |
195
|
|
|
|
196
|
|
|
if ($member->Yubikey) { |
197
|
|
|
$validationResult = $this->provider->validateToken($member, $yubiFingerprint); |
198
|
|
|
if (!$validationResult->isValid()) { |
199
|
|
|
$member->registerFailedLogin(); |
200
|
|
|
|
201
|
|
|
return $validationResult; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
try { |
205
|
|
|
/** @var Response $result */ |
206
|
|
|
$result = $this->yubiService->check($yubiCode); |
207
|
|
|
|
208
|
|
|
// Only check if the call itself doesn't throw an error |
209
|
|
|
if ($result->success() === true) { |
210
|
|
|
$this->updateMember($member, $yubiFingerprint); |
211
|
|
|
|
212
|
|
|
return $member; |
213
|
|
|
} |
214
|
|
|
} catch (Exception $e) { |
215
|
|
|
$validationResult->addError($e->getMessage()); |
216
|
|
|
|
217
|
|
|
$member->registerFailedLogin(); |
218
|
|
|
|
219
|
|
|
return $validationResult; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$validationResult->addError(_t('YubikeyAuthenticator.ERROR', 'Yubikey authentication error')); |
223
|
|
|
$member->registerFailedLogin(); |
224
|
|
|
|
225
|
|
|
return $validationResult; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Update the member to forcefully enable YubiAuth |
230
|
|
|
* Also, register the Yubikey to the member. |
231
|
|
|
* Documentation: |
232
|
|
|
* https://developers.yubico.com/yubikey-val/Getting_Started_Writing_Clients.html |
233
|
|
|
* |
234
|
|
|
* @param Member $member |
235
|
|
|
* @param string $yubiString The Identifier String of the Yubikey |
236
|
|
|
* @throws ValidationException |
237
|
|
|
*/ |
238
|
|
|
private function updateMember($member, $yubiString) |
239
|
|
|
{ |
240
|
|
|
$member->registerSuccessfulLogin(); |
241
|
|
|
$member->NoYubikeyCount = 0; |
242
|
|
|
|
243
|
|
|
if (!$member->MFAEnabled) { |
244
|
|
|
$member->MFAEnabled = true; |
245
|
|
|
} |
246
|
|
|
if (!$member->Yubikey) { |
247
|
|
|
$member->Yubikey = $yubiString; |
248
|
|
|
} |
249
|
|
|
$member->write(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param string $link |
254
|
|
|
* @return LoginHandler|static |
255
|
|
|
*/ |
256
|
|
|
public function getLoginHandler($link) |
257
|
|
|
{ |
258
|
|
|
return YubikeyLoginHandler::create($link, $this); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths