PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/include/SugarOAuthServer.php

https://github.com/BarnetikKoop/SuiteCRM
PHP | 268 lines | 146 code | 17 blank | 105 comment | 32 complexity | a01fc5891ce3f3d4db64ce645c1068bc MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /*********************************************************************************
  3. * SugarCRM Community Edition is a customer relationship management program developed by
  4. * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
  5. * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
  6. * Copyright (C) 2011 - 2014 Salesagility Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it under
  9. * the terms of the GNU Affero General Public License version 3 as published by the
  10. * Free Software Foundation with the addition of the following permission added
  11. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  12. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  13. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  14. *
  15. * This program is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  17. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License along with
  21. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  22. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  23. * 02110-1301 USA.
  24. *
  25. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  26. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  27. *
  28. * The interactive user interfaces in modified source and object code versions
  29. * of this program must display Appropriate Legal Notices, as required under
  30. * Section 5 of the GNU Affero General Public License version 3.
  31. *
  32. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  33. * these Appropriate Legal Notices must retain the display of the "Powered by
  34. * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
  35. * reasonably feasible for technical reasons, the Appropriate Legal Notices must
  36. * display the words "Powered by SugarCRM" and "Supercharged by SuiteCRM".
  37. ********************************************************************************/
  38. require_once 'modules/OAuthTokens/OAuthToken.php';
  39. require_once 'modules/OAuthKeys/OAuthKey.php';
  40. /**
  41. * Sugar OAuth provider implementation
  42. * @api
  43. */
  44. class SugarOAuthServer
  45. {
  46. /**
  47. * OAuth token
  48. * @var OAuthToken
  49. */
  50. protected $token;
  51. /**
  52. * Check if everything is OK
  53. * @throws OAuthException
  54. */
  55. protected function check()
  56. {
  57. if(!function_exists('mhash') && !function_exists('hash_hmac')) {
  58. // define exception class
  59. throw new OAuthException("MHash extension required for OAuth support");
  60. }
  61. }
  62. /**
  63. * Is this functionality enabled?
  64. */
  65. public static function enabled()
  66. {
  67. return function_exists('mhash') || function_exists('hash_hmac');
  68. }
  69. /**
  70. * Find consumer by key
  71. * @param $provider
  72. */
  73. public function lookupConsumer($provider)
  74. {
  75. // check $provider->consumer_key
  76. // on unknown: Zend_Oauth_Provider::CONSUMER_KEY_UNKNOWN
  77. // on bad key: Zend_Oauth_Provider::CONSUMER_KEY_REFUSED
  78. $GLOBALS['log']->debug("OAUTH: lookupConsumer, key={$provider->consumer_key}");
  79. $consumer = OAuthKey::fetchKey($provider->consumer_key);
  80. if(!$consumer) {
  81. return Zend_Oauth_Provider::CONSUMER_KEY_UNKNOWN;
  82. }
  83. $provider->consumer_secret = $consumer->c_secret;
  84. $this->consumer = $consumer;
  85. return Zend_Oauth_Provider::OK;
  86. }
  87. /**
  88. * Check timestamps & nonces
  89. * @param OAuthProvider $provider
  90. */
  91. public function timestampNonceChecker($provider)
  92. {
  93. // FIXME: add ts/nonce verification
  94. if(empty($provider->nonce)) {
  95. return Zend_Oauth_Provider::BAD_NONCE;
  96. }
  97. if(empty($provider->timestamp)) {
  98. return Zend_Oauth_Provider::BAD_TIMESTAMP;
  99. }
  100. return OAuthToken::checkNonce($provider->consumer_key, $provider->nonce, $provider->timestamp);
  101. }
  102. /**
  103. * Vefiry incoming token
  104. * @param OAuthProvider $provider
  105. */
  106. public function tokenHandler($provider)
  107. {
  108. $GLOBALS['log']->debug("OAUTH: tokenHandler, token={$provider->token}, verify={$provider->verifier}");
  109. $token = OAuthToken::load($provider->token);
  110. if(empty($token)) {
  111. return Zend_Oauth_Provider::TOKEN_REJECTED;
  112. }
  113. if($token->consumer != $this->consumer->id) {
  114. return Zend_Oauth_Provider::TOKEN_REJECTED;
  115. }
  116. $GLOBALS['log']->debug("OAUTH: tokenHandler, found token=".var_export($token->id, true));
  117. if($token->tstate == OAuthToken::REQUEST) {
  118. if(!empty($token->verify) && $provider->verifier == $token->verify) {
  119. $provider->token_secret = $token->secret;
  120. $this->token = $token;
  121. return Zend_Oauth_Provider::OK;
  122. } else {
  123. return Zend_Oauth_Provider::TOKEN_USED;
  124. }
  125. }
  126. if($token->tstate == OAuthToken::ACCESS) {
  127. $provider->token_secret = $token->secret;
  128. $this->token = $token;
  129. return Zend_Oauth_Provider::OK;
  130. }
  131. return Zend_Oauth_Provider::TOKEN_REJECTED;
  132. }
  133. /**
  134. * Decode POST/GET via from_html()
  135. * @return array decoded data
  136. */
  137. protected function decodePostGet()
  138. {
  139. $data = $_GET;
  140. $data = array_merge($data, $_POST);
  141. foreach($data as $k => $v) {
  142. $data[$k] = from_html($v);
  143. }
  144. return $data;
  145. }
  146. /**
  147. * Create OAuth provider
  148. *
  149. * Checks current request for OAuth valitidy
  150. * @param bool $add_rest add REST endpoint as request path
  151. */
  152. public function __construct($req_path = '')
  153. {
  154. $GLOBALS['log']->debug("OAUTH: __construct($req_path): ".var_export($_REQUEST, true));
  155. $this->check();
  156. $this->provider = new Zend_Oauth_Provider();
  157. try {
  158. $this->provider->setConsumerHandler(array($this,'lookupConsumer'));
  159. $this->provider->setTimestampNonceHandler(array($this,'timestampNonceChecker'));
  160. $this->provider->setTokenHandler(array($this,'tokenHandler'));
  161. if(!empty($req_path)) {
  162. $this->provider->isRequestTokenEndpoint($req_path); // No token needed for this end point
  163. }
  164. $this->provider->checkOAuthRequest(null, $this->decodePostGet());
  165. if(mt_rand() % 10 == 0) {
  166. // cleanup 1 in 10 times
  167. OAuthToken::cleanup();
  168. }
  169. } catch(Exception $e) {
  170. $GLOBALS['log']->debug($this->reportProblem($e));
  171. throw $e;
  172. }
  173. }
  174. /**
  175. * Generate request token string
  176. * @return string
  177. */
  178. public function requestToken()
  179. {
  180. $GLOBALS['log']->debug("OAUTH: requestToken");
  181. $token = OAuthToken::generate();
  182. $token->setConsumer($this->consumer);
  183. $params = $this->provider->getOAuthParams();
  184. if(!empty($params['oauth_callback']) && $params['oauth_callback'] != 'oob') {
  185. $token->setCallbackURL($params['oauth_callback']);
  186. }
  187. $token->save();
  188. return $token->queryString();
  189. }
  190. /**
  191. * Generate access token string - must have validated request token
  192. * @return string
  193. */
  194. public function accessToken()
  195. {
  196. $GLOBALS['log']->debug("OAUTH: accessToken");
  197. if(empty($this->token) || $this->token->tstate != OAuthToken::REQUEST) {
  198. return null;
  199. }
  200. $this->token->invalidate();
  201. $token = OAuthToken::generate();
  202. $token->setState(OAuthToken::ACCESS);
  203. $token->setConsumer($this->consumer);
  204. // transfer user data from request token
  205. $token->copyAuthData($this->token);
  206. $token->save();
  207. return $token->queryString();
  208. }
  209. /**
  210. * Return authorization URL
  211. * @return string
  212. */
  213. public function authUrl()
  214. {
  215. return urlencode(rtrim($GLOBALS['sugar_config']['site_url'],'/')."/index.php?module=OAuthTokens&action=authorize");
  216. }
  217. /**
  218. * Fetch current token if it is authorized
  219. * @return OAuthToken|null
  220. */
  221. public function authorizedToken()
  222. {
  223. if($this->token->tstate == OAuthToken::ACCESS) {
  224. return $this->token;
  225. }
  226. return null;
  227. }
  228. /**
  229. * Fetch authorization data from current token
  230. * @return mixed Authorization data or null if none
  231. */
  232. public function authorization()
  233. {
  234. if($this->token->tstate == OAuthToken::ACCESS) {
  235. return $this->token->authdata;
  236. }
  237. return null;
  238. }
  239. /**
  240. * Report OAuth problem as string
  241. */
  242. public function reportProblem(Exception $e)
  243. {
  244. return $this->provider->reportProblem($e);
  245. }
  246. }
  247. if(!class_exists('OAuthException')) {
  248. // we will use this in case oauth extension is not loaded
  249. class OAuthException extends Exception {}
  250. }