Просмотр исходного кода

UNA Market like on Boonex site: OAuth.

AntonLV 10 лет назад
Родитель
Сommit
d828f51f88

+ 4 - 1
studio/classes/BxDolStudioInstallerUtils.php

@@ -12,6 +12,9 @@ bx_import('BxDolLanguages');
 define('BX_DOL_UNITY_URL_ROOT', 'https://www.boonex.com/');
 define('BX_DOL_UNITY_URL_MARKET', BX_DOL_UNITY_URL_ROOT . 'market/');
 
+define('BX_DOL_UNA_URL_ROOT', 'https://d.una.io/');
+define('BX_DOL_UNA_URL_MARKET', BX_DOL_UNA_URL_ROOT . 'm/market_api/');
+
 define('BX_DOL_STUDIO_IU_RC_SUCCESS', 0);
 define('BX_DOL_STUDIO_IU_RC_FAILED', 1);
 define('BX_DOL_STUDIO_IU_RC_SCHEDULED', 2);
@@ -29,7 +32,7 @@ class BxDolStudioInstallerUtils extends BxDolInstallerUtils implements iBxDolSin
         parent::__construct();
 
         $this->bUseFtp = BX_FORCE_USE_FTP_FILE_TRANSFER;
-        $this->sAuthorizedAccessClass = 'BxDolStudioOAuthPlugin';
+        $this->sAuthorizedAccessClass = 'BxDolStudioOAuthOAuth2'; //'BxDolStudioOAuthOAuth1Plugin';
     }
 
     /**

+ 0 - 6
studio/classes/BxDolStudioOAuth.php

@@ -9,12 +9,6 @@
 
 bx_import('BxDolStudioInstallerUtils');
 
-define('BX_DOL_OAUTH_URL_BASE', BX_DOL_UNITY_URL_ROOT . 'scripts_public/');
-define('BX_DOL_OAUTH_URL_REQUEST_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_request_token.php5');
-define('BX_DOL_OAUTH_URL_AUTHORIZE', BX_DOL_OAUTH_URL_BASE . 'oauth_authorize.php5');
-define('BX_DOL_OAUTH_URL_ACCESS_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_access_token.php5');
-define('BX_DOL_OAUTH_URL_FETCH_DATA', BX_DOL_OAUTH_URL_BASE . 'oauth_fetch_data.php5');
-
 class BxDolStudioOAuth extends BxDol
 {
     protected $oSession;

+ 1 - 1
studio/classes/BxDolStudioOAuthLib.php

@@ -7,7 +7,7 @@
  * @{
  */
 
-class BxDolStudioOAuthLib extends BxDolStudioOAuth implements iBxDolSingleton
+class BxDolStudioOAuthLib extends BxDolStudioOAuthOAuth1 implements iBxDolSingleton
 {
     public function __construct()
     {

+ 25 - 0
studio/classes/BxDolStudioOAuthOAuth1.php

@@ -0,0 +1,25 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    TridentStudio Trident Studio
+ * @{
+ */
+
+
+define('BX_DOL_OAUTH_URL_BASE', BX_DOL_UNITY_URL_ROOT . 'scripts_public/');
+define('BX_DOL_OAUTH_URL_REQUEST_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_request_token.php5');
+define('BX_DOL_OAUTH_URL_AUTHORIZE', BX_DOL_OAUTH_URL_BASE . 'oauth_authorize.php5');
+define('BX_DOL_OAUTH_URL_ACCESS_TOKEN', BX_DOL_OAUTH_URL_BASE . 'oauth_access_token.php5');
+define('BX_DOL_OAUTH_URL_FETCH_DATA', BX_DOL_OAUTH_URL_BASE . 'oauth_fetch_data.php5');
+
+class BxDolStudioOAuthOAuth1 extends BxDolStudioOAuth
+{
+    public function __construct()
+    {
+    	parent::__construct();
+    }
+}
+
+/** @} */

+ 153 - 0
studio/classes/BxDolStudioOAuthOAuth2.php

@@ -0,0 +1,153 @@
+<?php defined('BX_DOL') or die('hack attempt');
+/**
+ * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
+ * CC-BY License - http://creativecommons.org/licenses/by/3.0/
+ *
+ * @defgroup    TridentStudio Trident Studio
+ * @{
+ */
+
+class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
+{
+	protected $sApiUrl;
+	protected $sScope;
+	protected $sPageHandle;
+
+    public function __construct()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
+
+        parent::__construct ();
+
+        $this->sKey = getParam('sys_oauth_key');
+        $this->sSecret = getParam('sys_oauth_secret');
+        $this->sApiUrl = BX_DOL_UNA_URL_ROOT . 'm/oauth2/';
+        $this->sScope = 'basic';
+        $this->sPageHandle = BX_DOL_URL_STUDIO . 'store.php?page=goodies';
+        $this->sDataRetrieveMethod = 'post';
+    }
+
+    public function __clone()
+    {
+        if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
+            trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
+    }
+
+    public static function getInstance()
+    {
+        if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
+            $sClass = __CLASS__;
+            $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
+        }
+
+        return $GLOBALS['bxDolClasses'][__CLASS__];
+    }
+
+    protected function authorize()
+    {
+		if($this->isAuthorized())
+			return true;
+
+		$bCode = bx_get('code') !== false;
+		$bState = bx_get('state') !== false;
+	    if($bCode && $bState && $this->_getCsrfToken() != bx_get('state'))
+            return _t('_adm_err_oauth_cannot_read_answer');
+
+		//--- Get access token.
+		if($bCode)
+			return $this->getAccessToken(bx_get('code'));
+
+		//--- Get request token and redirect to authorize.
+        return $this->getRequestToken();
+    }
+
+    protected function getRequestToken()
+    {
+    	$sUrl = bx_append_url_params($this->sApiUrl . 'auth', array(
+			'response_type' => 'code',
+			'client_id' => $this->sKey,
+			'redirect_uri' => $this->sPageHandle,
+			'scope' => $this->sScope,
+			'state' => $this->_genCsrfToken(),
+		));
+
+		return _t('_adm_msg_oauth_need_authorize', $sUrl);
+    }
+
+    protected function getAccessToken($sCode)
+    {
+    	
+    	$sResponse = bx_file_get_contents($this->sApiUrl . 'token', array(
+            'client_id' => $this->sKey,
+            'client_secret' => $this->sSecret,
+            'grant_type'    => 'authorization_code',
+            'code' => $sCode,
+            'redirect_uri'  => $this->sPageHandle,
+        ), $this->sDataRetrieveMethod);
+
+        if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !isset($aResponse['access_token']) || isset($aResponse['error']))
+            return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
+
+        // get access_token 
+        $sAccessToken = $aResponse['access_token'];
+
+        // request info about profile
+        $sResponse = bx_file_get_contents($this->sApiUrl . 'api/me', array(), 'get', array(
+            'Authorization: Bearer ' . $sAccessToken,
+        ));
+
+        // handle error
+        if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error']))
+            return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
+
+		$this->oSession->setValue('sys_oauth_token', $sAccessToken);
+		$this->oSession->setValue('sys_oauth_authorized', 1);
+		$this->oSession->setValue('sys_oauth_authorized_user', $aResponse['id']);
+
+		return true;
+    }
+
+    protected function fetch($aParams = array())
+    {
+        if(!$this->isAuthorized())
+            return array();
+
+		$sResponse = bx_file_get_contents($this->sApiUrl . 'api/service', array(), 'get', array(
+            'Authorization: Bearer ' . $this->oSession->getValue('sys_oauth_token'),
+        ));
+
+        if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error']))
+			return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
+
+        return $aResponse;
+    }
+
+	protected function _genCsrfToken($bReturn = false)
+    {
+        if (getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
+            return false;
+
+        $oSession = BxDolSession::getInstance();
+
+        $iCsrfTokenLifetime = (int)getParam('sys_security_form_token_lifetime');
+        if ($oSession->getValue('bx_studio_store_csrf_token') === false || ($iCsrfTokenLifetime != 0 && time() - (int)$oSession->getValue('bx_studio_store_csrf_token_time') > $iCsrfTokenLifetime)) {
+            $sToken = genRndPwd(20, false);
+            $oSession->setValue('bx_studio_store_csrf_token', $sToken);
+            $oSession->setValue('bx_studio_store_csrf_token_time', time());
+        }
+        else {
+            $sToken = $oSession->getValue('bx_studio_store_csrf_token');
+        }
+
+        return $sToken;
+    }
+
+    protected function _getCsrfToken()
+    {
+        $oSession = BxDolSession::getInstance();
+        return $oSession->getValue('bx_studio_store_csrf_token');
+    }
+}
+
+/** @} */

+ 1 - 1
studio/classes/BxDolStudioOAuthPlugin.php

@@ -17,7 +17,7 @@ use OAuth\Common\Consumer\Credentials;
 
 require_once (BX_DIRECTORY_PATH_PLUGINS . 'OAuth/bootstrap.php');
 
-class BxDolStudioOAuthPlugin extends BxDolStudioOAuth implements iBxDolSingleton
+class BxDolStudioOAuthPlugin extends BxDolStudioOAuthOAuth1 implements iBxDolSingleton
 {
     protected $sService;
     protected $oStorage;