• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/security/authentication.php
  • Класс: BitrixMainSecurityAuthentication
  • Вызов: Authentication::getStoredHashId
static function getStoredHashId(CurrentUser $user, $hash, $onlyTempHash = false)
{
	if (!$user->isAuthenticated())
		throw new SecurityException();

	$context = MainApplication::getInstance()->getContext();
	if (!($context instanceof BitrixMainHttpContext))
		throw new MainNotSupportedException();

	/** @var $request BitrixMainHttpRequest */
	$request = $context->getRequest();
	$remoteAddress = $request->getRemoteAddress();

	$policy = $user->getPolicy();

	$cnt = 0;
	$hashId = null;

	$connection = MainApplication::getDbConnection();

	$sql =
		"SELECT A.* ".
		"FROM b_user_stored_auth A ".
		"WHERE A.USER_ID = ".intval($user->getUserId())." ".
		"ORDER BY A.LAST_AUTH DESC";
	$recordset = $connection->query($sql);
	while ($record = $recordset->fetch())
	{
		if ($record["TEMP_HASH"] === "N")
			$cnt++;

		/** @var $lastAuth BitrixMainTypeDateTime */
		$lastAuth = $record["LAST_AUTH"];
		if ($policy["MAX_STORE_NUM"] < $cnt
			|| ($record["TEMP_HASH"] === "N" && mktime() - $policy["STORE_TIMEOUT"] * 60 > $lastAuth->getTimestamp())
			|| ($record["TEMP_HASH"] === "Y" && mktime() - $policy["SESSION_TIMEOUT"] * 60 > $lastAuth->getTimestamp())
		)
		{
			$connection->queryExecute("DELETE FROM b_user_stored_auth WHERE ID = ".$record["ID"]);
		}
		elseif (!$hashId)
		{
			//for domain spreaded external auth we should check only temporary hashes
			if ($onlyTempHash === false || $record["TEMP_HASH"] === "Y")
			{
				$remoteNet = ip2long($policy["STORE_IP_MASK"]) & ip2long($remoteAddress);
				$storedNet = ip2long($policy["STORE_IP_MASK"]) & (float)$record["IP_ADDR"];
				if ($hash === $record["STORED_HASH"] && $remoteNet === $storedNet)
					$hashId = $record["ID"];
			}
		}
	}
	return $hashId;
}