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

Google::assertMatchingDomain()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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