File tree Expand file tree Collapse file tree
library/Icinga/Web/Form/Validator Expand file tree Collapse file tree Original file line number Diff line number Diff line change 77use Icinga \Authentication \TwoFactorTotp ;
88use Icinga \Common \Database ;
99use Icinga \User ;
10+ use Icinga \Web \Form \Validator \TotpTokenValidator ;
1011use Icinga \Web \Notification ;
1112use ipl \Html \Attributes ;
1213use ipl \Html \HtmlElement ;
13- use ipl \Validator \CallbackValidator ;
1414use ipl \Web \Common \FormUid ;
1515use ipl \Web \Compat \CompatForm ;
1616use ipl \Web \Url ;
@@ -124,27 +124,15 @@ protected function assemble(): void
124124 );
125125
126126 $ this ->addElement (
127- 'number ' ,
127+ 'text ' ,
128128 '2fa_verification_token ' ,
129129 [
130+ 'required ' => true ,
130131 'label ' => $ this ->translate ('Verification Token ' ),
131132 'description ' => $ this ->translate (
132133 'Please enter the token from your authenticator app to verify your setup. '
133134 ),
134- 'min ' => 0 ,
135- 'max ' => 999999 ,
136- 'step ' => 1 ,
137- 'validators ' => [
138- new CallbackValidator (function (string $ value , CallbackValidator $ validator ) {
139- if (strlen ($ value ) !== 6 ) {
140- $ validator ->addMessage ($ this ->translate ('The token must be exactly 6 digits long. ' ));
141-
142- return false ;
143- }
144-
145- return true ;
146- })
147- ]
135+ 'validators ' => [new TotpTokenValidator ()]
148136 ]
149137 );
150138
Original file line number Diff line number Diff line change 1111use Icinga \Authentication \Auth ;
1212use Icinga \Authentication \TwoFactorTotp ;
1313use Icinga \Common \Database ;
14+ use Icinga \Web \Form \Validator \TotpTokenValidator ;
1415use Icinga \Web \Response ;
1516use Icinga \Web \Session ;
1617use Icinga \Web \Url ;
@@ -62,7 +63,8 @@ protected function assemble(): void
6263 'decorators ' => [
6364 'RenderElement ' => new RenderElementDecorator (),
6465 'Errors ' => ['name ' => 'Errors ' , 'options ' => ['class ' => 'errors ' ]]
65- ]
66+ ],
67+ 'validators ' => [new TotpTokenValidator ()]
6668 ]
6769 );
6870
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ /* Icinga Web 2 | (c) 2025 Icinga GmbH | GPLv2+ */
4+
5+ namespace Icinga \Web \Form \Validator ;
6+
7+ use ipl \I18n \Translation ;
8+ use ipl \Validator \BaseValidator ;
9+
10+ class TotpTokenValidator extends BaseValidator
11+ {
12+ use Translation;
13+
14+ public function __construct (
15+ /** @var int Token length */
16+ protected int $ digits = 6
17+ ) {
18+ }
19+
20+ public function isValid ($ value ): bool
21+ {
22+ // Multiple isValid() calls must not stack validation messages
23+ $ this ->clearMessages ();
24+
25+ if (! is_numeric ($ value )) {
26+ $ this ->addMessage ($ this ->translate ('The token must only contain numbers ' ));
27+
28+ return false ;
29+ }
30+
31+ if (strlen ($ value ) !== $ this ->digits ) {
32+ $ this ->addMessage ($ this ->translate ('The token must be exactly 6 digits long. ' ));
33+
34+ return false ;
35+ }
36+
37+ return true ;
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments