3namespace Bitrix\Mail\Helper\OAuth;
7use Bitrix\Main\ArgumentException;
14 $this->oauthEntity =
new Office365Interface(
15 \CSocServOffice365OAuth::getOption(
'office365_appid'),
16 \CSocServOffice365OAuth::getOption(
'office365_appsecret')
20 $this->oauthEntity->setScope($this->oauthEntity->getGraphScopes());
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'] ??
'',
50 return \CSocServOffice365OAuth::getControllerUrl();
55if (
Main\Loader::includeModule(
'socialservices'))
57 class_exists(
'CSocServOffice365OAuth');
64 const OUTLOOK_API_VERSION =
"/v2.0";
69 const OUTLOOK_RESOURCE =
"https://outlook.office.com/api";
71 public function getStorageTokens()
81 private function refreshToOutlookAccessToken(): void
83 $this->refreshWithScopes($this->getOutlookScopes());
91 public function getOutlookScopes():
array
95 'https://outlook.office.com/IMAP.AccessAsUser.All',
96 'https://outlook.office.com/SMTP.Send',
107 private function getGraphPrincipalName(
bool $isSwitchedAlready =
false): string
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);
115 if (!empty($decoded[
'userPrincipalName']) && is_string($decoded[
'userPrincipalName']))
117 return $decoded[
'userPrincipalName'];
119 else if (!empty($decoded[
'error']) && !$isSwitchedAlready)
121 $this->refreshToGraphAccessToken();
122 return $this->getGraphPrincipalName(
true);
125 catch (ArgumentException $e)
137 private function refreshToGraphAccessToken(): void
139 $this->refreshWithScopes($this->getGraphScopes());
149 private function refreshWithScopes(
array $scopes): void
151 if (empty($this->refresh_token))
156 $httpClient = new \Bitrix\Main\Web\HttpClient();
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),
168 $decoded = \Bitrix\Main\Web\Json::decode($jsonResponse);
169 if (!empty($decoded[
'access_token']))
171 $this->access_token = (string)$decoded[
'access_token'];
172 $this->refresh_token = (string)$decoded[
'refresh_token'];
173 $this->accessTokenExpires = (int)$decoded[
"expires_in"];
176 catch (ArgumentException $e)
187 public function getGraphScopes():
array
192 'IMAP.AccessAsUser.All',
197 public function getTokenData():
array
200 'access_token' => $this->access_token,
201 'refresh_token' => $this->refresh_token,
202 'expires_in' => Mail\Helper\OAuth::convertTokenExpiresToUnixtimeIfNeed($this->accessTokenExpires),
206 public function getCurrentUser()
208 if (empty($this->access_token))
212 $userPrincipalName = $this->getGraphPrincipalName();
213 $this->refreshToOutlookAccessToken();
215 $httpClient = new \Bitrix\Main\Web\HttpClient();
216 $httpClient->setHeader(
"Authorization",
"Bearer ". $this->access_token);
218 $result = $httpClient->get(static::OUTLOOK_RESOURCE . static::OUTLOOK_API_VERSION . static::CONTACTS_URL);
223 catch (ArgumentException $e)
229 if(isset(
$result[
'EmailAddress']))
232 $emailIsIntended =
false;
242 $emailIsIntended =
true;
248 'emailIsIntended' => $emailIsIntended,
249 'userPrincipalName' => $userPrincipalName,
251 $this->getTokenData()
if(!Loader::includeModule('messageservice')) $provider
mapUserData(array $userData)
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
AddMessage2Log($text, $module='', $traceDepth=6, $showArgs=false)