You are here

public function OpenidConnectWindowsAadClient::retrieveTokens in OpenID Connect Microsoft Azure Active Directory client 7

Overrides OpenIDConnectClientInterface::retrieveIDToken().

Overrides OpenIDConnectClientBase::retrieveTokens

File

plugins/openid_connect_client/windows_aad/OpenidConnectWindowsAadClient.class.php, line 78
OpenID Connect client for Windows Azure AD.

Class

OpenidConnectWindowsAadClient
Class OpenidConnectWindowsAadClient adds the client to OpenID Connect.

Code

public function retrieveTokens($authorization_code) {

  // Exchange `code` for access token and ID token.
  $redirect_uri = OPENID_CONNECT_REDIRECT_PATH_BASE . '/' . $this->name;
  $post_data = array(
    'code' => $authorization_code,
    'client_id' => $this
      ->getSetting('client_id'),
    'client_secret' => $this
      ->getSetting('client_secret'),
    'redirect_uri' => url($redirect_uri, array(
      'absolute' => TRUE,
    )),
    'grant_type' => 'authorization_code',
  );

  // Add Graph API as resource if option is set.
  if ($this
    ->getSetting('userinfo_graph_api_wa') == 1) {
    $post_data['resource'] = 'https://graph.windows.net';
  }
  $request_options = array(
    'method' => 'POST',
    'data' => drupal_http_build_query($post_data),
    'timeout' => 15,
    'headers' => array(
      'Content-Type' => 'application/x-www-form-urlencoded',
    ),
  );
  $endpoints = $this
    ->getEndpoints();
  $response = drupal_http_request($endpoints['token'], $request_options);
  if (!isset($response->error) && $response->code == 200) {
    $response_data = drupal_json_decode($response->data);
    return array(
      'id_token' => $response_data['id_token'],
      'access_token' => $response_data['access_token'],
      'refresh_token' => isset($response_data['refresh_token']) ? $response_data['refresh_token'] : FALSE,
      'expire' => REQUEST_TIME + $response_data['expires_in'],
    );
  }
  else {
    openid_connect_log_request_error(__FUNCTION__, $this->name, $response);
    return FALSE;
  }
}