diff --git a/code_examples/totp/_misc/totp-append-Message.php b/code_examples/totp/_misc/totp-append-Message.php new file mode 100644 index 0000000..caf434d --- /dev/null +++ b/code_examples/totp/_misc/totp-append-Message.php @@ -0,0 +1 @@ +public const VALIDATOR_INVALID_CODE = 'Invalid recovery code.' diff --git a/code_examples/totp/_misc/totp-append-authorization-guards.global.php b/code_examples/totp/_misc/totp-append-authorization-guards.global.php new file mode 100644 index 0000000..5d34e89 --- /dev/null +++ b/code_examples/totp/_misc/totp-append-authorization-guards.global.php @@ -0,0 +1,4 @@ +'admin::validate-totp-form' => ['authenticated'], +'admin::disable-totp-form' => ['authenticated'], +'admin::enable-totp-form' => ['authenticated'], +'admin::recovery-form' => ['authenticated'], diff --git a/code_examples/totp/src/Admin/src/InputFilter/RecoveryInputFilter.php b/code_examples/totp/src/Admin/src/InputFilter/RecoveryInputFilter.php new file mode 100644 index 0000000..43e2a66 --- /dev/null +++ b/code_examples/totp/src/Admin/src/InputFilter/RecoveryInputFilter.php @@ -0,0 +1,41 @@ + + */ +class RecoveryInputFilter extends AbstractInputFilter +{ + public function init(): void + { + $this->add([ + 'name' => 'recoveryCode', + 'required' => true, + 'filters' => [ + ['name' => 'StringTrim'], + ], + 'validators' => [ + [ + 'name' => 'Regex', + 'options' => [ + 'pattern' => '/^[A-Z0-9]{5}-[A-Z0-9]{5}$/', + 'message' => 'Recovery code must be in format XXXXX-XXXXX using letters A-Z and digits 0-9.', + ], + ], + ], + ]); + + $this->add(new CsrfInput('recoveryCsrf')); + } +} diff --git a/code_examples/totp/src/Admin/src/InputFilter/TotpInputFilter.php b/code_examples/totp/src/Admin/src/InputFilter/TotpInputFilter.php new file mode 100644 index 0000000..7f467f9 --- /dev/null +++ b/code_examples/totp/src/Admin/src/InputFilter/TotpInputFilter.php @@ -0,0 +1,50 @@ + + */ +class TotpInputFilter extends AbstractInputFilter +{ + public function init(): void + { + $this->add([ + 'name' => 'code', + 'required' => true, + 'filters' => [ + ['name' => 'StringTrim'], + ], + 'validators' => [ + [ + 'name' => Digits::class, + 'options' => [ + 'message' => 'Code must contain only digits.', + ], + ], + [ + 'name' => StringLength::class, + 'options' => [ + 'min' => 6, + 'max' => 6, + 'message' => 'Code must be exactly 6 digits.', + ], + ], + ], + ]); + + $this->add(new CsrfInput('totpCsrf')); + } +} diff --git a/code_examples/totp/src/Admin/templates/admin/list-recovery-codes.html.twig b/code_examples/totp/src/Admin/templates/admin/list-recovery-codes.html.twig new file mode 100644 index 0000000..dc672bc --- /dev/null +++ b/code_examples/totp/src/Admin/templates/admin/list-recovery-codes.html.twig @@ -0,0 +1,30 @@ + + + + + + +
+
+
+

Recovery codes

+ + {% if plainCodes|length > 0 %} +
+

Save these recovery codes. Each code can be used only once:

+
    + {% for code in plainCodes %} +
  • {{ code }}
  • + {% endfor %} +
+
+ {% endif %} + +
+ Ok +
+
+
+
+ + diff --git a/docs/book/v7/tutorials/install-dot-totp.md b/docs/book/v7/tutorials/install-dot-totp.md index ca1ae73..ff2ce4e 100644 --- a/docs/book/v7/tutorials/install-dot-totp.md +++ b/docs/book/v7/tutorials/install-dot-totp.md @@ -23,7 +23,11 @@ If you follow the links from the [main totp integration example](https://github. - [src/Admin/src/Handler/Account/PostEnableTotpHandler.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/Handler/Account/PostEnableTotpHandler.php) - [src/Admin/src/Handler/Account/PostValidateRecoveryHandler.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/Handler/Account/PostValidateRecoveryHandler.php) - [src/Admin/src/Handler/Account/PostValidateTotpHandler.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/Handler/Account/PostValidateTotpHandler.php) +- [src/Admin/src/InputFilter/RecoveryInputFilter.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/InputFilter/RecoveryInputFilter.php) +- [src/Admin/src/InputFilter/TotpInputFilter.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/InputFilter/TotpInputFilter.php) +- [src/Admin/templates/admin/list-recovery-codes.html.twig](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/templates/admin/list-recovery-codes.html.twig) - [src/Admin/templates/admin/recovery-form.html.twig](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/templates/admin/recovery-form.html.twig) +- [src/Admin/templates/admin/validate-totp-form.html.twig](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/templates/admin/validate-totp-form.html.twig) - [src/App/src/Middleware/CancelUrlMiddleware.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/App/src/Middleware/CancelUrlMiddleware.php) - [src/App/src/Middleware/TotpMiddleware.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/App/src/Middleware/TotpMiddleware.php) @@ -37,6 +41,12 @@ There are still some code snippets in the [_misc](https://github.com/dotkernel/a - [the routes updates](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/_misc/totp-append-routes.php) must be added in the `src/Admin/src/RoutesDelegator.php` file. - [the pipeline updates](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/_misc/totp-append-Pipeline.php) must be added in the `config/pipeline.php` file after `$app->pipe(AuthMiddleware::class);`. - [the ConfigProvider updates](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/_misc/totp-append-ConfigProvider.php) must be added in the `src/Admin/src/ConfigProvider.php` file. +- [append these routes](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/_misc/totp-append-authorization-guards.global.php) to your `authorization-guards.global.php` file. +- Add the constant below in `src/Core/src/App/src/Message.php` to return an error message when the recovery code is invalid. + +```php +public const VALIDATOR_INVALID_CODE = 'Invalid recovery code.' +``` ## Dot-totp in Action