Skip to content

Commit b7c88ec

Browse files
authored
IBX-10654: Made authorization error messages less verbose
For more details see https://issues.ibexa.co/browse/IBX-10654 Key changes: * Made authorization error messages less verbose
1 parent a32ad20 commit b7c88ec

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/bundle/Security/Authentication/DefaultAuthenticationFailureHandler.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Symfony\Component\HttpFoundation\Request;
1313
use Symfony\Component\HttpFoundation\Response;
1414
use Symfony\Component\Security\Core\Exception\AuthenticationException;
15+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
1516
use Symfony\Component\Security\Http\Authentication\DefaultAuthenticationFailureHandler as HttpDefaultAuthenticationFailureHandler;
1617

1718
final class DefaultAuthenticationFailureHandler extends HttpDefaultAuthenticationFailureHandler
@@ -26,6 +27,17 @@ public function onAuthenticationFailure(Request $request, AuthenticationExceptio
2627
]);
2728
}
2829

30+
if ($exception instanceof BadCredentialsException) {
31+
$previous = $exception->getPrevious();
32+
$code = $previous ? $previous->getCode() : 0;
33+
34+
$exception = new BadCredentialsException(
35+
'Bad credentials.',
36+
$code,
37+
$previous
38+
);
39+
}
40+
2941
return parent::onAuthenticationFailure($request, $exception);
3042
}
3143
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) Ibexa AS. All rights reserved.
5+
* @license For full copyright and license information view LICENSE file distributed with this source code.
6+
*/
7+
declare(strict_types=1);
8+
9+
namespace Ibexa\Tests\Bundle\User\Security\Authentication;
10+
11+
use Ibexa\Bundle\User\Security\Authentication\DefaultAuthenticationFailureHandler;
12+
use Ibexa\Contracts\Core\Repository\Exceptions\PasswordInUnsupportedFormatException;
13+
use PHPUnit\Framework\MockObject\MockObject;
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Component\HttpFoundation\Session\Session;
17+
use Symfony\Component\HttpKernel\HttpKernelInterface;
18+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
19+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
20+
use Symfony\Component\Security\Http\HttpUtils;
21+
22+
final class DefaultAuthenticationFailureHandlerTest extends TestCase
23+
{
24+
private DefaultAuthenticationFailureHandler $handler;
25+
26+
private MockObject&HttpUtils $httpUtils;
27+
28+
protected function setUp(): void
29+
{
30+
$this->httpUtils = $this->createMock(HttpUtils::class);
31+
$this->handler = new DefaultAuthenticationFailureHandler(
32+
$this->createMock(HttpKernelInterface::class),
33+
$this->httpUtils
34+
);
35+
}
36+
37+
public function testHandlePasswordInUnsupportedFormatException(): void
38+
{
39+
$request = $this->getRequest();
40+
$exception = new PasswordInUnsupportedFormatException();
41+
$expectedUrl = '/forgot-password';
42+
43+
$this->httpUtils
44+
->expects(self::once())
45+
->method('generateUri')
46+
->with($request, 'ibexa.user.forgot_password.migration')
47+
->willReturn($expectedUrl);
48+
49+
$this->handler->onAuthenticationFailure($request, $exception);
50+
}
51+
52+
public function testOnAuthenticationFailureAltersBadCredentialsExceptionMessage(): void
53+
{
54+
$session = $this->getSession();
55+
$session
56+
->expects(self::once())
57+
->method('set')
58+
->with(
59+
'_security.last_error',
60+
self::callback(static function (AuthenticationException $exception): bool {
61+
self::assertInstanceOf(BadCredentialsException::class, $exception);
62+
self::assertSame('Bad credentials.', $exception->getMessage());
63+
64+
return true;
65+
})
66+
);
67+
68+
$request = $this->getRequest($session);
69+
$originalException = new BadCredentialsException('Original message');
70+
71+
$this->httpUtils
72+
->expects(self::once())
73+
->method('createRedirectResponse')
74+
->with($request);
75+
76+
$this->handler->onAuthenticationFailure($request, $originalException);
77+
}
78+
79+
private function getRequest(?Session $session = null): Request
80+
{
81+
$request = new Request();
82+
$request->setSession($session ?? $this->getSession());
83+
84+
return $request;
85+
}
86+
87+
public function getSession(): MockObject&Session
88+
{
89+
$session = $this->createMock(Session::class);
90+
$session->expects(self::any())->method('isStarted')->willReturn(true);
91+
92+
return $session;
93+
}
94+
}

0 commit comments

Comments
 (0)