|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Svycka\SocialUser\OAuth2\GrantType; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
6
|
|
|
use Svycka\SocialUser\Service\SocialUserService; |
|
7
|
|
|
use Svycka\SocialUser\UserProfile; |
|
8
|
|
|
use Svycka\SocialUser\UserProfileInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Vytautas Stankus <[email protected]> |
|
12
|
|
|
* @license MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
class Google extends AbstractSocialGrantType |
|
15
|
|
|
{ |
|
16
|
|
|
const PROVIDER_NAME = 'google'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var SocialUserService |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $socialUserService; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \GuzzleHttp\Client |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $httpClient; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $options; |
|
32
|
|
|
|
|
33
|
9 |
|
public function __construct(SocialUserService $socialUserService, \GuzzleHttp\Client $httpClient, array $options) |
|
34
|
|
|
{ |
|
35
|
9 |
|
if (empty($options['audience'])) { |
|
36
|
1 |
|
throw new \InvalidArgumentException('"audience" option is required but not provided.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
9 |
|
$this->socialUserService = $socialUserService; |
|
40
|
9 |
|
$this->httpClient = $httpClient; |
|
41
|
9 |
|
$this->options = $options; |
|
42
|
9 |
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
public function getQuerystringIdentifier() |
|
45
|
|
|
{ |
|
46
|
2 |
|
return 'google'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param string $token |
|
51
|
|
|
* |
|
52
|
|
|
* @return UserProfileInterface|null |
|
53
|
|
|
*/ |
|
54
|
6 |
|
protected function getTokenInfo($token) |
|
55
|
|
|
{ |
|
56
|
|
|
try { |
|
57
|
6 |
|
$response = $this->httpClient->request('GET', 'https://www.googleapis.com/oauth2/v3/tokeninfo', [ |
|
58
|
|
|
'query' => [ |
|
59
|
6 |
|
'id_token' => $token, |
|
60
|
|
|
] |
|
61
|
|
|
]); |
|
62
|
|
|
|
|
63
|
5 |
|
$tokenInfo = json_decode($response->getBody()->getContents(), true); |
|
64
|
|
|
|
|
65
|
|
|
// check if we can get user identifier |
|
66
|
5 |
|
if (empty($tokenInfo) || empty($tokenInfo['sub'])) { |
|
67
|
1 |
|
return null; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// do not accept tokens generated not for our application even if they are valid, |
|
71
|
|
|
// to protect against "man in the middle" attack |
|
72
|
4 |
|
if ($tokenInfo['aud'] != $this->options['audience']) { |
|
73
|
1 |
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
3 |
|
$userProfile = new UserProfile(); |
|
77
|
3 |
|
$userProfile->setIdentifier($tokenInfo['sub']); |
|
78
|
3 |
|
$userProfile->setDisplayName(isset($tokenInfo['name']) ? $tokenInfo['name'] : null); |
|
79
|
3 |
|
$userProfile->setFirstName(isset($tokenInfo['given_name']) ? $tokenInfo['given_name'] : null); |
|
80
|
3 |
|
$userProfile->setLastName(isset($tokenInfo['family_name']) ? $tokenInfo['family_name'] : null); |
|
81
|
3 |
|
$userProfile->setEmail(isset($tokenInfo['email']) ? $tokenInfo['email'] : null); |
|
82
|
3 |
|
$userProfile->setEmailVerified(isset($tokenInfo['email_verified']) ? $tokenInfo['email_verified'] : false); |
|
83
|
|
|
|
|
84
|
3 |
|
return $userProfile; |
|
85
|
1 |
|
} catch (ClientException $e) { |
|
86
|
1 |
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param UserProfileInterface $socialUser |
|
92
|
|
|
* |
|
93
|
|
|
* @return int|null |
|
94
|
|
|
*/ |
|
95
|
3 |
|
protected function getLocalUser(UserProfileInterface $socialUser) |
|
96
|
|
|
{ |
|
97
|
3 |
|
return $this->socialUserService->getLocalUser(self::PROVIDER_NAME, $socialUser); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|