OTPAction   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A post() 0 26 5
A delete() 0 21 5
1
<?php
2
3
namespace yrc\actions;
4
5
use common\models\User;
0 ignored issues
show
Bug introduced by
The type common\models\User was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

22
    public function post(/** @scrutinizer ignore-unused */ $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

55
    public function delete(/** @scrutinizer ignore-unused */ $params)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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