Initial commit
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
class DisableTwoFactorRequest extends TwoFactorRequest
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
class EnableTwoFactorRequest extends TwoFactorRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
class ReSendTwoFactorTokenRequest extends TwoFactorRequest
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
use Laravel\Fortify\Contracts\TwoFactorAuthenticationProvider;
|
||||
use Vanguard\Http\Requests\Request;
|
||||
|
||||
class TwoFactorLoginRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
if ($userId = $this->get('user')) {
|
||||
// Only users with "users.manage" permission can enable 2FA for other users.
|
||||
return $this->user()->hasPermission('users.manage') || $this->user()->id == $userId;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code' => 'nullable|string',
|
||||
];
|
||||
}
|
||||
|
||||
private function clear2FAUserId($result)
|
||||
{
|
||||
if ($result) {
|
||||
$this->session()->forget('auth.2fa.id');
|
||||
}
|
||||
}
|
||||
|
||||
public function hasValidCode($user): bool
|
||||
{
|
||||
try {
|
||||
if (!$this->code) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$twoFactorProvider = app(TwoFactorAuthenticationProvider::class);
|
||||
$decryptedSecret = decrypt($user->two_factor_secret);
|
||||
$verificationResult = $twoFactorProvider->verify($decryptedSecret, $this->code);
|
||||
|
||||
return tap($verificationResult, fn($result) => $this->clear2FAUserId($result));
|
||||
} catch (\Exception $e) {
|
||||
\Log::info($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
use Vanguard\Http\Requests\Request;
|
||||
use Vanguard\Repositories\User\UserRepository;
|
||||
use Vanguard\User;
|
||||
|
||||
abstract class TwoFactorRequest extends Request
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
if ($userId = $this->get('user')) {
|
||||
// Only users with "users.manage" permission can enable 2FA for other users.
|
||||
return $this->user()->hasPermission('users.manage') || $this->user()->id == $userId;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user for which we should enable the 2FA.
|
||||
*/
|
||||
public function theUser(): User
|
||||
{
|
||||
if ($userId = $this->get('user')) {
|
||||
return app(UserRepository::class)->find($userId);
|
||||
}
|
||||
|
||||
return $this->user();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace Vanguard\Http\Requests\TwoFactor;
|
||||
|
||||
class VerifyTwoFactorTokenRequest extends TwoFactorRequest
|
||||
{
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'code' => 'required',
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user