Completed
Pull Request — master (#33)
by
unknown
01:58
created

Google::setUserFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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