1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SocialConnect project |
4
|
|
|
* @author: Patsura Dmitry https://github.com/ovr <[email protected]> |
5
|
|
|
* @author Alexander Fedyashov <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace SocialConnect\OAuth2\Provider; |
10
|
|
|
|
11
|
|
|
use SocialConnect\Common\ArrayHydrator; |
12
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
13
|
|
|
use SocialConnect\OAuth2\AbstractProvider; |
14
|
|
|
use SocialConnect\Common\Entity\User; |
15
|
|
|
|
16
|
|
|
class Google extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
const NAME = 'google'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
*/ |
23
|
4 |
|
public function getBaseUri() |
24
|
|
|
{ |
25
|
4 |
|
return 'https://www.googleapis.com/'; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
2 |
|
public function getAuthorizeUri() |
32
|
|
|
{ |
33
|
2 |
|
return 'https://accounts.google.com/o/oauth2/auth'; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public function getRequestTokenUri() |
40
|
|
|
{ |
41
|
2 |
|
return 'https://accounts.google.com/o/oauth2/token'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
3 |
|
public function getName() |
48
|
|
|
{ |
49
|
3 |
|
return self::NAME; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
3 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
56
|
|
|
{ |
57
|
3 |
|
$query = []; |
58
|
|
|
|
59
|
3 |
|
$fields = $this->getArrayOption('identity.fields', [ |
60
|
|
|
'id', |
61
|
|
|
'email', |
62
|
|
|
'verified_email', |
63
|
|
|
'name', |
64
|
|
|
'given_name', |
65
|
|
|
'family_name', |
66
|
|
|
'picture', |
67
|
|
|
'locale', |
68
|
|
|
// |
69
|
|
|
'gender', |
70
|
|
|
'hd', |
71
|
|
|
'link', |
72
|
|
|
]); |
73
|
3 |
|
if ($fields) { |
74
|
3 |
|
$query['fields'] = implode(',', $fields); |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
$response = $this->request('GET', 'oauth2/v1/userinfo', $query, $accessToken); |
78
|
|
|
|
79
|
1 |
|
$hydrator = new ArrayHydrator([ |
80
|
|
|
'id' => 'id', |
81
|
|
|
'given_name' => 'firstname', |
82
|
|
|
'family_name' => 'lastname', |
83
|
|
|
'email' => 'email', |
84
|
|
|
'verified_email' => 'emailVerified', |
85
|
|
|
'name' => 'fullname', |
86
|
1 |
|
'gender' => static function ($value, User $user) { |
87
|
|
|
$user->setSex($value); |
88
|
|
|
}, |
89
|
|
|
'picture' => 'pictureURL' |
90
|
|
|
]); |
91
|
|
|
|
92
|
1 |
|
return $hydrator->hydrate(new User(), $response); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function getScopeInline() |
99
|
|
|
{ |
100
|
1 |
|
return implode(' ', $this->scope); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|