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