You are here

public static function Gauth::getGauthClient in Google Auth 8

Function to retrieve the google client for different operations.

Developers can pass the gauth object and get the api client ready for operations.

Parameters

Gauth|null $gauth: Gauth account class object.

Return value

\Google_Client Google_Client object with all params from the account.

Throws

\Drupal\Core\Entity\EntityStorageException

4 calls to Gauth::getGauthClient()
Gauth::getServiceObjects in src/Entity/Gauth.php
This function is designed to return objects of services classes.
GauthResponseHandler::responseHandler in src/Entity/Controller/GauthResponseHandler.php
Function handles google response.
GauthRevokeForm::submitForm in src/Form/GauthRevokeForm.php
This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state…
_gauth_read_scope_info in ./gauth.module
Helper function which reads the installed library and discovery api to build cache for scopes and names.

File

src/Entity/Gauth.php, line 504

Class

Gauth
Defines the Gauth entity.

Namespace

Drupal\gauth\Entity

Code

public static function getGauthClient(Gauth $gauth = NULL) {
  gauth_load_library();
  $client = new Google_Client();
  $client
    ->setRedirectUri(gauth_callback_url());
  if ($gauth == NULL) {
    return $client;
  }
  $client
    ->setClientId($gauth
    ->getClientId());
  if ($gauth
    ->getAuthenticated()) {
    $client
      ->setAccessToken($gauth
      ->getAccessToken());
  }
  if ($gauth
    ->getAccessType()) {
    $client
      ->setAccessType('offline');
  }
  $client
    ->setClientSecret($gauth
    ->getClientSecret());
  $client
    ->setDeveloperKey($gauth
    ->getDeveloperKey());
  $client
    ->setRedirectUri(gauth_callback_url());
  if ($gauth
    ->getAccessType()) {
    $client
      ->setApprovalPrompt('force');
  }
  $client
    ->setApplicationName("Google OAuth2");
  $scopes = gauth_google_services_scopes($gauth
    ->getServices());

  // Let other modules change scopes.
  $gauth_id = $gauth
    ->getId();
  \Drupal::moduleHandler()
    ->alter('gauth_account_scopes', $scopes, $gauth_id);
  $client
    ->addScope($scopes);
  if ($client
    ->getAccessToken() && $client
    ->isAccessTokenExpired()) {
    if ($client
      ->getRefreshToken() != '') {

      // Access Type is Offline.
      $client
        ->refreshToken($client
        ->getRefreshToken());
      $token = $client
        ->getAccessToken();
      $gauth
        ->setAccessToken($token);
      $gauth
        ->save();
    }
    else {
      $client
        ->revokeToken();
      $gauth
        ->setAuthenticated(FALSE);
      $gauth
        ->setAccessToken('');
      $gauth
        ->save();
      \Drupal::messenger()
        ->addMessage(t('Access token is expired. If you are admin then you need to authenticate again. Consider configuring access type to offline.'));
    }
  }
  return $client;
}