BxDolStudioOAuthOAuth2.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaStudio UNA Studio
  7. * @{
  8. */
  9. class BxDolStudioOAuthOAuth2 extends BxDolStudioOAuth implements iBxDolSingleton
  10. {
  11. protected $sApiUrl;
  12. protected $sScope;
  13. protected $sPageHandle;
  14. protected function __construct()
  15. {
  16. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  17. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  18. parent::__construct ();
  19. $this->sKey = getParam('sys_oauth_key');
  20. $this->sSecret = getParam('sys_oauth_secret');
  21. $this->sApiUrl = BX_DOL_MARKET_URL_ROOT . 'm/oauth2/';
  22. $this->sScope = 'market';
  23. $this->sPageHandle = BX_DOL_URL_STUDIO . 'store.php?page=goodies';
  24. $this->sDataRetrieveMethod = 'post';
  25. }
  26. public function __clone()
  27. {
  28. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  29. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  30. }
  31. public static function getInstance()
  32. {
  33. if (!isset($GLOBALS['bxDolClasses'][__CLASS__])) {
  34. $sClass = __CLASS__;
  35. $GLOBALS['bxDolClasses'][__CLASS__] = new $sClass();
  36. }
  37. return $GLOBALS['bxDolClasses'][__CLASS__];
  38. }
  39. protected function authorize()
  40. {
  41. if($this->isAuthorized())
  42. return true;
  43. $bCode = bx_get('code') !== false;
  44. $bState = bx_get('state') !== false;
  45. if($bCode && $bState && $this->_getCsrfToken() != bx_get('state'))
  46. return _t('_adm_err_oauth_cannot_read_answer');
  47. //--- Get access token.
  48. if($bCode)
  49. return $this->getAccessToken(bx_get('code'));
  50. //--- Get request token and redirect to authorize.
  51. return $this->getRequestToken();
  52. }
  53. protected function getRequestToken()
  54. {
  55. $sUrl = bx_append_url_params($this->sApiUrl . 'auth', array(
  56. 'response_type' => 'code',
  57. 'client_id' => $this->sKey,
  58. 'redirect_uri' => $this->sPageHandle,
  59. 'scope' => $this->sScope,
  60. 'state' => $this->_genCsrfToken(),
  61. ));
  62. return array(
  63. 'redirect' => $sUrl,
  64. 'message' => _t('_adm_msg_oauth_need_authorize', $sUrl)
  65. );
  66. }
  67. protected function getAccessToken($sCode)
  68. {
  69. $sResponse = bx_file_get_contents($this->sApiUrl . 'token', array(
  70. 'client_id' => $this->sKey,
  71. 'client_secret' => $this->sSecret,
  72. 'grant_type' => 'authorization_code',
  73. 'code' => $sCode,
  74. 'redirect_uri' => $this->sPageHandle,
  75. 'scope' => $this->sScope,
  76. ), $this->sDataRetrieveMethod);
  77. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !isset($aResponse['access_token']) || isset($aResponse['error']))
  78. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  79. // get access_token
  80. $sAccessToken = $aResponse['access_token'];
  81. // request info about profile
  82. $sResponse = bx_file_get_contents($this->sApiUrl . 'api/me', array(), 'get', array(
  83. 'Authorization: Bearer ' . $sAccessToken,
  84. ));
  85. // handle error
  86. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error']))
  87. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  88. $this->oSession->setValue('sys_oauth_token', $sAccessToken);
  89. $this->oSession->setValue('sys_oauth_authorized', 1);
  90. $this->oSession->setValue('sys_oauth_authorized_user', $aResponse['id']);
  91. return true;
  92. }
  93. protected function fetch($aParams = array())
  94. {
  95. if(!$this->isAuthorized())
  96. return array();
  97. $iTimeout = 120;
  98. if(ini_get('safe_mode') == 0) {
  99. ini_set('max_execution_time', $iTimeout);
  100. ini_set('memory_limit', '512M');
  101. }
  102. $sHttpCode = null;
  103. $sResponse = bx_file_get_contents($this->sApiUrl . 'api/market', $aParams, 'get', array(
  104. 'Authorization: Bearer ' . $this->oSession->getValue('sys_oauth_token'),
  105. ), $sHttpCode, array(), $iTimeout);
  106. //echo $sResponse; exit; //--- Uncomment to debug
  107. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error'])) {
  108. if(isset($aResponse['error']) && $this->isReloginRequired($aResponse['error']))
  109. $this->unsetAuthorizedUser();
  110. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  111. }
  112. return $aResponse['data'];
  113. }
  114. protected function isReloginRequired($sError)
  115. {
  116. if(in_array($sError, array('invalid_token', 'expired_token')))
  117. return true;
  118. return false;
  119. }
  120. protected function _genCsrfToken($bReturn = false)
  121. {
  122. if (getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
  123. return false;
  124. $oSession = BxDolSession::getInstance();
  125. $iCsrfTokenLifetime = (int)getParam('sys_security_form_token_lifetime');
  126. if ($oSession->getValue('bx_studio_store_csrf_token') === false || ($iCsrfTokenLifetime != 0 && time() - (int)$oSession->getValue('bx_studio_store_csrf_token_time') > $iCsrfTokenLifetime)) {
  127. $sToken = genRndPwd(20, false);
  128. $oSession->setValue('bx_studio_store_csrf_token', $sToken);
  129. $oSession->setValue('bx_studio_store_csrf_token_time', time());
  130. }
  131. else {
  132. $sToken = $oSession->getValue('bx_studio_store_csrf_token');
  133. }
  134. return $sToken;
  135. }
  136. protected function _getCsrfToken()
  137. {
  138. $oSession = BxDolSession::getInstance();
  139. return $oSession->getValue('bx_studio_store_csrf_token');
  140. }
  141. }
  142. /** @} */