|
@@ 58-82 (lines=25) @@
|
| 55 |
|
|
| 56 |
|
$service->post( |
| 57 |
|
'/set_yubi_key_id', |
| 58 |
|
function (Request $request, array $hookData) { |
| 59 |
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
| 60 |
|
|
| 61 |
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
| 62 |
|
$yubiKeyOtp = InputValidation::yubiKeyOtp($request->getPostParameter('yubi_key_otp')); |
| 63 |
|
|
| 64 |
|
// check if there is already a YubiKey ID registered for this user |
| 65 |
|
if ($this->storage->hasYubiKeyId($userId)) { |
| 66 |
|
return new ApiErrorResponse('set_yubi_key_id', 'YubiKey ID already set'); |
| 67 |
|
} |
| 68 |
|
|
| 69 |
|
$yubiKey = new YubiKey(); |
| 70 |
|
try { |
| 71 |
|
$yubiKeyId = $yubiKey->verify($userId, $yubiKeyOtp); |
| 72 |
|
$this->storage->setYubiKeyId($userId, $yubiKeyId); |
| 73 |
|
$this->storage->addUserMessage($userId, 'notification', sprintf('YubiKey ID "%s" registered', $yubiKeyId)); |
| 74 |
|
|
| 75 |
|
return new ApiResponse('set_yubi_key_id'); |
| 76 |
|
} catch (YubiKeyException $e) { |
| 77 |
|
$msg = sprintf('YubiKey OTP verification failed: %s', $e->getMessage()); |
| 78 |
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
| 79 |
|
|
| 80 |
|
return new ApiErrorResponse('set_yubi_key_id', $msg); |
| 81 |
|
} |
| 82 |
|
} |
| 83 |
|
); |
| 84 |
|
|
| 85 |
|
$service->post( |
|
@@ 154-180 (lines=27) @@
|
| 151 |
|
|
| 152 |
|
$service->post( |
| 153 |
|
'/set_totp_secret', |
| 154 |
|
function (Request $request, array $hookData) { |
| 155 |
|
AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
| 156 |
|
|
| 157 |
|
$userId = InputValidation::userId($request->getPostParameter('user_id')); |
| 158 |
|
$totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
| 159 |
|
$totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret')); |
| 160 |
|
|
| 161 |
|
// check if there is already a TOTP secret registered for this user |
| 162 |
|
if ($this->storage->hasTotpSecret($userId)) { |
| 163 |
|
return new ApiErrorResponse('set_totp_secret', 'TOTP secret already set'); |
| 164 |
|
} |
| 165 |
|
|
| 166 |
|
$totp = new Totp($this->storage); |
| 167 |
|
try { |
| 168 |
|
$totp->verify($userId, $totpKey, $totpSecret); |
| 169 |
|
} catch (TotpException $e) { |
| 170 |
|
$msg = sprintf('TOTP verification failed: %s', $e->getMessage()); |
| 171 |
|
$this->storage->addUserMessage($userId, 'notification', $msg); |
| 172 |
|
|
| 173 |
|
return new ApiErrorResponse('set_totp_secret', $msg); |
| 174 |
|
} |
| 175 |
|
|
| 176 |
|
$this->storage->setTotpSecret($userId, $totpSecret); |
| 177 |
|
$this->storage->addUserMessage($userId, 'notification', 'TOTP secret registered'); |
| 178 |
|
|
| 179 |
|
return new ApiResponse('set_totp_secret'); |
| 180 |
|
} |
| 181 |
|
); |
| 182 |
|
|
| 183 |
|
$service->post( |