Skip to content

Commit

Permalink
TASK: Clean up routes, use const routes in Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesAlias committed May 6, 2023
1 parent 359d279 commit 8107f48
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function askForSecondFactorAction(?string $username = null)
* @throws StopActionException
* @throws SessionNotStartedException
*/
public function checkOtpAction(string $otp)
public function checkSecondFactorAction(string $otp)
{
$account = $this->securityContext->getAccount();

Expand Down
10 changes: 6 additions & 4 deletions Classes/Http/Middleware/SecondFactorMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
class SecondFactorMiddleware implements MiddlewareInterface
{
const LOGGING_PREFIX = 'Sandstorm/NeosTwoFactorAuthentication: ';
const SECOND_FACTOR_LOGIN_URI = 'neos/second-factor-login';
const SECOND_FACTOR_SETUP_URI = 'neos/second-factor-setup';

/**
* @Flow\Inject
Expand Down Expand Up @@ -128,7 +130,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
&& $authenticationStatus === AuthenticationStatus::AUTHENTICATION_NEEDED
) {
// WHY: We use the request URI as part of state. This prevents the middleware to enter a redirect loop.
$isAskingForOTP = str_ends_with($request->getUri()->getPath(), 'neos/two-factor-login');
$isAskingForOTP = str_ends_with($request->getUri()->getPath(), self::SECOND_FACTOR_LOGIN_URI);
if ($isAskingForOTP) {
return $next->handle($request);
}
Expand All @@ -143,15 +145,15 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// See Sandstorm/NeosTwoFactorAuthentication/LoginController
$this->registerOriginalRequestForRedirect($request);

return new Response(303, ['Location' => '/neos/two-factor-login']);
return new Response(303, ['Location' => self::SECOND_FACTOR_LOGIN_URI]);
}

if (
$this->enforceTwoFactorAuthentication &&
!$this->secondFactorRepository->isEnabledForAccount($account)
) {
// WHY: We use the request URI as part of state. This prevents the middleware to enter a redirect loop.
$isSettingUp2FA = str_ends_with($request->getUri()->getPath(), 'neos/setup-second-factor');
$isSettingUp2FA = str_ends_with($request->getUri()->getPath(), self::SECOND_FACTOR_SETUP_URI);
if ($isSettingUp2FA) {
return $next->handle($request);
}
Expand All @@ -165,7 +167,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
// See Sandstorm/NeosTwoFactorAuthentication/LoginController
$this->registerOriginalRequestForRedirect($request);

return new Response(303, ['Location' => '/neos/setup-second-factor']);
return new Response(303, ['Location' => self::SECOND_FACTOR_SETUP_URI]);
}

throw new AuthenticationRequiredException("You have to be logged in with second factor!");
Expand Down
10 changes: 5 additions & 5 deletions Configuration/Routes.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- name: 'Sandstorm Two Factor Authentication'
uriPattern: 'neos/two-factor-login'
uriPattern: 'neos/second-factor-login'
httpMethods: ['GET']
defaults:
'@package': 'Sandstorm.NeosTwoFactorAuthentication'
Expand All @@ -9,17 +9,17 @@
appendExceedingArguments: true

- name: 'Sandstorm Two Factor Authentication - Validation'
uriPattern: 'neos/two-factor-login'
uriPattern: 'neos/second-factor-login'
httpMethods: ['POST']
defaults:
'@package': 'Sandstorm.NeosTwoFactorAuthentication'
'@controller': 'Login'
'@action': 'checkOtp'
'@action': 'checkSecondFactor'
'@format': 'html'
appendExceedingArguments: true

- name: 'Sandstorm Two Factor Authentication - Setup'
uriPattern: 'neos/setup-second-factor'
uriPattern: 'neos/second-factor-setup'
defaults:
'@package': 'Sandstorm.NeosTwoFactorAuthentication'
'@controller': 'Login'
Expand All @@ -29,7 +29,7 @@
appendExceedingArguments: true

- name: 'Sandstorm Two Factor Authentication - Create 2FA'
uriPattern: 'neos/setup-second-factor'
uriPattern: 'neos/second-factor-setup'
defaults:
'@package': 'Sandstorm.NeosTwoFactorAuthentication'
'@controller': 'Login'
Expand Down

0 comments on commit 8107f48

Please sign in to comment.