-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGoogle.php
117 lines (99 loc) · 3.3 KB
/
Google.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace Dbogdanoff\Bitrix\Auth\Adapter;
/**
* Class Google
* @package Bitrix Social Auth
* @author Denis Bogdanov <[email protected]>
*/
class Google extends Adapter
{
const VERSION = 'v2';
const NAME = 'Google';
const ID = "GoogleOAuth"; // EXTERNAL_AUTH_ID
const LOGIN_PREFIX = "G_";
public function getAuthUrl(array $state = []): string
{
$params = http_build_query([
'scope' => $this->getScope(),
'state' => $this->getState($state),
'redirect_uri' => $this->getServerName(),
'response_type' => 'code',
'client_id' => $this->client_id
]);
return 'https://accounts.google.com/o/oauth2/' . self::VERSION . '/auth?' . $params;
}
/**
* Возвращает строку прав доступа
* @return string
*/
protected function getScope(): string
{
$this->params['scope'] = (array)$this->params['scope'];
$this->params['scope'][] = 'https://www.googleapis.com/auth/userinfo.profile';
$this->params['scope'][] = 'https://www.googleapis.com/auth/userinfo.email';
return implode(' ', array_unique($this->params['scope']));
}
/**
* @return array
* @throws \Exception
*/
protected function getToken(): array
{
$json = $this->curl('https://www.googleapis.com/oauth2/v4/token', [
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'redirect_uri' => $this->getServerName(),
'code' => $this->request['code'],
'grant_type' => 'authorization_code'
], true);
$array = json_decode($json, true);
if (array_key_exists('error', $array)) {
throw new \Exception($array['error_description'] . ' (' . $array['error'] . ')');
}
if (array_key_exists('access_token', $array)) {
$this->token = $array['access_token'];
}
if (array_key_exists('expires_in', $array)) {
$this->token_expires = intval($array['expires_in']);
}
return $array ?: [];
}
/**
* @return int
*/
protected function getTokenExpires(): int
{
return intval(time() + $this->token_expires);
}
/**
* @param $tokenResponse
* @return array
* @throws \Exception
*/
protected function getUserInfo($tokenResponse): array
{
$json = $this->curl('https://www.googleapis.com/oauth2/' . self::VERSION . '/userinfo', [
'access_token' => $tokenResponse['access_token'],
'fields' => 'id,link,given_name,family_name,gender,picture,email'
]);
$array = json_decode($json, true);
if (array_key_exists('error', $array)) {
throw new \Exception($array['error']['message']);
}
return $array ?: [];
}
/**
* @param array $userInfo
* @return array
*/
public function getUserFields(array $userInfo): array
{
return [
'NAME' => $userInfo['given_name'],
'LAST_NAME' => $userInfo['family_name'],
'EMAIL' => $userInfo['email'],
'PERSONAL_GENDER' => $userInfo['sex'] == 'female' ? 'F' : 'M',
'PERSONAL_PHOTO' => $this->downloadPictureToTemp($userInfo['picture'])
];
}
}