Skip to content

Commit

Permalink
add precognition update
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed May 26, 2024
1 parent bc9928e commit e38820b
Show file tree
Hide file tree
Showing 31 changed files with 827 additions and 692 deletions.
8 changes: 2 additions & 6 deletions app/Http/Controllers/Auth/ForgotPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\ForgotPasswordRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Password;

class ForgotPasswordController extends Controller
{
public function __invoke(Request $request)
public function __invoke(ForgotPasswordRequest $request)
{
$key = 'reset-passwords:'.$request->ip();
// Throttle requests
Expand All @@ -22,10 +22,6 @@ public function __invoke(Request $request)

RateLimiter::hit($key);

$request->validate([
"email" => "email|required|exists:users,email"
]);

$status = Password::sendResetLink(
$request->only('email')
);
Expand Down
13 changes: 2 additions & 11 deletions app/Http/Controllers/Auth/PasswordResetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\PasswordResetRequest;
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
Expand All @@ -17,18 +18,8 @@ public function view(Request $request)
return Inertia::render('Auth/ResetPassword', $request->only(['email', 'token']));
}

public function store(Request $request)
public function store(PasswordResetRequest $request)
{
$request->validate([
'token' => 'required',
'email' => 'required|email',
'password' => [
'required',
'confirmed',
\Illuminate\Validation\Rules\Password::min(8)->mixedCase()->numbers()
],
]);

$status = Password::reset(
$request->only('email', 'password', 'password_confirmation', 'token'),
function ($user, $password) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use App\Enums\TwoFactorTypeEnum;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\TwoFactor\TotpDestroyRequest;
use App\Http\Requests\TwoFactor\TotpStoreRequest;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\ValidationException;
Expand Down Expand Up @@ -38,13 +39,10 @@ function () use ($tfa) {
}

// Add new totp device
public function store(Request $request)
public function store(TotpStoreRequest $request)
{
$tfa = $this->createTwoFactorAuth();
$data = $request->validate([
'code' => 'required|numeric|digits:6',
'secret' => 'required|string',
]);
$data = $request->validated();
// Verify that data->code is equal to cached value
$cachedValue = Cache::get('user-'.auth()->user()->id.'-two-factor-user-cache');
if (!isset($cachedValue['secret'])) {
Expand All @@ -68,11 +66,9 @@ public function store(Request $request)
}

// Delete totp device
public function destroy(Request $request)
public function destroy(TotpDestroyRequest $request)
{
$data = $request->validate([
'password' => 'required|string',
]);
$data = $request->validated();
// Verify that password is correct
$userPassword = auth()->user()->password;
if (!Hash::check($data['password'], $userPassword)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace App\Http\Controllers\Profile\Settings\TwoFactor;

use App\Http\Controllers\Controller;
use App\Http\Requests\YubikeyDestroyRequest;
use App\Http\Requests\YubikeyStoreRequest;
use App\Services\YubicoService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Validation\ValidationException;
Expand All @@ -30,12 +31,8 @@ public function index()
}

// Add new Yubikey
public function store(Request $request)
public function store(YubikeyStoreRequest $request)
{
$request->validate([
'code' => 'required|string',
'name' => 'nullable|string|max:80',
]);
$limitKey = 'yubikey-setup-'.$request->user()->id;
// Rate limit this endpoint
if (RateLimiter::tooManyAttempts($limitKey, 10)) {
Expand All @@ -46,25 +43,22 @@ public function store(Request $request)
$yubico->verify($request->input('code'));

// Check if the Yubikey is already registered
if ($request->user()->twoFactors()->where('identifier', $identifier)->exists()) {
if ($request->user()->twoFactors()->where('identifier', $yubico->identifier)->exists()) {
throw ValidationException::withMessages(['code' => 'This Yubikey is already registered.']);
}
// Create the Yubikey
$request->user()->twoFactors()->create([
'name' => $request->input('name'),
'identifier' => $identifier,
'identifier' => $yubico->identifier,
'type' => 'yubikey',
]);
return redirect()->route('settings.two-factor.yubikey');
}

// Delete Yubikey
public function destroy(Request $request)
public function destroy(YubikeyDestroyRequest $request)
{
$data = $request->validate([
'password' => 'required|string',
'keyId' => 'required|integer|exists:two_factors,id',
]);
$data = $request->validated();
// Verify that password is correct
$userPassword = auth()->user()->password;
if (!Hash::check($data['password'], $userPassword)) {
Expand Down
16 changes: 3 additions & 13 deletions app/Http/Controllers/Profile/Settings/UpdatePasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@
namespace App\Http\Controllers\Profile\Settings;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Requests\UpdatePasswordRequest;
use Illuminate\Support\Facades\Hash;
use Inertia\Inertia;

class UpdatePasswordController extends Controller
{
public function __invoke(Request $request)
public function __invoke(UpdatePasswordRequest $request)
{
$data = $request->validate([
"current_password" => [
"required",
"current_password",
],
"password" => [
"required",
"confirmed",
\Illuminate\Validation\Rules\Password::min(8)->mixedCase()->numbers(),
],
]);
$data = $request->validated();

$request->user()->update(['password' => Hash::make($data['password'])]);
return Inertia::render("Settings/UpdatePassword", ["success" => true]);
Expand Down
15 changes: 15 additions & 0 deletions app/Http/Requests/ForgotPasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ForgotPasswordRequest extends FormRequest
{
public function rules(): array
{
return [
"email" => "email|required|exists:users,email"
];
}
}
21 changes: 21 additions & 0 deletions app/Http/Requests/PasswordResetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class PasswordResetRequest extends FormRequest
{
public function rules(): array
{
return [
'token' => 'required',
'email' => 'required|email',
'password' => [
'required',
'confirmed',
\Illuminate\Validation\Rules\Password::min(8)->mixedCase()->numbers()
],
];
}
}
15 changes: 15 additions & 0 deletions app/Http/Requests/TwoFactor/TotpDestroyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Http\Requests\TwoFactor;

use Illuminate\Foundation\Http\FormRequest;

class TotpDestroyRequest extends FormRequest
{
public function rules(): array
{
return [
'password' => 'required|string',
];
}
}
16 changes: 16 additions & 0 deletions app/Http/Requests/TwoFactor/TotpStoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Requests\TwoFactor;

use Illuminate\Foundation\Http\FormRequest;

class TotpStoreRequest extends FormRequest
{
public function rules(): array
{
return [
'code' => 'required|numeric|digits:6',
'secret' => 'required|string',
];
}
}
23 changes: 23 additions & 0 deletions app/Http/Requests/UpdatePasswordRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UpdatePasswordRequest extends FormRequest
{
public function rules(): array
{
return [
"current_password" => [
"required",
"current_password",
],
"password" => [
"required",
"confirmed",
\Illuminate\Validation\Rules\Password::min(8)->mixedCase()->numbers(),
],
];
}
}
16 changes: 16 additions & 0 deletions app/Http/Requests/YubikeyDestroyRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YubikeyDestroyRequest extends FormRequest
{
public function rules(): array
{
return [
'password' => 'required|string',
'keyId' => 'required|integer|exists:two_factors,id',
];
}
}
16 changes: 16 additions & 0 deletions app/Http/Requests/YubikeyStoreRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class YubikeyStoreRequest extends FormRequest
{
public function rules(): array
{
return [
'code' => 'required|string',
'name' => 'required|string|max:80',
];
}
}
Loading

0 comments on commit e38820b

Please sign in to comment.