You are here

function gauth_response_handler in Google Auth 7.2

Same name and namespace in other branches
  1. 7 gauth.module \gauth_response_handler()

Function to handle authentication and response from google.

Parameters

string $account_id: Account id of the account to be authenticated.

1 call to gauth_response_handler()
gauth_account_authenticate in ./gauth.module
Authenticate a google account.
1 string reference to 'gauth_response_handler'
gauth_menu in ./gauth.module
Implements hook_menu().

File

./gauth.module, line 168
Google Auth Api for drupal.

Code

function gauth_response_handler($account_id = NULL) {
  $info = libraries_load('google-api-php-client');
  if (!$info['loaded']) {
    drupal_set_message(t("Can't authenticate with google as library is missing check Status report or Readme for requirements"), 'error');
    return FALSE;
  }
  if ($account_id == NULL && isset($_SESSION['gauth_account_id'])) {
    $account_id = $_SESSION['gauth_account_id'];
  }
  elseif ($account_id) {
    $_SESSION['gauth_account_id'] = $account_id;
  }
  if ($account_id) {
    $account = gauth_account_load($account_id, FALSE);
    if (isset($account['client_id']) && isset($account['developer_key']) && isset($account['client_secret'])) {
      $client = new Google_Client();
      $client
        ->setApplicationName("Google OAuth2");
      $client
        ->setClientId($account['client_id']);
      $client
        ->setClientSecret($account['client_secret']);
      $client
        ->setRedirectUri(gauth_callback_url());
      $client
        ->setDeveloperKey($account['developer_key']);
      if ($account['access_type'] == 'offline') {
        $client
          ->setApprovalPrompt('force');
      }
      $client
        ->setAccessType($account['access_type']);
      $scopes = gauth_google_services_scopes(explode(",", $account['services']));

      // Let other modules change scopes
      drupal_alter('gauth_account_scopes', $scopes, $account['id']);
      $client
        ->addScope($scopes);
    }
    if (isset($_GET['code'])) {
      $client
        ->fetchAccessTokenWithAuthCode($_GET['code']);
      $account['access_token'] = json_encode($client
        ->getAccessToken());
      gauth_account_save($account);
      unset($_SESSION['gauth_account_id']);
      if (!user_access('administer site configuration') && module_exists('gauth_user')) {
        drupal_goto('user/' . $GLOBALS['user']->uid . '/gauth');
      }
      else {
        drupal_goto('admin/config/services/gauth_account');
      }
      drupal_set_message(t('Api Account saved'));
    }
    if ($client) {
      $auth_url = $client
        ->createAuthUrl();
      drupal_goto($auth_url);
    }
  }

  // Let other modules act of google response.
  module_invoke_all('gauth_google_response');
}