AntonLV před 9 roky
rodič
revize
fbfdec0025
1 změnil soubory, kde provedl 61 přidání a 13 odebrání
  1. 61 13
      studio/classes/BxDolStudioOAuthOAuth2.php

+ 61 - 13
studio/classes/BxDolStudioOAuthOAuth2.php

@@ -9,6 +9,10 @@
 
 
 class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
 class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
 {
 {
+    protected $sSessionKeyStateToken;
+    protected $sSessionKeyCsrfToken;
+    protected $sSessionKeyCsrfTokenTime;
+
 	protected $sApiUrl;
 	protected $sApiUrl;
 	protected $sScope;
 	protected $sScope;
 	protected $sPageHandle;
 	protected $sPageHandle;
@@ -20,6 +24,10 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
 
 
         parent::__construct ();
         parent::__construct ();
 
 
+        $this->sSessionKeyStateToken = 'bx_studio_store_state_token';
+        $this->sSessionKeyCsrfToken = 'bx_studio_store_csrf_token';
+        $this->sSessionKeyCsrfTokenTime = 'bx_studio_store_csrf_token_time';
+
         $this->sKey = getParam('sys_oauth_key');
         $this->sKey = getParam('sys_oauth_key');
         $this->sSecret = getParam('sys_oauth_secret');
         $this->sSecret = getParam('sys_oauth_secret');
         $this->sApiUrl = BX_DOL_MARKET_URL_ROOT . 'm/oauth2/';
         $this->sApiUrl = BX_DOL_MARKET_URL_ROOT . 'm/oauth2/';
@@ -51,7 +59,7 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
 
 
 		$bCode = bx_get('code') !== false;
 		$bCode = bx_get('code') !== false;
 		$bState = bx_get('state') !== false;
 		$bState = bx_get('state') !== false;
-	    if($bCode && $bState && $this->_getCsrfToken() != bx_get('state'))
+	    if($bCode && $bState && $this->_getState() != bx_get('state'))
             return _t('_adm_err_oauth_cannot_read_answer');
             return _t('_adm_err_oauth_cannot_read_answer');
 
 
 		//--- Get access token.
 		//--- Get access token.
@@ -64,12 +72,14 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
 
 
     protected function getRequestToken()
     protected function getRequestToken()
     {
     {
+        
+
     	$sUrl = bx_append_url_params($this->sApiUrl . 'auth', array(
     	$sUrl = bx_append_url_params($this->sApiUrl . 'auth', array(
 			'response_type' => 'code',
 			'response_type' => 'code',
 			'client_id' => $this->sKey,
 			'client_id' => $this->sKey,
 			'redirect_uri' => $this->sPageHandle,
 			'redirect_uri' => $this->sPageHandle,
 			'scope' => $this->sScope,
 			'scope' => $this->sScope,
-			'state' => $this->_genCsrfToken(),
+			'state' => $this->_genState(),
 		));
 		));
 
 
 		return array(
 		return array(
@@ -146,30 +156,68 @@ class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
     	return false;
     	return false;
     }
     }
 
 
-	protected function _genCsrfToken($bReturn = false)
+    protected function _genState()
+    {
+        $sResult = $this->_genCsrfToken();
+        if($sResult !== false)
+            return $sResult;
+
+        return $this->_genStateToken();
+    }
+
+    protected function _getState()
     {
     {
-        if (getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
+        $sResult = $this->_getCsrfToken();
+        if($sResult !== false)
+            return $sResult;
+
+        return $this->_getStateToken();
+    }
+
+	protected function _genCsrfToken()
+    {
+        if(getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
             return false;
             return false;
 
 
         $oSession = BxDolSession::getInstance();
         $oSession = BxDolSession::getInstance();
 
 
         $iCsrfTokenLifetime = (int)getParam('sys_security_form_token_lifetime');
         $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');
-        }
+        $sToken = $oSession->getValue($this->sSessionKeyCsrfToken);
+        if($sToken !== false && ($iCsrfTokenLifetime == 0 || time() - (int)$oSession->getValue($this->sSessionKeyCsrfTokenTime) < $iCsrfTokenLifetime))
+            return $sToken;
+
+        $sToken = genRndPwd(20, false);
+        $oSession->setValue($this->sSessionKeyCsrfToken, $sToken);
+        $oSession->setValue($this->sSessionKeyCsrfTokenTime, time());
 
 
         return $sToken;
         return $sToken;
     }
     }
 
 
     protected function _getCsrfToken()
     protected function _getCsrfToken()
+    {
+        if(getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
+            return false;
+
+        return BxDolSession::getInstance()->getValue($this->sSessionKeyCsrfToken);
+    }
+
+    protected function _genStateToken()
     {
     {
         $oSession = BxDolSession::getInstance();
         $oSession = BxDolSession::getInstance();
-        return $oSession->getValue('bx_studio_store_csrf_token');
+
+        $sToken = $oSession->getValue($this->sSessionKeyStateToken);
+        if($sToken !== false)
+            return $sToken;
+
+        $sToken = genRndPwd(20, false);
+        $oSession->setValue($this->sSessionKeyStateToken, $sToken);
+
+        return $sToken;
+    }
+
+    protected function _getStateToken()
+    {
+        return BxDolSession::getInstance()->getValue($this->sSessionKeyStateToken);
     }
     }
 }
 }