|
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\OpenIDConnect\Provider; |
|
10
|
|
|
|
|
11
|
|
|
use SocialConnect\Common\ArrayHydrator; |
|
12
|
|
|
use SocialConnect\Common\Exception\InvalidArgumentException; |
|
13
|
|
|
use SocialConnect\OpenIDConnect\AccessToken; |
|
14
|
|
|
use SocialConnect\Provider\AccessTokenInterface; |
|
15
|
|
|
use SocialConnect\OpenIDConnect\AbstractProvider; |
|
16
|
|
|
use SocialConnect\Common\Entity\User; |
|
17
|
|
|
|
|
18
|
|
|
class Google extends AbstractProvider |
|
19
|
|
|
{ |
|
20
|
|
|
const NAME = 'google'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* {@inheritdoc} |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function getOpenIdUrl() |
|
26
|
|
|
{ |
|
27
|
1 |
|
return 'https://accounts.google.com/.well-known/openid-configuration'; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public function getBaseUri() |
|
34
|
|
|
{ |
|
35
|
2 |
|
return 'https://www.googleapis.com/'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
1 |
|
public function getAuthorizeUri() |
|
42
|
|
|
{ |
|
43
|
1 |
|
return 'https://accounts.google.com/o/oauth2/auth'; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* {@inheritdoc} |
|
48
|
|
|
*/ |
|
49
|
1 |
|
public function getRequestTokenUri() |
|
50
|
|
|
{ |
|
51
|
1 |
|
return 'https://accounts.google.com/o/oauth2/token'; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* {@inheritdoc} |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getName() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return self::NAME; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function extractIdentity(AccessTokenInterface $accessToken) |
|
66
|
|
|
{ |
|
67
|
|
|
if (!$accessToken instanceof AccessToken) { |
|
68
|
|
|
throw new InvalidArgumentException( |
|
69
|
|
|
'$accessToken must be instance AccessToken' |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$jwt = $accessToken->getJwt(); |
|
74
|
|
|
|
|
75
|
|
|
$hydrator = new ArrayHydrator([ |
|
76
|
|
|
'sub' => 'id', |
|
77
|
|
|
'email' => 'email', |
|
78
|
|
|
'email_verified' => 'emailVerified', |
|
79
|
|
|
'name' => 'fullname', |
|
80
|
|
|
'picture' => 'pictureURL', |
|
81
|
|
|
'given_name' => 'firstname', |
|
82
|
|
|
'family_name' => 'lastname', |
|
83
|
|
|
]); |
|
84
|
|
|
|
|
85
|
|
|
/** @var User $user */ |
|
86
|
|
|
$user = $hydrator->hydrate(new User(), $jwt->getPayload()); |
|
87
|
|
|
|
|
88
|
|
|
return $user; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getIdentity(AccessTokenInterface $accessToken) |
|
95
|
|
|
{ |
|
96
|
1 |
|
$response = $this->request('GET', 'oauth2/v1/userinfo', [], $accessToken); |
|
97
|
|
|
|
|
98
|
1 |
|
$hydrator = new ArrayHydrator([ |
|
99
|
1 |
|
'id' => 'id', |
|
100
|
1 |
|
'given_name' => 'firstname', |
|
101
|
1 |
|
'family_name' => 'lastname', |
|
102
|
1 |
|
'email' => 'email', |
|
103
|
1 |
|
'verified_email' => 'emailVerified', |
|
104
|
1 |
|
'name' => 'fullname', |
|
105
|
|
|
'gender' => static function ($value, User $user) { |
|
106
|
|
|
$user->setSex($value); |
|
107
|
1 |
|
}, |
|
108
|
|
|
]); |
|
109
|
|
|
|
|
110
|
1 |
|
return $hydrator->hydrate(new User(), $response); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* {@inheritdoc} |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getScopeInline() |
|
117
|
|
|
{ |
|
118
|
|
|
return implode(' ', $this->scope); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|