22
33namespace Sandstorm \NeosTwoFactorAuthentication \Controller ;
44
5- /*
6- * This file is part of the Sandstorm.NeosTwoFactorAuthentication package.
7- */
8-
95use Neos \Error \Messages \Message ;
106use Neos \Flow \Annotations as Flow ;
117use Neos \Flow \Configuration \ConfigurationManager ;
1511use Neos \Flow \Mvc \Exception \StopActionException ;
1612use Neos \Flow \Mvc \FlashMessage \FlashMessageService ;
1713use Neos \Flow \Persistence \Exception \IllegalObjectTypeException ;
18- use Neos \Flow \Security \Account ;
1914use Neos \Flow \Security \Context as SecurityContext ;
2015use Neos \Flow \Session \Exception \SessionNotStartedException ;
2116use Neos \Fusion \View \FusionView ;
2217use Neos \Neos \Domain \Repository \DomainRepository ;
2318use Neos \Neos \Domain \Repository \SiteRepository ;
2419use Sandstorm \NeosTwoFactorAuthentication \Domain \AuthenticationStatus ;
25- use Sandstorm \NeosTwoFactorAuthentication \Domain \Model \SecondFactor ;
2620use Sandstorm \NeosTwoFactorAuthentication \Domain \Repository \SecondFactorRepository ;
21+ use Sandstorm \NeosTwoFactorAuthentication \Service \SecondFactorService ;
2722use Sandstorm \NeosTwoFactorAuthentication \Service \SecondFactorSessionStorageService ;
2823use Sandstorm \NeosTwoFactorAuthentication \Service \TOTPService ;
2924
@@ -35,52 +30,49 @@ class LoginController extends ActionController
3530 protected $ defaultViewObjectName = FusionView::class;
3631
3732 /**
38- * @var SecurityContext
3933 * @Flow\Inject
4034 */
41- protected $ securityContext ;
35+ protected SecurityContext $ securityContext ;
36+
37+ /**
38+ * @Flow\Inject
39+ */
40+ protected DomainRepository $ domainRepository ;
4241
4342 /**
44- * @var DomainRepository
4543 * @Flow\Inject
4644 */
47- protected $ domainRepository ;
45+ protected SiteRepository $ siteRepository ;
4846
4947 /**
5048 * @Flow\Inject
51- * @var SiteRepository
5249 */
53- protected $ siteRepository ;
50+ protected FlashMessageService $ flashMessageService ;
5451
5552 /**
5653 * @Flow\Inject
57- * @var FlashMessageService
5854 */
59- protected $ flashMessageService ;
55+ protected SecondFactorRepository $ secondFactorRepository ;
6056
6157 /**
62- * @var SecondFactorRepository
6358 * @Flow\Inject
6459 */
65- protected $ secondFactorRepository ;
60+ protected SecondFactorSessionStorageService $ secondFactorSessionStorageService ;
6661
6762 /**
6863 * @Flow\Inject
69- * @var SecondFactorSessionStorageService
7064 */
71- protected $ secondFactorSessionStorageService ;
65+ protected TOTPService $ tOTPService ;
7266
7367 /**
7468 * @Flow\Inject
75- * @var TOTPService
7669 */
77- protected $ tOTPService ;
70+ protected SecondFactorService $ secondFactorService ;
7871
7972 /**
8073 * @Flow\Inject
81- * @var Translator
8274 */
83- protected $ translator ;
75+ protected Translator $ translator ;
8476
8577 /**
8678 * This action decides which tokens are already authenticated
@@ -112,7 +104,7 @@ public function checkSecondFactorAction(string $otp): void
112104 {
113105 $ account = $ this ->securityContext ->getAccount ();
114106
115- $ isValidOtp = $ this ->enteredTokenMatchesAnySecondFactor ($ otp , $ account );
107+ $ isValidOtp = $ this ->secondFactorService -> validateOtpForAccount ($ otp , $ account );
116108
117109 if ($ isValidOtp ) {
118110 $ this ->secondFactorSessionStorageService ->setAuthenticationStatus (AuthenticationStatus::AUTHENTICATED );
@@ -175,9 +167,6 @@ public function setupSecondFactorAction(?string $username = null): void
175167 }
176168
177169 /**
178- * @param string $secret
179- * @param string $secondFactorFromApp
180- * @return void
181170 * @throws IllegalObjectTypeException
182171 * @throws SessionNotStartedException
183172 * @throws StopActionException
@@ -238,7 +227,6 @@ public function cancelLoginAction(): void
238227 }
239228
240229 /**
241- * @return array
242230 * @throws InvalidConfigurationTypeException
243231 */
244232 protected function getNeosSettings (): array
@@ -250,24 +238,4 @@ protected function getNeosSettings(): array
250238 );
251239 }
252240
253- /**
254- * Check if the given token matches any registered second factor
255- *
256- * @param string $enteredSecondFactor
257- * @param Account $account
258- * @return bool
259- */
260- private function enteredTokenMatchesAnySecondFactor (string $ enteredSecondFactor , Account $ account ): bool
261- {
262- /** @var SecondFactor[] $secondFactors */
263- $ secondFactors = $ this ->secondFactorRepository ->findByAccount ($ account );
264- foreach ($ secondFactors as $ secondFactor ) {
265- $ isValid = TOTPService::checkIfOtpIsValid ($ secondFactor ->getSecret (), $ enteredSecondFactor );
266- if ($ isValid ) {
267- return true ;
268- }
269- }
270-
271- return false ;
272- }
273241}
0 commit comments