|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server; |
|
20
|
|
|
|
|
21
|
|
|
use fkooman\YubiTwee\CurlMultiHttpClient; |
|
22
|
|
|
use fkooman\YubiTwee\Exception\YubiTweeException; |
|
23
|
|
|
use fkooman\YubiTwee\Validator; |
|
24
|
|
|
use SURFnet\VPN\Server\Exception\YubiKeyException; |
|
25
|
|
|
|
|
26
|
|
|
class YubiKey |
|
27
|
|
|
{ |
|
28
|
|
|
public function verify($userId, $yubiKeyOtp, $yubiKeyId = null) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
try { |
|
31
|
|
|
$validator = new Validator(new CurlMultiHttpClient()); |
|
32
|
|
|
$response = $validator->verify($yubiKeyOtp); |
|
33
|
|
|
|
|
34
|
|
|
if ($response->success()) { |
|
35
|
|
|
if (!is_null($yubiKeyId)) { |
|
36
|
|
|
// the yubiKeyId MUST match the Id from the validation |
|
37
|
|
|
// response |
|
38
|
|
|
if ($yubiKeyId !== $response->id()) { |
|
39
|
|
|
throw new YubiKeyException('user not bound to this YubiKey ID'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $response->id(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
throw new YubiKeyException(sprintf('YubiKey OTP is not valid: %s', $response->status())); |
|
47
|
|
|
} catch (YubiTweeException $e) { |
|
48
|
|
|
throw new YubiKeyException(sprintf('YubiKey OTP validation failed: %s', $e->getMessage())); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.