Completed
Push — master ( 07f7e6...c0faed )
by Woody
9s
created

Google::getBaseAuthorizationUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\OAuth2\Client\Provider;
4
5
use League\OAuth2\Client\Exception\HostedDomainException;
6
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7
use League\OAuth2\Client\Token\AccessToken;
8
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Google extends AbstractProvider
12
{
13
    use BearerAuthorizationTrait;
14
15
    const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'id';
16
17
    /**
18
     * @var string If set, this will be sent to google as the "access_type" parameter.
19
     * @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
20
     */
21
    protected $accessType;
22
23
    /**
24
     * @var string If set, this will be sent to google as the "hd" parameter.
25
     * @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
26
     */
27
    protected $hostedDomain;
28
29
    /**
30
     * @var array Default fields to be requested from the user profile.
31
     * @link https://developers.google.com/+/web/api/rest/latest/people
32
     */
33
    protected $defaultUserFields = [
34
        'id',
35
        'name(familyName,givenName)',
36
        'displayName',
37
        'emails/value',
38
        'image/url',
39
    ];
40
    /**
41
     * @var array Additional fields to be requested from the user profile.
42
     *            If set, these values will be included with the defaults.
43
     */
44
    protected $userFields = [];
45
46
    /**
47
     * Use OpenID Connect endpoints for getting the user info/resource owner
48
     * @var bool
49
     */
50
    protected $useOidcMode = false;
51
52
    public function getBaseAuthorizationUrl()
53
    {
54
        return 'https://accounts.google.com/o/oauth2/auth';
55
    }
56
57
    public function getBaseAccessTokenUrl(array $params)
58
    {
59
        return 'https://www.googleapis.com/oauth2/v4/token';
60
    }
61
62
    public function getResourceOwnerDetailsUrl(AccessToken $token)
63
    {
64
        if ($this->useOidcMode) {
65
            // OIDC endpoints can be found https://accounts.google.com/.well-known/openid-configuration
66
            return 'https://www.googleapis.com/oauth2/v3/userinfo';
67
        }
68
        // fields that are required based on other configuration options
69
        $configurationUserFields = [];
70
        if (isset($this->hostedDomain)) {
71
            $configurationUserFields[] = 'domain';
72
        }
73
        $fields = array_merge($this->defaultUserFields, $this->userFields, $configurationUserFields);
74
        return 'https://www.googleapis.com/plus/v1/people/me?' . http_build_query([
75
            'fields' => implode(',', $fields),
76
            'alt'    => 'json',
77
        ]);
78
    }
79
80
    protected function getAuthorizationParameters(array $options)
81
    {
82
        $params = array_merge(
83
            parent::getAuthorizationParameters($options),
84
            array_filter([
85
                'hd'          => $this->hostedDomain,
86
                'access_type' => $this->accessType,
87
                // if the user is logged in with more than one account ask which one to use for the login!
88
                'authuser'    => '-1'
89
            ])
90
        );
91
92
        return $params;
93
    }
94
95
    protected function getDefaultScopes()
96
    {
97
        return [
98
            'email',
99
            'openid',
100
            'profile',
101
        ];
102
    }
103
104
    protected function getScopeSeparator()
105
    {
106
        return ' ';
107
    }
108
109
    protected function checkResponse(ResponseInterface $response, $data)
110
    {
111
        if (!empty($data['error'])) {
112
            $code  = 0;
113
            $error = $data['error'];
114
115
            if (is_array($error)) {
116
                $code  = $error['code'];
117
                $error = $error['message'];
118
            }
119
120
            throw new IdentityProviderException($error, $code, $data);
121
        }
122
    }
123
124
    protected function createResourceOwner(array $response, AccessToken $token)
125
    {
126
        $user = new GoogleUser($response);
127
        // Validate hosted domain incase the user edited the initial authorization code grant request
128
        if ($this->hostedDomain === '*') {
129
            if (empty($user->getHostedDomain())) {
130
                throw HostedDomainException::notMatchingDomain($this->hostedDomain);
131
            }
132
        } elseif (!empty($this->hostedDomain) && $this->hostedDomain !== $user->getHostedDomain()) {
133
            throw HostedDomainException::notMatchingDomain($this->hostedDomain);
134
        }
135
136
        return $user;
137
    }
138
}
139