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

Always redirect ajax requests when session expired #5868

Merged
merged 3 commits into from Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions core-bundle/src/Resources/public/mootao.js
Expand Up @@ -45,6 +45,8 @@ Request.Contao = new Class(
var url = this.getHeader('X-Ajax-Location'),
json;

this.redirectIfUnauthorized();

if (url && this.options.followRedirects) {
location.replace(url);
return;
Expand Down Expand Up @@ -80,12 +82,23 @@ Request.Contao = new Class(
failure: function() {
var url = this.getHeader('X-Ajax-Location');

this.redirectIfUnauthorized();

if (url && this.options.followRedirects && this.status >= 300 && this.status < 400) {
location.replace(url);
return;
}

this.onFailure();
},

redirectIfUnauthorized: function() {
var url = this.getHeader('X-Ajax-Location'),
unauthorized = null !== this.getHeader('X-Is-Unauthorized');
Toflar marked this conversation as resolved.
Show resolved Hide resolved

if (url && unauthorized) {
location.replace(url);
}
}
});

Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Resources/public/mootao.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Expand Up @@ -15,6 +15,7 @@
use Contao\CoreBundle\Routing\ScopeMatcher;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
Expand Down Expand Up @@ -47,14 +48,20 @@ public function start(Request $request, AuthenticationException $authException =
throw new UnauthorizedHttpException('', 'Not authorized');
}

private function redirectToBackend(Request $request): RedirectResponse
private function redirectToBackend(Request $request): Response
{
$url = $this->router->generate(
'contao_backend_login',
['redirect' => $request->getUri()],
UrlGeneratorInterface::ABSOLUTE_URL
);

return new RedirectResponse($this->uriSigner->sign($url));
$location = $this->uriSigner->sign($url);

if ($request->isXmlHttpRequest()) {
return new Response($location, 303, ['X-Is-Unauthorized' => 'true', 'X-Ajax-Location' => $location]);
}

return new RedirectResponse($location, 302);
}
}
Expand Up @@ -17,6 +17,7 @@
use Contao\CoreBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\HttpKernel\UriSigner;
use Symfony\Component\Routing\RouterInterface;
Expand Down Expand Up @@ -77,6 +78,41 @@ public function testSignsTheBackendRedirectUrl(): void
$this->assertSame('http://localhost/contao/login?_hash=%2FxSCw6cwMlws5DEhBCvs0%2F75oQA8q%2FgMkZEnYCf6QSE%3D&redirect=https%3A%2F%2Fcontao.org%2Fpreview.php%2Fabout-contao.html', $response->getTargetUrl());
}

public function testRedirectsAjaxRequests(): void
{
$request = Request::create('http://localhost/contao/login?redirect=https%3A%2F%2Fcontao.org%2Fpreview.php%2Fabout-contao.html');
$request->headers->set('X-Requested-With', 'XMLHttpRequest');

$router = $this->createMock(RouterInterface::class);
$router
->expects($this->once())
->method('generate')
->with('contao_backend_login', ['redirect' => $request->getUri()])
->willReturn('http://localhost/contao/login?redirect=https%3A%2F%2Fcontao.org%2Fpreview.php%2Fabout-contao.html')
;

$scopeMatcher = $this->createMock(ScopeMatcher::class);
$scopeMatcher
->expects($this->once())
->method('isBackendRequest')
->with($request)
->willReturn(true)
;

$entryPoint = new AuthenticationEntryPoint(
$router,
new UriSigner('secret'),
$scopeMatcher
);

$response = $entryPoint->start($request);

$this->assertInstanceOf(Response::class, $response);
$this->assertFalse($response->headers->has('Location'));
$this->assertTrue($response->headers->has('X-Is-Unauthorized'));
$this->assertSame('http://localhost/contao/login?_hash=%2FxSCw6cwMlws5DEhBCvs0%2F75oQA8q%2FgMkZEnYCf6QSE%3D&redirect=https%3A%2F%2Fcontao.org%2Fpreview.php%2Fabout-contao.html', $response->headers->get('X-Ajax-Location'));
}

public function testAddsARefererToTheBackendRedirectUrlIfTheQueryIsEmpty(): void
{
$request = Request::create('/');
Expand Down