From 1f2b263413096249abd2569bf772e3841ba282c7 Mon Sep 17 00:00:00 2001 From: Marco Raddatz Date: Sat, 20 Apr 2024 00:12:39 +0200 Subject: [PATCH] Renamed package from PIN to TOTP --- .github/ISSUE_TEMPLATE/config.yml | 6 +- README.md | 48 ++-- composer.json | 12 +- config/{pin-login.php => totp-login.php} | 36 +-- ..._150000_add_pin_columns_to_users_table.php | 26 -- ...150000_add_totp_columns_to_users_table.php | 26 ++ .../add_pin_columns_to_users_table.php.stub | 26 -- .../add_totp_columns_to_users_table.php.stub | 26 ++ resources/lang/de/controller.php | 6 +- resources/lang/de/notification.php | 12 +- resources/lang/en/controller.php | 6 +- resources/lang/en/notification.php | 4 +- .../views/{pin.blade.php => code.blade.php} | 34 +-- resources/views/identifier.blade.php | 10 +- resources/views/notification.blade.php | 2 +- routes/web.php | 20 +- ...lePinRequest.php => HandleCodeRequest.php} | 22 +- src/Controllers/HandleIdentifierRequest.php | 18 +- src/Controllers/ShowCodeForm.php | 22 ++ src/Controllers/ShowIdentifierForm.php | 4 +- src/Controllers/ShowPinForm.php | 22 -- ...LoggedInViaPin.php => LoggedInViaTotp.php} | 4 +- ...uestViaPin.php => LoginRequestViaTotp.php} | 4 +- src/Exceptions/MissingCode.php | 9 + src/Exceptions/MissingPin.php | 9 - src/Exceptions/MissingSessionInformation.php | 2 +- ...oginPin.php => CreateAndSendLoginCode.php} | 20 +- .../{ResetLoginPin.php => ResetLoginCode.php} | 6 +- src/Models/User.php | 5 +- .../{LoginPin.php => LoginCode.php} | 26 +- src/Requests/BaseRequest.php | 14 +- .../{PinRequest.php => CodeRequest.php} | 54 ++--- src/Requests/IdentifierRequest.php | 16 +- ...vider.php => TotpLoginServiceProvider.php} | 8 +- .../Controllers/HandleCodeRequestTest.php | 223 ++++++++++++++++++ .../HandleIdentifierRequestTest.php | 44 ++-- .../Controllers/HandlePinRequestTest.php | 223 ------------------ ...owPinFormTest.php => ShowCodeFormTest.php} | 12 +- .../Controllers/ShowIdentifierFormTest.php | 6 +- tests/Feature/Jobs/ResetLoginCodeTest.php | 32 +++ tests/Feature/Jobs/ResetLoginPinTest.php | 32 --- tests/Feature/Jobs/SendLoginCodeTest.php | 38 +++ tests/Feature/Jobs/SendLoginPinTest.php | 38 --- tests/TestbenchTestCase.php | 19 +- 44 files changed, 619 insertions(+), 613 deletions(-) rename config/{pin-login.php => totp-login.php} (74%) delete mode 100644 database/migrations/2023_06_23_150000_add_pin_columns_to_users_table.php create mode 100644 database/migrations/2023_06_23_150000_add_totp_columns_to_users_table.php delete mode 100644 database/migrations/add_pin_columns_to_users_table.php.stub create mode 100644 database/migrations/add_totp_columns_to_users_table.php.stub rename resources/views/{pin.blade.php => code.blade.php} (88%) rename src/Controllers/{HandlePinRequest.php => HandleCodeRequest.php} (50%) create mode 100644 src/Controllers/ShowCodeForm.php delete mode 100644 src/Controllers/ShowPinForm.php rename src/Events/{LoggedInViaPin.php => LoggedInViaTotp.php} (84%) rename src/Events/{LoginRequestViaPin.php => LoginRequestViaTotp.php} (83%) create mode 100644 src/Exceptions/MissingCode.php delete mode 100644 src/Exceptions/MissingPin.php rename src/Jobs/{CreateAndSendLoginPin.php => CreateAndSendLoginCode.php} (55%) rename src/Jobs/{ResetLoginPin.php => ResetLoginCode.php} (69%) rename src/Notifications/{LoginPin.php => LoginCode.php} (53%) rename src/Requests/{PinRequest.php => CodeRequest.php} (53%) rename src/{PinLoginServiceProvider.php => TotpLoginServiceProvider.php} (63%) create mode 100644 tests/Feature/Controllers/HandleCodeRequestTest.php delete mode 100644 tests/Feature/Controllers/HandlePinRequestTest.php rename tests/Feature/Controllers/{ShowPinFormTest.php => ShowCodeFormTest.php} (54%) create mode 100644 tests/Feature/Jobs/ResetLoginCodeTest.php delete mode 100644 tests/Feature/Jobs/ResetLoginPinTest.php create mode 100644 tests/Feature/Jobs/SendLoginCodeTest.php delete mode 100644 tests/Feature/Jobs/SendLoginPinTest.php diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 354448a..b581797 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,11 +1,11 @@ blank_issues_enabled: false contact_links: - name: Ask a question - url: https://github.com/empuxa/laravel-pin-login/discussions/new?category=q-a + url: https://github.com/empuxa/laravel-totp-login/discussions/new?category=q-a about: Ask the community for help - name: Request a feature - url: https://github.com/empuxa/laravel-pin-login/discussions/new?category=ideas + url: https://github.com/empuxa/laravel-totp-login/discussions/new?category=ideas about: Share ideas for new features - name: Report a security issue - url: https://github.com/empuxa/laravel-pin-login/security/policy + url: https://github.com/empuxa/laravel-totp-login/security/policy about: Learn how to notify us for sensitive bugs diff --git a/README.md b/README.md index d322df9..f39b254 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,22 @@ -# Laravel PIN Login +# Laravel TOTP Login -[![Latest Version on Packagist](https://img.shields.io/packagist/v/empuxa/laravel-pin-login.svg?style=flat-square)](https://packagist.org/packages/empuxa/laravel-pin-login) -[![Tests](https://img.shields.io/github/actions/workflow/status/empuxa/laravel-pin-login/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/empuxa/laravel-pin-login/actions/workflows/run-tests.yml) -[![Total Downloads](https://img.shields.io/packagist/dt/empuxa/laravel-pin-login.svg?style=flat-square)](https://packagist.org/packages/empuxa/laravel-pin-login) +[![Latest Version on Packagist](https://img.shields.io/packagist/v/empuxa/laravel-totp-login.svg?style=flat-square)](https://packagist.org/packages/empuxa/laravel-totp-login) +[![Tests](https://img.shields.io/github/actions/workflow/status/empuxa/laravel-totp-login/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/empuxa/laravel-totp-login/actions/workflows/run-tests.yml) +[![Total Downloads](https://img.shields.io/packagist/dt/empuxa/laravel-totp-login.svg?style=flat-square)](https://packagist.org/packages/empuxa/laravel-totp-login) -![Banner](https://banners.beyondco.de/Laravel%20PIN%20Login.png?theme=light&packageManager=composer+require&packageName=empuxa%2Fpin-login&pattern=architect&style=style_1&description=Goodbye+passwords%21&md=1&showWatermark=0&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg) +![Banner](https://banners.beyondco.de/Laravel%20TOTP%20Login.png?theme=light&packageManager=composer+require&packageName=empuxa%2Ftotp-login&pattern=architect&style=style_1&description=Goodbye+passwords%21&md=1&showWatermark=0&fontSize=100px&images=https%3A%2F%2Flaravel.com%2Fimg%2Flogomark.min.svg) -Say goodbye to passwords and sign in via PIN instead! -Laravel PIN Login is a convenient package that allows you to easily add a PIN login feature to your Laravel application. +Say goodbye to passwords and sign in via a time-based one-time password instead! +Laravel TOTP Login is a convenient package that allows you to easily add a TOTP login feature to your Laravel application. -## Why Choose Laravel PIN Login? -You might wonder why you should opt for a PIN login instead of a magic link solution. Well, this package is designed to complement the existing login methods in your application. It provides an alternative sign-in option for users who haven't set a password yet or don't have an email address. For instance, users who signed up with only a phone number can still enjoy the benefits of secure login through a PIN. +## Why Choose Laravel TOTP Login? +You might wonder why you should opt for a TOTP login instead of a magic link solution. +Well, this package is designed to complement the existing login methods in your application. +It provides an alternative sign-in option for users who haven't set a password yet or don't have an email address. +For instance, users who signed up with only a phone number can still enjoy the benefits of secure login through a TOTP. ## Features -- Simplified sign-in process using a PIN +- Simplified sign-in process using a TOTP - Compatibility with existing login methods - Support for users without passwords or email addresses @@ -31,13 +34,13 @@ Also, you need to have a notifiable user model. Install the package via composer: ```bash -composer require empuxa/laravel-pin-login +composer require empuxa/laravel-totp-login ``` -Copy the vendor files and adjust the config file `config/pin-login.php` to your needs: +Copy the vendor files and adjust the config file `config/totp-login.php` to your needs: ```bash -php artisan vendor:publish --provider="Empuxa\PinLogin\PinLoginServiceProvider" +php artisan vendor:publish --provider="Empuxa\TotpLogin\TotpLoginServiceProvider" ``` Run the migrations: @@ -47,37 +50,38 @@ php artisan migrate ``` That's it! -You're ready to start using the PIN login feature in your Laravel application. +You're ready to start using the TOTP login feature in your Laravel application. ## Usage The sign-in process for this repository involves three steps: -1. Enter the user's email address, phone number, or any other specified identifier, and request a PIN. -2. If the entered information is valid, a PIN will be sent to the user. You may need to customize the notification channel based on the user model you are using. -3. Enter the received PIN to log in the user. +1. Enter the user's email address, phone number, or any other specified identifier, and request a TOTP. +2. If the entered information is valid, a TOTP will be sent to the user. You may need to customize the notification channel based on the user model you are using. +3. Enter the received TOTP to log in the user. ### Customizing the Views While the initial steps are relatively straightforward, it's now necessary to customize the views. -These views have been designed to be as simple as possible (some might even consider them "ugly") and can be located in the `resources/views/vendor/pin-login` directory. +These views have been designed to be as simple as possible (some might even consider them "ugly") and can be located in the `resources/views/vendor/totp-login` directory. *Why are they not visually appealing?* Different applications adopt various layouts and frameworks. Since you have the most knowledge about your application, you can change the views to suit your specific requirements. ### Modifying the Notification -Within the copied views, you will come across a notification that's sent to the user. +Within the copied views, you will come across a notification sent to the user. You may want to make adjustments to this notification to align it with your preferences and needs. #### Different Notification Channels -If you plan on utilizing SMS or similar as your preferred notification channel, you have the option to create a custom notification class. -The PIN and the user's IP address will be passed to the constructor of this class. Finally, replace the default notification class within the `config/pin-login.php` file with your custom notification. +If you plan on using SMS or similar as your preferred notification channel, you can create a custom notification class. +The TOTP and the user's IP address will be passed to the constructor of this class. +Finally, replace the default notification class within the `config/totp-login.php` file with your custom notification. ### Custom User Model Scope To apply a scope to your user model, add the following method to your model: ```php -public static function pinLoginScope(): Builder +public static function totpLoginScope(): Builder { return self::yourGlobalScope(); } diff --git a/composer.json b/composer.json index f278e17..32c325b 100644 --- a/composer.json +++ b/composer.json @@ -1,12 +1,12 @@ { - "name": "empuxa/laravel-pin-login", + "name": "empuxa/laravel-totp-login", "description": "Say goodbye to passwords and sign in via PIN instead.", "keywords": [ "empuxa", - "pin-login", + "totp-login", "passwordless" ], - "homepage": "https://github.com/empuxa/laravel-pin-login", + "homepage": "https://github.com/empuxa/laravel-totp-login", "license": "MIT", "authors": [ { @@ -33,12 +33,12 @@ }, "autoload": { "psr-4": { - "Empuxa\\PinLogin\\": "src" + "Empuxa\\TotpLogin\\": "src" } }, "autoload-dev": { "psr-4": { - "Empuxa\\PinLogin\\Tests\\": "tests" + "Empuxa\\TotpLogin\\Tests\\": "tests" } }, "scripts": { @@ -55,7 +55,7 @@ "extra": { "laravel": { "providers": [ - "Empuxa\\PinLogin\\PinLoginServiceProvider" + "Empuxa\\TotpLogin\\TotpLoginServiceProvider" ] } }, diff --git a/config/pin-login.php b/config/totp-login.php similarity index 74% rename from config/pin-login.php rename to config/totp-login.php index 92b0c1f..4c0becc 100644 --- a/config/pin-login.php +++ b/config/totp-login.php @@ -9,9 +9,9 @@ /** * The notification to send to the user. - * Default: \Empuxa\PinLogin\Notifications\LoginPin::class + * Default: \Empuxa\TotpLogin\Notifications\LoginCode::class */ - 'notification' => \Empuxa\PinLogin\Notifications\LoginPin::class, + 'notification' => \Empuxa\TotpLogin\Notifications\LoginCode::class, 'columns' => [ /** @@ -19,19 +19,19 @@ * We will use this column to authenticate the user and to send the PIN to. * Default: 'email' */ - 'identifier' => 'email', + 'identifier' => 'email', /** * The column where the PIN is stored. - * Default: 'login_pin' + * Default: 'login_totp_code' */ - 'pin' => 'login_pin', + 'code' => 'login_totp_code', /** * The column where we store the information, how long the PIN is valid. - * Default: 'login_pin_valid_until' + * Default: 'login_totp_code_valid_until' */ - 'pin_valid_until' => 'login_pin_valid_until', + 'code_valid_until' => 'login_totp_code_valid_until', ], 'route' => [ @@ -71,7 +71,7 @@ 'enable_throttling' => true, ], - 'pin' => [ + 'code' => [ /** * The length of the PIN. * Keep in mind that longer PINs might break the layout. @@ -81,7 +81,7 @@ /** * The time in seconds after which the PIN expires. - * This is the information being stored in the `login_pin_valid_until` column. + * This is the information being stored in the `login_totp_code_valid_until` column. * Default: 600 */ 'expires_in' => 600, @@ -111,10 +111,10 @@ /** * Enable the "superpin" feature. * When enabled, any user can also sign in with the PIN of your choice on non-production environments. - * Set the environment variable `PIN_LOGIN_SUPERPIN` to the PIN you want to use. - * Default: env('PIN_LOGIN_SUPERPIN', false) + * Set the environment variable `TOTP_LOGIN_SUPERPIN` to the PIN you want to use. + * Default: env('TOTP_LOGIN_SUPERPIN', false) */ - 'superpin' => env('PIN_LOGIN_SUPERPIN', false), + 'superpin' => env('TOTP_LOGIN_SUPERPIN', false), /** * The redirect path after a successful login. @@ -124,21 +124,21 @@ 'events' => [ /** - * This event is fired when a user submits a PIN. - * Default: \Empuxa\PinLogin\Events\PinRequested::class + * This event is fired when a user submits a TOTP. + * Default: \Empuxa\TotpLogin\Events\PinRequested::class */ - 'login_request_via_pin' => \Empuxa\PinLogin\Events\LoginRequestViaPin::class, + 'login_request_via_totp' => \Empuxa\TotpLogin\Events\LoginRequestViaTotp::class, /** * This event is fired when a user was successfully logged in. - * Default: \Empuxa\PinLogin\Events\LoggedInViaPin::class + * Default: \Empuxa\TotpLogin\Events\LoggedInViaTotp::class */ - 'logged_in_via_pin' => \Empuxa\PinLogin\Events\LoggedInViaPin::class, + 'logged_in_via_totp' => \Empuxa\TotpLogin\Events\LoggedInViaTotp::class, /** * This event is fired when a user was successfully logged in. * Default: \Illuminate\Auth\Events\Lockout::class */ - 'lockout' => \Illuminate\Auth\Events\Lockout::class, + 'lockout' => \Illuminate\Auth\Events\Lockout::class, ], ]; diff --git a/database/migrations/2023_06_23_150000_add_pin_columns_to_users_table.php b/database/migrations/2023_06_23_150000_add_pin_columns_to_users_table.php deleted file mode 100644 index ccba5cd..0000000 --- a/database/migrations/2023_06_23_150000_add_pin_columns_to_users_table.php +++ /dev/null @@ -1,26 +0,0 @@ -getTable(), static function (Blueprint $table): void { - $table->string(config('pin-login.columns.pin'))->nullable(); - $table->timestamp(config('pin-login.columns.pin_valid_until'))->nullable(); - }); - } - - public function down(): void - { - Schema::table(app(config('pin-login.model'))->getTable(), static function (Blueprint $table): void { - $table->dropColumn([ - config('pin-login.columns.pin'), - config('pin-login.columns.pin_valid_until'), - ]); - }); - } -}; diff --git a/database/migrations/2023_06_23_150000_add_totp_columns_to_users_table.php b/database/migrations/2023_06_23_150000_add_totp_columns_to_users_table.php new file mode 100644 index 0000000..e4b26d8 --- /dev/null +++ b/database/migrations/2023_06_23_150000_add_totp_columns_to_users_table.php @@ -0,0 +1,26 @@ +getTable(), static function (Blueprint $table): void { + $table->string(config('totp-login.columns.code'))->nullable(); + $table->timestamp(config('totp-login.columns.code_valid_until'))->nullable(); + }); + } + + public function down(): void + { + Schema::table(app(config('totp-login.model'))->getTable(), static function (Blueprint $table): void { + $table->dropColumn([ + config('totp-login.columns.code'), + config('totp-login.columns.code_valid_until'), + ]); + }); + } +}; diff --git a/database/migrations/add_pin_columns_to_users_table.php.stub b/database/migrations/add_pin_columns_to_users_table.php.stub deleted file mode 100644 index ccba5cd..0000000 --- a/database/migrations/add_pin_columns_to_users_table.php.stub +++ /dev/null @@ -1,26 +0,0 @@ -getTable(), static function (Blueprint $table): void { - $table->string(config('pin-login.columns.pin'))->nullable(); - $table->timestamp(config('pin-login.columns.pin_valid_until'))->nullable(); - }); - } - - public function down(): void - { - Schema::table(app(config('pin-login.model'))->getTable(), static function (Blueprint $table): void { - $table->dropColumn([ - config('pin-login.columns.pin'), - config('pin-login.columns.pin_valid_until'), - ]); - }); - } -}; diff --git a/database/migrations/add_totp_columns_to_users_table.php.stub b/database/migrations/add_totp_columns_to_users_table.php.stub new file mode 100644 index 0000000..e4b26d8 --- /dev/null +++ b/database/migrations/add_totp_columns_to_users_table.php.stub @@ -0,0 +1,26 @@ +getTable(), static function (Blueprint $table): void { + $table->string(config('totp-login.columns.code'))->nullable(); + $table->timestamp(config('totp-login.columns.code_valid_until'))->nullable(); + }); + } + + public function down(): void + { + Schema::table(app(config('totp-login.model'))->getTable(), static function (Blueprint $table): void { + $table->dropColumn([ + config('totp-login.columns.code'), + config('totp-login.columns.code_valid_until'), + ]); + }); + } +}; diff --git a/resources/lang/de/controller.php b/resources/lang/de/controller.php index db06619..225c423 100644 --- a/resources/lang/de/controller.php +++ b/resources/lang/de/controller.php @@ -1,12 +1,12 @@ [ + 'handle_code_request' => [ 'success' => 'Login erfolgreich.', 'error' => [ 'rate_limit' => 'Zu viele falsche Anfragen. Ihre Account wurde für :seconds Sekunden geblockt.', - 'expired' => 'Der eingegebene PIN ist nicht mehr gültig. Wir haben Ihnen einen neuen PIN per E-Mail geschickt.', - 'wrong_pin' => 'Der PIN ist ungültig. Sie haben noch :attempts_left Versuche bis wir Ihren Account temporär blockieren.', + 'expired' => 'Der eingegebene Code ist nicht mehr gültig. Wir haben Ihnen einen neuen Code per E-Mail geschickt.', + 'wrong_totp' => 'Der Code ist ungültig. Sie haben noch :attempts_left Versuche bis wir Ihren Account temporär blockieren.', ], ], ]; diff --git a/resources/lang/de/notification.php b/resources/lang/de/notification.php index 3e7bbbc..70ddbb2 100644 --- a/resources/lang/de/notification.php +++ b/resources/lang/de/notification.php @@ -2,11 +2,11 @@ return [ 'mail' => [ - 'subject' => 'Your login PIN for :app', - 'greeting' => 'Hello :name,', - 'line-1' => 'There was a login request from :ip. Here is your PIN, which is valid until :valid_until:', - 'line-2' => "If it wasn't you: no reason to panic. You can find more information about our login process here: https://google.com", - 'line-3' => "Thank you for using our services. If you have any questions, please contact us. We're happy to help!", - 'cta' => 'Sign in now', + 'subject' => 'Ihr Anmeldecode für :app', + 'greeting' => 'Hallo :name,', + 'line-1' => 'Es gab eine Anmeldeanforderung von :ip. Hier ist Ihr Code, gültig bis :valid_until:', + 'line-2' => 'Falls Sie dies nicht waren: Kein Grund zur Panik. Weitere Informationen zu unserem Anmeldeverfahren finden Sie hier: https://google.com', + 'line-3' => 'Vielen Dank, dass Sie unsere Dienste nutzen. Bei Fragen stehen wir Ihnen gerne zur Verfügung!', + 'cta' => 'Jetzt anmelden', ], ]; diff --git a/resources/lang/en/controller.php b/resources/lang/en/controller.php index d1d82b7..9bb2f00 100644 --- a/resources/lang/en/controller.php +++ b/resources/lang/en/controller.php @@ -1,12 +1,12 @@ [ + 'handle_code_request' => [ 'success' => 'Successfully logged in.', 'error' => [ 'rate_limit' => 'Too many wrong requests. Your account is blocked for :seconds seconds.', - 'expired' => "The PIN isn't valid any longer. We've sent you a new mail.", - 'wrong_pin' => 'The PIN is wrong. You have :attempts_left more attempts until we temporarily block your account.', + 'expired' => "The code isn't valid any longer. We've sent you a new mail.", + 'wrong_totp' => 'The code is wrong. You have :attempts_left more attempts until we temporarily block your account.', ], ], ]; diff --git a/resources/lang/en/notification.php b/resources/lang/en/notification.php index 3e7bbbc..4c10883 100644 --- a/resources/lang/en/notification.php +++ b/resources/lang/en/notification.php @@ -2,9 +2,9 @@ return [ 'mail' => [ - 'subject' => 'Your login PIN for :app', + 'subject' => 'Your login code for :app', 'greeting' => 'Hello :name,', - 'line-1' => 'There was a login request from :ip. Here is your PIN, which is valid until :valid_until:', + 'line-1' => 'There was a login request from :ip. Here is your code, which is valid until :valid_until:', 'line-2' => "If it wasn't you: no reason to panic. You can find more information about our login process here: https://google.com", 'line-3' => "Thank you for using our services. If you have any questions, please contact us. We're happy to help!", 'cta' => 'Sign in now', diff --git a/resources/views/pin.blade.php b/resources/views/code.blade.php similarity index 88% rename from resources/views/pin.blade.php rename to resources/views/code.blade.php index f3aadc8..2e4173e 100644 --- a/resources/views/pin.blade.php +++ b/resources/views/code.blade.php @@ -5,30 +5,30 @@ - Enter your PIN + Enter your code

- Enter your PIN + Enter your code

Blabla …

-
+ @csrf -
- @error ('pin') +
+ @error ('code')
@@ -48,15 +48,15 @@
@enderror - {{-- PIN inputs --}} -
-