You are here

protected function GoogleAuth::initSdk in Social Auth Google 3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Network/GoogleAuth.php \Drupal\social_auth_google\Plugin\Network\GoogleAuth::initSdk()
  2. 8 src/Plugin/Network/GoogleAuth.php \Drupal\social_auth_google\Plugin\Network\GoogleAuth::initSdk()

Sets the underlying SDK library.

Return value

\League\OAuth2\Client\Provider\Google|false The initialized 3rd party library instance. False if library could not be initialized.

Throws

\Drupal\social_api\SocialApiException If the SDK library does not exist.

Overrides NetworkBase::initSdk

File

src/Plugin/Network/GoogleAuth.php, line 40

Class

GoogleAuth
Defines a Network Plugin for Social Auth Google.

Namespace

Drupal\social_auth_google\Plugin\Network

Code

protected function initSdk() {
  $class_name = '\\League\\OAuth2\\Client\\Provider\\Google';
  if (!class_exists($class_name)) {
    throw new SocialApiException(sprintf('The Google library for PHP League OAuth2 not found. Class: %s.', $class_name));
  }

  /** @var \Drupal\social_auth_google\Settings\GoogleAuthSettings $settings */
  $settings = $this->settings;
  if ($this
    ->validateConfig($settings)) {

    // All these settings are mandatory.
    $league_settings = [
      'clientId' => $settings
        ->getClientId(),
      'clientSecret' => $settings
        ->getClientSecret(),
      'redirectUri' => Url::fromRoute('social_auth_google.callback')
        ->setAbsolute()
        ->toString(),
      'accessType' => 'offline',
      'verify' => FALSE,
      'hostedDomain' => $settings
        ->getRestrictedDomain() == '' ? NULL : $settings
        ->getRestrictedDomain(),
    ];

    // Proxy configuration data for outward proxy.
    $config = $this->siteSettings
      ->get('http_client_config');
    if (!empty($config['proxy']['http'])) {
      $league_settings['proxy'] = $config['proxy']['http'];
    }
    return new Google($league_settings);
  }
  return FALSE;
}