From df2fe12225538617ad7ce0ad98ea98de1bf02cf3 Mon Sep 17 00:00:00 2001 From: Tony Lea Date: Sun, 19 May 2024 14:38:18 -0400 Subject: [PATCH] Adding more functionality to fix the PHP stan bugs --- .../two-factor-authentication/index.blade.php | 7 +++-- .../DisableTwoFactorAuthentication.php | 5 ++-- .../GenerateNewRecoveryCodes.php | 27 ------------------- .../GenerateQrCodeAndSecretKey.php | 19 +++++++------ src/Http/Controllers/SocialController.php | 3 ++- src/Models/User.php | 10 ++++++- 6 files changed, 30 insertions(+), 41 deletions(-) delete mode 100644 src/Actions/TwoFactorAuth/GenerateNewRecoveryCodes.php diff --git a/resources/views/pages/user/two-factor-authentication/index.blade.php b/resources/views/pages/user/two-factor-authentication/index.blade.php index cd42a40..d297538 100644 --- a/resources/views/pages/user/two-factor-authentication/index.blade.php +++ b/resources/views/pages/user/two-factor-authentication/index.blade.php @@ -2,6 +2,8 @@ use function Laravel\Folio\{middleware, name}; use Livewire\Volt\Component; +use Illuminate\Support\Collection; +use Illuminate\Support\Str; use Livewire\Attributes\On; use PragmaRX\Google2FA\Google2FA; use Devdojo\Auth\Actions\TwoFactorAuth\DisableTwoFactorAuthentication; @@ -47,8 +49,9 @@ public function enable(){ } private function generateCodes(){ - $generateCodesFor = new GenerateNewRecoveryCodes(); - return $generateCodesFor(auth()->user()); + Collection::times(8, function () { + return Str::random(10).'-'.Str::random(10);; + }); } public function cancelTwoFactor(){ diff --git a/src/Actions/TwoFactorAuth/DisableTwoFactorAuthentication.php b/src/Actions/TwoFactorAuth/DisableTwoFactorAuthentication.php index 8be7ea2..3977802 100644 --- a/src/Actions/TwoFactorAuth/DisableTwoFactorAuthentication.php +++ b/src/Actions/TwoFactorAuth/DisableTwoFactorAuthentication.php @@ -3,16 +3,17 @@ namespace Devdojo\Auth\Actions\TwoFactorAuth; use Devdojo\Auth\Events\TwoFactorAuthenticationDisabled; +use Devdojo\Auth\Models\User; class DisableTwoFactorAuthentication { /** * Disable two factor authentication for the user. * - * @param mixed $user + * @param \Devdojo\Auth\Models\User $user * @return void */ - public function __invoke($user) + public function __invoke(User $user) { if (! is_null($user->two_factor_secret) || ! is_null($user->two_factor_recovery_codes) || diff --git a/src/Actions/TwoFactorAuth/GenerateNewRecoveryCodes.php b/src/Actions/TwoFactorAuth/GenerateNewRecoveryCodes.php deleted file mode 100644 index 98c0616..0000000 --- a/src/Actions/TwoFactorAuth/GenerateNewRecoveryCodes.php +++ /dev/null @@ -1,27 +0,0 @@ -generate(); - }); - } - - public function generate() - { - return Str::random(10).'-'.Str::random(10); - } -} diff --git a/src/Actions/TwoFactorAuth/GenerateQrCodeAndSecretKey.php b/src/Actions/TwoFactorAuth/GenerateQrCodeAndSecretKey.php index 1ed2765..1e272f4 100644 --- a/src/Actions/TwoFactorAuth/GenerateQrCodeAndSecretKey.php +++ b/src/Actions/TwoFactorAuth/GenerateQrCodeAndSecretKey.php @@ -6,29 +6,32 @@ use BaconQrCode\Renderer\ImageRenderer; use BaconQrCode\Renderer\RendererStyle\RendererStyle; use BaconQrCode\Writer; +use Devdojo\Auth\Models\User; use PragmaRX\Google2FA\Google2FA; class GenerateQrCodeAndSecretKey { + public string $companyName; /** * Generate new recovery codes for the user. * - * @param mixed $user - * @return void + * @param \Devdojo\Auth\Models\User $user + * @return array{string, string} */ - public function __invoke($user): array + public function __invoke(User $user): array { $google2fa = new Google2FA(); $secret_key = $google2fa->generateSecretKey(); - //$secretKeyEncrypted = encrypt($secret_key); - //echo $google2fa->generateSecretKey(); + $this->companyName = 'Auth'; + if(is_string(config('app.name'))){ + $this->companyName = config('app.name'); + } - // TODO - Make sure config('app.name') works below. $g2faUrl = $google2fa->getQRCodeUrl( - config('app.name'), - $user->email, + $this->companyName, + (string)$user->email, $secret_key ); diff --git a/src/Http/Controllers/SocialController.php b/src/Http/Controllers/SocialController.php index 8fb629a..5bb2094 100644 --- a/src/Http/Controllers/SocialController.php +++ b/src/Http/Controllers/SocialController.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Request; +use Illuminate\Http\RedirectResponse; use Laravel\Socialite\Facades\Socialite; class SocialController @@ -18,7 +19,7 @@ public function __construct() } - public function redirect(Request $request, $driver) + public function redirect(Request $request, string $driver): RedirectResponse { $this->dynamicallySetSocialProviderCredentials($driver); diff --git a/src/Models/User.php b/src/Models/User.php index 5cbb516..8438053 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -7,12 +7,20 @@ use Illuminate\Foundation\Auth\User as Authenticatable; use PragmaRX\Google2FA\Google2FA; +/** + * Class User + * + * @property string|null $email + * @property string|null $two_factor_secret + * @property string|null $two_factor_recovery_codes + * @property \Illuminate\Support\Carbon|null $two_factor_confirmed_at + */ class User extends Authenticatable implements MustVerifyEmail { use HasSocialProviders; protected $fillable = [ - 'name', 'email', 'password', 'two_factor_secret', 'two_factor_recovery_codes', + 'name', 'email', 'password', 'two_factor_secret', 'two_factor_recovery_codes', 'two_factor_confirmed_at' ]; public function hasVerifiedEmail()