src/Service/Authorization/MybizRequestAuthorizationChecker.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\Service\Authorization;
  3. use App\Dto\Authorization\AuthorizationHeaderDto;
  4. use App\Entity\Space;
  5. use App\Exception\Authorization\MybizApplicationVersionNotSupportedException;
  6. use App\Exception\Authorization\MybizAuthorizationException;
  7. use App\Exception\Maintenance\MaintenanceException;
  8. use App\Repository\ApplicationRepository;
  9. use App\Repository\ApplicationVersionRepository;
  10. use App\Repository\SpaceRepository;
  11. use App\Service\Lock\MaintenanceLocker;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. use Symfony\Component\HttpFoundation\JsonResponse;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. class MybizRequestAuthorizationChecker
  18. {
  19.     private ParameterBagInterface $parameter;
  20.     private TranslatorInterface $translator;
  21.     private ApplicationRepository $applicationRepository;
  22.     private ApplicationVersionRepository $applicationVersionRepository;
  23.     private SpaceRepository $spaceRepository;
  24.     private MaintenanceLocker $maintenanceLocker;
  25.     public function __construct(
  26.         ParameterBagInterface        $parameter,
  27.         TranslatorInterface          $translator,
  28.         ApplicationRepository        $applicationRepository,
  29.         ApplicationVersionRepository $applicationVersionRepository,
  30.         SpaceRepository              $spaceRepository,
  31.         MaintenanceLocker            $maintenanceLocker
  32.     )
  33.     {
  34.         $this->maintenanceLocker $maintenanceLocker;
  35.         $this->parameter $parameter;
  36.         $this->translator $translator;
  37.         $this->applicationRepository $applicationRepository;
  38.         $this->applicationVersionRepository $applicationVersionRepository;
  39.         $this->spaceRepository $spaceRepository;
  40.     }
  41.     /**
  42.      * @param Request $request
  43.      * @return void
  44.      * @throws MybizApplicationVersionNotSupportedException
  45.      * @throws MybizAuthorizationException
  46.      */
  47.     public function checkAuthorization(
  48.         Request $request
  49.     ): void
  50.     {
  51.         $this->checkHeaderValidity(AuthorizationHeaderDto::generateFromRequest($request));
  52.     }
  53.     /**
  54.      * @param AuthorizationHeaderDto $authorizationHeaderDto
  55.      * @throws MybizApplicationVersionNotSupportedException
  56.      * @throws MybizAuthorizationException
  57.      */
  58.     public function checkHeaderValidity(
  59.         AuthorizationHeaderDto $authorizationHeaderDto
  60.     ): void
  61.     {
  62.         if ($this->maintenanceLocker->isLocked()) {
  63.             throw new MaintenanceException($this->translator->trans("authorization.maintenance", [], "authorization"$authorizationHeaderDto->getLanguage()));
  64.         }
  65.         $space $this->spaceRepository->findOneBy([
  66.             "name" => $authorizationHeaderDto->getName()
  67.         ]);
  68.         if (null === $space) {
  69.             throw new MybizAuthorizationException($this->translator->trans("authorization.space.not_found", [], "authorization"$authorizationHeaderDto->getLanguage()));
  70.         }
  71.         if (false === $space->isEnabled()) {
  72.             throw new MybizAuthorizationException($this->translator->trans("authorization.space.inactive", [], "authorization"$authorizationHeaderDto->getLanguage()));
  73.         }
  74.         // On va checker les versions (uniquement sur la partie mobile)
  75.         try{
  76.             $this->checkMobileApplicationVersion(
  77.                 $space,
  78.                 $authorizationHeaderDto
  79.             );
  80.         }catch (\Throwable $e){
  81.             throw new MybizAuthorizationException($e->getMessage());
  82.         }
  83.         if (true === $this->parameter->get("app_mybiz_api_check_bearer")) {
  84.             $calculatedBearer AuthorizationHashedStringProvider::getAuthorizationHashedString($space->getToken(), $authorizationHeaderDto);
  85.             if ($calculatedBearer !== $authorizationHeaderDto->getToken()) {
  86.                 throw new MybizAuthorizationException($this->translator->trans("authorization.token_invalid", [], "authorization"$authorizationHeaderDto->getLanguage()));
  87.             }
  88.         }
  89.     }
  90.     /**
  91.      * @param Space $space
  92.      * @param AuthorizationHeaderDto $authorizationHeaderDto
  93.      * @return void
  94.      */
  95.     private function checkMobileApplicationVersion(
  96.         Space $space,
  97.         AuthorizationHeaderDto $authorizationHeaderDto
  98.     ): void
  99.     {
  100.         // Si le provider contient "web" alors on ne check pas la version
  101.         if (str_contains($authorizationHeaderDto->getProvider(), "web")) {
  102.             return;
  103.         }
  104.         $application $this->applicationRepository->findOneBy([
  105.             "space" => $space,
  106.             "name" => $authorizationHeaderDto->getProvider()
  107.         ]);
  108.         if (null === $application) {
  109.             throw new MybizAuthorizationException($this->translator->trans("authorization.application.not_found", [], "authorization"$authorizationHeaderDto->getLanguage()));
  110.         }
  111.         if (false === $application->isEnabled()) {
  112.             throw new MybizAuthorizationException($this->translator->trans("authorization.application.inactive", [], "authorization"$authorizationHeaderDto->getLanguage()));
  113.         }
  114.         $applicationVersion $this->applicationVersionRepository->findOneBy([
  115.             "application" => $application,
  116.             "versionNumber" => $authorizationHeaderDto->getVersionNumber()
  117.         ]);
  118.         if (null === $applicationVersion) {
  119.             throw new MybizAuthorizationException($this->translator->trans("authorization.application_version.not_found", [], "authorization"$authorizationHeaderDto->getLanguage()));
  120.         }
  121.         if (false === $applicationVersion->isEnabled()) {
  122.             throw new MybizApplicationVersionNotSupportedException($this->translator->trans("authorization.application_version.inactive", [], "authorization"$authorizationHeaderDto->getLanguage()));
  123.         }
  124.     }
  125.     /**
  126.      * @param \Throwable $e
  127.      * @return JsonResponse
  128.      */
  129.     public function getDefaultErrorMessage(\Throwable $e): JsonResponse
  130.     {
  131.         return new JsonResponse([
  132.             "exception" => $e->getMessage(),
  133.             "error" => $this->translator->trans("authorization.generic_error", [], "authorization"// Par défaut en anglais
  134.         ], Response::HTTP_FORBIDDEN);
  135.     }
  136.     /**
  137.      * @return JsonResponse
  138.      */
  139.     public function getUnknownJWTErrorMessage(): JsonResponse
  140.     {
  141.         return new JsonResponse([
  142.             "error" => $this->translator->trans("authorization.jwt_error", [], "authorization"// Par défaut en anglais
  143.         ], Response::HTTP_UNAUTHORIZED);
  144.     }
  145.     /**
  146.      * @param \Throwable $e
  147.      * @return JsonResponse
  148.      */
  149.     public function getParseErrorMessage(\Throwable $e): JsonResponse
  150.     {
  151.         return new JsonResponse([
  152.             "exception" => $e->getMessage(),
  153.             "error" => $this->translator->trans("authorization.generic_parse_error", [], "authorization"// Par défaut en anglais
  154.         ], Response::HTTP_FORBIDDEN);
  155.     }
  156. }