Completed
Push — master ( 523461...d26349 )
by Woody
25s
created

Google::getAuthorizationParameters()   B

Complexity

Conditions 8
Paths 16

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.4444
c 0
b 0
f 0
cc 8
nc 16
nop 1
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
    /**
16
     * @var string If set, this will be sent to google as the "access_type" parameter.
17
     * @link https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
18
     */
19
    protected $accessType;
20
21
    /**
22
     * @var string If set, this will be sent to google as the "hd" parameter.
23
     * @link https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
24
     */
25
    protected $hostedDomain;
26
27
    /**
28
     * @var string If set, this will be sent to google as the "prompt" parameter.
29
     * @link https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
30
     */
31
    protected $prompt;
32
33
    /**
34
     * @var array List of scopes that will be used for authentication.
35
     * @link https://developers.google.com/identity/protocols/googlescopes
36
     */
37
    protected $scopes = [];
38
39
    public function getBaseAuthorizationUrl()
40
    {
41
        return 'https://accounts.google.com/o/oauth2/v2/auth';
42
    }
43
44
    public function getBaseAccessTokenUrl(array $params)
45
    {
46
        return 'https://www.googleapis.com/oauth2/v4/token';
47
    }
48
49
    public function getResourceOwnerDetailsUrl(AccessToken $token)
50
    {
51
        return 'https://openidconnect.googleapis.com/v1/userinfo';
52
    }
53
54
    protected function getAuthorizationParameters(array $options)
55
    {
56
        if (empty($options['hd']) && $this->hostedDomain) {
57
            $options['hd'] = $this->hostedDomain;
58
        }
59
60
        if (empty($options['access_type']) && $this->accessType) {
61
            $options['access_type'] = $this->accessType;
62
        }
63
64
        if (empty($options['prompt']) && $this->prompt) {
65
            $options['prompt'] = $this->prompt;
66
        }
67
68
        // Default scopes MUST be included for OpenID Connect.
69
        // Additional scopes MAY be added by constructor or option.
70
        $scopes = array_merge($this->getDefaultScopes(), $this->scopes);
71
72
        if (!empty($options['scope'])) {
73
            $scopes = array_merge($scopes, $options['scope']);
74
        }
75
76
        $options['scope'] = array_unique($scopes);
77
78
        return parent::getAuthorizationParameters($options);
79
    }
80
81
    protected function getDefaultScopes()
82
    {
83
        // "openid" MUST be the first scope in the list.
84
        return [
85
            'openid',
86
            'email',
87
            'profile',
88
        ];
89
    }
90
91
    protected function getScopeSeparator()
92
    {
93
        return ' ';
94
    }
95
96
    protected function checkResponse(ResponseInterface $response, $data)
97
    {
98
        // @codeCoverageIgnoreStart
99
        if (empty($data['error'])) {
100
            return;
101
        }
102
        // @codeCoverageIgnoreEnd
103
104
        $code  = 0;
105
        $error = $data['error'];
106
107
        if (is_array($error)) {
108
            $code  = $error['code'];
109
            $error = $error['message'];
110
        }
111
112
        throw new IdentityProviderException($error, $code, $data);
113
    }
114
115
    protected function createResourceOwner(array $response, AccessToken $token)
116
    {
117
        $user = new GoogleUser($response);
118
119
        $this->assertMatchingDomain($user->getHostedDomain());
120
121
        return $user;
122
    }
123
124
    /**
125
     * @throws HostedDomainException If the domain does not match the configured domain.
126
     */
127
    protected function assertMatchingDomain($hostedDomain)
128
    {
129
        if ($this->hostedDomain === null) {
130
            // No hosted domain configured.
131
            return;
132
        }
133
134
        if ($this->hostedDomain === '*' && $hostedDomain) {
135
            // Any hosted domain is allowed.
136
            return;
137
        }
138
139
        if ($this->hostedDomain === $hostedDomain) {
140
            // Hosted domain is correct.
141
            return;
142
        }
143
144
        throw HostedDomainException::notMatchingDomain($this->hostedDomain);
145
    }
146
}
147