src/Authenticator/XeroAuthenticator.php line 48

Open in your IDE?
  1. <?php
  2. // src/Authenticator/XeroAuthenticator.php
  3. namespace App\Authenticator;
  4. use App\Repository\XeroStorageRepository;
  5. use App\String\Constant;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
  8. use League\OAuth2\Client\Provider\GenericProvider;
  9. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\HttpFoundation\RedirectResponse;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Response;
  14. class XeroAuthenticator
  15. {
  16.     private $provider;
  17.     private $requestStack;
  18.     private $entityManager;
  19.     private $xeroEm;
  20.     public function __construct(RequestStack $requestStackContainerBagInterface $params,
  21.         ManagerRegistry $doctrineXeroStorageRepository $xeroStorageRepository)
  22.     {
  23.         $this->provider = new GenericProvider([
  24.             'clientId'                => $params->get('app.xero_client_id'),
  25.             'clientSecret'            => $params->get('app.xero_client_secret'),
  26.             'redirectUri'             => Constant::XERO_REDIRECT_URL,
  27.             'urlAuthorize'            => Constant::XERO_URL_AUTHORIZE,
  28.             'urlAccessToken'          => Constant::XERO_URL_ACCESS_TOKEN,
  29.             'urlResourceOwnerDetails' => Constant::XERO_URL_RESOURCE,
  30.         ]);
  31.         $this->requestStack $requestStack;
  32.         $this->entityManager $doctrine->getManager();
  33.         $this->xeroEm $xeroStorageRepository;
  34.     }
  35.     /**
  36.      * Return semantic Xero provider
  37.      *
  38.      * @return GenericProvider
  39.      */
  40.     public function getProvider(): GenericProvider
  41.     {
  42.         return $this->provider;
  43.     }
  44.     public function getStorage()
  45.     {
  46.         // New way of storing storage through DB
  47.         $storage $this->xeroEm->findOneBy(['tag' => 'primary']);
  48.         if ($storage){
  49.             $xeroTenantId $storage->getTenantId();
  50.             if ($storage->getHasExpired()) {
  51.                 $newAccessToken $this->provider->getAccessToken('refresh_token', [
  52.                     'refresh_token' => $storage->getRefreshToken()
  53.                 ]);
  54.                 $storage->setAccessToken($newAccessToken->getToken())
  55.                     ->setExpires($newAccessToken->getExpires())
  56.                     ->setTenantId($xeroTenantId)
  57.                     ->setRefreshToken($newAccessToken->getRefreshToken())
  58.                     ->setIdToken($newAccessToken->getValues()["id_token"]);
  59.             
  60.                 $this->entityManager->persist($storage);
  61.                 $this->entityManager->flush();
  62.             }
  63.             return $storage;
  64.         }
  65.         return new JsonResponse('Storage is empty'400);
  66.     }
  67.     public function authenticate(): Response {
  68.         $session $this->requestStack->getSession();
  69.         try {
  70.             $options = [
  71.                 'scope' => ['openid email profile offline_access accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments']
  72.             ];
  73.             $authorizationUrl $this->provider->getAuthorizationUrl($options); // get authentication url before getting state
  74.             $session->set('oauth2state'$this->provider->getState()); // Set authentication state
  75.             return new RedirectResponse($authorizationUrl301);
  76.         } catch (IdentityProviderException $e) {
  77.             return new JsonResponse($e->getMessage(), 400);
  78.         }
  79.     }
  80. }