Update 2fa handling

This commit is contained in:
DariusIII
2025-06-05 16:36:49 +02:00
parent bf0d130875
commit 95fd8978a3
7 changed files with 504 additions and 6 deletions
@@ -69,16 +69,29 @@ class PasswordSecurityController extends Controller
$user->passwordSecurity->google2fa_enable = 1;
$user->passwordSecurity->save();
// Check if we should redirect to profile page
if ($request->has('redirect_to_profile')) {
return redirect()->to('profileedit#security')->with('success_2fa', '2FA is Enabled Successfully.');
}
return redirect()->to('2fa')->with('success', '2FA is Enabled Successfully.');
}
// Check if we should redirect to profile page on failure as well
if ($request->has('redirect_to_profile')) {
return redirect()->to('profileedit#security')->with('error_2fa', 'Invalid Verification Code, Please try again.');
}
return redirect()->to('2fa')->with('error', 'Invalid Verification Code, Please try again.');
}
public function disable2fa(Disable2faPasswordSecurityRequest $request): \Illuminate\Routing\Redirector|RedirectResponse|\Illuminate\Contracts\Foundation\Application
{
if (! (Hash::check($request->get('current-password'), $request->user()->password))) {
// The passwords matches
// Password doesn't match
if ($request->has('redirect_to_profile') || $request->has('from_profile')) {
return redirect()->to('profileedit#security')->with('error_2fa', 'Your password does not match with your account password. Please try again.');
}
return redirect()->back()->with('error', 'Your password does not match with your account password. Please try again.');
}
@@ -87,6 +100,11 @@ class PasswordSecurityController extends Controller
$user->passwordSecurity->google2fa_enable = 0;
$user->passwordSecurity->save();
// Check if this request is from the profile edit page
if ($request->has('redirect_to_profile') || $request->has('from_profile')) {
return redirect()->to('profileedit#security')->with('success_2fa', '2FA is now Disabled.');
}
return redirect()->to('2fa')->with('success', '2FA is now Disabled.');
}
@@ -182,4 +200,60 @@ class PasswordSecurityController extends Controller
return app('smarty.view')->display($theme.'/2fa_verify.tpl');
}
/**
* Handle disabling 2FA directly from profile page to avoid form conflicts.
* This route is specifically for the profile page 2FA section.
*/
public function profileDisable2fa(Request $request): RedirectResponse
{
$request->validate([
'current-password' => 'required',
]);
if (! (Hash::check($request->get('current-password'), $request->user()->password))) {
return redirect()->to('profileedit#security')->with('error_2fa', 'Your password does not match with your account password. Please try again.');
}
$user = $request->user();
if ($user->passwordSecurity) {
$user->passwordSecurity->google2fa_enable = 0;
$user->passwordSecurity->save();
}
return redirect()->to('profileedit#security')->with('success_2fa', '2FA is now Disabled.');
}
/**
* Show the 2FA enable form on a dedicated page
*/
public function showEnable2faForm(Request $request): Application|View|Factory|\Illuminate\Contracts\Foundation\Application
{
$user = $request->user();
$success = $request->session()->get('success');
$error = $request->session()->get('error');
$google2fa_url = '';
if ($user->passwordSecurity()->exists()) {
$google2fa_url = \Google2FA::getQRCodeInline(
config('app.name'),
$user->email,
$user->passwordSecurity->google2fa_secret
);
}
return view('themes.Gentele.2fa_enable', compact('user', 'google2fa_url', 'success', 'error'));
}
/**
* Show the 2FA disable form on a dedicated page
*/
public function showDisable2faForm(Request $request): Application|View|Factory|\Illuminate\Contracts\Foundation\Application
{
$user = $request->user();
$success = $request->session()->get('success');
$error = $request->session()->get('error');
return view('themes.Gentele.2fa_disable', compact('user', 'success', 'error'));
}
}
@@ -117,6 +117,18 @@ class ProfileController extends BasePageController
}
$errorStr = '';
$success_2fa = $request->session()->get('success');
$error_2fa = $request->session()->get('error');
// Generate 2FA QR code URL if 2FA is set up but not enabled
$google2fa_url = '';
if ($this->userdata->passwordSecurity()->exists() && !$this->userdata->passwordSecurity->google2fa_enable) {
$google2fa_url = \Google2FA::getQRCodeInline(
config('app.name'),
$this->userdata->email,
$this->userdata->passwordSecurity->google2fa_secret
);
}
switch ($action) {
case 'newapikey':
@@ -247,6 +259,9 @@ class ProfileController extends BasePageController
$this->smarty->assign('error', $errorStr);
$this->smarty->assign('user', $this->userdata);
$this->smarty->assign('userexccat', User::getCategoryExclusionById($userid));
$this->smarty->assign('success_2fa', $success_2fa);
$this->smarty->assign('error_2fa', $error_2fa);
$this->smarty->assign('google2fa_url', $google2fa_url);
$meta_title = 'Edit User Profile';
$meta_keywords = 'edit,profile,user,details';
@@ -0,0 +1,75 @@
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
/**
* Controller specifically for handling profile security operations
* like 2FA management with no dependencies on other profile functions
*/
class ProfileSecurityController extends BasePageController
{
/**
* Disable 2FA for the authenticated user from the profile page
* This is separate from the main profile edit functionality
*
* @param Request $request
* @return JsonResponse|RedirectResponse
*/
public function disable2fa(Request $request)
{
// Simple validation - only password is required
$validated = $request->validate([
'current_password' => 'required',
]);
// Check if password is correct
if (!Hash::check($validated['current_password'], Auth::user()->password)) {
if ($request->expectsJson() || $request->ajax()) {
return response()->json([
'success' => false,
'message' => 'Your password does not match. Please try again.'
]);
}
return redirect()
->to('profileedit#security')
->with('error_2fa', 'Your password does not match. Please try again.');
}
// Get the user and disable 2FA
$user = Auth::user();
if ($user->passwordSecurity) {
$user->passwordSecurity->google2fa_enable = 0;
$user->passwordSecurity->save();
if ($request->expectsJson() || $request->ajax()) {
return response()->json([
'success' => true,
'message' => '2FA has been successfully disabled.'
]);
}
return redirect()
->to('profileedit#security')
->with('success_2fa', '2FA has been successfully disabled.');
}
if ($request->expectsJson() || $request->ajax()) {
return response()->json([
'success' => false,
'message' => 'No 2FA configuration found for this user.'
]);
}
return redirect()
->to('profileedit#security')
->with('error_2fa', 'No 2FA configuration found for this user.');
}
}
@@ -0,0 +1,120 @@
<!DOCTYPE html>
<html lang="{{App::getLocale()}}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{csrf_token()}}">
<title>{$meta_title}{if $meta_title != "" && $site->metatitle != ""} - {/if}{$site->metatitle}</title>
{{Html::style("{{asset('/assets/css/all-css.css')}}")}}
</head>
<body class="2fa-page">
<div class="container">
<div class="row justify-content-center mt-4">
<div class="col-md-8 col-lg-6">
<!-- Breadcrumb -->
<nav aria-label="breadcrumb" class="mb-3">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{url("{$site->home_link}")}}">Home</a></li>
<li class="breadcrumb-item"><a href="{{url("/profile")}}">Profile</a></li>
<li class="breadcrumb-item"><a href="{{url("/profileedit")}}">Edit Profile</a></li>
<li class="breadcrumb-item active">Disable 2FA</li>
</ol>
</nav>
<!-- 2FA Disable Card -->
<div class="card shadow-sm mb-4">
<div class="card-header bg-light">
<h4 class="text-center mb-0"><i class="fa fa-lock-open me-2"></i>Disable Two-Factor Authentication</h4>
</div>
<div class="card-body p-4">
{if isset($error)}
<div class="alert alert-danger notification-fade" role="alert">
<i class="fa fa-exclamation-circle me-2"></i>{$error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{/if}
{if isset($success)}
<div class="alert alert-success notification-fade" role="alert">
<i class="fa fa-check-circle me-2"></i>{$success}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{/if}
<div class="text-center mb-4">
<div class="d-flex justify-content-center align-items-center mb-2">
<div class="app-icon text-danger">
<i class="fas fa-shield-alt fa-3x" aria-hidden="true"></i>
</div>
</div>
<h5 class="mb-1">Disable Two-Factor Authentication</h5>
<p class="text-muted">Remove the extra security layer from your account</p>
</div>
{if $user->passwordSecurity && $user->passwordSecurity->google2fa_enable}
<div class="alert alert-warning mb-4">
<i class="fa fa-exclamation-triangle me-2"></i><strong>Warning:</strong> Disabling 2FA will make your account less secure. Only proceed if absolutely necessary.
</div>
<div class="card bg-light border-0 p-4 mb-4">
<h6 class="mb-3 text-center">Confirm Password to Disable 2FA</h6>
<p class="text-center text-muted mb-4">Please enter your current password to verify your identity:</p>
<form action="{{url('/profileedit/disable2fa')}}" method="POST" autocomplete="off">
{{csrf_field()}}
<input type="hidden" name="redirect_to_profile" value="1">
<div class="mb-4">
<div class="input-group">
<span class="input-group-text"><i class="fas fa-lock"></i></span>
<input id="current-password" type="password" class="form-control" name="current-password" required placeholder="Current Password" autocomplete="off">
</div>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-danger">
<i class="fa fa-times-circle me-2"></i>Disable 2FA
</button>
<a href="{{url("/profileedit#security")}}" class="btn btn-outline-secondary">
<i class="fa fa-arrow-left me-2"></i>Cancel and Go Back
</a>
</div>
</form>
</div>
{else}
<div class="alert alert-info mb-4">
<i class="fa fa-info-circle me-2"></i>Two-factor authentication is not currently enabled on your account.
</div>
<div class="text-center">
<a href="{{url("/profileedit#security")}}" class="btn btn-primary">
<i class="fa fa-arrow-left me-2"></i>Back to Profile
</a>
</div>
{/if}
</div>
</div>
</div>
</div>
</div>
<style>
.notification-fade {
opacity: 0;
transition: opacity 0.6s ease-in-out;
}
.notification-fade.show {
opacity: 1;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
const alerts = document.querySelectorAll('.notification-fade');
alerts.forEach(function(alert) {
alert.classList.add('show');
});
}, 100);
});
</script>
</body>
</html>
@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html lang="{{App::getLocale()}}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="csrf-token" content="{{csrf_token()}}">
<title>{$meta_title}{if $meta_title != "" && $site->metatitle != ""} - {/if}{$site->metatitle}</title>
{{Html::style("{{asset('/assets/css/all-css.css')}}")}}
</head>
<body class="2fa-page">
<div class="container">
<div class="row justify-content-center mt-4">
<div class="col-md-8 col-lg-6">
<!-- Breadcrumb -->
<nav aria-label="breadcrumb" class="mb-3">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="{{url("{$site->home_link}")}}">Home</a></li>
<li class="breadcrumb-item"><a href="{{url("/profile")}}">Profile</a></li>
<li class="breadcrumb-item"><a href="{{url("/profileedit")}}">Edit Profile</a></li>
<li class="breadcrumb-item active">Enable 2FA</li>
</ol>
</nav>
<!-- 2FA Setup Card -->
<div class="card shadow-sm mb-4">
<div class="card-header bg-light">
<h4 class="text-center mb-0"><i class="fa fa-lock me-2"></i>Enable Two-Factor Authentication</h4>
</div>
<div class="card-body p-4">
{if isset($error)}
<div class="alert alert-danger notification-fade" role="alert">
<i class="fa fa-exclamation-circle me-2"></i>{$error}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{/if}
{if isset($success)}
<div class="alert alert-success notification-fade" role="alert">
<i class="fa fa-check-circle me-2"></i>{$success}
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
{/if}
<div class="text-center mb-4">
<div class="d-flex justify-content-center align-items-center mb-2">
<div class="app-icon text-primary">
<i class="fas fa-shield-alt fa-3x" aria-hidden="true"></i>
</div>
</div>
<h5 class="mb-1">Two-Factor Authentication Setup</h5>
<p class="text-muted">Add an extra layer of security to your account</p>
</div>
<div class="alert alert-info">
<i class="fa fa-info-circle me-2"></i>Two factor authentication (2FA) strengthens access security by requiring two methods to verify your identity. It protects against phishing, social engineering, and password brute force attacks.
</div>
{if !isset($user->passwordSecurity)}
<div class="text-center my-4">
<form action="{{url("generate2faSecret")}}" method="POST">
{{csrf_field()}}
<input type="hidden" name="redirect_to_setup" value="1">
<div class="d-grid">
<button type="submit" class="btn btn-primary">
<i class="fa fa-key me-2"></i>Generate Secret Key to Begin Setup
</button>
</div>
</form>
</div>
{elseif !$user->passwordSecurity->google2fa_enable}
<div class="row mb-4">
<div class="col-md-6 mb-4 mb-md-0">
<div class="card h-100 bg-light border-0">
<div class="card-body text-center">
<h6 class="mb-3">1. Scan this QR code with your Google Authenticator app:</h6>
<div class="mb-3 qr-container p-3 bg-white rounded d-inline-block">
<img src="{$google2fa_url}" alt="2FA QR Code" class="img-fluid">
</div>
<p class="text-muted small">If you can't scan the QR code, please set up manually using the code provided.</p>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card h-100 bg-light border-0">
<div class="card-body">
<h6 class="mb-3">2. Enter the verification code from your app:</h6>
<form action="{{url('/profileedit/enable2fa')}}" method="POST" autocomplete="off">
{{csrf_field()}}
<input type="hidden" name="redirect_to_profile" value="1">
<div class="mb-4">
<div class="input-group">
<span class="input-group-text"><i class="fas fa-key"></i></span>
<input id="verify-code" type="text" class="form-control" name="verify-code" required placeholder="Enter 6-digit code" autocomplete="off">
</div>
<div class="form-text">Enter the 6-digit code from your authenticator app</div>
</div>
<div class="d-grid gap-2">
<button type="submit" class="btn btn-success">
<i class="fa fa-check-circle me-2"></i>Verify and Enable 2FA
</button>
<a href="{{url("/profileedit#security")}}" class="btn btn-outline-secondary">
<i class="fa fa-arrow-left me-2"></i>Back to Profile
</a>
</div>
</form>
</div>
</div>
</div>
</div>
<div class="alert alert-warning">
<i class="fa fa-exclamation-triangle me-2"></i><strong>Important:</strong> Store your backup codes in a secure location. If you lose your device, you will need these codes to regain access to your account.
</div>
{elseif $user->passwordSecurity->google2fa_enable}
<div class="alert alert-success mb-4">
<i class="fa fa-check-circle me-2"></i>Two-factor authentication is currently <strong>enabled</strong> for your account.
</div>
<div class="text-center">
<a href="{{url("/profileedit#security")}}" class="btn btn-primary">
<i class="fa fa-arrow-left me-2"></i>Back to Profile
</a>
</div>
{/if}
</div>
</div>
</div>
</div>
</div>
<style>
.notification-fade {
opacity: 0;
transition: opacity 0.6s ease-in-out;
}
.notification-fade.show {
opacity: 1;
}
</style>
<script>
document.addEventListener('DOMContentLoaded', function() {
setTimeout(function() {
const alerts = document.querySelectorAll('.notification-fade');
alerts.forEach(function(alert) {
alert.classList.add('show');
});
}, 100);
});
</script>
</body>
</html>
+59 -5
View File
@@ -375,9 +375,19 @@
<div class="tab-pane fade" id="security" role="tabpanel" aria-labelledby="security-tab">
<div class="card border mb-4">
<div class="card-header bg-light">
<h6 class="mb-0"><i class="fa fa-shield me-2"></i>Security Settings</h6>
<h6 class="mb-0"><i class="fa fa-shield me-2"></i>Two-Factor Authentication (2FA)</h6>
</div>
<div class="card-body">
{if isset($error_2fa)}
<div class="alert alert-danger">
<i class="fa fa-exclamation-circle me-2"></i>{$error_2fa}
</div>
{/if}
{if isset($success_2fa)}
<div class="alert alert-success">
<i class="fa fa-check-circle me-2"></i>{$success_2fa}
</div>
{/if}
<div class="d-flex align-items-center mb-4">
<div class="me-4">
<i class="fa fa-lock fa-3x text-primary"></i>
@@ -386,12 +396,56 @@
<h5 class="mb-1">Two-Factor Authentication</h5>
<p class="mb-0 text-muted">Add an extra layer of security to your account</p>
</div>
<div class="ms-auto">
<a href="{{url("{'2fa'}")}}" class="btn btn-primary">
<i class="fa fa-cog me-2"></i>Manage 2FA
</a>
</div>
<div class="card bg-light mb-4">
<div class="card-body">
<div class="d-flex align-items-center">
{if !isset($user->passwordSecurity) || !$user->passwordSecurity->google2fa_enable}
<div class="me-3">
<span class="badge bg-warning rounded-pill">Not Enabled</span>
</div>
<div class="me-auto">
<h6 class="mb-0">Two-factor authentication is currently disabled</h6>
<p class="text-muted small mb-0">Enable 2FA to add an additional layer of security to your account</p>
</div>
<div>
<a href="{{url("2fa/enable")}}" class="btn btn-primary">
<i class="fa fa-lock me-2"></i>Enable 2FA
</a>
</div>
{else}
<div class="me-3">
<span class="badge bg-success rounded-pill">Enabled</span>
</div>
<div class="me-auto">
<h6 class="mb-0">Two-factor authentication is active</h6>
<p class="text-muted small mb-0">Your account is protected with an additional layer of security</p>
</div>
<div>
<a href="{{url("2fa/disable")}}" class="btn btn-outline-danger">
<i class="fa fa-lock-open me-2"></i>Disable 2FA
</a>
</div>
{/if}
</div>
</div>
</div>
<div class="alert alert-info mb-4">
<h6 class="mb-2"><i class="fa fa-info-circle me-2"></i>What is Two-Factor Authentication?</h6>
<p class="mb-0">Two-factor authentication adds a second layer of security to your account. In addition to your password, you'll need a code from your authenticator app to sign in. This helps protect your account even if your password is compromised.</p>
</div>
<div class="mt-4">
<h6>Security Best Practices:</h6>
<ul class="text-muted">
<li>Use a strong, unique password for your account</li>
<li>Store your 2FA backup codes in a secure location</li>
<li>Never share your authentication codes with others</li>
<li>Consider using a password manager for all your accounts</li>
</ul>
</div>
</div>
</div>
</div>
+8
View File
@@ -64,6 +64,7 @@ use App\Http\Controllers\MyShowsController;
use App\Http\Controllers\NfoController;
use App\Http\Controllers\PasswordSecurityController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ProfileSecurityController;
use App\Http\Controllers\RssController;
use App\Http\Controllers\SearchController;
use App\Http\Controllers\SeriesController;
@@ -140,9 +141,16 @@ Route::middleware('isVerified')->group(function () {
Route::match(['GET', 'POST'], 'series/{id?}', [SeriesController::class, 'index'])->name('series');
Route::match(['GET', 'POST'], 'ajax_profile', [AjaxController::class, 'profile'])->name('ajax_profile');
Route::match(['GET', 'POST'], '2fa', [PasswordSecurityController::class, 'show2faForm'])->name('2fa');
Route::get('2fa/enable', [PasswordSecurityController::class, 'showEnable2faForm'])->name('2fa.enable');
Route::get('2fa/disable', [PasswordSecurityController::class, 'showDisable2faForm'])->name('2fa.disable');
Route::post('generate2faSecret', [PasswordSecurityController::class, 'generate2faSecret'])->name('generate2faSecret');
Route::post('2fa', [PasswordSecurityController::class, 'enable2fa'])->name('enable2fa');
Route::post('disable2fa', [PasswordSecurityController::class, 'disable2fa'])->name('disable2fa');
Route::post('profile-disable2fa', [PasswordSecurityController::class, 'profileDisable2fa'])->name('profile-disable2fa');
// Custom 2FA routes that redirect to profile page
Route::post('profileedit/enable2fa', [PasswordSecurityController::class, 'enable2fa'])->name('profileedit.enable2fa');
Route::post('profileedit/disable2fa', [PasswordSecurityController::class, 'disable2fa'])->name('profileedit.disable2fa');
Route::post('profile-security/disable-2fa', [ProfileSecurityController::class, 'disable2fa'])->name('profile.security.disable2fa');
});
Route::middleware('role:Admin', '2fa')->prefix('admin')->group(function () {