| @@ -5,11 +5,11 @@ namespace App\Security; | |||
| use ApiPlatform\Metadata\Get; | |||
| use ApiPlatform\Metadata\IriConverterInterface; | |||
| use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | |||
| use ApiPlatform\State\Provider\ContentNegotiationProvider; | |||
| use ApiPlatform\State\SerializerContextBuilderInterface; | |||
| use App\ApiResource\UserApi; | |||
| use App\Entity\MediaObject; | |||
| use App\Entity\User; | |||
| use App\Repository\UserRepository; | |||
| use App\State\EntityToDtoStateProvider; | |||
| use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface; | |||
| use Symfony\Component\HttpFoundation\JsonResponse; | |||
| use Symfony\Component\HttpFoundation\Request; | |||
| @@ -21,7 +21,6 @@ use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; | |||
| use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | |||
| use Symfony\Component\Security\Http\Authenticator\Passport\Credentials\PasswordCredentials; | |||
| use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | |||
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |||
| use Symfony\Component\Serializer\SerializerInterface; | |||
| use Symfony\Contracts\HttpClient\HttpClientInterface; | |||
| use Symfonycasts\MicroMapper\MicroMapperInterface; | |||
| @@ -31,11 +30,41 @@ class JwtAuthenticator extends AbstractAuthenticator | |||
| public function __construct( | |||
| private JWTTokenManagerInterface $jwtManager, | |||
| private MicroMapperInterface $microMapper, | |||
| private HttpClientInterface $httpClient, | |||
| private SerializerInterface $serializer, | |||
| private SerializerContextBuilderInterface $serializerContextBuilder, | |||
| private SerializerContextBuilderInterface $serializerContextBuilder | |||
| ) {} | |||
| public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | |||
| { | |||
| /** @var User $user */ | |||
| $user = $token->getUser(); | |||
| $userApi = $this->microMapper->map($user, UserApi::class); | |||
| $context = [ | |||
| 'groups' => ['Default'], | |||
| 'resource_class' => UserApi::class, | |||
| 'api_normalize' => true, | |||
| 'jsonld_has_context' => true, | |||
| ]; | |||
| $data = $this->serializer->normalize($userApi, 'jsonld', $context); | |||
| $propertyData = [ | |||
| 'dbId' => $userApi->dbId, | |||
| 'email' => $userApi->email, | |||
| 'firstName' => $userApi->firstName, | |||
| 'lastName' => $userApi->lastName, | |||
| 'image' => $userApi->image ? '/api/media_objects/' . $userApi->image->getId() : null, | |||
| 'imageUrl' => $userApi->imageUrl, | |||
| 'fullName' => $userApi->fullName, | |||
| 'roles' => $userApi->roles, | |||
| 'createdAt' => $userApi->createdAt?->format('Y-m-d\TH:i:sP'), | |||
| 'token' => $this->jwtManager->create($user) | |||
| ]; | |||
| return new JsonResponse(['user' => $data + $propertyData]); | |||
| } | |||
| public function supports(Request $request): ?bool | |||
| { | |||
| return $request->getPathInfo() === '/api/auth' && $request->isMethod('POST'); | |||
| @@ -58,26 +87,6 @@ class JwtAuthenticator extends AbstractAuthenticator | |||
| ); | |||
| } | |||
| public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response | |||
| { | |||
| /** @var User $user */ | |||
| $user = $token->getUser(); | |||
| $userApi = $this->microMapper->map($user, UserApi::class); | |||
| // NOTE: This is a necessary workaround, since it is ot possible to map this in the usual api platform style at this point | |||
| $userApiArray = []; | |||
| $userApiArray['@id'] = '/api/users/' . $user->getId(); | |||
| $userApiArray['@type'] = 'User'; | |||
| foreach (get_object_vars($userApi) as $property => $value) { | |||
| if ($property !== 'id') { | |||
| $userApiArray[$property] = $value; | |||
| } | |||
| } | |||
| $userApiArray['token'] = $this->jwtManager->create($user); | |||
| return new JsonResponse(['user' => $userApiArray]); | |||
| } | |||
| public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response | |||
| { | |||
| return new JsonResponse([ | |||