src/App/Controller/API/AuthController.php line 254

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller\API;
  4. use App\DTO\Auth\AuthJWTResponse;
  5. use App\DTO\Auth\AuthUserRawData;
  6. use App\Entity\Users;
  7. use App\Service\User\UsersManagementService;
  8. use Exception;
  9. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  10. use KnpU\OAuth2ClientBundle\Client\Provider\FacebookClient;
  11. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  12. use League\OAuth2\Client\Token\AccessToken;
  13. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  14. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. class AuthController extends AbstractController
  20. {
  21.     private UsersManagementService $usersManagementService;
  22.     public function __construct(UsersManagementService $usersManagementService)
  23.     {
  24.         $this->usersManagementService $usersManagementService;
  25.     }
  26.     /**
  27.      * @Route("/auth/facebook/verify", name="api.auth.facebook.check", methods={"GET"})
  28.      */
  29.     public function getAuthFacebookCheck(
  30.         Request $request,
  31.         ClientRegistry $clientRegistry,
  32.         JWTTokenManagerInterface $jwt,
  33.         UsersManagementService $usersManagementService
  34.     ) {
  35.         $accessToken $request->get('access_token');
  36.         /** @var FacebookClient $client */
  37.         $client $clientRegistry->getClient('facebook_main');
  38.         try {
  39.             $t = new AccessToken(
  40.                 [
  41.                     'access_token' => $accessToken,
  42.                 ]
  43.             );
  44.             $facebookUser $client->fetchUserFromToken($t);
  45.         } catch (IdentityProviderException $e) {
  46.             return $this->json(
  47.                 new AuthJWTResponse(
  48.                     AuthJWTResponse::STATUS_ERROR,
  49.                     null,
  50.                     $e->getMessage()
  51.                 ),
  52.                 Response::HTTP_FORBIDDEN
  53.             );
  54.         }
  55.         /** @var Users $user */
  56.         $user $this
  57.             ->getDoctrine()
  58.             ->getRepository(Users::class)
  59.             ->findByFacebook($facebookUser->getId());
  60.         if (!$user) {
  61.             $authUserRawData = (new AuthUserRawData())
  62.                 ->setIdentity($facebookUser->getId())
  63.                 ->setFirstName($facebookUser->getFirstName())
  64.                 ->setLastName($facebookUser->getLastName())
  65.                 ->setAvatar($facebookUser->getPictureUrl())
  66.                 ->setLanguage($request->getLocale());
  67.             $user $usersManagementService->createUser(Users::AUTH_FB$authUserRawData$this->getUser());
  68.         }
  69.         $token $jwt->create($user);
  70.         return $this->json(
  71.             new AuthJWTResponse(
  72.                 AuthJWTResponse::STATUS_SUCCESS,
  73.                 $token
  74.             )
  75.         );
  76.     }
  77.     /**
  78.      * @Route("/auth/apple/verify", name="api.auth.apple.check", methods={"GET"})
  79.      */
  80.     public function getAuthAppleCheck(
  81.         Request $request,
  82.         ClientRegistry $clientRegistry,
  83.         JWTTokenManagerInterface $jwt,
  84.         UsersManagementService $usersManagementService
  85.     ) {
  86.         $accessToken $request->get('access_token');
  87.         $code $request->get('code');
  88.         $firstName = (string)$request->get('first_name');
  89.         $lastName = (string)$request->get('last_name');
  90.         try {
  91.             $appleUser $usersManagementService->decodeAppleAccessToken($accessToken);
  92.         } catch (Exception $e) {
  93.             return $this->json(
  94.                 new AuthJWTResponse(
  95.                     AuthJWTResponse::STATUS_ERROR,
  96.                     null,
  97.                     $e->getMessage()
  98.                 ),
  99.                 Response::HTTP_FORBIDDEN
  100.             );
  101.         }
  102.         /** @var Users $user */
  103.         $user $this
  104.             ->getDoctrine()
  105.             ->getRepository(Users::class)
  106.             ->findByApple($appleUser['id']);
  107.         if (!$user) {
  108.             $authUserRawData = (new AuthUserRawData())
  109.                 ->setIdentity($appleUser['id'])
  110.                 ->setEmail($appleUser['email'])
  111.                 ->setFirstName($firstName)
  112.                 ->setLastName($lastName);
  113.             $user $usersManagementService->createUser(Users::AUTH_APPLE$authUserRawData$this->getUser());
  114.         }
  115.         $token $jwt->create($user);
  116.         return $this->json(
  117.             new AuthJWTResponse(
  118.                 AuthJWTResponse::STATUS_SUCCESS,
  119.                 $token
  120.             )
  121.         );
  122.     }
  123.     /**
  124.      * @Route("/auth/google/verify", name="api.auth.google.check", methods={"GET"})
  125.      */
  126.     public function getAuthGoogleCheck(
  127.         Request $request,
  128.         ClientRegistry $clientRegistry,
  129.         JWTTokenManagerInterface $jwt,
  130.         UsersManagementService $usersManagementService
  131.     ) {
  132.         $accessToken $request->get('access_token');
  133.         try {
  134.             $googleUser $usersManagementService->decodeGoogleAccessToken($accessToken);
  135.         } catch (Exception $e) {
  136.             return $this->json(
  137.                 new AuthJWTResponse(
  138.                     AuthJWTResponse::STATUS_ERROR,
  139.                     null,
  140.                     $e->getMessage()
  141.                 ),
  142.                 Response::HTTP_FORBIDDEN
  143.             );
  144.         }
  145.         // $identity $googleUser['id']
  146.         $identity $googleUser['email'];
  147.         /** @var Users $user */
  148.         $user $this
  149.             ->getDoctrine()
  150.             ->getRepository(Users::class)
  151.             ->findByGoogle($identity);
  152.         if (!$user) {
  153.             $authUserRawData = (new AuthUserRawData())
  154.                 ->setIdentity($identity)
  155.                 ->setEmail($googleUser['email'])
  156.                 ->setFirstName($googleUser['first_name'])
  157.                 ->setLastName($googleUser['last_name'])
  158.                 ->setAvatar($googleUser['picture'])
  159.                 ->setLanguage($googleUser['locale']);
  160.             $user $usersManagementService->createUser(Users::AUTH_GOOGLE$authUserRawData$this->getUser());
  161.         }
  162.         $token $jwt->create($user);
  163.         return $this->json(
  164.             new AuthJWTResponse(
  165.                 AuthJWTResponse::STATUS_SUCCESS,
  166.                 $token
  167.             )
  168.         );
  169.     }
  170.     /**
  171.      * @Route("/auth/huawei/verify", name="api.auth.huawei.check", methods={"GET"})
  172.      */
  173.     public function getAuthHuaweiCheck(
  174.         Request $request,
  175.         ClientRegistry $clientRegistry,
  176.         JWTTokenManagerInterface $jwt,
  177.         UsersManagementService $usersManagementService
  178.     ) {
  179.         $accessToken $request->get('access_token');
  180.         try {
  181.             $huaweiUser $usersManagementService->decodeHuaweiAccessToken($accessToken);
  182.         } catch (Exception $e) {
  183.             return $this->json(
  184.                 new AuthJWTResponse(
  185.                     AuthJWTResponse::STATUS_ERROR,
  186.                     null,
  187.                     $e->getMessage()
  188.                 ),
  189.                 Response::HTTP_FORBIDDEN
  190.             );
  191.         }
  192.         // $identity $googleUser['id']
  193.         $identity $huaweiUser['id'];
  194.         /** @var Users $user */
  195.         $user $this
  196.             ->getDoctrine()
  197.             ->getRepository(Users::class)
  198.             ->findByHuawei($identity);
  199.         if (!$user) {
  200.             $authUserRawData = (new AuthUserRawData())
  201.                 ->setIdentity($identity)
  202.                 ->setEmail($huaweiUser['id']."@huawei.com")
  203.                 ->setFirstName($huaweiUser['first_name'])
  204.                 ->setLastName($huaweiUser['last_name'])
  205.                 ->setLanguage($huaweiUser['locale']);
  206.             $user $usersManagementService->createUser(Users::AUTH_HUAWEI$authUserRawData$this->getUser());
  207.         }
  208.         $token $jwt->create($user);
  209.         return $this->json(
  210.             new AuthJWTResponse(
  211.                 AuthJWTResponse::STATUS_SUCCESS,
  212.                 $token
  213.             )
  214.         );
  215.     }
  216.     /**
  217.      * @Route("/auth/guest", name="api.auth.guest", methods={"GET"})
  218.      */
  219.     public function getAuthGuest(
  220.         Request $request,
  221.         JWTTokenManagerInterface $jwt,
  222.         UsersManagementService $usersManagementService
  223.     ): JsonResponse {
  224.         $deviceIdentity $request->get('device_identity');
  225.         if ($deviceIdentity) {
  226.             $user $usersManagementService->findOrCreateGuestUserByDeviceIdentity($deviceIdentity);
  227.         } else {
  228.             $user $usersManagementService->createGuestUser();
  229.         }
  230.         $token $jwt->create($user);
  231.         return $this->json(
  232.             new AuthJWTResponse(
  233.                 AuthJWTResponse::STATUS_SUCCESS,
  234.                 $token
  235.             )
  236.         );
  237.     }
  238. }