PageRenderTime 20ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Oauth/Helper/Data.php

https://bitbucket.org/dnejedly/eaparts
PHP | 302 lines | 137 code | 30 blank | 135 comment | 17 complexity | b5b3876463ade4e34ee3d39aff113a9c MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Oauth
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * OAuth Helper
  28. *
  29. * @category Mage
  30. * @package Mage_Oauth
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Oauth_Helper_Data extends Mage_Core_Helper_Abstract
  34. {
  35. /**#@+
  36. * Endpoint types with appropriate routes
  37. */
  38. const ENDPOINT_AUTHORIZE_CUSTOMER = 'oauth/authorize';
  39. const ENDPOINT_AUTHORIZE_ADMIN = 'adminhtml/oauth_authorize';
  40. const ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE = 'oauth/authorize/simple';
  41. const ENDPOINT_AUTHORIZE_ADMIN_SIMPLE = 'adminhtml/oauth_authorize/simple';
  42. const ENDPOINT_INITIATE = 'oauth/initiate';
  43. const ENDPOINT_TOKEN = 'oauth/token';
  44. /**#@-*/
  45. /**#@+
  46. * Cleanup xpath config settings
  47. */
  48. const XML_PATH_CLEANUP_PROBABILITY = 'oauth/cleanup/cleanup_probability';
  49. const XML_PATH_CLEANUP_EXPIRATION_PERIOD = 'oauth/cleanup/expiration_period';
  50. /**#@-*/
  51. /**#@+ Email template */
  52. const XML_PATH_EMAIL_TEMPLATE = 'oauth/email/template';
  53. const XML_PATH_EMAIL_IDENTITY = 'oauth/email/identity';
  54. /**#@-*/
  55. /**
  56. * Cleanup expiration period in minutes
  57. */
  58. const CLEANUP_EXPIRATION_PERIOD_DEFAULT = 120;
  59. /**
  60. * Query parameter as a sign that user rejects
  61. */
  62. const QUERY_PARAM_REJECTED = 'rejected';
  63. /**
  64. * Available endpoints list
  65. *
  66. * @var array
  67. */
  68. protected $_endpoints = array(
  69. self::ENDPOINT_AUTHORIZE_CUSTOMER,
  70. self::ENDPOINT_AUTHORIZE_ADMIN,
  71. self::ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE,
  72. self::ENDPOINT_AUTHORIZE_ADMIN_SIMPLE,
  73. self::ENDPOINT_INITIATE,
  74. self::ENDPOINT_TOKEN
  75. );
  76. /**
  77. * Generate random string for token or secret or verifier
  78. *
  79. * @param int $length String length
  80. * @return string
  81. */
  82. protected function _generateRandomString($length)
  83. {
  84. /** @var $helper Mage_Core_Helper_Data */
  85. $helper = Mage::helper('core');
  86. return $helper->getRandomString(
  87. $length, Mage_Core_Helper_Data::CHARS_DIGITS . Mage_Core_Helper_Data::CHARS_LOWERS
  88. );
  89. }
  90. /**
  91. * Generate random string for token
  92. *
  93. * @return string
  94. */
  95. public function generateToken()
  96. {
  97. return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_TOKEN);
  98. }
  99. /**
  100. * Generate random string for token secret
  101. *
  102. * @return string
  103. */
  104. public function generateTokenSecret()
  105. {
  106. return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_SECRET);
  107. }
  108. /**
  109. * Generate random string for verifier
  110. *
  111. * @return string
  112. */
  113. public function generateVerifier()
  114. {
  115. return $this->_generateRandomString(Mage_Oauth_Model_Token::LENGTH_VERIFIER);
  116. }
  117. /**
  118. * Generate random string for consumer key
  119. *
  120. * @return string
  121. */
  122. public function generateConsumerKey()
  123. {
  124. return $this->_generateRandomString(Mage_Oauth_Model_Consumer::KEY_LENGTH);
  125. }
  126. /**
  127. * Generate random string for consumer secret
  128. *
  129. * @return string
  130. */
  131. public function generateConsumerSecret()
  132. {
  133. return $this->_generateRandomString(Mage_Oauth_Model_Consumer::SECRET_LENGTH);
  134. }
  135. /**
  136. * Return complete callback URL or boolean FALSE if no callback provided
  137. *
  138. * @param Mage_Oauth_Model_Token $token Token object
  139. * @param bool $rejected OPTIONAL Add user reject sign
  140. * @return bool|string
  141. */
  142. public function getFullCallbackUrl(Mage_Oauth_Model_Token $token, $rejected = false)
  143. {
  144. $callbackUrl = $token->getCallbackUrl();
  145. if (Mage_Oauth_Model_Server::CALLBACK_ESTABLISHED == $callbackUrl) {
  146. return false;
  147. }
  148. if ($rejected) {
  149. /** @var $consumer Mage_Oauth_Model_Consumer */
  150. $consumer = Mage::getModel('oauth/consumer')->load($token->getConsumerId());
  151. if ($consumer->getId() && $consumer->getRejectedCallbackUrl()) {
  152. $callbackUrl = $consumer->getRejectedCallbackUrl();
  153. }
  154. } elseif (!$token->getAuthorized()) {
  155. Mage::throwException('Token is not authorized');
  156. }
  157. $callbackUrl .= (false === strpos($callbackUrl, '?') ? '?' : '&');
  158. $callbackUrl .= 'oauth_token=' . $token->getToken() . '&';
  159. $callbackUrl .= $rejected ? self::QUERY_PARAM_REJECTED . '=1' : 'oauth_verifier=' . $token->getVerifier();
  160. return $callbackUrl;
  161. }
  162. /**
  163. * Retrieve URL of specified endpoint.
  164. *
  165. * @param string $type Endpoint type (one of ENDPOINT_ constants)
  166. * @return string
  167. * @throws Exception Exception when endpoint not found
  168. */
  169. public function getProtocolEndpointUrl($type)
  170. {
  171. if (!in_array($type, $this->_endpoints)) {
  172. throw new Exception('Invalid endpoint type passed.');
  173. }
  174. return rtrim(Mage::getUrl($type), '/');
  175. }
  176. /**
  177. * Calculate cleanup possibility for data with lifetime property
  178. *
  179. * @return bool
  180. */
  181. public function isCleanupProbability()
  182. {
  183. // Safe get cleanup probability value from system configuration
  184. $configValue = (int) Mage::getStoreConfig(self::XML_PATH_CLEANUP_PROBABILITY);
  185. return $configValue > 0 ? 1 == mt_rand(1, $configValue) : false;
  186. }
  187. /**
  188. * Get cleanup expiration period value from system configuration in minutes
  189. *
  190. * @return int
  191. */
  192. public function getCleanupExpirationPeriod()
  193. {
  194. $minutes = (int) Mage::getStoreConfig(self::XML_PATH_CLEANUP_EXPIRATION_PERIOD);
  195. return $minutes > 0 ? $minutes : self::CLEANUP_EXPIRATION_PERIOD_DEFAULT;
  196. }
  197. /**
  198. * Send Email to Token owner
  199. *
  200. * @param string $userEmail
  201. * @param string $userName
  202. * @param string $applicationName
  203. * @param string $status
  204. */
  205. public function sendNotificationOnTokenStatusChange($userEmail, $userName, $applicationName, $status)
  206. {
  207. /* @var $mailTemplate Mage_Core_Model_Email_Template */
  208. $mailTemplate = Mage::getModel('core/email_template');
  209. $mailTemplate->sendTransactional(
  210. Mage::getStoreConfig(self::XML_PATH_EMAIL_TEMPLATE),
  211. Mage::getStoreConfig(self::XML_PATH_EMAIL_IDENTITY),
  212. $userEmail,
  213. $userName,
  214. array(
  215. 'name' => $userName,
  216. 'email' => $userEmail,
  217. 'applicationName' => $applicationName,
  218. 'status' => $status,
  219. )
  220. );
  221. }
  222. /**
  223. * Is current authorize page is simple
  224. *
  225. * @return boolean
  226. */
  227. protected function _getIsSimple()
  228. {
  229. $simple = false;
  230. if (stristr($this->_getRequest()->getActionName(), 'simple')
  231. || !is_null($this->_getRequest()->getParam('simple', null))
  232. ) {
  233. $simple = true;
  234. }
  235. return $simple;
  236. }
  237. /**
  238. * Get authorize endpoint url
  239. *
  240. * @param string $userType
  241. * @return string
  242. */
  243. public function getAuthorizeUrl($userType)
  244. {
  245. $simple = $this->_getIsSimple();
  246. if (Mage_Oauth_Model_Token::USER_TYPE_CUSTOMER == $userType) {
  247. if ($simple) {
  248. $route = self::ENDPOINT_AUTHORIZE_CUSTOMER_SIMPLE;
  249. } else {
  250. $route = self::ENDPOINT_AUTHORIZE_CUSTOMER;
  251. }
  252. } elseif (Mage_Oauth_Model_Token::USER_TYPE_ADMIN == $userType) {
  253. if ($simple) {
  254. $route = self::ENDPOINT_AUTHORIZE_ADMIN_SIMPLE;
  255. } else {
  256. $route = self::ENDPOINT_AUTHORIZE_ADMIN;
  257. }
  258. } else {
  259. throw new Exception('Invalid user type.');
  260. }
  261. return $this->_getUrl($route, array('_query' => array('oauth_token' => $this->getOauthToken())));
  262. }
  263. /**
  264. * Retrieve oauth_token param from request
  265. *
  266. * @return string|null
  267. */
  268. public function getOauthToken()
  269. {
  270. return $this->_getRequest()->getParam('oauth_token', null);
  271. }
  272. }