Completed
Pull Request — master (#65)
by Woody
01:26
created

Google   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 118
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBaseAuthorizationUrl() 0 4 1
A getBaseAccessTokenUrl() 0 4 1
A getResourceOwnerDetailsUrl() 0 4 1
A getAuthorizationParameters() 0 12 1
A getDefaultScopes() 0 9 1
A getScopeSeparator() 0 4 1
A checkResponse() 0 14 3
A createResourceOwner() 0 8 1
A assertMatchingDomain() 0 19 5
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/OAuth2WebServer#request-parameter-access_type
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/accounts/docs/OAuth2Login#hd-param
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/OAuth2WebServer#request-parameter-prompt
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
        $additionalOptions = array_filter([
57
            'hd' => $this->hostedDomain,
58
            'access_type' => $this->accessType,
59
            'prompt' => $this->prompt,
60
        ]);
61
62
        $options = array_replace(parent::getAuthorizationParameters($options), $additionalOptions);
63
64
        return $options;
65
    }
66
67
    protected function getDefaultScopes()
68
    {
69
        // "openid" SHOULD be the first scope in the list.
70
        return [
71
            'openid',
72
            'email',
73
            'profile',
74
        ];
75
    }
76
77
    protected function getScopeSeparator()
78
    {
79
        return ' ';
80
    }
81
82
    protected function checkResponse(ResponseInterface $response, $data)
83
    {
84
        if (!empty($data['error'])) {
85
            $code  = 0;
86
            $error = $data['error'];
87
88
            if (is_array($error)) {
89
                $code  = $error['code'];
90
                $error = $error['message'];
91
            }
92
93
            throw new IdentityProviderException($error, $code, $data);
94
        }
95
    }
96
97
    protected function createResourceOwner(array $response, AccessToken $token)
98
    {
99
        $user = new GoogleUser($response);
100
101
        $this->assertMatchingDomain($user->getHostedDomain());
102
103
        return $user;
104
    }
105
106
    /**
107
     * @throws HostedDomainException If the domain does not match the configured domain.
108
     */
109
    protected function assertMatchingDomain($hostedDomain)
110
    {
111
        if ($this->hostedDomain === null) {
112
            // No hosted domain configured.
113
            return;
114
        }
115
116
        if ($this->hostedDomain === '*' && $hostedDomain) {
117
            // Any hosted domain is allowed.
118
            return;
119
        }
120
121
        if ($this->hostedDomain === $hostedDomain) {
122
            // Hosted domain is correct.
123
            return;
124
        }
125
126
        throw HostedDomainException::notMatchingDomain($this->hostedDomain);
127
    }
128
}
129