/** * @param Google2FA $google2fa * * @return View */ public function code(Google2FA $google2fa) { $domain = $this->getDomain(); /** @noinspection PhpMethodParametersCountMismatchInspection */ $secret = $google2fa->generateSecretKey(16, auth()->user()->id); Session::flash('two-factor-secret', $secret); $image = $google2fa->getQRCodeInline('Firefly III at ' . $domain, null, $secret, 150); return view('preferences.code', compact('image')); }
/** * Activates two factor authentication. * * @param Request $request * * @return \Illuminate\Http\RedirectResponse */ public function twoFactor(Request $request) { $secret = null; if ($request->has('two_factor')) { $secret = $request->get('google_code'); if (!$this->google2fa->verifyKey($secret, $request->get('2fa_code'))) { $secret = null; return redirect()->back()->withInput($request->only('google_code', 'two_factor'))->withError(Lang::get('auth.invalid_code')); } } $user = Auth::user(); $user->google2fa_secret = $secret; $user->save(); return redirect()->to('/'); }
/** * Validates the 2FA code. * * @param Request $request * * @return \Illuminate\Http\RedirectResponse */ public function postTwoFactorAuthentication(Request $request) { $user_id = Session::pull('2fa_user_id'); $remember = Session::pull('2fa_login_remember'); if ($user_id) { $auth = Auth::guard($this->getGuard()); $auth->loginUsingId($user_id, $remember); if ($this->google2fa->verifyKey($auth->user()->google2fa_secret, $request->get('2fa_code'))) { return $this->handleUserWasAuthenticated($request, true); } $auth->logout(); return redirect()->route('auth.login')->withError(Lang::get('auth.invalid_code')); } return redirect()->route('auth.login')->withError(Lang::get('auth.invalid_code')); }