-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TODO: * update views (inc text view of generated key for manual input) * allow secrete regeneration * allow recovery * allow admin to force disable on a users account (for recovery) * tie into roles
- Loading branch information
Showing
14 changed files
with
1,002 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace HMS\Auth; | ||
|
||
use PragmaRX\Google2FALaravel\Support\Authenticator; | ||
|
||
class Google2FAAuthenticator extends Authenticator | ||
{ | ||
/** | ||
* Check if it is already logged in or passable without checking for an OTP. | ||
* | ||
* @return bool | ||
*/ | ||
protected function canPassWithoutCheckingOTP() | ||
{ | ||
return ! $this->getUser()->isGoogle2faEnable() || | ||
! $this->isEnabled() || | ||
$this->noUserIsAuthenticated() || | ||
$this->twoFactorAuthStillValid(); | ||
} | ||
|
||
/** | ||
* Get the user Google2FA secret. | ||
* | ||
* @throws InvalidSecretKey | ||
* | ||
* @return mixed | ||
*/ | ||
protected function getGoogle2FASecretKey() | ||
{ | ||
$secret = $this->getUser()->getGoogle2faSecret(); | ||
|
||
if (is_null($secret) || empty($secret)) { | ||
throw new InvalidSecretKey('Secret key cannot be empty.'); | ||
} | ||
|
||
return $secret; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
app/Http/Controllers/Auth/TwoFactorAuthenticationController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Auth; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
use HMS\Repositories\UserRepository; | ||
use Illuminate\Support\Facades\Auth; | ||
use PragmaRX\Google2FALaravel\Google2FA; | ||
|
||
class TwoFactorAuthenticationController extends Controller | ||
{ | ||
/** | ||
* @var Google2FA | ||
*/ | ||
protected $google2fa; | ||
|
||
/** | ||
* @var UserRepository | ||
*/ | ||
protected $userRepository; | ||
|
||
/** | ||
* Create a new controller instance. | ||
* | ||
* @param Google2FA $google2fa | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Google2FA $google2fa, UserRepository $userRepository) | ||
{ | ||
$this->google2fa = $google2fa; | ||
$this->userRepository = $userRepository; | ||
|
||
$this->middleware('auth'); | ||
} | ||
|
||
/** | ||
* Show the 2fa enable/disable form. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function show2faForm(Request $request) | ||
{ | ||
$user = Auth::user(); | ||
|
||
$google2faUrl = ''; | ||
if (! empty($user->getGoogle2faSecret())) { | ||
$google2faUrl = $this->google2fa->getQRCodeInline( | ||
'Nottingham Hackspace HMS', | ||
$user->getEmail(), | ||
$user->getGoogle2faSecret() | ||
); | ||
} | ||
|
||
return view('auth.2fa') | ||
->with('user', $user) | ||
->with('google2faUrl', $google2faUrl); | ||
} | ||
|
||
/** | ||
* Generate new 2fa secret for user. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function generate2faSecret(Request $request) | ||
{ | ||
$user = Auth::user(); | ||
|
||
// Add the secret key to the registration data | ||
$user->setGoogle2faEnable(false); | ||
$user->setGoogle2faSecret($this->google2fa->generateSecretKey(32)); | ||
$this->userRepository->save($user); | ||
|
||
return redirect('2fa')->with('success', 'Secret Key is generated, Please verify Code to Enable 2FA'); | ||
} | ||
|
||
/** | ||
* Enable 2fa for USer. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function enable2fa(Request $request) | ||
{ | ||
$user = Auth::user(); | ||
// $google2fa = app('pragmarx.google2fa'); | ||
$secret = $request->input('verify-code'); | ||
|
||
$valid = $this->google2fa->verifyKey($user->getGoogle2faSecret(), $secret); | ||
|
||
if ($valid) { | ||
$user->setGoogle2faEnable(true); | ||
$this->userRepository->save($user); | ||
|
||
return redirect('2fa')->with('success', '2FA is Enabled Successfully.'); | ||
} else { | ||
return redirect('2fa')->with('error', 'Invalid Verification Code, Please try again.'); | ||
} | ||
} | ||
|
||
/** | ||
* Disable 2fa for User. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function disable2fa(Request $request) | ||
{ | ||
$validatedData = $request->validate([ | ||
'current-password' => 'required', | ||
]); | ||
|
||
$user = Auth::user(); | ||
$credentials = [ | ||
$user->getAuthIdentifierName() => $user->getAuthIdentifier(), | ||
'password' => $validatedData['current-password'], | ||
]; | ||
if (! Auth::attempt($credentials)) { | ||
return redirect() | ||
->back() | ||
->with('error', 'Your password does not matches with your account password. Please try again.'); | ||
} | ||
|
||
$user = Auth::user(); | ||
$user->setGoogle2faEnable(false); | ||
$user->setGoogle2faSecret(null); | ||
$this->userRepository->save($user); | ||
|
||
return redirect('2fa')->with('success', '2FA is now Disabled.'); | ||
} | ||
|
||
/** | ||
* Google2FAMiddleware verify redirect. | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function verify() | ||
{ | ||
return redirect(request()->session()->get('_previous')['url']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use HMS\Auth\Google2FAAuthenticator; | ||
|
||
class Google2FAMiddleware | ||
{ | ||
/** | ||
* @var Google2FAAuthenticator | ||
*/ | ||
protected $authenticator; | ||
|
||
/** | ||
* Construct Middleware. | ||
* | ||
* @param Google2FAAuthenticator $authenticator | ||
*/ | ||
public function __construct(Google2FAAuthenticator $authenticator) | ||
{ | ||
$this->authenticator = $authenticator; | ||
} | ||
|
||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @param \Closure $next | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
$this->authenticator->boot($request); | ||
|
||
if ($this->authenticator->isAuthenticated()) { | ||
return $next($request); | ||
} | ||
|
||
return $this->authenticator->makeRequestOneTimePasswordResponse(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.