You are here

GoogleAuthManager.php in Social Auth Google 8

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

File

src/GoogleAuthManager.php
View source
<?php

namespace Drupal\social_auth_google;

use Drupal\social_auth\AuthManager\OAuth2Manager;
use Symfony\Component\HttpFoundation\RequestStack;
use Google_Service_Oauth2;

/**
 * Manages the authentication requests.
 */
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;
  }

}

Classes

Namesort descending Description
GoogleAuthManager Manages the authentication requests.