32 static $knownServices;
34 if (is_null($knownServices))
37 OAuth\Google::getServiceName(),
38 OAuth\LiveId::getServiceName(),
39 OAuth\Yandex::getServiceName(),
40 OAuth\Mailru::getServiceName(),
41 OAuth\Office365::getServiceName(),
45 return $knownServices;
56 if (get_called_class() != get_class())
58 $className = get_called_class();
59 $service = $className::getServiceName();
63 if (!in_array(
$service, self::getKnownServices()))
68 $className = sprintf(
'%s\OAuth\%s', __NAMESPACE__,
$service);
71 if (!Main\Loader::includeModule(
'socialservices'))
76 $instance =
new $className;
79 $instance->storedUid = sprintf(
'%x%x', time(), rand(0, 0xffffffff));
81 if (!$instance->check())
97 if ($meta = static::parseMeta($meta))
99 if ($instance = self::getInstance($meta[
'service']))
101 if (
'oauthb' == $meta[
'type'])
103 $instance->storedUid = $meta[
'key'];
120 "\x00oauthb\x00%s\x00%s",
135 '/^\x00(oauthb)\x00(%s)\x00([a-f0-9]+)$/',
141 return preg_quote($item,
'/');
143 self::getKnownServices()
148 if (!preg_match($regex, $meta, $matches))
150 if (!preg_match(
'/^\x00(oauth)\x00(google|liveid)\x00(\d+)$/', $meta, $matches))
157 'type' => $matches[1],
158 'service' => $matches[2],
159 'key' => $matches[3],
170 private static function getSocservToken($service, $key)
172 if (Main\Loader::includeModule(
'socialservices'))
177 $oauthClient = new \CSocServGoogleOAuth($key);
178 $oauthClient->getEntityOAuth()->addScope(
'https://mail.google.com/');
181 $oauthClient = new \CSocServLiveIDOAuth($key);
182 $oauthClient->getEntityOAuth()->addScope([
'wl.imap',
'wl.offline_access']);
186 if (!empty($oauthClient))
188 return $oauthClient->getStorageToken() ?:
false;
205 if ($meta = static::parseMeta($meta))
207 if (
'oauthb' == $meta[
'type'])
209 if ($oauthHelper = self::getInstance($meta[
'service']))
211 return $oauthHelper->getStoredToken($meta[
'key'], $expireGapSeconds) ?:
false;
214 else if (
'oauth' == $meta[
'type'])
216 return self::getSocservToken($meta[
'service'], $meta[
'key']);
230 if ($meta = static::parseMeta($meta))
232 if ($oauthHelper = self::getInstance($meta[
'service']))
234 if (
'oauthb' == $meta[
'type'])
236 $oauthHelper->getStoredToken($meta[
'key']);
238 else if (
'oauth' == $meta[
'type'])
240 if ($token = self::getSocservToken($meta[
'service'], $meta[
'key']))
242 $oauthHelper->getOAuthEntity()->setToken($token);
246 return $oauthHelper->getUserData($secure);
260 return $this->oauthEntity;
270 return $this->storedUid;
281 if (isModuleInstalled(
'bitrix24') && defined(
'BX24_HOST_NAME') && !$final)
283 return $this->getControllerUrl() .
'/redirect.php';
287 $uri =
new Uri(Main\Engine\UrlManager::getInstance()->getHostUrl().
'/bitrix/tools/mail_oauth.php');
288 return $uri->getLocator();
301 if (isModuleInstalled(
'bitrix24') && defined(
'BX24_HOST_NAME'))
306 http_build_query(array(
307 'check_key' => \CSocServAuthManager::getUniqueKey(),
308 'dummy' =>
'https://dummy.bitrix24.com/',
309 'state' => rawurlencode(http_build_query(array(
310 'service' => $this->service,
311 'uid' => $this->storedUid,
318 $state = http_build_query(array(
319 'check_key' => \CSocServAuthManager::getUniqueKey(),
320 'service' => $this->service,
321 'uid' => $this->storedUid,
325 return $this->oauthEntity->getAuthUrl($this->
getRedirect(
false), $state);
335 return Mail\Internals\OAuthTable::getList(array(
337 '=UID' => $this->storedUid,
359 $this->storedUid = $uid;
366 $this->oauthEntity->setToken($token = $item[
'TOKEN']);
367 $this->oauthEntity->setRefreshToken($item[
'REFRESH_TOKEN']);
368 $expireThreshold = time() + $expireGapSeconds;
370 if (empty($token) || $item[
'TOKEN_EXPIRES'] > 0 && $item[
'TOKEN_EXPIRES'] < $expireThreshold)
372 $this->oauthEntity->setToken(
null);
374 if (!empty($item[
'REFRESH_TOKEN']))
376 if ($this->oauthEntity->getNewAccessToken($item[
'REFRESH_TOKEN']))
378 $tokenData = $this->oauthEntity->getTokenData();
380 Mail\Internals\OAuthTable::update(
383 'TOKEN' => $tokenData[
'access_token'],
384 'REFRESH_TOKEN' => $tokenData[
'refresh_token'],
385 'TOKEN_EXPIRES' => $tokenData[
'expires_in'],
391 $token = $this->oauthEntity->getToken();
408 $this->oauthEntity->setCode($code);
411 $oauthData = $_SESSION[
'OAUTH_DATA'];
413 $result = $this->oauthEntity->getAccessToken($this->
getRedirect(
false));
415 $_SESSION[
'OAUTH_DATA'] = $oauthData;
430 $userData = $this->oauthEntity->getCurrentUser();
436 if (!empty($userData))
440 $secure ? array() : array(
'__data' => $userData)
466 $this->storedUid = $state[
'uid'];
470 $this->oauthEntity->setRefreshToken($item[
'REFRESH_TOKEN']);
473 if (!empty($_REQUEST[
'code']) && \CSocServAuthManager::checkUniqueKey())
481 'TOKEN' => $userData[
'__data'][
'access_token'],
482 'REFRESH_TOKEN' => $userData[
'__data'][
'refresh_token'],
483 'TOKEN_EXPIRES' => $userData[
'__data'][
'expires_in'],
488 Mail\Internals\OAuthTable::add($fields);
492 Mail\Internals\OAuthTable::update($item[
'ID'], $fields);
496 (
string) $userData[
'email'],
497 (
string) $userData[
'first_name'],
498 (
string) $userData[
'last_name'],
499 (
string) $userData[
'full_name'],
500 (
string) $userData[
'image']
503 if (isset($userData[
'__data'][
'emailIsIntended']))
505 $userDataObject->setEmailIsIntended((
bool)$userData[
'__data'][
'emailIsIntended']);
509 $userDataObject->setEmailIsIntended(
false);
512 if (isset($userData[
'__data'][
'userPrincipalName']))
514 $userDataObject->setUserPrincipalName((
string)$userData[
'__data'][
'userPrincipalName']);
528 $this->publicUserData = $userData;
533 return $this->publicUserData;
546 if ($context === self::WEB_TYPE)
550 <script type=
"text/javascript">
552 targetWindow = window.opener ? window.opener : window;
554 targetWindow.BX.onCustomEvent(
555 'OnMailOAuthBCompleted',
557 '<?=\CUtil::jsEscape($this->getStoredUid()) ?>',
558 '<?=\CUtil::jsEscape($this->getUrl()) ?>',
563 if (targetWindow !== window)
572 else if ($context === self::MOBILE_TYPE)
574 $params = http_build_query([
578 header(
'Location: bitrix24://?'.$params);
getStoredToken($uid=null, int $expireGapSeconds=10)
static getInstanceByMeta($meta)
getRedirect(bool $final=true)
static getTokenByMeta($meta, int $expireGapSeconds=10)
mapUserData(array $userData)
handleResponse(array $state, $context=self::WEB_TYPE)
static getUserDataByMeta($meta, $secure=true)
getAccessToken($code=null)
setPublicUserData(UserData $userData)
getUserData($secure=true)
static getKnownServices()
static getInstance($service=null)