1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
office365.php
См. документацию.
1<?php
2
3namespace Bitrix\Mail\Helper\OAuth;
4
5use Bitrix\Main;
6use Bitrix\Mail;
7use Bitrix\Main\ArgumentException;
8
10{
11
12 protected function __construct()
13 {
14 $this->oauthEntity = new Office365Interface(
15 \CSocServOffice365OAuth::getOption('office365_appid'),
16 \CSocServOffice365OAuth::getOption('office365_appsecret')
17 );
18
19 // get graph universal scopes, for user profile read access
20 $this->oauthEntity->setScope($this->oauthEntity->getGraphScopes());
21 }
22
23 protected function check()
24 {
25 $provider = new \CSocServOffice365OAuth;
26
27 return $provider->checkSettings();
28 }
29
30 protected function mapUserData(array $userData)
31 {
32 return array(
33 'email' => $userData['email'],
34 'first_name' => $userData['first_name'],
35 'last_name' => $userData['last_name'],
36 'full_name' => $userData['name'],
37 'image' => $userData['image'],
38 'error' => $userData['error_description'],
39 'userPrincipalName' => $userData['userPrincipalName'] ?? '',
40 );
41 }
42
43 public static function getServiceName()
44 {
45 return 'office365';
46 }
47
48 public function getControllerUrl()
49 {
50 return \CSocServOffice365OAuth::getControllerUrl();
51 }
52
53}
54
55if (Main\Loader::includeModule('socialservices'))
56{
57 class_exists('CSocServOffice365OAuth');
58
59 class Office365Interface extends \COffice365OAuthInterface
60 {
64 const OUTLOOK_API_VERSION = "/v2.0";
65
69 const OUTLOOK_RESOURCE = "https://outlook.office.com/api";
70
71 public function getStorageTokens()
72 {
73 return false;
74 }
75
81 private function refreshToOutlookAccessToken(): void
82 {
83 $this->refreshWithScopes($this->getOutlookScopes());
84 }
85
91 public function getOutlookScopes(): array
92 {
93 return [
94 'offline_access',
95 'https://outlook.office.com/IMAP.AccessAsUser.All',
96 'https://outlook.office.com/SMTP.Send',
97 ];
98 }
99
107 private function getGraphPrincipalName(bool $isSwitchedAlready = false): string
108 {
109 $httpClient = new \Bitrix\Main\Web\HttpClient();
110 $httpClient->setHeader("Authorization", "Bearer " . $this->access_token);
111 $jsonResponse = $httpClient->get($this->resource . static::VERSION . static::CONTACTS_URL);
112 try
113 {
114 $decoded = \Bitrix\Main\Web\Json::decode($jsonResponse);
115 if (!empty($decoded['userPrincipalName']) && is_string($decoded['userPrincipalName']))
116 {
117 return $decoded['userPrincipalName'];
118 }
119 else if (!empty($decoded['error']) && !$isSwitchedAlready)
120 {
121 $this->refreshToGraphAccessToken();
122 return $this->getGraphPrincipalName(true);
123 }
124 }
125 catch (ArgumentException $e)
126 {
127 AddMessage2Log($e->getMessage(), 'mail', 2, true);
128 }
129 return '';
130 }
131
137 private function refreshToGraphAccessToken(): void
138 {
139 $this->refreshWithScopes($this->getGraphScopes());
140 }
141
149 private function refreshWithScopes(array $scopes): void
150 {
151 if (empty($this->refresh_token))
152 {
153 return;
154 }
155
156 $httpClient = new \Bitrix\Main\Web\HttpClient();
157
158 $jsonResponse = $httpClient->post(static::TOKEN_URL, [
159 'refresh_token' => $this->refresh_token,
160 'client_id' => $this->appID,
161 'client_secret' => $this->appSecret,
162 'grant_type' => 'refresh_token',
163 'scope' => implode(' ', $scopes),
164 ]);
165
166 try
167 {
168 $decoded = \Bitrix\Main\Web\Json::decode($jsonResponse);
169 if (!empty($decoded['access_token']))
170 {
171 $this->access_token = (string)$decoded['access_token'];
172 $this->refresh_token = (string)$decoded['refresh_token'];
173 $this->accessTokenExpires = (int)$decoded["expires_in"];
174 }
175 }
176 catch (ArgumentException $e)
177 {
178 AddMessage2Log($e->getMessage(), 'mail', 2, true);
179 }
180 }
181
187 public function getGraphScopes(): array
188 {
189 return [
190 'User.read',
191 'offline_access',
192 'IMAP.AccessAsUser.All',
193 'SMTP.Send',
194 ];
195 }
196
197 public function getTokenData(): array
198 {
199 return [
200 'access_token' => $this->access_token,
201 'refresh_token' => $this->refresh_token,
202 'expires_in' => Mail\Helper\OAuth::convertTokenExpiresToUnixtimeIfNeed($this->accessTokenExpires),
203 ];
204 }
205
206 public function getCurrentUser()
207 {
208 if (empty($this->access_token))
209 {
210 return false;
211 }
212 $userPrincipalName = $this->getGraphPrincipalName();
213 $this->refreshToOutlookAccessToken();
214
215 $httpClient = new \Bitrix\Main\Web\HttpClient();
216 $httpClient->setHeader("Authorization", "Bearer ". $this->access_token);
217
218 $result = $httpClient->get(static::OUTLOOK_RESOURCE . static::OUTLOOK_API_VERSION . static::CONTACTS_URL);
219 try
220 {
221 $result = \Bitrix\Main\Web\Json::decode($result);
222 }
223 catch (ArgumentException $e)
224 {
225 AddMessage2Log($e->getMessage(), 'mail', 2, true);
226 $result = [];
227 }
228
229 if(isset($result['EmailAddress']))
230 {
231 $email = $result['EmailAddress'];
232 $emailIsIntended = false;
233 }
234 else
235 {
236 global $USER;
237 $email = $USER->GetEmail();
238 if(is_null($email))
239 {
240 $email = '';
241 }
242 $emailIsIntended = true;
243 }
244
245 return array_merge(
246 [
247 'email' => $email,
248 'emailIsIntended' => $emailIsIntended,
249 'userPrincipalName' => $userPrincipalName,
250 ],
251 $this->getTokenData()
252 );
253 }
254
255 }
256}
if(!Loader::includeModule('messageservice')) $provider
Определения callback_ednaruimhpx.php:21
static getServiceName()
Определения office365.php:43
mapUserData(array $userData)
Определения office365.php:30
static decode($data)
Определения json.php:50
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
global $USER
Определения csv_new_run.php:40
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)
Определения tools.php:3941
$email
Определения payment.php:49