@@ 22-48 (lines=27) @@ | ||
19 | * Enables OTP for an account |
|
20 | * @return mixed |
|
21 | */ |
|
22 | public function post($params) |
|
23 | { |
|
24 | // Find the user |
|
25 | $user = User::findOne(Yii::$app->user->id); |
|
26 | if ($user === null) { |
|
27 | return false; |
|
28 | } |
|
29 | ||
30 | if ($user->isOTPEnabled() === true) { |
|
31 | throw new HttpException(400, Yii::t('yrc', 'OTP is already enabled')); |
|
32 | } |
|
33 | ||
34 | // If an OTP code was provided, assume the account has been provisioned and just needs activation |
|
35 | $otpVerificationCode = Yii::$app->request->post('code', false); |
|
36 | if ($otpVerificationCode !== false) { |
|
37 | if ($user->verifyOTP((string)$otpVerificationCode) !== false) { |
|
38 | return $user->enableOTP(); |
|
39 | } |
|
40 | } else { |
|
41 | // Otherwise return the provisioning string |
|
42 | return [ |
|
43 | 'provisioning_code' => $user->provisionOTP() |
|
44 | ]; |
|
45 | } |
|
46 | ||
47 | return false; |
|
48 | } |
|
49 | ||
50 | /** |
|
51 | * [DELETE] /api/[...]/otp |
|
@@ 55-76 (lines=22) @@ | ||
52 | * Disables OTP for an account |
|
53 | * @return mixed |
|
54 | */ |
|
55 | public function delete($params) |
|
56 | { |
|
57 | // Find the user |
|
58 | $user = User::findOne(Yii::$app->user->id); |
|
59 | if ($user === null) { |
|
60 | return false; |
|
61 | } |
|
62 | ||
63 | if ($user->isOTPEnabled() === false) { |
|
64 | throw new HttpException(400, Yii::t('yrc', 'Two-factor is not enabled')); |
|
65 | } |
|
66 | ||
67 | // Grab the code from the GET parameter, and check it |
|
68 | $otpVerificationCode = Yii::$app->request->post('code', false); |
|
69 | if ($otpVerificationCode !== false) { |
|
70 | if ($user->verifyOTP((string)$otpVerificationCode) !== false) { |
|
71 | return $user->disableOTP(); |
|
72 | } |
|
73 | } |
|
74 | ||
75 | return false; |
|
76 | } |
|
77 | } |