GoogleAuthManager.php in Social Auth Google 8.2
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;
class GoogleAuthManager extends OAuth2Manager {
public function __construct(ConfigFactory $configFactory, LoggerChannelFactoryInterface $logger_factory, RequestStack $request_stack) {
parent::__construct($configFactory
->get('social_auth_google.settings'), $logger_factory, $request_stack
->getCurrentRequest());
}
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());
}
}
public function getUserInfo() {
if (!$this->user) {
$this->user = $this->client
->getResourceOwner($this
->getAccessToken());
}
return $this->user;
}
public function getAuthorizationUrl() {
$scopes = [
'email',
'profile',
];
$extra_scopes = $this
->getScopes();
if ($extra_scopes) {
$scopes = array_merge($scopes, explode(',', $extra_scopes));
}
return $this->client
->getAuthorizationUrl([
'scope' => $scopes,
]);
}
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;
}
public function getState() {
return $this->client
->getState();
}
}