15
15
use Psr \Http \Server \RequestHandlerInterface ;
16
16
use Psr \Log \LoggerInterface ;
17
17
use Sandstorm \NeosTwoFactorAuthentication \Domain \AuthenticationStatus ;
18
- use Sandstorm \NeosTwoFactorAuthentication \Domain \ Repository \ SecondFactorRepository ;
18
+ use Sandstorm \NeosTwoFactorAuthentication \Service \ SecondFactorService ;
19
19
use Sandstorm \NeosTwoFactorAuthentication \Service \SecondFactorSessionStorageService ;
20
20
21
21
class SecondFactorMiddleware implements MiddlewareInterface
@@ -30,12 +30,6 @@ class SecondFactorMiddleware implements MiddlewareInterface
30
30
*/
31
31
protected $ securityContext ;
32
32
33
- /**
34
- * @Flow\Inject
35
- * @var SecondFactorRepository
36
- */
37
- protected $ secondFactorRepository ;
38
-
39
33
/**
40
34
* @Flow\Inject
41
35
* @var ActionRequestFactory
@@ -55,23 +49,10 @@ class SecondFactorMiddleware implements MiddlewareInterface
55
49
protected $ secondFactorSessionStorageService ;
56
50
57
51
/**
58
- * @Flow\InjectConfiguration(path="enforceTwoFactorAuthentication")
59
- * @var bool
60
- */
61
- protected $ enforceTwoFactorAuthentication ;
62
-
63
- /**
64
- * @Flow\InjectConfiguration(path="enforce2FAForAuthenticationProviders")
65
- * @var array
66
- */
67
- protected $ enforce2FAForAuthenticationProviders ;
68
-
69
- /**
70
- * @Flow\InjectConfiguration(path="enforce2FAForRoles")
71
- * @var array
52
+ * @Flow\Inject
53
+ * @var SecondFactorService
72
54
*/
73
- protected $ enforce2FAForRoles ;
74
-
55
+ protected SecondFactorService $ secondFactorService ;
75
56
76
57
/**
77
58
* This middleware checks if the user is authenticated with a second factor "if necessary".
@@ -167,13 +148,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
167
148
168
149
$ account = $ this ->securityContext ->getAccount ();
169
150
151
+ $ isEnabledForAccount = $ this ->secondFactorService ->isSecondFactorEnabledForAccount ($ account );
152
+ $ isEnforcedForAccount = $ this ->secondFactorService ->isSecondFactorEnforcedForAccount ($ account );
153
+
170
154
// 4. Skip, if second factor is not set up for account and not enforced via settings.
171
- if (
172
- !$ this ->secondFactorRepository ->isEnabledForAccount ($ account )
173
- && !$ this ->enforceTwoFactorAuthentication
174
- && !count (array_intersect (array_map (fn ($ item ) => $ item ->getIdentifier (),$ account ->getRoles ()),$ this ->enforce2FAForRoles ))
175
- && !in_array ($ account ->getAuthenticationProviderName (),$ this ->enforce2FAForAuthenticationProviders )
176
- ) {
155
+ if (!$ isEnabledForAccount && !$ isEnforcedForAccount ) {
177
156
$ this ->log ('Second factor not enabled for account and not enforced by system, skipping second factor. ' );
178
157
179
158
return $ handler ->handle ($ request );
@@ -193,7 +172,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
193
172
// 6. Redirect to 2FA login, if second factor is set up for account but not authenticated.
194
173
// Skip, if already on 2FA login route.
195
174
if (
196
- $ this -> secondFactorRepository -> isEnabledForAccount ( $ account )
175
+ $ isEnabledForAccount
197
176
&& $ authenticationStatus === AuthenticationStatus::AUTHENTICATION_NEEDED
198
177
) {
199
178
// WHY: We use the request URI as state here to keep the middleware from entering a redirect loop.
@@ -213,13 +192,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
213
192
214
193
// 7. Redirect to 2FA setup, if second factor is not set up for account but is enforced by system.
215
194
// Skip, if already on 2FA setup route.
216
- if (
217
- ($ this ->enforceTwoFactorAuthentication
218
- || count (array_intersect (array_map (fn ($ item ) => $ item ->getIdentifier (),$ account ->getRoles ()),$ this ->enforce2FAForRoles ))
219
- || in_array ($ account ->getAuthenticationProviderName (),$ this ->enforce2FAForAuthenticationProviders )
220
- )
221
- && !$ this ->secondFactorRepository ->isEnabledForAccount ($ account )
222
- ) {
195
+ if ($ isEnforcedForAccount && !$ isEnabledForAccount ) {
223
196
// WHY: We use the request URI as state here to keep the middleware from entering a redirect loop.
224
197
$ isSettingUp2FA = str_ends_with ($ request ->getUri ()->getPath (), self ::SECOND_FACTOR_SETUP_URI );
225
198
if ($ isSettingUp2FA ) {
0 commit comments