TwoStepController   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 192
c 0
b 0
f 0
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A verify() 0 39 5
A resend() 0 15 2
A setUser2StepData() 0 10 1
A invalidCodeReturnData() 0 16 2
B showVerification() 0 48 7
A __construct() 0 8 1
1
<?php
2
3
namespace jeremykenedy\laravel2step\App\Http\Controllers;
4
5
use App\Http\Controllers\Controller;
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Controller 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 Auth;
7
use Carbon\Carbon;
8
use Illuminate\Http\Request;
9
use jeremykenedy\laravel2step\App\Traits\Laravel2StepTrait;
10
use Validator;
11
12
class TwoStepController extends Controller
13
{
14
    use Laravel2StepTrait;
15
16
    private $_authCount;
17
    private $_authStatus;
18
    private $_twoStepAuth;
19
    private $_remainingAttempts;
20
    private $_user;
21
22
    /**
23
     * Create a new controller instance.
24
     *
25
     * @return void
26
     */
27
    public function __construct()
28
    {
29
        $this->middleware('auth');
30
31
        $this->middleware(function ($request, $next) {
32
            $this->setUser2StepData();
33
34
            return $next($request);
35
        });
36
    }
37
38
    /**
39
     * Set the User2Step Variables.
40
     *
41
     * @return void
42
     */
43
    private function setUser2StepData()
44
    {
45
        $user = Auth::User();
46
        $twoStepAuth = $this->getTwoStepAuthStatus($user->id);
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
47
        $authCount = $twoStepAuth->authCount;
0 ignored issues
show
Bug introduced by
The property authCount does not seem to exist on jeremykenedy\laravel2step\App\Models\TwoStepAuth. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
48
        $this->_user = $user;
49
        $this->_twoStepAuth = $twoStepAuth;
50
        $this->_authCount = $authCount;
51
        $this->_authStatus = $twoStepAuth->authStatus;
0 ignored issues
show
Bug introduced by
The property authStatus does not seem to exist on jeremykenedy\laravel2step\App\Models\TwoStepAuth. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
52
        $this->_remainingAttempts = config('laravel2step.laravel2stepExceededCount') - $authCount;
53
    }
54
55
    /**
56
     * Validation and Invalid code failed actions and return message.
57
     *
58
     * @param array $errors (optional)
59
     *
60
     * @return array
61
     */
62
    private function invalidCodeReturnData($errors = null)
63
    {
64
        $this->_authCount = $this->_twoStepAuth->authCount += 1;
65
        $this->_twoStepAuth->save();
66
67
        $returnData = [
68
            'message'           => trans('laravel2step::laravel-verification.titleFailed'),
69
            'authCount'         => $this->_authCount,
70
            'remainingAttempts' => $this->_remainingAttempts,
71
        ];
72
73
        if ($errors) {
74
            $returnData['errors'] = $errors;
75
        }
76
77
        return $returnData;
78
    }
79
80
    /**
81
     * Show the twostep verification form.
82
     *
83
     * @return \Illuminate\Http\Response
84
     */
85
    public function showVerification()
86
    {
87
        if (! config('laravel2step.laravel2stepEnabled')) {
88
            abort(404);
89
        }
90
91
        $twoStepAuth = $this->_twoStepAuth;
92
        $authStatus = $this->_authStatus;
0 ignored issues
show
Unused Code introduced by
The assignment to $authStatus is dead and can be removed.
Loading history...
93
94
        if ($this->checkExceededTime($twoStepAuth->updated_at)) {
95
            $this->resetExceededTime($twoStepAuth);
96
        }
97
98
        $data = [
99
            'user'              => $this->_user,
100
            'remainingAttempts' => $this->_remainingAttempts + 1,
101
        ];
102
103
        if ($this->_authCount > config('laravel2step.laravel2stepExceededCount')) {
104
            $exceededTimeDetails = $this->exceededTimeParser($twoStepAuth->updated_at);
105
106
            $data['timeUntilUnlock'] = $exceededTimeDetails['tomorrow'];
107
            $data['timeCountdownUnlock'] = $exceededTimeDetails['remaining'];
108
109
            return View('laravel2step::twostep.exceeded')->with($data);
110
        }
111
112
        $now = new Carbon();
113
        $sentTimestamp = $twoStepAuth->requestDate;
114
115
        if (! $twoStepAuth->authCode) {
116
            $twoStepAuth->authCode = $this->generateCode();
117
            $twoStepAuth->save();
118
        }
119
120
        if (! $sentTimestamp) {
121
            $this->sendVerificationCodeNotification($twoStepAuth);
122
        } else {
123
            $timeBuffer = config('laravel2step.laravel2stepTimeResetBufferSeconds');
124
            $timeAllowedToSendCode = $sentTimestamp->addSeconds($timeBuffer);
125
            if ($now->gt($timeAllowedToSendCode)) {
126
                $this->sendVerificationCodeNotification($twoStepAuth);
127
                $twoStepAuth->requestDate = new Carbon();
128
                $twoStepAuth->save();
129
            }
130
        }
131
132
        return View('laravel2step::twostep.verification')->with($data);
133
    }
134
135
    /**
136
     * Verify the user code input.
137
     *
138
     * @param Request $request
139
     *
140
     * @return \Illuminate\Http\Response
141
     */
142
    public function verify(Request $request)
143
    {
144
        if (! config('laravel2step.laravel2stepEnabled')) {
145
            abort(404);
146
        }
147
148
        if ($request->ajax()) {
149
            $validator = Validator::make($request->all(), [
150
                'v_input_1' => 'required|min:1|max:1',
151
                'v_input_2' => 'required|min:1|max:1',
152
                'v_input_3' => 'required|min:1|max:1',
153
                'v_input_4' => 'required|min:1|max:1',
154
            ]);
155
156
            if ($validator->fails()) {
157
                $returnData = $this->invalidCodeReturnData($validator->errors());
0 ignored issues
show
Bug introduced by
$validator->errors() of type Illuminate\Support\MessageBag is incompatible with the type array expected by parameter $errors of jeremykenedy\laravel2ste...invalidCodeReturnData(). ( Ignorable by Annotation )

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

157
                $returnData = $this->invalidCodeReturnData(/** @scrutinizer ignore-type */ $validator->errors());
Loading history...
158
159
                return response()->json($returnData, 418);
160
            }
161
162
            $code = $request->v_input_1.$request->v_input_2.$request->v_input_3.$request->v_input_4;
163
            $validCode = $this->_twoStepAuth->authCode;
164
165
            if ($validCode !== $code) {
166
                $returnData = $this->invalidCodeReturnData();
167
168
                return response()->json($returnData, 418);
169
            }
170
171
            $this->resetActivationCountdown($this->_twoStepAuth);
172
173
            $returnData = [
174
                'nextUri' => session('nextUri', '/'),
175
                'message' => trans('laravel2step::laravel-verification.titlePassed'),
176
            ];
177
178
            return response()->json($returnData, 200);
179
        } else {
180
            abort(404);
181
        }
182
    }
183
184
    /**
185
     * Resend the validation code triggered by user.
186
     *
187
     * @return \Illuminate\Http\Response
188
     */
189
    public function resend()
190
    {
191
        if (! config('laravel2step.laravel2stepEnabled')) {
192
            abort(404);
193
        }
194
195
        $twoStepAuth = $this->_twoStepAuth;
196
        $this->sendVerificationCodeNotification($twoStepAuth);
197
198
        $returnData = [
199
            'title'   => trans('laravel2step::laravel-verification.verificationEmailSuccess'),
200
            'message' => trans('laravel2step::laravel-verification.verificationEmailSentMsg'),
201
        ];
202
203
        return response()->json($returnData, 200);
204
    }
205
}
206