|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace jeremykenedy\laravel2step\App\Http\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
|
|
|
|
|
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); |
|
|
|
|
|
|
47
|
|
|
$authCount = $twoStepAuth->authCount; |
|
|
|
|
|
|
48
|
|
|
$this->_user = $user; |
|
49
|
|
|
$this->_twoStepAuth = $twoStepAuth; |
|
50
|
|
|
$this->_authCount = $authCount; |
|
51
|
|
|
$this->_authStatus = $twoStepAuth->authStatus; |
|
|
|
|
|
|
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; |
|
|
|
|
|
|
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()); |
|
|
|
|
|
|
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
|
|
|
|
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