vendor/uvdesk/core-framework/Services/MicrosoftIntegration.php line 12

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Services;
  3. use Doctrine\ORM\EntityManagerInterface;
  4. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Microsoft\MicrosoftApp;
  5. class MicrosoftIntegration
  6. {
  7.     const MICROSOFT_OAUTH "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/authorize?client_id={client_id}&response_type=code&redirect_uri={redirect_uri}&response_mode=query&scope={scope}&state={state}";
  8.     public function __construct(EntityManagerInterface $entityManager)
  9.     {
  10.         $this->entityManager $entityManager;
  11.     }
  12.     public function getAuthorizationUrl(MicrosoftApp $app$redirectEndpoint, array $state = [])
  13.     {
  14.         $params = [
  15.             '{tenant}'       => $app->getTenantId(),
  16.             '{client_id}'    => $app->getClientId(),
  17.             '{redirect_uri}' => urlencode($redirectEndpoint),
  18.             '{scope}'        => urlencode(implode(' '$app->getApiPermissions())),
  19.         ];
  20.         if (!empty($state)) {
  21.             $params['{state}'] = urlencode(json_encode($state));
  22.         }
  23.         return strtr(self::MICROSOFT_OAUTH$params);
  24.     }
  25.     public function getAccessToken(MicrosoftApp $app$accessCode$redirectEndpoint)
  26.     {
  27.         $tenantId $app->getTenantId();
  28.         $endpoint "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";
  29.         $curlHandler curl_init();
  30.         curl_setopt($curlHandlerCURLOPT_HEADER0);
  31.         curl_setopt($curlHandlerCURLOPT_RETURNTRANSFER1);
  32.         curl_setopt($curlHandlerCURLOPT_POST1);
  33.         curl_setopt($curlHandlerCURLOPT_URL$endpoint);
  34.         curl_setopt($curlHandlerCURLOPT_POSTFIELDShttp_build_query([
  35.             'client_id'     => $app->getClientId(),
  36.             'scope'         => urldecode(implode(' '$app->getApiPermissions())),
  37.             'code'          => $accessCode,
  38.             'redirect_uri'  => $redirectEndpoint,
  39.             'grant_type'    => 'authorization_code',
  40.             'client_secret' => $app->getClientSecret(),
  41.         ]));
  42.         $curlResponse curl_exec($curlHandler);
  43.         $jsonResponse json_decode($curlResponsetrue);
  44.         curl_close($curlHandler);
  45.         return $jsonResponse;
  46.     }
  47.     public function refreshAccessToken(MicrosoftApp $app$refreshToken)
  48.     {
  49.         $tenantId $app->getTenantId();
  50.         $endpoint "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token";
  51.         $curlHandler curl_init();
  52.         curl_setopt($curlHandlerCURLOPT_HEADER0);
  53.         curl_setopt($curlHandlerCURLOPT_RETURNTRANSFER1);
  54.         curl_setopt($curlHandlerCURLOPT_POST1);
  55.         curl_setopt($curlHandlerCURLOPT_URL$endpoint);
  56.         curl_setopt($curlHandlerCURLOPT_POSTFIELDShttp_build_query([
  57.             'tenant'        => $app->getTenantId(),
  58.             'client_id'     => $app->getClientId(),
  59.             'grant_type'    => 'refresh_token',
  60.             'scope'         => urldecode(implode(' '$app->getApiPermissions())),
  61.             'refresh_token' => $refreshToken,
  62.             'client_secret' => $app->getClientSecret(),
  63.         ]));
  64.         $curlResponse curl_exec($curlHandler);
  65.         $jsonResponse json_decode($curlResponsetrue);
  66.         curl_close($curlHandler);
  67.         return $jsonResponse;
  68.     }
  69. }