88
99namespace EzSystems \EzPlatformRest \Server \Controller ;
1010
11- use eZ \Publish \API \Repository \Exceptions \NotFoundException ;
12- use eZ \Publish \API \Repository \UserService ;
1311use 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 ;
1513use EzSystems \EzPlatformRest \Message ;
1614use EzSystems \EzPlatformRest \Server \Controller as RestController ;
1715use EzSystems \EzPlatformRest \Server \Values ;
1816use Lexik \Bundle \JWTAuthenticationBundle \Services \JWTTokenManagerInterface ;
1917use 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 */
2523final 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