1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yrc\actions; |
4
|
|
|
|
5
|
|
|
use common\models\User; |
|
|
|
|
6
|
|
|
use yrc\rest\Action as RestAction; |
7
|
|
|
|
8
|
|
|
use yii\web\HttpException; |
9
|
|
|
use Yii; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Handles enabling and disabling of OTP |
13
|
|
|
* @class OTPAction |
14
|
|
|
*/ |
15
|
|
|
class OTPAction extends RestAction |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* [POST] /api/[...]/otp |
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 |
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
|
|
|
} |
78
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths