You are here

protected function WindowsAad::retrieveGroupInfo in OpenID Connect Microsoft Azure Active Directory client 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/OpenIDConnectClient/WindowsAad.php \Drupal\openid_connect_windows_aad\Plugin\OpenIDConnectClient\WindowsAad::retrieveGroupInfo()

Calls a graph api to retrieve teh user's group membership information.

Parameters

string $access_token: An access token string.

Return value

array An array of group informaion.

1 call to WindowsAad::retrieveGroupInfo()
WindowsAad::retrieveUserInfo in src/Plugin/OpenIDConnectClient/WindowsAad.php
Implements OpenIDConnectClientInterface::retrieveUserInfo().

File

src/Plugin/OpenIDConnectClient/WindowsAad.php, line 432

Class

WindowsAad
Generic OpenID Connect client.

Namespace

Drupal\openid_connect_windows_aad\Plugin\OpenIDConnectClient

Code

protected function retrieveGroupInfo($access_token) {

  // By default or if an error occurs return empty group information.
  $group_data = [];
  switch ($this->configuration['userinfo_graph_api_wa']) {
    case 1:
      $uri = 'https://graph.windows.net/me/memberOf?api-version=1.6';
      break;
    case 2:
      $uri = 'https://graph.microsoft.com/v1.0/me/memberOf';
      break;
    default:
      $uri = FALSE;
      break;
  }
  if ($uri) {

    // Perform the request.
    $options = [
      'method' => 'GET',
      'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' . $access_token,
      ],
    ];
    $client = $this->httpClient;
    try {
      $response = $client
        ->get($uri, $options);
      $response_data = (string) $response
        ->getBody();

      // Group Information.
      $group_data = json_decode($response_data, TRUE);
    } catch (RequestException $e) {
      $variables = [
        '@api' => $uri,
        '@error_message' => $e
          ->getMessage(),
      ];
      $this->loggerFactory
        ->get('openid_connect_windows_aad')
        ->error('Failed to retrieve AD group information from graph api (@api). Details: @error_message', $variables);
    }
  }

  // Return group information or an empty array.
  return $group_data;
}