<?php
 namespace League\OAuth2\Client\Provider; use League\OAuth2\Client\Exception\HostedDomainException; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; use League\OAuth2\Client\Tool\BearerAuthorizationTrait; use Psr\Http\Message\ResponseInterface; class Google extends AbstractProvider { use BearerAuthorizationTrait; protected $accessType; protected $hostedDomain; protected $prompt; protected $scopes = []; public function getBaseAuthorizationUrl(): string { return 'https://accounts.google.com/o/oauth2/v2/auth'; } public function getBaseAccessTokenUrl(array $params): string { return 'https://oauth2.googleapis.com/token'; } public function getResourceOwnerDetailsUrl(AccessToken $token): string { return 'https://openidconnect.googleapis.com/v1/userinfo'; } protected function getAuthorizationParameters(array $options): array { if (empty($options['hd']) && $this->hostedDomain) { $options['hd'] = $this->hostedDomain; } if (empty($options['access_type']) && $this->accessType) { $options['access_type'] = $this->accessType; } if (empty($options['prompt']) && $this->prompt) { $options['prompt'] = $this->prompt; } $scopes = array_merge($this->getDefaultScopes(), $this->scopes); if (!empty($options['scope'])) { $scopes = array_merge($scopes, $options['scope']); } $options['scope'] = array_unique($scopes); $options = parent::getAuthorizationParameters($options); unset($options['approval_prompt']); return $options; } protected function getDefaultScopes(): array { return [ 'openid', 'email', 'profile', ]; } protected function getScopeSeparator(): string { return ' '; } protected function checkResponse(ResponseInterface $response, $data): void { if (empty($data['error'])) { return; } $code = 0; $error = $data['error']; if (is_array($error)) { $code = $error['code']; $error = $error['message']; } throw new IdentityProviderException($error, $code, $data); } protected function createResourceOwner(array $response, AccessToken $token): GoogleUser { $user = new GoogleUser($response); $this->assertMatchingDomain($user->getHostedDomain()); return $user; } protected function assertMatchingDomain(?string $hostedDomain): void { if ($this->hostedDomain === null) { return; } if ($this->hostedDomain === '*' && $hostedDomain) { return; } if ($this->hostedDomain === $hostedDomain) { return; } throw HostedDomainException::notMatchingDomain($this->hostedDomain); } } 