summaryrefslogtreecommitdiff
path: root/vendor/league/oauth2-client/src/Provider/Google.php
blob: 87393ee3347888783aeb4a43574fc627d50e3081 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php

namespace League\OAuth2\Client\Provider;

use League\OAuth2\Client\Entity\User;

class Google extends AbstractProvider
{
    public $scopeSeparator = ' ';

    public $scopes = [
        'profile',
        'email',
    ];

    public $authorizationHeader = 'OAuth';

    /**
     * @var string If set, this will be sent to google as the "hd" parameter.
     * @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
     */
    public $hostedDomain = '';

    public function setHostedDomain($hd)
    {
        $this->hostedDomain = $hd;
    }

    public function getHostedDomain()
    {
        return $this->hostedDomain;
    }

    /**
     * @var string If set, this will be sent to google as the "access_type" parameter.
     * @link https://developers.google.com/accounts/docs/OAuth2WebServer#offline
     */
    public $accessType = '';

    public function setAccessType($accessType)
    {
        $this->accessType = $accessType;
    }

    public function getAccessType()
    {
        return $this->accessType;
    }

    public function urlAuthorize()
    {
        return 'https://accounts.google.com/o/oauth2/auth';
    }

    public function urlAccessToken()
    {
        return 'https://accounts.google.com/o/oauth2/token';
    }

    public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
    {
        return
            'https://www.googleapis.com/plus/v1/people/me?'.
            'fields=id%2Cname(familyName%2CgivenName)%2CdisplayName%2C'.
            'emails%2Fvalue%2Cimage%2Furl&alt=json';
    }

    public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
    {
        $response = (array) $response;

        $user = new User();

        $imageUrl = (isset($response['image']) &&
            $response['image']->url) ? $response['image']->url : null;
        $email =
            (isset($response['emails']) &&
            count($response['emails']) &&
            $response['emails'][0]->value)? $response['emails'][0]->value : null;

        $user->exchangeArray([
            'uid' => $response['id'],
            'name' => $response['displayName'],
            'firstname' => $response['name']->givenName,
            'lastName' => $response['name']->familyName,
            'email' => $email,
            'imageUrl' => $imageUrl,
        ]);

        return $user;
    }

    public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token)
    {
        return $response->id;
    }

    public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token)
    {
        return ($response->emails &&
            count($response->emails) &&
            $response->emails[0]->value) ? $response->emails[0]->value : null;
    }

    public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
    {
        return [$response->name->givenName, $response->name->familyName];
    }

    public function getAuthorizationUrl($options = array())
    {
        $url = parent::getAuthorizationUrl($options);

        if (!empty($this->hostedDomain)) {
            $url .= '&' . $this->httpBuildQuery(['hd' => $this->hostedDomain]);
        }

        if (!empty($this->accessType)) {
            $url .= '&' . $this->httpBuildQuery(['access_type'=> $this->accessType]);
        }

        return $url;
    }
}