Skip to content

Commit f12d780

Browse files
authored
Fixed IBX-925: Unable to use JWT if email user provider is configured (#80)
1 parent 3c0747b commit f12d780

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77
"psr-4": {
88
"EzSystems\\EzPlatformRestBundle\\": "src/bundle/",
99
"EzSystems\\EzPlatformRest\\": "src/lib/",
10+
"Ibexa\\Bundle\\Rest\\": "src/bundle/",
11+
"Ibexa\\Rest\\": "src/lib/"
12+
}
13+
},
14+
"autoload-dev": {
15+
"psr-4": {
1016
"EzSystems\\EzPlatformRestBundle\\Tests\\": "tests/bundle/",
11-
"EzSystems\\EzPlatformRest\\Tests\\": "tests/lib/"
17+
"EzSystems\\EzPlatformRest\\Tests\\": "tests/lib/",
18+
"Ibexa\\Tests\\Rest\\": "tests/lib/",
19+
"Ibexa\\Tests\\Bundle\\Rest\\": "tests/bundle/"
1220
}
1321
},
1422
"require": {

src/lib/Server/Controller/JWT.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use EzSystems\EzPlatformRest\Message;
1414
use EzSystems\EzPlatformRest\Server\Controller as RestController;
1515
use EzSystems\EzPlatformRest\Server\Values;
16+
use Ibexa\Rest\Server\Security\JWTUser;
1617
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
1718
use Symfony\Component\HttpFoundation\Request;
1819
use Symfony\Component\Security\Core\Exception\AuthenticationException;
@@ -50,9 +51,11 @@ public function createToken(Request $request): Values\JWT
5051
$request->attributes->set('username', $jwtTokenInput->username);
5152
$request->attributes->set('password', (string) $jwtTokenInput->password);
5253

53-
$token = $this->getAuthenticator()->authenticate($request);
54+
$user = $this->getAuthenticator()->authenticate($request)->getUser();
5455

55-
$jwtToken = $this->tokenManager->create($token->getUser());
56+
$jwtToken = $this->tokenManager->create(
57+
new JWTUser($user, $jwtTokenInput->username)
58+
);
5659

5760
return new Values\JWT($jwtToken);
5861
} catch (AuthenticationException $e) {
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (C) eZ Systems 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\Rest\Server\Security;
10+
11+
use Symfony\Component\Security\Core\User\UserInterface;
12+
13+
final class JWTUser implements UserInterface
14+
{
15+
/** @var \Symfony\Component\Security\Core\User\UserInterface */
16+
private $wrappedUser;
17+
18+
/** @var string|null */
19+
private $userIdentifier;
20+
21+
public function __construct(UserInterface $wrappedUser, ?string $userIdentifier)
22+
{
23+
$this->wrappedUser = $wrappedUser;
24+
$this->userIdentifier = $userIdentifier;
25+
}
26+
27+
public function getPassword(): ?string
28+
{
29+
return $this->wrappedUser->getPassword();
30+
}
31+
32+
public function eraseCredentials(): void
33+
{
34+
$this->wrappedUser->eraseCredentials();
35+
}
36+
37+
public function getRoles(): array
38+
{
39+
return $this->wrappedUser->getRoles();
40+
}
41+
42+
public function getSalt(): ?string
43+
{
44+
return $this->wrappedUser->getSalt();
45+
}
46+
47+
public function getUsername(): string
48+
{
49+
return $this->userIdentifier ?? $this->wrappedUser->getUsername();
50+
}
51+
52+
public function getWrappedUser(): UserInterface
53+
{
54+
return $this->wrappedUser;
55+
}
56+
}

0 commit comments

Comments
 (0)