|
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
|
|
|
namespace SURFnet\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
use Psr\Log\LoggerInterface; |
|
21
|
|
|
use SURFnet\VPN\Common\HttpClient\ServerClient; |
|
22
|
|
|
use SURFnet\VPN\Server\Exception\InputValidationException; |
|
23
|
|
|
|
|
24
|
|
|
class Otp |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
27
|
|
|
private $logger; |
|
28
|
|
|
|
|
29
|
|
|
/** @var \SURFnet\VPN\Common\HttpClient\HttpClientInterface */ |
|
30
|
|
|
private $serverClient; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(LoggerInterface $logger, ServerClient $serverClient) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->logger = $logger; |
|
35
|
|
|
$this->serverClient = $serverClient; |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function verify(array $envData) |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
InputValidation::userName($envData['username']); |
|
42
|
|
|
$commonName = InputValidation::commonName($envData['common_name']); |
|
43
|
|
|
$otpKey = InputValidation::otpKey($envData['password']); |
|
44
|
|
|
$userId = self::getUserId($commonName); |
|
45
|
|
|
|
|
46
|
|
|
// user has OTP secret registered? |
|
47
|
|
|
if (false === $this->serverClient->hasOtpSecret($userId)) { |
|
|
|
|
|
|
48
|
|
|
$this->logger->error('no OTP secret registered', $envData); |
|
49
|
|
|
|
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// verify the OTP key |
|
54
|
|
|
if (false === $this->serverClient->verifyOtpKey($userId, $otpKey)) { |
|
|
|
|
|
|
55
|
|
|
$this->logger->error('invalid OTP key', $envData); |
|
56
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$this->logger->info('OTP verified', $envData); |
|
61
|
|
|
|
|
62
|
|
|
return true; |
|
63
|
|
|
} catch (InputValidationException $e) { |
|
64
|
|
|
$this->logger->error($e->getMessage(), $envData); |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private static function getUserId($commonName) |
|
71
|
|
|
{ |
|
72
|
|
|
// XXX share this with "Connection" class and possibly others |
|
73
|
|
|
|
|
74
|
|
|
// return the part before the first underscore, it is already validated |
|
75
|
|
|
// so we can be sure this is fine |
|
76
|
|
|
return substr($commonName, 0, strpos($commonName, '_')); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..