From 4bb3e70f1fe97a1bf2a1698e793d32812849d481 Mon Sep 17 00:00:00 2001 From: f4ilji Date: Thu, 21 Nov 2024 21:22:39 +0500 Subject: [PATCH] Changes --- app/Actions/Fortify/CreateNewUser.php | 40 +++++ .../Fortify/PasswordValidationRules.php | 18 ++ app/Actions/Fortify/ResetUserPassword.php | 29 ++++ app/Providers/FortifyServiceProvider.php | 46 +++++ config/filament-2fa.php | 86 ++++++++++ config/fortify.php | 159 ++++++++++++++++++ 6 files changed, 378 insertions(+) create mode 100644 app/Actions/Fortify/CreateNewUser.php create mode 100644 app/Actions/Fortify/PasswordValidationRules.php create mode 100644 app/Actions/Fortify/ResetUserPassword.php create mode 100644 app/Providers/FortifyServiceProvider.php create mode 100644 config/filament-2fa.php create mode 100644 config/fortify.php diff --git a/app/Actions/Fortify/CreateNewUser.php b/app/Actions/Fortify/CreateNewUser.php new file mode 100644 index 0000000..7bf18d0 --- /dev/null +++ b/app/Actions/Fortify/CreateNewUser.php @@ -0,0 +1,40 @@ + $input + */ + public function create(array $input): User + { + Validator::make($input, [ + 'name' => ['required', 'string', 'max:255'], + 'email' => [ + 'required', + 'string', + 'email', + 'max:255', + Rule::unique(User::class), + ], + 'password' => $this->passwordRules(), + ])->validate(); + + return User::create([ + 'name' => $input['name'], + 'email' => $input['email'], + 'password' => Hash::make($input['password']), + ]); + } +} diff --git a/app/Actions/Fortify/PasswordValidationRules.php b/app/Actions/Fortify/PasswordValidationRules.php new file mode 100644 index 0000000..76b19d3 --- /dev/null +++ b/app/Actions/Fortify/PasswordValidationRules.php @@ -0,0 +1,18 @@ +|string> + */ + protected function passwordRules(): array + { + return ['required', 'string', Password::default(), 'confirmed']; + } +} diff --git a/app/Actions/Fortify/ResetUserPassword.php b/app/Actions/Fortify/ResetUserPassword.php new file mode 100644 index 0000000..7a57c50 --- /dev/null +++ b/app/Actions/Fortify/ResetUserPassword.php @@ -0,0 +1,29 @@ + $input + */ + public function reset(User $user, array $input): void + { + Validator::make($input, [ + 'password' => $this->passwordRules(), + ])->validate(); + + $user->forceFill([ + 'password' => Hash::make($input['password']), + ])->save(); + } +} diff --git a/app/Providers/FortifyServiceProvider.php b/app/Providers/FortifyServiceProvider.php new file mode 100644 index 0000000..2d741e3 --- /dev/null +++ b/app/Providers/FortifyServiceProvider.php @@ -0,0 +1,46 @@ +input(Fortify::username())).'|'.$request->ip()); + + return Limit::perMinute(5)->by($throttleKey); + }); + + RateLimiter::for('two-factor', function (Request $request) { + return Limit::perMinute(5)->by($request->session()->get('login.id')); + }); + } +} diff --git a/config/filament-2fa.php b/config/filament-2fa.php new file mode 100644 index 0000000..17c9c49 --- /dev/null +++ b/config/filament-2fa.php @@ -0,0 +1,86 @@ + [ +// TwoFactorType::authenticator, + TwoFactorType::email, + // TwoFactorType::phone, + ], + + 'enabled_features' => [ + /* + |-------------------------------------------------------------------------- + | Register + |-------------------------------------------------------------------------- + | + | This value determines whether users may register in the application. + | + */ + 'register' => true, + + /* + |-------------------------------------------------------------------------- + | Tenant + |-------------------------------------------------------------------------- + | + | Set to true if you're using Filament in a multi-tenant setup. If true, you + | need to manually set the user menu item for the two factor authentication + | page panel class. Take a look at the documentation for more information. + | + */ + 'multi_tenancy' => false, + ], + + /* + |-------------------------------------------------------------------------- + | SMS Service + |-------------------------------------------------------------------------- + | + | To use an SMS service, you need to install the corresponding package. + | You then have to create a App\Notifications\SendOTP class that extends + | the Vormkracht10\TwoFactorAuth\Notifications\SendOTP class. After that, + | you can set the class alias in the sms_service key. + | + */ + 'sms_service' => null, // For example 'vonage', 'twilio', 'nexmo', etc. + 'send_otp_class' => null, + 'phone_number_field' => 'phone', + + /* + |-------------------------------------------------------------------------- + | Class Aliases + |-------------------------------------------------------------------------- + | + | If you want to customize the pages, you can override the used classes here. + | Make your that your classes extend the original classes. + | + */ + 'login' => Login::class, + 'register' => Register::class, + 'challenge' => LoginTwoFactor::class, + 'two_factor_settings' => TwoFactor::class, + 'password_reset' => PasswordReset::class, + 'password_confirmation' => PasswordConfirmation::class, + 'request_password_reset' => RequestPasswordReset::class, +]; diff --git a/config/fortify.php b/config/fortify.php new file mode 100644 index 0000000..cfe8272 --- /dev/null +++ b/config/fortify.php @@ -0,0 +1,159 @@ + 'web', + + /* + |-------------------------------------------------------------------------- + | Fortify Password Broker + |-------------------------------------------------------------------------- + | + | Here you may specify which password broker Fortify can use when a user + | is resetting their password. This configured value should match one + | of your password brokers setup in your "auth" configuration file. + | + */ + + 'passwords' => 'users', + + /* + |-------------------------------------------------------------------------- + | Username / Email + |-------------------------------------------------------------------------- + | + | This value defines which model attribute should be considered as your + | application's "username" field. Typically, this might be the email + | address of the users but you are free to change this value here. + | + | Out of the box, Fortify expects forgot password and reset password + | requests to have a field named 'email'. If the application uses + | another name for the field you may define it below as needed. + | + */ + + 'username' => 'email', + + 'email' => 'email', + + /* + |-------------------------------------------------------------------------- + | Lowercase Usernames + |-------------------------------------------------------------------------- + | + | This value defines whether usernames should be lowercased before saving + | them in the database, as some database system string fields are case + | sensitive. You may disable this for your application if necessary. + | + */ + + 'lowercase_usernames' => true, + + /* + |-------------------------------------------------------------------------- + | Home Path + |-------------------------------------------------------------------------- + | + | Here you may configure the path where users will get redirected during + | authentication or password reset when the operations are successful + | and the user is authenticated. You are free to change this value. + | + */ + + 'home' => '/home', + + /* + |-------------------------------------------------------------------------- + | Fortify Routes Prefix / Subdomain + |-------------------------------------------------------------------------- + | + | Here you may specify which prefix Fortify will assign to all the routes + | that it registers with the application. If necessary, you may change + | subdomain under which all of the Fortify routes will be available. + | + */ + + 'prefix' => '', + + 'domain' => null, + + /* + |-------------------------------------------------------------------------- + | Fortify Routes Middleware + |-------------------------------------------------------------------------- + | + | Here you may specify which middleware Fortify will assign to the routes + | that it registers with the application. If necessary, you may change + | these middleware but typically this provided default is preferred. + | + */ + + 'middleware' => ['web'], + + /* + |-------------------------------------------------------------------------- + | Rate Limiting + |-------------------------------------------------------------------------- + | + | By default, Fortify will throttle logins to five requests per minute for + | every email and IP address combination. However, if you would like to + | specify a custom rate limiter to call then you may specify it here. + | + */ + + 'limiters' => [ + 'login' => 'login', + 'two-factor' => 'two-factor', + ], + + /* + |-------------------------------------------------------------------------- + | Register View Routes + |-------------------------------------------------------------------------- + | + | Here you may specify if the routes returning views should be disabled as + | you may not need them when building your own application. This may be + | especially true if you're writing a custom single-page application. + | + */ + + 'views' => true, + + /* + |-------------------------------------------------------------------------- + | Features + |-------------------------------------------------------------------------- + | + | Some of the Fortify features are optional. You may disable the features + | by removing them from this array. You're free to only remove some of + | these features or you can even remove all of these if you need to. + | + */ + + 'features' => [ + Features::registration(), + Features::resetPasswords(), + // Features::emailVerification(), + Features::updateProfileInformation(), + Features::updatePasswords(), + Features::twoFactorAuthentication([ + 'confirm' => true, + 'confirmPassword' => true, + // 'window' => 0, + ]), + ], + +];