You are here

gauth.module in Google Auth 7

Same filename and directory in other branches
  1. 8 gauth.module
  2. 6 gauth.module
  3. 7.2 gauth.module

Google Auth Api for drupal.

File

gauth.module
View source
<?php

/**
 * @file
 * Google Auth Api for drupal.
 */

/**
 * Implements hook_init().
 */
function gauth_init() {
  $path = libraries_get_path('google-api-php-client');
  if ($path) {
    set_include_path($path . "/src/" . PATH_SEPARATOR . get_include_path());
  }
}

/**
 * Implements hook_help().
 */
function gauth_help($path, $arg) {
  switch ($path) {
    case 'admin/help#gauth':
      $path = $GLOBALS['base_url'] . '/' . drupal_get_path('module', 'gauth');
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t("This module allows you to authenticate with google and use this authentication to carry other api requests. This module will help you to manage accounts, authenticate with google (i.e. get access token) and use this authentication to carry api requests in other modules.") . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<p>' . t("This module allows you to enter google account details like client id, client secret key, developer key, select google services to be enabled and gets the OAuth2 access token from google.  You can also revoke access(unauthenticate) a account.") . '</p>';
      $output .= '<p>' . t("For more details read the <a href='@url'>README.txt</a> file in the Google Auth module directory.", array(
        '@url' => "{$path}/README.txt",
      )) . '</p>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function gauth_menu() {
  $items = array();
  $items['admin/config/services/gauth_account'] = array(
    'title' => 'Google Account Settings',
    'description' => 'Google Account settings.',
    'page callback' => 'gauth_account_list',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'gauth.admin.inc',
  );
  $items['admin/config/services/gauth_account/add'] = array(
    'title' => 'Add Account',
    'description' => 'Google Account Add.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gauth_account_edit_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'gauth.admin.inc',
  );
  $items['admin/config/services/gauth_account/edit/%'] = array(
    'title' => 'Google Account Edit',
    'description' => 'Google Account Edit.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gauth_account_edit_form',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'gauth.admin.inc',
  );
  $items['admin/config/services/gauth_account/delete/%'] = array(
    'title' => 'Google Account Delete',
    'description' => 'Google Account delete.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gauth_account_delete_form',
      5,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'gauth.admin.inc',
  );
  $items['gauth/response_handler'] = array(
    'page callback' => 'gauth_response_handler',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  $items['gauth/revoke_token/%'] = array(
    'title' => 'Google Account Token Revoke',
    'description' => 'Revoke a access token.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'gauth_account_token_revoke_form',
      2,
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'gauth.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_theme().
 */
function gauth_theme() {
  return array(
    // Theme functions in gauth.admin.inc.
    'gauth_account_list' => array(
      'variables' => array(
        'accounts' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_libraries_info().
 */
function gauth_libraries_info() {
  $config_dir = conf_path();
  $lib_path = 'sites/all/libraries/google-api-php-client';
  if (file_exists(DRUPAL_ROOT . '/' . $config_dir . '/libraries')) {
    $lib_path = $config_dir . '/libraries/google-api-php-client';
  }
  $file_path = 'src/Google/Client.php';
  $include_path = 'vendor/autoload.php';
  $line_number = 42;
  if (!file_exists(DRUPAL_ROOT . '/' . $lib_path . '/vendor')) {

    // Composer install;
    $file_path = 'google/apiclient/src/Google/Client.php';
    $include_path = 'autoload.php';
    $line_number = 40;
  }
  $libraries['google-api-php-client'] = array(
    'name' => 'Google OAuth2 Library',
    'vendor url' => 'http://code.google.com/p/google-api-php-client',
    'download url' => 'https://github.com/google/google-api-php-client/releases',
    'version arguments' => array(
      'file' => $file_path,
      'pattern' => '/LIBVER = "([0-9\\.a-z\\-]+)/',
      'lines' => $line_number,
    ),
    'files' => array(
      'php' => array(
        $include_path,
      ),
    ),
  );
  return $libraries;
}

/**
 * Implements hook_user_delete().
 */
function gauth_user_delete($user) {
  $gauth_account = gauth_account_load_by_uid($user->uid);
  if ($gauth_account) {
    gauth_account_delete($gauth_account);
  }
}

/**
 * Implements hook_menu_site_status_alter().
 */
function gauth_menu_site_status_alter(&$menu_site_status, $path) {
  if ($menu_site_status == MENU_SITE_OFFLINE && user_is_anonymous() && $path == 'gauth/response_handler') {

    // Disable offline mode.
    $menu_site_status = MENU_SITE_ONLINE;
  }
}

/**
 * Implements hook_flush_caches().
 */
function gauth_flush_caches() {
  if (db_table_exists('cache_gauth_scopes')) {
    return array(
      'cache_gauth_scopes',
    );
  }
}

/**
 * Function to handle authentication and response from google.
 *
 * @param string $account_id
 *   Account id of the account to be authenticated.
 */
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');
}

/**
 * Returns a array services api versions.
 */
function gauth_google_services_scopes($services) {
  if (!$services) {
    return FALSE;
  }
  if (!is_array($services)) {
    $services = array(
      $services,
    );
  }
  $cache = cache_get_multiple($services, 'cache_gauth_scopes');
  if (!empty($cache)) {
    $scopes = array();
    foreach ($cache as $s => $c) {
      if ($c->data) {
        $scopes = array_merge($scopes, $c->data);
      }
    }
    return $scopes;
  }
  else {
    $services = array_flip($services);
    $scopes = _gauth_read_scope_info();
    return array_intersect_key($scopes, $services);
  }
}

/**
 * Returns a array services api names.
 */
function gauth_google_services_names($services = FALSE) {
  $names = variable_get('gauth_google_api_services', array());
  if (empty($names)) {
    _gauth_read_scope_info();
    $names = variable_get('gauth_google_api_services', array());
  }
  if (!$services) {
    return $names;
  }
  else {
    $service_names = array();
    foreach (explode(",", $services) as $service) {
      $service_names[trim($service)] = $names[$service];
    }
    return $service_names;
  }
}

/**
 * Helper function which reads the installed library and discovery api to build cache for scopes and names.
 */
function _gauth_read_scope_info() {
  $library = libraries_load('google-api-php-client');
  $path = $library['library path'] . '/vendor/google/apiclient-services/src/Google/Service/';
  $files = scandir($path);
  $names = array(
    'abusiveexperiencereport' => 'Abusive Experience Report API',
    'acceleratedmobilepageurl' => 'Accelerated Mobile Pages (AMP) URL API',
    'accessapproval' => 'Access Approval API',
    'accesscontextmanager' => 'Access Context Manager API',
    'adexchangebuyer' => 'Ad Exchange Buyer API',
    'adexchangebuyer2' => 'Ad Exchange Buyer API II',
    'adexperiencereport' => 'Ad Experience Report API',
    'admin' => 'Admin Reports API',
    'adsense' => 'AdSense Management API',
    'adsensehost' => 'AdSense Host API',
    'alertcenter' => 'G Suite Alert Center API',
    'analytics' => 'Google Analytics API',
    'analyticsreporting' => 'Analytics Reporting API',
    'androiddeviceprovisioning' => 'Android Device Provisioning Partner API',
    'androidenterprise' => 'Google Play EMM API',
    'androidmanagement' => 'Android Management API',
    'androidpublisher' => 'Google Play Developer API',
    'appengine' => 'App Engine Admin API',
    'appsactivity' => 'Drive Activity API',
    'bigquery' => 'BigQuery API',
    'bigqueryconnection' => 'BigQuery Connection API',
    'bigquerydatatransfer' => 'BigQuery Data Transfer API',
    'bigqueryreservation' => 'BigQuery Reservation API',
    'bigtableadmin' => 'Cloud Bigtable Admin API',
    'binaryauthorization' => 'Binary Authorization API',
    'blogger' => 'Blogger API',
    'books' => 'Books API',
    'calendar' => 'Calendar API',
    'chat' => 'Hangouts Chat API',
    'civicinfo' => 'Google Civic Information API',
    'classroom' => 'Google Classroom API',
    'cloudasset' => 'Cloud Asset API',
    'cloudbilling' => 'Cloud Billing API',
    'cloudbuild' => 'Cloud Build API',
    'clouddebugger' => 'Stackdriver Debugger API',
    'clouderrorreporting' => 'Stackdriver Error Reporting API',
    'cloudfunctions' => 'Cloud Functions API',
    'cloudidentity' => 'Cloud Identity API',
    'cloudiot' => 'Cloud IoT API',
    'cloudkms' => 'Cloud Key Management Service (KMS) API',
    'cloudprivatecatalog' => 'Cloud Private Catalog API',
    'cloudprivatecatalogproducer' => 'Cloud Private Catalog Producer API',
    'cloudprofiler' => 'Stackdriver Profiler API',
    'cloudresourcemanager' => 'Cloud Resource Manager API',
    'cloudscheduler' => 'Cloud Scheduler API',
    'cloudsearch' => 'Cloud Search API',
    'cloudshell' => 'Cloud Shell API',
    'cloudtasks' => 'Cloud Tasks API',
    'cloudtrace' => 'Stackdriver Trace API',
    'commentanalyzer' => 'Perspective Comment Analyzer API',
    'composer' => 'Cloud Composer API',
    'compute' => 'Compute Engine API',
    'container' => 'Kubernetes Engine API',
    'containeranalysis' => 'Container Analysis API',
    'content' => 'Content API for Shopping',
    'customsearch' => 'CustomSearch API',
    'dataflow' => 'Dataflow API',
    'datafusion' => 'Cloud Data Fusion API',
    'dataproc' => 'Cloud Dataproc API',
    'datastore' => 'Cloud Datastore API',
    'deploymentmanager' => 'Google Cloud Deployment Manager API',
    'dfareporting' => 'DCM/DFA Reporting And Trafficking API',
    'dialogflow' => 'Dialogflow API',
    'digitalassetlinks' => 'Digital Asset Links API',
    'discovery' => 'API Discovery Service',
    'dlp' => 'Cloud Data Loss Prevention (DLP) API',
    'dns' => 'Google Cloud DNS API',
    'docs' => 'Google Docs API',
    'domainsrdap' => 'Domains RDAP API',
    'doubleclickbidmanager' => 'DoubleClick Bid Manager API',
    'doubleclicksearch' => 'Search Ads 360 API',
    'drive' => 'Drive API',
    'driveactivity' => 'Drive Activity API',
    'factchecktools' => 'Fact Check Tools API',
    'fcm' => 'Firebase Cloud Messaging API',
    'file' => 'Cloud Filestore API',
    'firebase' => 'Firebase Management API',
    'firebasedynamiclinks' => 'Firebase Dynamic Links API',
    'firebasehosting' => 'Firebase Hosting API',
    'firebaserules' => 'Firebase Rules API',
    'firestore' => 'Cloud Firestore API',
    'fitness' => 'Fitness',
    'fusiontables' => 'Fusion Tables API',
    'games' => 'Google Play Game Services API',
    'gamesConfiguration' => 'Google Play Game Services Publishing API',
    'gamesManagement' => 'Google Play Game Services Management API',
    'genomics' => 'Genomics API',
    'gmail' => 'Gmail API',
    'groupsmigration' => 'Groups Migration API',
    'groupssettings' => 'Groups Settings API',
    'healthcare' => 'Cloud Healthcare API',
    'homegraph' => 'HomeGraph API',
    'iam' => 'Identity and Access Management (IAM) API',
    'iamcredentials' => 'IAM Service Account Credentials API',
    'iap' => 'Cloud Identity-Aware Proxy API',
    'identitytoolkit' => 'Google Identity Toolkit API',
    'indexing' => 'Indexing API',
    'jobs' => 'Cloud Talent Solution API',
    'kgsearch' => 'Knowledge Graph Search API',
    'language' => 'Cloud Natural Language API',
    'libraryagent' => 'Library Agent API',
    'licensing' => 'Enterprise License Manager API',
    'logging' => 'Stackdriver Logging API',
    'manufacturers' => 'Manufacturer Center API',
    'mirror' => 'Google Mirror API',
    'ml' => 'Cloud Machine Learning Engine',
    'monitoring' => 'Stackdriver Monitoring API',
    'oauth2' => 'Google OAuth2 API',
    'osconfig' => 'Cloud OS Config API',
    'oslogin' => 'Cloud OS Login API',
    'pagespeedonline' => 'PageSpeed Insights API',
    'people' => 'People API',
    'playcustomapp' => 'Google Play Custom App Publishing API',
    'plus' => 'Google+ API',
    'plusDomains' => 'Google+ Domains API',
    'poly' => 'Poly API',
    'proximitybeacon' => 'Proximity Beacon API',
    'pubsub' => 'Cloud Pub/Sub API',
    'redis' => 'Google Cloud Memorystore for Redis API',
    'remotebuildexecution' => 'Remote Build Execution API',
    'replicapool' => 'Replica Pool API',
    'reseller' => 'Enterprise Apps Reseller API',
    'run' => 'Cloud Run API',
    'runtimeconfig' => 'Cloud Runtime Configuration API',
    'safebrowsing' => 'Safe Browsing API',
    'script' => 'Apps Script API',
    'searchconsole' => 'Google Search Console URL Testing Tools API',
    'securitycenter' => 'Cloud Security Command Center API',
    'servicebroker' => 'Service Broker API',
    'serviceconsumermanagement' => 'Service Consumer Management API',
    'servicecontrol' => 'Service Control API',
    'servicemanagement' => 'Service Management API',
    'servicenetworking' => 'Service Networking API',
    'serviceusage' => 'Service Usage API',
    'sheets' => 'Google Sheets API',
    'siteVerification' => 'Google Site Verification API',
    'slides' => 'Google Slides API',
    'sourcerepo' => 'Cloud Source Repositories API',
    'spanner' => 'Cloud Spanner API',
    'speech' => 'Cloud Speech-to-Text API',
    'sqladmin' => 'Cloud SQL Admin API',
    'storage' => 'Cloud Storage JSON API',
    'storagetransfer' => 'Storage Transfer API',
    'streetviewpublish' => 'Street View Publish API',
    'surveys' => 'Surveys API',
    'tagmanager' => 'Tag Manager API',
    'tasks' => 'Tasks API',
    'testing' => 'Cloud Testing API',
    'texttospeech' => 'Cloud Text-to-Speech API',
    'toolresults' => 'Cloud Tool Results API',
    'tpu' => 'Cloud TPU API',
    'translate' => 'Cloud Translation API',
    'urlshortener' => 'URL Shortener API',
    'vault' => 'G Suite Vault API',
    'verifiedaccess' => 'Chrome Verified Access API',
    'videointelligence' => 'Cloud Video Intelligence API',
    'vision' => 'Cloud Vision API',
    'webfonts' => 'Google Fonts Developer API',
    'webmasters' => 'Search Console API',
    'websecurityscanner' => 'Web Security Scanner API',
    'youtube' => 'YouTube Data API',
    'youtubeAnalytics' => 'YouTube Analytics API',
    'youtubereporting' => 'YouTube Reporting API�',
  );
  $api_names = array();
  foreach ($files as $file) {
    if (substr($file, -3) == 'php') {
      require_once $path . $file;
      $cname = substr($file, 0, -4);
      $tname = strtolower($cname);
      if (isset($names[$tname])) {
        $api_names[$tname] = $names[$tname];
      }
      else {
        $api_names[$tname] = $cname;
      }
    }
  }
  $scopes = array();
  $client = gauth_client_get();
  foreach (get_declared_classes() as $class) {
    if (is_subclass_of($class, 'Google_Service')) {
      $classes[] = $class;
      $temp = new ReflectionClass($class);
      $instance = $temp
        ->newInstance($client);
      $aname = $instance->serviceName;
      if (!isset($api_names[$aname])) {
        $aname = $temp
          ->getName();
        $aname = strtolower(str_replace('Google_Service_', '', $aname));
      }
      $scopes[$aname] = $temp
        ->getConstants();
      cache_set($aname, $scopes[$aname], 'cache_gauth_scopes');
    }
  }
  variable_set('gauth_google_api_services', $api_names);
  return $scopes;
}

/**
 * Checks https settings and returns appropriate callback url.
 */
function gauth_callback_url() {
  global $is_https;
  return url('gauth/response_handler', array(
    'https' => $is_https,
    'absolute' => TRUE,
  ));
}

/*** API Functions start ***/

/**
 * Get an array of all accounts or load a specific account.
 *
 * @param string $account_id
 *   Name or id of the account to be loaded
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 * @param array $fields
 *   Array of fields to be retrieved from the database
 *
 * @returns An array of accounts and their details.
 */
function gauth_account_load($account_id = NULL, $by_name = TRUE, $fields = 'gauth_accounts') {
  $accounts = array();
  if ($by_name) {
    $filter = 'name';
  }
  else {
    $filter = 'id';
  }
  $query = db_select('gauth_accounts');
  if (is_array($fields)) {
    $query
      ->fields('gauth_accounts', $fields);
  }
  else {
    $query
      ->fields('gauth_accounts');
  }
  if ($account_id) {
    $accounts = $query
      ->condition($filter, $account_id, '=')
      ->execute()
      ->fetchAssoc();
  }
  else {
    $accounts = $query
      ->orderBy('id')
      ->execute()
      ->fetchAllAssoc($filter);
  }
  return $accounts;
}

/**
 * Save an account.
 *
 * @param array $account
 *   Account array that needs to be saved
 * If you want to create a new account omit the id field in the array
 * If you want to update existing account do have the id field
 *
 * @returns a account array
 *
 * Same account array reflects the changes
 */
function gauth_account_save(&$account) {
  global $user;
  if (isset($account['id'])) {
    $fields = array(
      'id' => $account['id'],
      'uid' => isset($account['uid']) ? $account['uid'] : $user->uid,
    );
    if (isset($_SESSION['gauth_account_id']) && isset($account['access_token']) && $_SESSION['gauth_account_id'] == $account['id']) {
      $fields['access_token'] = $account['access_token'];
      $fields['is_authenticated'] = TRUE;
    }
    else {
      if (isset($account['name'])) {
        $fields['name'] = check_plain($account['name']);
      }
      $old_account = gauth_account_load($account['id'], FALSE);
      $is_authenticated = $old_account['is_authenticated'];
      if (isset($account['client_id'])) {
        $fields['client_id'] = check_plain($account['client_id']);
        if ($is_authenticated && $old_account['client_id'] != $account['client_id']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['client_secret'])) {
        $fields['client_secret'] = check_plain($account['client_secret']);
        if ($is_authenticated && $old_account['client_secret'] != $account['client_secret']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['developer_key'])) {
        $fields['developer_key'] = check_plain($account['developer_key']);
        if ($is_authenticated && $old_account['developer_key'] != $account['developer_key']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['services'])) {
        if (is_array($account['services'])) {
          $account['services'] = implode(",", $account['services']);
        }
        $fields['services'] = check_plain($account['services']);
        if ($is_authenticated && $old_account['services'] != $account['services']) {
          $is_authenticated = FALSE;
        }
      }
      if (isset($account['access_type'])) {
        $fields['access_type'] = check_plain($account['access_type']);
        if ($is_authenticated && $old_account['access_type'] != $account['access_type']) {
          $is_authenticated = FALSE;
        }
      }
      $fields['is_authenticated'] = $is_authenticated;
    }

    // Let other modules modify the account before saving existing account.
    foreach (module_implements('gauth_account_update') as $module) {
      $function = $module . '_gauth_account_update';
      $function($fields, gauth_account_load($fields['id'], FALSE));
    }
    if (drupal_write_record('gauth_accounts', $fields, 'id') == SAVED_UPDATED) {
      return $fields;
    }
    else {
      return FALSE;
    }
  }
  else {
    if (!isset($account['name'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Name is required for creating new account',
      );
    }
    if (!isset($account['client_id'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Client Id can\'t be Null',
      );
    }
    if (!isset($account['client_secret'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Client Secret can\'t be Null',
      );
    }
    if (!isset($account['developer_key'])) {
      return array(
        'is_error' => TRUE,
        'message' => 'Developer Key can\'t be Null',
      );
    }
    $fields = array(
      'name' => check_plain($account['name']),
      'developer_key' => check_plain($account['developer_key']),
      'client_id' => check_plain($account['client_id']),
      'client_secret' => check_plain($account['client_secret']),
      'access_type' => check_plain($account['access_type']),
      'uid' => $user->uid,
    );
    if (is_array($account['services'])) {
      $account['services'] = implode(",", $account['services']);
    }
    $fields['services'] = check_plain($account['services']);
    $accounts = gauth_account_load(NULL, TRUE, array(
      'name',
    ));
    $accounts = array_keys($accounts);
    if (in_array($account['name'], $accounts)) {
      return array(
        'is_error' => TRUE,
        'message' => 'Name is already in use. Please choose a unique name for the account',
      );
    }

    // Let other modules modify the account before saving new account.
    module_invoke_all('gauth_account_insert', $fields);
    if (drupal_write_record('gauth_accounts', $fields) == SAVED_NEW) {
      return $fields;
    }
    else {
      return FALSE;
    }
  }
}

/**
 * Delete an account.
 *
 * @param string $account_id
 *   Name or id of the account to be deleted
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 */
function gauth_account_delete($account_id, $by_name = TRUE) {
  if ($by_name) {
    $filter = 'name';
  }
  else {
    $filter = 'id';
  }

  // Let other modules perform delete for associated data if any.
  module_invoke_all('gauth_account_delete', gauth_account_load($account_id, $by_name));
  return db_delete("gauth_accounts")
    ->condition($filter, $account_id, '=')
    ->execute();
}

/**
 * Check if an account is authenticated or not.
 *
 * @param string $account_id
 *   Name or id of the account to be checked
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 */
function gauth_account_is_authenticated($account_id, $by_name = TRUE) {
  return gauth_account_load($account_id, $by_name, array(
    'is_authenticated',
  ));
}

/**
 * Get Google_client object of an account.
 *
 * @param string $account_id
 *   Name or id of the account which will be used to create google client.
 *   Account array can also be passed with all parameters
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 */
function gauth_client_get($account_id = NULL, $by_name = TRUE) {
  $info = libraries_load('google-api-php-client');
  if (!$info['loaded']) {
    drupal_set_message(t("Can't create client object as library is missing check Status report or Readme for requirements"), 'error');
    return FALSE;
  }
  if ($account_id == NULL) {
    $client = new Google_Client();
  }
  else {
    if (is_array($account_id)) {
      $account = $account_id;
    }
    else {
      $account = gauth_account_load($account_id, $by_name);
    }
    $client = new Google_Client();
    $client
      ->setClientId($account['client_id']);
    $client
      ->setAccessToken($account['access_token']);
    $client
      ->setAccessType($account['access_type']);
    $client
      ->setClientSecret($account['client_secret']);
    $client
      ->setDeveloperKey($account['developer_key']);
    if ($account['access_type'] == 'offline') {
      $client
        ->setApprovalPrompt('force');
    }
  }
  $client
    ->setApplicationName("Google OAuth2");
  $client
    ->setRedirectUri(gauth_callback_url());
  if ($account_id == NULL) {
    return $client;
  }
  if ($client
    ->isAccessTokenExpired()) {
    if ($client
      ->getRefreshToken() != '') {

      // Access Type is Offline
      $client
        ->refreshToken($client
        ->getRefreshToken());
      $token = $client
        ->getAccessToken();
      $account['access_token'] = json_encode($token);
      drupal_write_record('gauth_accounts', $account, 'id');
    }
    else {
      $names = explode('|', $account['name']);
      if ($names[count($names) - 1] == $GLOBALS['user']->uid) {

        // Services account
        gauth_account_authenticate($account_id, $by_name);
      }
      else {
        $client
          ->revokeToken();
        $account['is_authenticated'] = FALSE;
        $account['access_token'] = '';
        drupal_write_record('gauth_accounts', $account, 'id');
        drupal_set_message(t('Access token is expired. If you are admin then you need to authenticate again. Consider configuring access type to offline.'));
      }
    }
  }
  return $client;
}

/**
 * Authenticate a google account.
 *
 * @param string $account_id
 *   Name or id of the account which will be used to create google client
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 */
function gauth_account_authenticate($account_id, $by_name) {
  if ($by_name) {
    $account = gauth_account_load($account_id, $by_name);
    gauth_response_handler($account['id']);
  }
  else {
    gauth_response_handler($account_id);
  }
}

/**
 * Unauthenticate an account.
 *
 * @param string $account_id
 *   Name or id of the account to be unauthenticated
 * @param bool $by_name
 *   Set False if passing account id and True for account name
 */
function gauth_account_revoke_token($account_id, $by_name = TRUE) {
  $account = gauth_account_load($account_id, $by_name);
  if ($account['is_authenticated']) {
    $client = gauth_client_get($account);
    $client
      ->revokeToken();
    $account['is_authenticated'] = FALSE;
    $account['access_token'] = '';
    drupal_write_record('gauth_accounts', $account, 'id');
    return TRUE;
  }
  else {
    drupal_set_message(t("The account is not authenticated"));
    return FALSE;
  }
}

/**
 * Function loads gauth account id by user id.
 */
function gauth_account_load_by_uid($uid) {
  return db_select('gauth_accounts')
    ->fields('gauth_accounts', array(
    'name',
  ))
    ->condition('uid', $uid, '=')
    ->execute()
    ->fetchField();
}

Functions

Namesort descending Description
gauth_account_authenticate Authenticate a google account.
gauth_account_delete Delete an account.
gauth_account_is_authenticated Check if an account is authenticated or not.
gauth_account_load Get an array of all accounts or load a specific account.
gauth_account_load_by_uid Function loads gauth account id by user id.
gauth_account_revoke_token Unauthenticate an account.
gauth_account_save Save an account.
gauth_callback_url Checks https settings and returns appropriate callback url.
gauth_client_get Get Google_client object of an account.
gauth_flush_caches Implements hook_flush_caches().
gauth_google_services_names Returns a array services api names.
gauth_google_services_scopes Returns a array services api versions.
gauth_help Implements hook_help().
gauth_init Implements hook_init().
gauth_libraries_info Implements hook_libraries_info().
gauth_menu Implements hook_menu().
gauth_menu_site_status_alter Implements hook_menu_site_status_alter().
gauth_response_handler Function to handle authentication and response from google.
gauth_theme Implements hook_theme().
gauth_user_delete Implements hook_user_delete().
_gauth_read_scope_info Helper function which reads the installed library and discovery api to build cache for scopes and names.