|
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\Api; |
|
19
|
|
|
|
|
20
|
|
|
use SURFnet\VPN\Common\FileIO; |
|
21
|
|
|
use RuntimeException; |
|
22
|
|
|
use PDO; |
|
23
|
|
|
use Base32\Base32; |
|
24
|
|
|
use Otp\Otp; |
|
25
|
|
|
use SURFnet\VPN\Server\Api\Exception\OtpException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Manages user configuration. |
|
29
|
|
|
* |
|
30
|
|
|
* XXX deal better with exceptions, not everything is a RuntimeException, |
|
31
|
|
|
*/ |
|
32
|
|
|
class Users |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
private $disableDir; |
|
36
|
|
|
|
|
37
|
|
|
/** @var string */ |
|
38
|
|
|
private $otpDir; |
|
39
|
|
|
|
|
40
|
|
|
/** @var OtpLog */ |
|
41
|
|
|
private $otpLog; |
|
42
|
|
|
|
|
43
|
|
|
/** @var string */ |
|
44
|
|
|
private $vootDir; |
|
45
|
|
|
|
|
46
|
|
|
public function __construct($dataDir) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->disableDir = sprintf('%s/disabled', $dataDir); |
|
49
|
|
|
FileIO::createDir($this->disableDir, 0711); |
|
50
|
|
|
|
|
51
|
|
|
$this->otpDir = sprintf('%s/otp_secrets', $dataDir); |
|
52
|
|
|
FileIO::createDir($this->otpDir, 0711); |
|
53
|
|
|
// XXX maybe we should feed OtpLog to the constructor instead? |
|
54
|
|
|
$this->otpLog = new OtpLog(new PDO(sprintf('sqlite://%s/otp.sqlite', $dataDir))); |
|
55
|
|
|
|
|
56
|
|
|
$this->vootDir = sprintf('%s/voot_tokens', $dataDir); |
|
57
|
|
|
FileIO::createDir($this->vootDir, 0711); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
View Code Duplication |
public function getDisabled() |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
$disabledList = []; |
|
63
|
|
|
if (false === $fileList = glob(sprintf('%s/*', $this->disableDir), GLOB_ERR)) { |
|
64
|
|
|
throw new RuntimeException(sprintf('unable to read directory "%s"', $this->disableDir)); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
foreach ($fileList as $fileName) { |
|
68
|
|
|
$disabledList[] = basename($fileName); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $disabledList; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function isDisabled($userId) |
|
75
|
|
|
{ |
|
76
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
77
|
|
|
|
|
78
|
|
|
return @file_exists($disableFile); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function setDisabled($userId) |
|
82
|
|
|
{ |
|
83
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
84
|
|
|
FileIO::writeFile($disableFile, time(), 0644); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function setEnabled($userId) |
|
88
|
|
|
{ |
|
89
|
|
|
$disableFile = sprintf('%s/%s', $this->disableDir, $userId); |
|
90
|
|
|
FileIO::deleteFile($disableFile); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Set a new OTP secret after validating it. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setOtpSecret($userId, $otpSecret, $otpKey) |
|
97
|
|
|
{ |
|
98
|
|
|
// do not allow override of the OTP secret |
|
99
|
|
|
if (false !== $this->hasOtpSecret($userId)) { |
|
100
|
|
|
throw new OtpException('cannot overwrite OTP secret'); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$otp = new Otp(); |
|
104
|
|
|
if (false === $otp->checkTotp(Base32::decode($otpSecret), $otpKey)) { |
|
105
|
|
|
// wrong otp key |
|
106
|
|
|
return false; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (false === $this->otpLog->record($userId, $otpKey, time())) { |
|
110
|
|
|
// otp replayed, this should not happen as there is no secret yet |
|
111
|
|
|
// for this user... |
|
112
|
|
|
throw new OtpException('OTP replay on registration'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
116
|
|
|
FileIO::writeFile($otpFile, $otpSecret, 0600); |
|
117
|
|
|
|
|
118
|
|
|
return true; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Verify an OTP key for an already registered OTP secret. |
|
123
|
|
|
*/ |
|
124
|
|
|
public function verifyOtpKey($userId, $otpKey) |
|
125
|
|
|
{ |
|
126
|
|
|
// we do not use FileIO::readFile here as a missing file is not fatal |
|
127
|
|
|
if (false === $otpSecret = @file_get_contents(sprintf('%s/%s', $this->otpDir, $userId))) { |
|
128
|
|
|
throw new OtpException('no OTP secret registered'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
$otp = new Otp(); |
|
132
|
|
|
if (false === $otp->checkTotp(Base32::decode($otpSecret), $otpKey)) { |
|
133
|
|
|
// wrong otp key |
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (false === $this->otpLog->record($userId, $otpKey, time())) { |
|
138
|
|
|
// replayed |
|
139
|
|
|
return false; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
return true; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
public function deleteOtpSecret($userId) |
|
146
|
|
|
{ |
|
147
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
148
|
|
|
FileIO::deleteFile($otpFile); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function hasOtpSecret($userId) |
|
152
|
|
|
{ |
|
153
|
|
|
$otpFile = sprintf('%s/%s', $this->otpDir, $userId); |
|
154
|
|
|
|
|
155
|
|
|
return @file_exists($otpFile); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
public function setVootToken($userId, $vootToken) |
|
159
|
|
|
{ |
|
160
|
|
|
$vootFile = sprintf('%s/%s', $this->vootDir, $userId); |
|
161
|
|
|
FileIO::writeFile($vootFile, $vootToken, 0644); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function hasVootToken($userId) |
|
165
|
|
|
{ |
|
166
|
|
|
$vootFile = sprintf('%s/%s', $this->vootDir, $userId); |
|
167
|
|
|
|
|
168
|
|
|
return @file_exists($vootFile); |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.