mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-30 09:48:55 +00:00
Add support for 2 Factor Authentification (2FA)
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\PasswordSecurity;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class PasswordSecurityController extends Controller
|
||||
{
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function show2faForm(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
$google2fa_url = '';
|
||||
if ($user->passwordSecurity()->exists()) {
|
||||
$google2fa_url = \Google2FA::getQRCodeInline(
|
||||
config('app.name'),
|
||||
$user->email,
|
||||
$user->passwordSecurity->google2fa_secret
|
||||
);
|
||||
}
|
||||
$data = [
|
||||
'user' => $user,
|
||||
'google2fa_url' => $google2fa_url,
|
||||
];
|
||||
|
||||
return view('auth.2fa')->with('data', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
|
||||
*/
|
||||
public function generate2faSecret(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
// Add the secret key to the registration data
|
||||
PasswordSecurity::create(
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'google2fa_enable' => 0,
|
||||
'google2fa_secret' => \Google2FA::generateSecretKey(),
|
||||
]
|
||||
);
|
||||
|
||||
return redirect('2fa')->with('success', 'Secret Key is generated, Please verify Code to Enable 2FA');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\IncompatibleWithGoogleAuthenticatorException
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\InvalidCharactersException
|
||||
* @throws \PragmaRX\Google2FA\Exceptions\SecretKeyTooShortException
|
||||
*/
|
||||
public function enable2fa(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$secret = $request->input('verify-code');
|
||||
$valid = \Google2FA::verifyKey($user->passwordSecurity->google2fa_secret, $secret);
|
||||
if ($valid) {
|
||||
$user->passwordSecurity->google2fa_enable = 1;
|
||||
$user->passwordSecurity->save();
|
||||
|
||||
return redirect('2fa')->with('success', '2FA is Enabled Successfully.');
|
||||
}
|
||||
|
||||
return redirect('2fa')->with('error', 'Invalid Verification Code, Please try again.');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
*/
|
||||
public function disable2fa(Request $request)
|
||||
{
|
||||
if (! (Hash::check($request->get('current-password'), Auth::user()->password))) {
|
||||
// The passwords matches
|
||||
return redirect()->back()->with('error', 'Your password does not match with your account password. Please try again.');
|
||||
}
|
||||
|
||||
$validatedData = $request->validate([
|
||||
'current-password' => 'required',
|
||||
]);
|
||||
$user = Auth::user();
|
||||
$user->passwordSecurity->google2fa_enable = 0;
|
||||
$user->passwordSecurity->save();
|
||||
|
||||
return redirect('2fa')->with('success', '2FA is now Disabled.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user