| @@ 60-76 (lines=17) @@ | ||
| 57 | ||
| 58 | $service->post( |
|
| 59 | '/set_totp_secret', |
|
| 60 | function (Request $request, array $hookData) { |
|
| 61 | AuthUtils::requireUser($hookData, ['vpn-user-portal']); |
|
| 62 | ||
| 63 | $userId = InputValidation::userId($request->getPostParameter('user_id')); |
|
| 64 | $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
|
| 65 | $totpSecret = InputValidation::totpSecret($request->getPostParameter('totp_secret')); |
|
| 66 | ||
| 67 | $twoFactor = new TwoFactor($this->storage, new DateTime('now')); |
|
| 68 | try { |
|
| 69 | $twoFactor->verifyTotp($userId, $totpKey, $totpSecret); |
|
| 70 | } catch (TwoFactorException $e) { |
|
| 71 | return new ApiErrorResponse('set_totp_secret', $e->getMessage()); |
|
| 72 | } |
|
| 73 | $this->storage->setTotpSecret($userId, $totpSecret); |
|
| 74 | ||
| 75 | return new ApiResponse('set_totp_secret'); |
|
| 76 | } |
|
| 77 | ); |
|
| 78 | ||
| 79 | $service->post( |
|
| @@ 121-139 (lines=19) @@ | ||
| 118 | return new ApiResponse('disconnect'); |
|
| 119 | } |
|
| 120 | ||
| 121 | public function verifyOtp(Request $request) |
|
| 122 | { |
|
| 123 | $commonName = InputValidation::commonName($request->getPostParameter('common_name')); |
|
| 124 | // we do not need 'otp_type', as only 'totp' is supported at the moment |
|
| 125 | InputValidation::otpType($request->getPostParameter('otp_type')); |
|
| 126 | $totpKey = InputValidation::totpKey($request->getPostParameter('totp_key')); |
|
| 127 | ||
| 128 | $certInfo = $this->storage->getUserCertificateInfo($commonName); |
|
| 129 | $userId = $certInfo['user_id']; |
|
| 130 | ||
| 131 | $twoFactor = new TwoFactor($this->storage, new DateTime('now')); |
|
| 132 | try { |
|
| 133 | $twoFactor->verifyTotp($userId, $totpKey); |
|
| 134 | } catch (TwoFactorException $e) { |
|
| 135 | return new ApiErrorResponse('verify_otp', $e->getMessage()); |
|
| 136 | } |
|
| 137 | ||
| 138 | return new ApiResponse('verify_otp'); |
|
| 139 | } |
|
| 140 | ||
| 141 | private function verifyConnection($profileId, $commonName) |
|
| 142 | { |
|