isGranted('ROLE_OAUTH2_ID')) { $scopes[] = 'id'; } if ($this->isGranted('ROLE_OAUTH2_PROFILE')) { $scopes[] = 'profile'; } if ($this->isGranted('ROLE_OAUTH2_ROLES')) { $scopes[] = 'roles'; } /** @var User $user */ $user = $this->getUser(); $email = $user->getEmail(); $password = $this->crypt->decrypt($user->getPassword()); try { $data = $this->apiClient->getPersonalData($email, $password); if ($data instanceof Notification) { return new JsonResponse(['message' => $data->message, 'code' => $data->code], Response::HTTP_BAD_REQUEST); } // Patch current user's roles $data->roles = $user->getRoles(); // extract userdata for resulting claims $userData = $this->getClaims($data, $scopes); return $this->json($userData); } catch (ApiClientException $e) { return new JsonResponse(['message' => $e->getMessage()], Response::HTTP_BAD_REQUEST); } } private function getClaims(PersonalData $data, array $scopes): array { // get all available claims $allClaims = $data->getClaims(); $keys = array_keys($allClaims); $claims = []; // filter claims by provided scopes foreach ($scopes as $scope) { if (false === in_array($scope, $keys)) { continue; } $claims[$scope] = $allClaims[$scope]; } return $claims; } }