Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] Respect referrer query param when redirecting user #13

Open
wants to merge 1 commit into
base: 3-4-x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Classes/Controller/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ protected function getAuth0(): Auth0
protected function getCallback(string $loginType = 'login'): string
{
$uri = $GLOBALS['TYPO3_REQUEST']->getUri();
$rawReferrer = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['referrer'];
$referrer = !empty($rawReferrer) ? $rawReferrer : sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $uri->getPath());

$referrer = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['referrer'] ?? sprintf('%s://%s%s', $uri->getScheme(), $uri->getHost(), $uri->getPath());
if ($this->settings['referrerAnchor']) {
$referrer .= '#' . $this->settings['referrerAnchor'];
}
Expand Down
13 changes: 8 additions & 5 deletions Classes/Middleware/CallbackMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ protected function handleFrontendCallback(ServerRequestInterface $request, Token
return $this->enrichReferrerByErrorCode($errorCode, $token);
}

$referrer = $token->getClaim('referrer');

if ($this->isUserLoggedIn($request)) {
$loginType = GeneralUtility::_GET('logintype');
$application = $token->getClaim('application');
Expand All @@ -116,18 +118,18 @@ protected function handleFrontendCallback(ServerRequestInterface $request, Token

if ((bool)$token->getClaim('redirectDisable') === false) {
$allowedMethods = ['groupLogin', 'userLogin', 'login', 'getpost', 'referrer'];
$this->performRedirectFromPluginConfiguration($token, $allowedMethods);
$this->performRedirectFromPluginConfiguration($token, $allowedMethods, $referrer);
} else {
return new RedirectResponse($token->getClaim('referrer'));
return new RedirectResponse($referrer);
}
} elseif ($loginType === 'logout') {
// User was logged out prior to this method. That's why there is no valid TYPO3 frontend user anymore.
$this->performRedirectFromPluginConfiguration($token, ['logout', 'referrer']);
$this->performRedirectFromPluginConfiguration($token, ['logout', 'referrer'], $referrer);
}
}

// Redirect back to logout page if no redirect was executed before
return new RedirectResponse($token->getClaim('referrer'));
return new RedirectResponse($referrer);
}

/**
Expand Down Expand Up @@ -182,7 +184,7 @@ protected function updateTypo3User(int $application, array $user): void
$updateUtility->updateGroups();
}

protected function performRedirectFromPluginConfiguration(Token $token, array $allowedMethods): void
protected function performRedirectFromPluginConfiguration(Token $token, array $allowedMethods, ?string $referrer = null): void
{
$redirectService = new RedirectService([
'redirectDisable' => false,
Expand All @@ -193,6 +195,7 @@ protected function performRedirectFromPluginConfiguration(Token $token, array $a
'redirectPageLogout' => $token->getClaim('redirectPageLogout')
]);

$redirectService->setReferrer($referrer);
$redirectService->handleRedirect($allowedMethods);
}
}
12 changes: 11 additions & 1 deletion Classes/Service/RedirectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class RedirectService implements LoggerAwareInterface
*/
protected $settings = [];

/**
* @param string|null
*/
protected $referrer;

public function __construct(array $redirectSettings)
{
$this->settings = $redirectSettings;
Expand All @@ -49,6 +54,11 @@ public function __construct(array $redirectSettings)
}
}

public function setReferrer(?string $referrer)
{
$this->referrer = $referrer;
}

public function handleRedirect(array $allowedMethods, array $additionalParameters = []): void
{
if ((bool)$this->settings['redirectDisable'] === false && !empty($this->settings['redirectMode'])) {
Expand Down Expand Up @@ -166,7 +176,7 @@ public function getRedirectUri(array $allowedRedirects): array
break;

case 'referrer':
$redirect_url[] = $this->validateRedirectUrl(GeneralUtility::_GP('referrer'));
$redirect_url[] = $this->validateRedirectUrl($this->referrer ?? GeneralUtility::_GP('referrer'));
break;

case 'loginError':
Expand Down