You are here

class GoogleAuthManager in Social Auth Google 8

Same name and namespace in other branches
  1. 8.2 src/GoogleAuthManager.php \Drupal\social_auth_google\GoogleAuthManager
  2. 3.x src/GoogleAuthManager.php \Drupal\social_auth_google\GoogleAuthManager

Manages the authentication requests.

Hierarchy

  • class \Drupal\social_auth_google\GoogleAuthManager extends \Drupal\social_auth\AuthManager\OAuth2Manager

Expanded class hierarchy of GoogleAuthManager

2 files declare their use of GoogleAuthManager
GoogleAuthController.php in src/Controller/GoogleAuthController.php
GoogleAuthManagerTest.php in tests/src/Unit/GoogleAuthManagerTest.php
1 string reference to 'GoogleAuthManager'
social_auth_google.services.yml in ./social_auth_google.services.yml
social_auth_google.services.yml
1 service uses GoogleAuthManager
social_auth_google.manager in ./social_auth_google.services.yml
Drupal\social_auth_google\GoogleAuthManager

File

src/GoogleAuthManager.php, line 12

Namespace

Drupal\social_auth_google
View source
class GoogleAuthManager extends OAuth2Manager {

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The Google service client.
   *
   * @var \Google_Client
   */
  protected $client;

  /**
   * Code returned by Google for authentication.
   *
   * @var string
   */
  protected $code;

  /**
   * GoogleLoginManager constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request
   *   Used to get the parameter code returned by Google.
   */
  public function __construct(RequestStack $request) {
    $this->request = $request
      ->getCurrentRequest();
  }

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

  /**
   * {@inheritdoc}
   */
  public function getAccessToken() {
    if (!$this->accessToken) {
      $this->accessToken = $this->client
        ->fetchAccessTokenWithAuthCode($this
        ->getCode());
    }
    return $this->accessToken;
  }

  /**
   * {@inheritdoc}
   */
  public function getUserInfo() {
    return $this
      ->getOauth2Service()->userinfo
      ->get();
  }

  /**
   * Gets Google Oauth2 Service.
   *
   * @return \Google_Service_Oauth2
   *   The Google Oauth2 service.
   */
  protected function getOauth2Service() {
    return new Google_Service_Oauth2($this
      ->getClient());
  }

  /**
   * Gets the code returned by Google to authenticate.
   *
   * @return string
   *   The code string returned by Google.
   */
  protected function getCode() {
    if (!$this->code) {
      $this->code = $this->request->query
        ->get('code');
    }
    return $this->code;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GoogleAuthManager::$client protected property The Google service client.
GoogleAuthManager::$code protected property Code returned by Google for authentication.
GoogleAuthManager::$request protected property The request object.
GoogleAuthManager::authenticate public function
GoogleAuthManager::getAccessToken public function
GoogleAuthManager::getCode protected function Gets the code returned by Google to authenticate.
GoogleAuthManager::getOauth2Service protected function Gets Google Oauth2 Service.
GoogleAuthManager::getUserInfo public function
GoogleAuthManager::__construct public function GoogleLoginManager constructor.