You are here

gauth.module in Google Auth 8

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

Google Auth Api for drupal.

File

gauth.module
View source
<?php

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

/**
 * Implements hook_help().
 */
function gauth_help($route_name, \Drupal\Core\Routing\RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.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_entity_delete().
 */
function gauth_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity
    ->getEntityType() == 'user') {
    $gauths = Drupal::entityTypeManager()
      ->getStorage('gauth')
      ->loadByProperties(array(
      'uid' => $entity
        ->getOriginalId(),
    ));
    if ($gauths) {
      Drupal::entityTypeManager()
        ->getStorage('gauth')
        ->delete($gauths);
    }
  }
}

/**
 * Implements hook_entity_operation().
 */
function gauth_entity_operation(\Drupal\Core\Entity\EntityInterface $entity) {
  if ($entity
    ->getEntityTypeId() == 'gauth') {
    $operations = array();
    $is_authenticated = $entity
      ->getAuthenticated();
    $operations['authenticate_revoke'] = array(
      'title' => $is_authenticated ? t('Revoke') : t('Authenticate'),
      'url' => $is_authenticated ? \Drupal\Core\Url::fromRoute('entity.gauth.revoke_form', array(
        'gauth' => $entity
          ->getId(),
      )) : \Drupal\Core\Url::fromRoute('gauth.response_handler', array(
        'id' => $entity
          ->getId(),
      )),
      'weight' => 50,
    );
    return $operations;
  }
}

/**
 * Returns a array services api versions.
 */
function gauth_google_services_scopes($services, $by_pass_cache = FALSE) {
  if (!$services) {
    return FALSE;
  }
  if (!is_array($services)) {
    $services = [
      $services,
    ];
  }
  $cache = \Drupal::cache('gauth_scopes')
    ->getMultiple($services);
  if (!$by_pass_cache && !empty($cache)) {
    $scopes = array();
    foreach ($cache as $s => $c) {
      if ($c->data) {
        $scopes[$s] = $c->data;
      }
    }
    return $scopes;
  }
  else {
    \Drupal::cache('gauth_scopes')
      ->deleteAll();
    $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($field, $entity, $cardinality, $services = FALSE) {
  $names = \Drupal::config('gauth.google_api_services')
    ->get('gauth_google_api_services');
  if (empty($names)) {
    _gauth_read_scope_info();
    $names = \Drupal::config('gauth.google_api_services')
      ->get('gauth_google_api_services');
  }
  if (!$services) {
    return $names;
  }
  else {
    $service_names = array();
    if (!is_array($services)) {
      $services = explode(",", $services);
    }
    foreach ($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() {
  $path = gauth_load_library(TRUE);
  $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();
  $classes = array();
  $client = Drupal\gauth\Entity\Gauth::getGauthClient();
  foreach (get_declared_classes() as $class) {
    if (is_subclass_of($class, 'Google_Service')) {
      $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();
      $classes[$aname] = $class;
      \Drupal::cache('gauth_scopes')
        ->set($aname, $scopes[$aname]);
    }
  }
  \Drupal::configFactory()
    ->getEditable('gauth.google_api_services')
    ->set('gauth_google_api_services', $api_names)
    ->save();
  \Drupal::configFactory()
    ->getEditable('gauth.google_api_classes')
    ->set('gauth_google_api_classes', $classes)
    ->save();
  return $scopes;
}

/**
 * Checks https settings and returns appropriate callback url.
 */
function gauth_callback_url() {
  global $is_https;
  return \Drupal\Core\Url::fromRoute('gauth.response_handler', array(), array(
    'https' => $is_https,
    'absolute' => TRUE,
  ))
    ->toString();
}
function gauth_load_library($return_path = FALSE) {
  $library = \Drupal::service('library.discovery')
    ->getLibraryByName('gauth', 'google-api-php-client');
  if ($library) {
    if (class_exists('Google_Client')) {

      // Composer install so no problem with lib.
      if ($return_path) {
        return '';
      }
      return TRUE;
    }
    else {
      $php_files = array_keys($library['php']);
      $library_exists = file_exists($php_files[0]);
      if ($library_exists) {
        require_once $php_files[0];
        if ($return_path) {
          return 'libraries/google-api-php-client/';
        }
        return TRUE;
      }
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
gauth_callback_url Checks https settings and returns appropriate callback url.
gauth_entity_delete Implements hook_entity_delete().
gauth_entity_operation Implements hook_entity_operation().
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_load_library
_gauth_read_scope_info Helper function which reads the installed library and discovery api to build cache for scopes and names.