Skip to content

Commit 04c4c56

Browse files
authored
Merge pull request from GHSA-36mj-6r7r-mqhf
* IBX-969: Refactored JWT authorization * IBX-969: Applied review remark * IBX-969: Updated caught exceptions
1 parent b8fed91 commit 04c4c56

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

src/bundle/Resources/config/services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ services:
261261
EzSystems\EzPlatformRest\Server\Controller\JWT:
262262
parent: ezpublish_rest.controller.base
263263
arguments:
264-
- '@ezpublish.api.service.user'
265264
- '@Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface'
265+
- '@?ezpublish_rest.session_authenticator'
266266
tags: [controller.service_arguments]
267267

268268
ezpublish_rest.request_listener:

src/lib/Server/Controller/JWT.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,32 @@
88

99
namespace EzSystems\EzPlatformRest\Server\Controller;
1010

11-
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
12-
use eZ\Publish\API\Repository\UserService;
1311
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
14-
use eZ\Publish\Core\MVC\Symfony\Security\User;
12+
use eZ\Publish\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface;
1513
use EzSystems\EzPlatformRest\Message;
1614
use EzSystems\EzPlatformRest\Server\Controller as RestController;
1715
use EzSystems\EzPlatformRest\Server\Values;
1816
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
1917
use Symfony\Component\HttpFoundation\Request;
20-
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2119

2220
/**
2321
* @internal
2422
*/
2523
final class JWT extends RestController
2624
{
27-
/** @var \eZ\Publish\API\Repository\UserService */
28-
private $userService;
29-
3025
/** @var \Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface */
3126
private $tokenManager;
3227

28+
/** @var \eZ\Publish\Core\MVC\Symfony\Security\Authentication\AuthenticatorInterface|null */
29+
private $authenticator;
30+
3331
public function __construct(
34-
UserService $userService,
35-
JWTTokenManagerInterface $tokenManager
32+
JWTTokenManagerInterface $tokenManager,
33+
?AuthenticatorInterface $authenticator = null
3634
) {
37-
$this->userService = $userService;
3835
$this->tokenManager = $tokenManager;
36+
$this->authenticator = $authenticator;
3937
}
4038

4139
public function createToken(Request $request): Values\JWT
@@ -49,15 +47,31 @@ public function createToken(Request $request): Values\JWT
4947
);
5048

5149
try {
52-
$user = $this->userService->loadUserByLogin($jwtTokenInput->username);
53-
if (!$this->userService->checkUserCredentials($user, $jwtTokenInput->password)) {
54-
throw new BadCredentialsException();
55-
}
56-
$token = $this->tokenManager->create(new User($user, ['ROLE_USER']));
57-
58-
return new Values\JWT($token);
59-
} catch (NotFoundException | BadCredentialsException $e) {
60-
throw new UnauthorizedException('Invalid username or password', $request->getPathInfo());
50+
$request->attributes->set('username', $jwtTokenInput->username);
51+
$request->attributes->set('password', (string) $jwtTokenInput->password);
52+
53+
$token = $this->getAuthenticator()->authenticate($request);
54+
55+
$jwtToken = $this->tokenManager->create($token->getUser());
56+
57+
return new Values\JWT($jwtToken);
58+
} catch (AuthenticationException $e) {
59+
$this->getAuthenticator()->logout($request);
60+
throw new UnauthorizedException('Invalid login or password', $request->getPathInfo());
6161
}
6262
}
63+
64+
private function getAuthenticator(): AuthenticatorInterface
65+
{
66+
if (null === $this->authenticator) {
67+
throw new \RuntimeException(
68+
sprintf(
69+
"No %s instance injected. Ensure 'ezpublish_rest_session' is configured under your firewall",
70+
AuthenticatorInterface::class
71+
)
72+
);
73+
}
74+
75+
return $this->authenticator;
76+
}
6377
}

0 commit comments

Comments
 (0)