BxDolStudioOAuthOAuth2.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 _t('_adm_msg_oauth_need_authorize', $sUrl);
  63. }
  64. protected function getAccessToken($sCode)
  65. {
  66. $sResponse = bx_file_get_contents($this->sApiUrl . 'token', array(
  67. 'client_id' => $this->sKey,
  68. 'client_secret' => $this->sSecret,
  69. 'grant_type' => 'authorization_code',
  70. 'code' => $sCode,
  71. 'redirect_uri' => $this->sPageHandle,
  72. 'scope' => $this->sScope,
  73. ), $this->sDataRetrieveMethod);
  74. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !isset($aResponse['access_token']) || isset($aResponse['error']))
  75. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  76. // get access_token
  77. $sAccessToken = $aResponse['access_token'];
  78. // request info about profile
  79. $sResponse = bx_file_get_contents($this->sApiUrl . 'api/me', array(), 'get', array(
  80. 'Authorization: Bearer ' . $sAccessToken,
  81. ));
  82. // handle error
  83. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error']))
  84. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  85. $this->oSession->setValue('sys_oauth_token', $sAccessToken);
  86. $this->oSession->setValue('sys_oauth_authorized', 1);
  87. $this->oSession->setValue('sys_oauth_authorized_user', $aResponse['id']);
  88. return true;
  89. }
  90. protected function fetch($aParams = array())
  91. {
  92. if(!$this->isAuthorized())
  93. return array();
  94. $sResponse = bx_file_get_contents($this->sApiUrl . 'api/market', $aParams, 'get', array(
  95. 'Authorization: Bearer ' . $this->oSession->getValue('sys_oauth_token'),
  96. ));
  97. //echo $sResponse; exit; //--- Uncomment to debug
  98. if (!$sResponse || ($aResponse = json_decode($sResponse, true)) === NULL || !$aResponse || isset($aResponse['error'])) {
  99. if($this->isReloginRequired($aResponse['error']))
  100. $this->unsetAuthorizedUser();
  101. return isset($aResponse['error_description']) ? $aResponse['error_description'] : _t('_error occured');
  102. }
  103. return $aResponse['data'];
  104. }
  105. protected function isReloginRequired($sError)
  106. {
  107. if(in_array($sError, array('expired_token')))
  108. return true;
  109. return false;
  110. }
  111. protected function _genCsrfToken($bReturn = false)
  112. {
  113. if (getParam('sys_security_form_token_enable') != 'on' || defined('BX_DOL_CRON_EXECUTE'))
  114. return false;
  115. $oSession = BxDolSession::getInstance();
  116. $iCsrfTokenLifetime = (int)getParam('sys_security_form_token_lifetime');
  117. if ($oSession->getValue('bx_studio_store_csrf_token') === false || ($iCsrfTokenLifetime != 0 && time() - (int)$oSession->getValue('bx_studio_store_csrf_token_time') > $iCsrfTokenLifetime)) {
  118. $sToken = genRndPwd(20, false);
  119. $oSession->setValue('bx_studio_store_csrf_token', $sToken);
  120. $oSession->setValue('bx_studio_store_csrf_token_time', time());
  121. }
  122. else {
  123. $sToken = $oSession->getValue('bx_studio_store_csrf_token');
  124. }
  125. return $sToken;
  126. }
  127. protected function _getCsrfToken()
  128. {
  129. $oSession = BxDolSession::getInstance();
  130. return $oSession->getValue('bx_studio_store_csrf_token');
  131. }
  132. }
  133. /** @} */