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

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