Skip to content

Commit c5a893a

Browse files
committed
updated totp tutorial
Signed-off-by: bidi <bidi@apidemia.com>
1 parent 86881f4 commit c5a893a

6 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public const VALIDATOR_INVALID_CODE = 'Invalid recovery code.'
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'admin::validate-totp-form' => ['authenticated'],
2+
'admin::disable-totp-form' => ['authenticated'],
3+
'admin::enable-totp-form' => ['authenticated'],
4+
'admin::recovery-form' => ['authenticated'],
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Admin\Admin\InputFilter;
6+
7+
use Admin\App\InputFilter\Input\CsrfInput;
8+
use Core\App\InputFilter\AbstractInputFilter;
9+
10+
/**
11+
* @phpstan-type RecoveryDataType array{
12+
* code: non-empty-string,
13+
* totpCsrf: non-empty-string,
14+
* submit?: non-empty-string,
15+
* }
16+
* @extends AbstractInputFilter<RecoveryDataType>
17+
*/
18+
class RecoveryInputFilter extends AbstractInputFilter
19+
{
20+
public function init(): void
21+
{
22+
$this->add([
23+
'name' => 'recoveryCode',
24+
'required' => true,
25+
'filters' => [
26+
['name' => 'StringTrim'],
27+
],
28+
'validators' => [
29+
[
30+
'name' => 'Regex',
31+
'options' => [
32+
'pattern' => '/^[A-Z0-9]{5}-[A-Z0-9]{5}$/',
33+
'message' => 'Recovery code must be in format XXXXX-XXXXX using letters A-Z and digits 0-9.',
34+
],
35+
],
36+
],
37+
]);
38+
39+
$this->add(new CsrfInput('recoveryCsrf'));
40+
}
41+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Admin\Admin\InputFilter;
6+
7+
use Admin\App\InputFilter\Input\CsrfInput;
8+
use Core\App\InputFilter\AbstractInputFilter;
9+
use Laminas\Validator\Digits;
10+
use Laminas\Validator\StringLength;
11+
12+
/**
13+
* @phpstan-type TotpDataType array{
14+
* code: non-empty-string,
15+
* totpCsrf: non-empty-string,
16+
* submit?: non-empty-string,
17+
* }
18+
* @extends AbstractInputFilter<TotpDataType>
19+
*/
20+
class TotpInputFilter extends AbstractInputFilter
21+
{
22+
public function init(): void
23+
{
24+
$this->add([
25+
'name' => 'code',
26+
'required' => true,
27+
'filters' => [
28+
['name' => 'StringTrim'],
29+
],
30+
'validators' => [
31+
[
32+
'name' => Digits::class,
33+
'options' => [
34+
'message' => 'Code must contain only digits.',
35+
],
36+
],
37+
[
38+
'name' => StringLength::class,
39+
'options' => [
40+
'min' => 6,
41+
'max' => 6,
42+
'message' => 'Code must be exactly 6 digits.',
43+
],
44+
],
45+
],
46+
]);
47+
48+
$this->add(new CsrfInput('totpCsrf'));
49+
}
50+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
5+
</head>
6+
<body class="app">
7+
<div class="d-flex justify-content-center align-items-center vh-100">
8+
<div class="text-center">
9+
<div class="mb-3">
10+
<h2 class="mx-3">Recovery codes</h2>
11+
12+
{% if plainCodes|length > 0 %}
13+
<div class="alert alert-info">
14+
<p>Save these recovery codes. Each code can be used only once:</p>
15+
<ul>
16+
{% for code in plainCodes %}
17+
<li>{{ code }}</li>
18+
{% endfor %}
19+
</ul>
20+
</div>
21+
{% endif %}
22+
23+
<div class="d-flex flex-column align-items-center mt-3">
24+
<a href="{{ path('dashboard::view-dashboard') }}" class="btn btn-secondary mt-2">Ok</a>
25+
</div>
26+
</div>
27+
</div>
28+
</div>
29+
</body>
30+
</html>

docs/book/v7/tutorials/install-dot-totp.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ If you follow the links from the [main totp integration example](https://github.
2323
- [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)
2424
- [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)
2525
- [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)
26+
- [src/Admin/src/InputFilter/RecoveryInputFilter.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/InputFilter/RecoveryInputFilter.php)
27+
- [src/Admin/src/InputFilter/TotpInputFilter.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/Admin/src/InputFilter/TotpInputFilter.php)
2628
- [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)
2729
- [src/App/src/Middleware/CancelUrlMiddleware.php](https://github.com/dotkernel/admin-documentation/tree/main/code_examples/totp/src/App/src/Middleware/CancelUrlMiddleware.php)
2830
- [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 +39,12 @@ There are still some code snippets in the [_misc](https://github.com/dotkernel/a
3739
- [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.
3840
- [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);`.
3941
- [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.
42+
- [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.
43+
- Add the constant below in `src/Core/src/App/src/Message.php` to return an error message when the recovery code is invalid.
44+
45+
```php
46+
public const VALIDATOR_INVALID_CODE = 'Invalid recovery code.'
47+
```
4048

4149
## Dot-totp in Action
4250

0 commit comments

Comments
 (0)