You are here

GoogleAuthManager.php in Social Auth Google 3.x

Same filename and directory in other branches
  1. 8.2 src/GoogleAuthManager.php
  2. 8 src/GoogleAuthManager.php

File

src/GoogleAuthManager.php
View source
<?php

namespace Drupal\social_auth_google;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\social_auth\AuthManager\OAuth2Manager;
use Drupal\Core\Config\ConfigFactory;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use Symfony\Component\HttpFoundation\RequestStack;

/**
 * Contains all the logic for Google OAuth2 authentication.
 */
class GoogleAuthManager extends OAuth2Manager {

  /**
   * Constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactory $configFactory
   *   Used for accessing configuration object factory.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   Used to get the authorization code from the callback request.
   */
  public function __construct(ConfigFactory $configFactory, LoggerChannelFactoryInterface $logger_factory, RequestStack $request_stack) {
    parent::__construct($configFactory
      ->get('social_auth_google.settings'), $logger_factory, $request_stack
      ->getCurrentRequest());
  }

  /**
   * {@inheritdoc}
   */
  public function authenticate() {
    try {
      $this
        ->setAccessToken($this->client
        ->getAccessToken('authorization_code', [
        'code' => $this->request->query
          ->get('code'),
      ]));
    } catch (IdentityProviderException $e) {
      $this->loggerFactory
        ->get('social_auth_google')
        ->error('There was an error during authentication. Exception: ' . $e
        ->getMessage());
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getUserInfo() {
    $access_token = $this
      ->getAccessToken();
    if (!$this->user && $access_token != NULL) {
      $this->user = $this->client
        ->getResourceOwner($this
        ->getAccessToken());
    }
    else {
      $this->loggerFactory
        ->get('social_auth_google')
        ->error('There was an error fetching the access token for user.');
    }
    return $this->user;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthorizationUrl() {
    $scopes = [
      'email',
      'profile',
    ];
    $extra_scopes = $this
      ->getScopes();
    if ($extra_scopes) {
      $scopes = array_merge($scopes, explode(',', $extra_scopes));
    }

    // Returns the URL where user will be redirected.
    return $this->client
      ->getAuthorizationUrl([
      'scope' => $scopes,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function requestEndPoint($method, $path, $domain = NULL, array $options = []) {
    if (!$domain) {
      $domain = 'https://www.googleapis.com';
    }
    $url = $domain . $path;
    $request = $this->client
      ->getAuthenticatedRequest($method, $url, $this
      ->getAccessToken(), $options);
    try {
      return $this->client
        ->getParsedResponse($request);
    } catch (IdentityProviderException $e) {
      $this->loggerFactory
        ->get('social_auth_google')
        ->error('There was an error when requesting ' . $url . '. Exception: ' . $e
        ->getMessage());
    }
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getState() {
    return $this->client
      ->getState();
  }

}

Classes

Namesort descending Description
GoogleAuthManager Contains all the logic for Google OAuth2 authentication.