|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use Illuminate\Http\Request; |
| 6 | +use App\Http\Controllers\Controller; |
| 7 | +use HMS\Repositories\UserRepository; |
| 8 | +use Illuminate\Support\Facades\Auth; |
| 9 | +use PragmaRX\Google2FALaravel\Google2FA; |
| 10 | + |
| 11 | +class TwoFactorAuthenticationController extends Controller |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var Google2FA |
| 15 | + */ |
| 16 | + protected $google2fa; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var UserRepository |
| 20 | + */ |
| 21 | + protected $userRepository; |
| 22 | + |
| 23 | + /** |
| 24 | + * Create a new controller instance. |
| 25 | + * |
| 26 | + * @param Google2FA $google2fa |
| 27 | + * |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct(Google2FA $google2fa, UserRepository $userRepository) |
| 31 | + { |
| 32 | + $this->google2fa = $google2fa; |
| 33 | + $this->userRepository = $userRepository; |
| 34 | + |
| 35 | + $this->middleware('auth'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Show the 2fa enable/disable form. |
| 40 | + * |
| 41 | + * @param \Illuminate\Http\Request $request |
| 42 | + * |
| 43 | + * @return \Illuminate\Http\Response |
| 44 | + */ |
| 45 | + public function show2faForm(Request $request) |
| 46 | + { |
| 47 | + $user = Auth::user(); |
| 48 | + |
| 49 | + $google2faUrl = ''; |
| 50 | + if (! empty($user->getGoogle2faSecret())) { |
| 51 | + $google2faUrl = $this->google2fa->getQRCodeInline( |
| 52 | + 'Nottingham Hackspace HMS', |
| 53 | + $user->getEmail(), |
| 54 | + $user->getGoogle2faSecret() |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + return view('auth.2fa') |
| 59 | + ->with('user', $user) |
| 60 | + ->with('google2faUrl', $google2faUrl); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Generate new 2fa secret for user. |
| 65 | + * |
| 66 | + * @param \Illuminate\Http\Request $request |
| 67 | + * |
| 68 | + * @return \Illuminate\Http\Response |
| 69 | + */ |
| 70 | + public function generate2faSecret(Request $request) |
| 71 | + { |
| 72 | + $user = Auth::user(); |
| 73 | + |
| 74 | + // Add the secret key to the registration data |
| 75 | + $user->setGoogle2faEnable(false); |
| 76 | + $user->setGoogle2faSecret($this->google2fa->generateSecretKey(32)); |
| 77 | + $this->userRepository->save($user); |
| 78 | + |
| 79 | + return redirect('2fa')->with('success', 'Secret Key is generated, Please verify Code to Enable 2FA'); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Enable 2fa for USer. |
| 84 | + * |
| 85 | + * @param \Illuminate\Http\Request $request |
| 86 | + * |
| 87 | + * @return \Illuminate\Http\Response |
| 88 | + */ |
| 89 | + public function enable2fa(Request $request) |
| 90 | + { |
| 91 | + $user = Auth::user(); |
| 92 | + // $google2fa = app('pragmarx.google2fa'); |
| 93 | + $secret = $request->input('verify-code'); |
| 94 | + |
| 95 | + $valid = $this->google2fa->verifyKey($user->getGoogle2faSecret(), $secret); |
| 96 | + |
| 97 | + if ($valid) { |
| 98 | + $user->setGoogle2faEnable(true); |
| 99 | + $this->userRepository->save($user); |
| 100 | + |
| 101 | + return redirect('2fa')->with('success', '2FA is Enabled Successfully.'); |
| 102 | + } else { |
| 103 | + return redirect('2fa')->with('error', 'Invalid Verification Code, Please try again.'); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Disable 2fa for User. |
| 109 | + * |
| 110 | + * @param \Illuminate\Http\Request $request |
| 111 | + * |
| 112 | + * @return \Illuminate\Http\Response |
| 113 | + */ |
| 114 | + public function disable2fa(Request $request) |
| 115 | + { |
| 116 | + $validatedData = $request->validate([ |
| 117 | + 'current-password' => 'required', |
| 118 | + ]); |
| 119 | + |
| 120 | + $user = Auth::user(); |
| 121 | + $credentials = [ |
| 122 | + $user->getAuthIdentifierName() => $user->getAuthIdentifier(), |
| 123 | + 'password' => $validatedData['current-password'], |
| 124 | + ]; |
| 125 | + if (! Auth::attempt($credentials)) { |
| 126 | + return redirect() |
| 127 | + ->back() |
| 128 | + ->with('error', 'Your password does not matches with your account password. Please try again.'); |
| 129 | + } |
| 130 | + |
| 131 | + $user = Auth::user(); |
| 132 | + $user->setGoogle2faEnable(false); |
| 133 | + $user->setGoogle2faSecret(null); |
| 134 | + $this->userRepository->save($user); |
| 135 | + |
| 136 | + return redirect('2fa')->with('success', '2FA is now Disabled.'); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Google2FAMiddleware verify redirect. |
| 141 | + * |
| 142 | + * @return \Illuminate\Http\Response |
| 143 | + */ |
| 144 | + public function verify() |
| 145 | + { |
| 146 | + return redirect(request()->session()->get('_previous')['url']); |
| 147 | + } |
| 148 | +} |
0 commit comments