staffmanager/app/Traits/AuthenticatesTwoFactor.php

41 lines
979 B
PHP
Raw Permalink Normal View History

2020-07-17 21:44:10 +00:00
<?php
namespace App\Traits;
2020-10-21 00:01:41 +00:00
use Google2FA;
use App\Http\Requests\Add2FASecretRequest;
2020-07-17 21:44:10 +00:00
use Illuminate\Support\Facades\Log;
2020-07-17 21:44:10 +00:00
trait AuthenticatesTwoFactor
{
2020-07-17 21:44:10 +00:00
public function verify2FA(Add2FASecretRequest $request)
{
$isValid = Google2FA::verifyKey($request->user()->twofa_secret, $request->otp);
if ($isValid)
{
Google2FA::login();
2020-07-17 21:44:10 +00:00
Log::info('SECURITY (postauth): One-time password verification succeeded', [
'initiator' => $request->user()->email,
'ip' => $request->ip()
]);
2020-07-17 21:44:10 +00:00
return redirect()->to($this->redirectTo);
}
else
{
Log::warning('SECURITY (preauth): One-time password verification failed', [
'initiator' => $request->user()->email,
'ip' => $request->ip()
]);
$request->session()->flash('error', 'Your one time password is invalid.');
return redirect()->back();
2020-07-17 21:44:10 +00:00
}
}
2020-07-17 21:44:10 +00:00
}