1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* SocialConnect project |
5
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
6
|
|
|
* @author Alexander Fedyashov <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace SocialConnect\OAuth2\Provider; |
10
|
|
|
|
11
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
12
|
|
|
use SocialConnect\Provider\Exception\InvalidAccessToken; |
13
|
|
|
use SocialConnect\Provider\Exception\InvalidResponse; |
14
|
|
|
use SocialConnect\OAuth2\AbstractProvider; |
15
|
|
|
use SocialConnect\OAuth2\AccessToken; |
16
|
|
|
use SocialConnect\Common\Entity\User; |
17
|
|
|
use SocialConnect\Common\Hydrator\ObjectMap; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class Provider |
21
|
|
|
* @package SocialConnect\Google |
22
|
|
|
*/ |
23
|
|
|
class Google extends AbstractProvider |
24
|
|
|
{ |
25
|
|
|
const NAME = 'google'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
3 |
|
public function getBaseUri() |
31
|
|
|
{ |
32
|
3 |
|
return 'https://www.googleapis.com/'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
2 |
|
public function getAuthorizeUri() |
39
|
|
|
{ |
40
|
2 |
|
return 'https://accounts.google.com/o/oauth2/auth'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
2 |
|
public function getRequestTokenUri() |
47
|
|
|
{ |
48
|
2 |
|
return 'https://accounts.google.com/o/oauth2/token'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
3 |
|
public function getName() |
55
|
|
|
{ |
56
|
3 |
|
return self::NAME; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
3 |
|
public function parseToken($body) |
63
|
|
|
{ |
64
|
3 |
|
$result = json_decode($body, true); |
65
|
3 |
|
if ($result) { |
66
|
1 |
|
return new AccessToken($result); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
throw new InvalidAccessToken('Provider response with not valid JSON'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
2 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
76
|
|
|
{ |
77
|
2 |
|
$response = $this->httpClient->request( |
78
|
2 |
|
$this->getBaseUri() . 'oauth2/v1/userinfo', |
79
|
|
|
[ |
80
|
2 |
|
'access_token' => $accessToken->getToken() |
81
|
|
|
] |
82
|
|
|
); |
83
|
|
|
|
84
|
2 |
|
$fields = $this->getArrayOption('identity.fields', [ |
85
|
2 |
|
'id', |
86
|
|
|
'email', |
87
|
|
|
'verified_email', |
88
|
|
|
'name', |
89
|
|
|
'given_name', |
90
|
|
|
'family_name', |
91
|
|
|
'picture', |
92
|
|
|
'locale', |
93
|
|
|
// |
94
|
|
|
'gender', |
95
|
|
|
'hd', |
96
|
|
|
'link', |
97
|
|
|
]); |
98
|
2 |
|
if ($fields) { |
|
|
|
|
99
|
2 |
|
$parameters['fields'] = implode(',', $fields); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
2 |
|
if (!$response->isSuccess()) { |
103
|
1 |
|
throw new InvalidResponse( |
104
|
1 |
|
'API response with error code', |
105
|
1 |
|
$response |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$result = $response->json(); |
110
|
1 |
|
if (!$result) { |
111
|
1 |
|
throw new InvalidResponse( |
112
|
1 |
|
'API response is not a valid JSON object', |
113
|
1 |
|
$response |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$hydrator = new ObjectMap( |
118
|
|
|
[ |
119
|
|
|
'id' => 'id', |
120
|
|
|
'given_name' => 'firstname', |
121
|
|
|
'family_name' => 'lastname', |
122
|
|
|
'email' => 'email', |
123
|
|
|
'verified_email' => 'emailVerified', |
124
|
|
|
'name' => 'fullname', |
125
|
|
|
'gender' => 'sex', |
126
|
|
|
'picture' => 'pictureURL' |
127
|
|
|
] |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $hydrator->hydrate(new User(), $result); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getScopeInline() |
137
|
|
|
{ |
138
|
|
|
return implode(' ', $this->scope); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.