jeremykenedy /
laravel2step
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace jeremykenedy\laravel2step\App\Traits; |
||||||
| 4 | |||||||
| 5 | use Auth; |
||||||
| 6 | use Carbon\Carbon; |
||||||
| 7 | use Illuminate\Http\Request; |
||||||
| 8 | use jeremykenedy\laravel2step\App\Models\TwoStepAuth; |
||||||
| 9 | use jeremykenedy\laravel2step\App\Notifications\SendVerificationCodeEmail; |
||||||
| 10 | |||||||
| 11 | trait Laravel2StepTrait |
||||||
| 12 | { |
||||||
| 13 | /** |
||||||
| 14 | * Check if the user is authorized. |
||||||
| 15 | * |
||||||
| 16 | * @param Request $request |
||||||
| 17 | * |
||||||
| 18 | * @return bool |
||||||
| 19 | */ |
||||||
| 20 | public function twoStepVerification($request) |
||||||
|
0 ignored issues
–
show
|
|||||||
| 21 | { |
||||||
| 22 | $user = Auth::User(); |
||||||
| 23 | |||||||
| 24 | if ($user) { |
||||||
| 25 | $twoStepAuthStatus = $this->checkTwoStepAuthStatus($user->id); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 26 | |||||||
| 27 | if ($twoStepAuthStatus->authStatus !== true) { |
||||||
| 28 | return false; |
||||||
| 29 | } else { |
||||||
| 30 | if ($this->checkTimeSinceVerified($twoStepAuthStatus)) { |
||||||
| 31 | return false; |
||||||
| 32 | } |
||||||
| 33 | } |
||||||
| 34 | |||||||
| 35 | return true; |
||||||
| 36 | } |
||||||
| 37 | |||||||
| 38 | return true; |
||||||
| 39 | } |
||||||
| 40 | |||||||
| 41 | /** |
||||||
| 42 | * Check time since user was last verified and take apprpriate action. |
||||||
| 43 | * |
||||||
| 44 | * @param collection $twoStepAuth |
||||||
|
0 ignored issues
–
show
The type
jeremykenedy\laravel2step\App\Traits\collection 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. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||||
| 45 | * |
||||||
| 46 | * @return bool |
||||||
| 47 | */ |
||||||
| 48 | private function checkTimeSinceVerified($twoStepAuth) |
||||||
| 49 | { |
||||||
| 50 | $expireMinutes = config('laravel2step.laravel2stepVerifiedLifetimeMinutes'); |
||||||
| 51 | $now = Carbon::now(); |
||||||
| 52 | $expire = Carbon::parse($twoStepAuth->authDate)->addMinutes($expireMinutes); |
||||||
| 53 | $expired = $now->gt($expire); |
||||||
| 54 | |||||||
| 55 | if ($expired) { |
||||||
| 56 | $this->resetAuthStatus($twoStepAuth); |
||||||
| 57 | |||||||
| 58 | return true; |
||||||
| 59 | } |
||||||
| 60 | |||||||
| 61 | return false; |
||||||
| 62 | } |
||||||
| 63 | |||||||
| 64 | /** |
||||||
| 65 | * Reset TwoStepAuth collection item and code. |
||||||
| 66 | * |
||||||
| 67 | * @param collection $twoStepAuth |
||||||
| 68 | * |
||||||
| 69 | * @return collection |
||||||
| 70 | */ |
||||||
| 71 | private function resetAuthStatus($twoStepAuth) |
||||||
| 72 | { |
||||||
| 73 | $twoStepAuth->authCode = $this->generateCode(); |
||||||
| 74 | $twoStepAuth->authCount = 0; |
||||||
| 75 | $twoStepAuth->authStatus = 0; |
||||||
| 76 | $twoStepAuth->authDate = null; |
||||||
| 77 | $twoStepAuth->requestDate = null; |
||||||
| 78 | |||||||
| 79 | $twoStepAuth->save(); |
||||||
| 80 | |||||||
| 81 | return $twoStepAuth; |
||||||
| 82 | } |
||||||
| 83 | |||||||
| 84 | /** |
||||||
| 85 | * Generate Authorization Code. |
||||||
| 86 | * |
||||||
| 87 | * @param int $length |
||||||
| 88 | * @param string $prefix |
||||||
| 89 | * @param string $suffix |
||||||
| 90 | * |
||||||
| 91 | * @return string |
||||||
| 92 | */ |
||||||
| 93 | private function generateCode(int $length = 4, string $prefix = '', string $suffix = '') |
||||||
| 94 | { |
||||||
| 95 | for ($i = 0; $i < $length; $i++) { |
||||||
| 96 | $prefix .= random_int(0, 1) ? chr(random_int(65, 90)) : random_int(0, 9); |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | return $prefix.$suffix; |
||||||
| 100 | } |
||||||
| 101 | |||||||
| 102 | /** |
||||||
| 103 | * Create/retreive 2step verification object. |
||||||
| 104 | * |
||||||
| 105 | * @param int $userId |
||||||
| 106 | * |
||||||
| 107 | * @return collection |
||||||
| 108 | */ |
||||||
| 109 | private function checkTwoStepAuthStatus(int $userId) |
||||||
| 110 | { |
||||||
| 111 | $twoStepAuth = TwoStepAuth::firstOrCreate( |
||||||
| 112 | [ |
||||||
| 113 | 'userId' => $userId, |
||||||
| 114 | ], |
||||||
| 115 | [ |
||||||
| 116 | 'userId' => $userId, |
||||||
| 117 | 'authCode' => $this->generateCode(), |
||||||
| 118 | 'authCount' => 0, |
||||||
| 119 | ] |
||||||
| 120 | ); |
||||||
| 121 | |||||||
| 122 | return $twoStepAuth; |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * Retreive the Verification Status. |
||||||
| 127 | * |
||||||
| 128 | * @param int $userId |
||||||
| 129 | * |
||||||
| 130 | * @return collection || void |
||||||
|
0 ignored issues
–
show
|
|||||||
| 131 | */ |
||||||
| 132 | protected function getTwoStepAuthStatus(int $userId) |
||||||
| 133 | { |
||||||
| 134 | return TwoStepAuth::where('userId', $userId)->firstOrFail(); |
||||||
| 135 | } |
||||||
| 136 | |||||||
| 137 | /** |
||||||
| 138 | * Format verification exceeded timings with Carbon. |
||||||
| 139 | * |
||||||
| 140 | * @param string $time |
||||||
| 141 | * |
||||||
| 142 | * @return collection |
||||||
| 143 | */ |
||||||
| 144 | protected function exceededTimeParser($time) |
||||||
| 145 | { |
||||||
| 146 | $tomorrow = Carbon::parse($time)->addMinutes(config('laravel2step.laravel2stepExceededCountdownMinutes'))->format('l, F jS Y h:i:sa'); |
||||||
| 147 | $remaining = $time->addMinutes(config('laravel2step.laravel2stepExceededCountdownMinutes'))->diffForHumans(null, true); |
||||||
| 148 | |||||||
| 149 | $data = [ |
||||||
| 150 | 'tomorrow' => $tomorrow, |
||||||
| 151 | 'remaining' => $remaining, |
||||||
| 152 | ]; |
||||||
| 153 | |||||||
| 154 | return collect($data); |
||||||
| 155 | } |
||||||
| 156 | |||||||
| 157 | /** |
||||||
| 158 | * Check if time since account lock has expired and return true if account verification can be reset. |
||||||
| 159 | * |
||||||
| 160 | * @param datetime $time |
||||||
|
0 ignored issues
–
show
|
|||||||
| 161 | * |
||||||
| 162 | * @return bool |
||||||
| 163 | */ |
||||||
| 164 | protected function checkExceededTime($time) |
||||||
| 165 | { |
||||||
| 166 | $now = Carbon::now(); |
||||||
| 167 | $expire = Carbon::parse($time)->addMinutes(config('laravel2step.laravel2stepExceededCountdownMinutes')); |
||||||
| 168 | $expired = $now->gt($expire); |
||||||
| 169 | |||||||
| 170 | if ($expired) { |
||||||
| 171 | return true; |
||||||
| 172 | } |
||||||
| 173 | |||||||
| 174 | return false; |
||||||
| 175 | } |
||||||
| 176 | |||||||
| 177 | /** |
||||||
| 178 | * Method to reset code and count. |
||||||
| 179 | * |
||||||
| 180 | * @param collection $twoStepEntry |
||||||
| 181 | * |
||||||
| 182 | * @return collection |
||||||
| 183 | */ |
||||||
| 184 | protected function resetExceededTime($twoStepEntry) |
||||||
| 185 | { |
||||||
| 186 | $twoStepEntry->authCount = 0; |
||||||
| 187 | $twoStepEntry->authCode = $this->generateCode(); |
||||||
| 188 | $twoStepEntry->save(); |
||||||
| 189 | |||||||
| 190 | return $twoStepEntry; |
||||||
| 191 | } |
||||||
| 192 | |||||||
| 193 | /** |
||||||
| 194 | * Successful activation actions. |
||||||
| 195 | * |
||||||
| 196 | * @param collection $twoStepAuth |
||||||
| 197 | * |
||||||
| 198 | * @return void |
||||||
| 199 | */ |
||||||
| 200 | protected function resetActivationCountdown($twoStepAuth) |
||||||
| 201 | { |
||||||
| 202 | $twoStepAuth->authCode = $this->generateCode(); |
||||||
| 203 | $twoStepAuth->authCount = 0; |
||||||
| 204 | $twoStepAuth->authStatus = 1; |
||||||
| 205 | $twoStepAuth->authDate = Carbon::now(); |
||||||
| 206 | $twoStepAuth->requestDate = null; |
||||||
| 207 | |||||||
| 208 | $twoStepAuth->save(); |
||||||
| 209 | } |
||||||
| 210 | |||||||
| 211 | /** |
||||||
| 212 | * Send verification code via notify. |
||||||
| 213 | * |
||||||
| 214 | * @param array $user |
||||||
| 215 | * @param string $deliveryMethod (nullable) |
||||||
| 216 | * @param string $code |
||||||
| 217 | * |
||||||
| 218 | * @return void |
||||||
| 219 | */ |
||||||
| 220 | protected function sendVerificationCodeNotification($twoStepAuth, $deliveryMethod = null) |
||||||
| 221 | { |
||||||
| 222 | $user = Auth::User(); |
||||||
| 223 | if ($deliveryMethod === null) { |
||||||
| 224 | $user->notify(new SendVerificationCodeEmail($user, $twoStepAuth->authCode)); |
||||||
|
0 ignored issues
–
show
The method
notify() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 225 | } |
||||||
| 226 | $twoStepAuth->requestDate = Carbon::now(); |
||||||
| 227 | |||||||
| 228 | $twoStepAuth->save(); |
||||||
| 229 | } |
||||||
| 230 | } |
||||||
| 231 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.