|
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
|
|
|
$parameters = [ |
|
78
|
2 |
|
'access_token' => $accessToken->getToken() |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
2 |
|
$fields = $this->getArrayOption('identity.fields', [ |
|
82
|
2 |
|
'id', |
|
83
|
|
|
'email', |
|
84
|
|
|
'verified_email', |
|
85
|
|
|
'name', |
|
86
|
|
|
'given_name', |
|
87
|
|
|
'family_name', |
|
88
|
|
|
'picture', |
|
89
|
|
|
'locale', |
|
90
|
|
|
// |
|
91
|
|
|
'gender', |
|
92
|
|
|
'hd', |
|
93
|
|
|
'link', |
|
94
|
|
|
]); |
|
95
|
2 |
|
if ($fields) { |
|
96
|
2 |
|
$parameters['fields'] = implode(',', $fields); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
$response = $this->httpClient->request( |
|
100
|
2 |
|
$this->getBaseUri() . 'oauth2/v1/userinfo', |
|
101
|
2 |
|
$parameters |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
2 |
|
if (!$response->isSuccess()) { |
|
105
|
1 |
|
throw new InvalidResponse( |
|
106
|
1 |
|
'API response with error code', |
|
107
|
1 |
|
$response |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
$result = $response->json(); |
|
112
|
1 |
|
if (!$result) { |
|
113
|
1 |
|
throw new InvalidResponse( |
|
114
|
1 |
|
'API response is not a valid JSON object', |
|
115
|
1 |
|
$response |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
$hydrator = new ObjectMap( |
|
120
|
|
|
[ |
|
121
|
|
|
'id' => 'id', |
|
122
|
|
|
'given_name' => 'firstname', |
|
123
|
|
|
'family_name' => 'lastname', |
|
124
|
|
|
'email' => 'email', |
|
125
|
|
|
'verified_email' => 'emailVerified', |
|
126
|
|
|
'name' => 'fullname', |
|
127
|
|
|
'gender' => 'sex', |
|
128
|
|
|
'picture' => 'pictureURL' |
|
129
|
|
|
] |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
return $hydrator->hydrate(new User(), $result); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* {@inheritdoc} |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getScopeInline() |
|
139
|
|
|
{ |
|
140
|
|
|
return implode(' ', $this->scope); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|