PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/incubator/library/Zend/Oauth/Config.php

https://github.com/jorgenils/zend-framework
PHP | 555 lines | 236 code | 48 blank | 271 comment | 15 complexity | 4b32bc7d8b9eacf1a62ebde025ca7918 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  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@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Oauth
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /** Zend_Oauth */
  22. require_once 'Zend/Oauth.php';
  23. /** Zend_Uri */
  24. require_once 'Zend/Uri.php';
  25. /** Zend_Oauth_Config_Interface */
  26. require_once 'Zend/Oauth/Config/Interface.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Oauth
  30. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Oauth_Config implements Zend_Oauth_Config_Interface
  34. {
  35. /**
  36. * Signature method used when signing all parameters for an HTTP request
  37. *
  38. * @var string
  39. */
  40. protected $_signatureMethod = 'HMAC-SHA1';
  41. /**
  42. * Three request schemes are defined by OAuth, of which passing
  43. * all OAuth parameters by Header is preferred. The other two are
  44. * POST Body and Query String.
  45. *
  46. * @var string
  47. */
  48. protected $_requestScheme = Zend_Oauth::REQUEST_SCHEME_HEADER;
  49. /**
  50. * Preferred request Method - one of GET or POST - which Zend_Oauth
  51. * will enforce as standard throughout the library. Generally a default
  52. * of POST works fine unless a Provider specifically requires otherwise.
  53. *
  54. * @var string
  55. */
  56. protected $_requestMethod = Zend_Oauth::POST;
  57. /**
  58. * OAuth Version; at present there is only 1.0.
  59. *
  60. * @var string
  61. */
  62. protected $_version = '1.0';
  63. /**
  64. * This optional value is used to define where the user is redirected to
  65. * after authorising a Request Token from an OAuth Providers website.
  66. * It's optional since a Provider may ask for this to be defined in advance
  67. * when registering a new application for a Consumer Key.
  68. *
  69. * @var string
  70. */
  71. protected $_callbackUrl = null;
  72. /**
  73. * The URL to which requests for a Request Token should be directed.
  74. *
  75. * @var string
  76. */
  77. protected $_requestTokenUrl = null;
  78. /**
  79. * The URL to which requests for an Access Token should be directed.
  80. *
  81. * @var string
  82. */
  83. protected $_accessTokenUrl = null;
  84. /**
  85. * The URL to which users should be redirected to authorise a Request Token.
  86. *
  87. * @var string
  88. */
  89. protected $_userAuthorisationUrl = null;
  90. /**
  91. * An OAuth application's Consumer Key.
  92. *
  93. * @var string
  94. */
  95. protected $_consumerKey = null;
  96. /**
  97. * Every Consumer Key has a Consumer Secret unless you're in RSA-land.
  98. *
  99. * @var string
  100. */
  101. protected $_consumerSecret = null;
  102. /**
  103. * If relevant, a PEM encoded RSA private key encapsulated as a
  104. * Zend_Crypt_Rsa Key
  105. *
  106. * @var Zend_Crypt_Rsa_Key_Private
  107. */
  108. protected $_rsaPrivateKey = null;
  109. /**
  110. * If relevant, a PEM encoded RSA public key encapsulated as a
  111. * Zend_Crypt_Rsa Key
  112. *
  113. * @var Zend_Crypt_Rsa_Key_Public
  114. */
  115. protected $_rsaPublicKey = null;
  116. /**
  117. * Generally this will nearly always be an Access Token represented as a
  118. * Zend_Oauth_Token_Access object.
  119. *
  120. * @var Zend_Oauth_Token
  121. */
  122. protected $_token = null;
  123. /**
  124. * Constructor; create a new object with an optional array|Zend_Config
  125. * instance containing initialising options.
  126. *
  127. * @param array|Zend_Config $options
  128. * @return void
  129. */
  130. public function __construct($options = null)
  131. {
  132. if (!is_null($options)) {
  133. if ($options instanceof Zend_Config) {
  134. $options = $options->toArray();
  135. }
  136. $this->setOptions($options);
  137. }
  138. }
  139. /**
  140. * Parse option array or Zend_Config instance and setup options using their
  141. * relevant mutators.
  142. *
  143. * @param array|Zend_Config $options
  144. * @return void
  145. */
  146. public function setOptions(array $options)
  147. {
  148. foreach ($options as $key=>$value) {
  149. switch ($key) {
  150. case 'consumerKey':
  151. $this->setConsumerKey($value);
  152. break;
  153. case 'consumerSecret':
  154. $this->setConsumerSecret($value);
  155. break;
  156. case 'signatureMethod':
  157. $this->setSignatureMethod($value);
  158. break;
  159. case 'version':
  160. $this->setVersion($value);
  161. break;
  162. case 'localUrl':
  163. $this->setCallbackUrl($value);
  164. break;
  165. case 'callbackUrl':
  166. $this->setCallbackUrl($value);
  167. break;
  168. case 'requestTokenUrl':
  169. $this->setRequestTokenUrl($value);
  170. break;
  171. case 'accessTokenUrl':
  172. $this->setAccessTokenUrl($value);
  173. break;
  174. case 'userAuthorisationUrl':
  175. $this->setUserAuthorisationUrl($value);
  176. break;
  177. case 'requestMethod':
  178. $this->setRequestMethod($value);
  179. break;
  180. case 'rsaPrivateKey':
  181. $this->setRsaPrivateKey($value);
  182. break;
  183. case 'rsaPublicKey':
  184. $this->setRsaPublicKey($value);
  185. break;
  186. }
  187. }
  188. if (isset($options['requestScheme'])) {
  189. $this->setRequestScheme($options['requestScheme']);
  190. }
  191. }
  192. /**
  193. * Generic mutator for an option value recognised in this class.
  194. *
  195. * @param string
  196. * @return void
  197. */
  198. public function setConsumerKey($key)
  199. {
  200. $this->_consumerKey = $key;
  201. }
  202. /**
  203. * Generic accessor for an option value recognised in this class.
  204. *
  205. * @return string
  206. */
  207. public function getConsumerKey()
  208. {
  209. return $this->_consumerKey;
  210. }
  211. /**
  212. * Generic mutator for an option value recognised in this class.
  213. *
  214. * @param string
  215. * @return void
  216. */
  217. public function setConsumerSecret($secret)
  218. {
  219. $this->_consumerSecret = $secret;
  220. }
  221. /**
  222. * Generic accessor for an option value recognised in this class.
  223. *
  224. * @return string
  225. */
  226. public function getConsumerSecret()
  227. {
  228. if (!is_null($this->_rsaPrivateKey)) {
  229. return $this->_rsaPrivateKey;
  230. }
  231. return $this->_consumerSecret;
  232. }
  233. /**
  234. * Generic mutator for an option value recognised in this class.
  235. *
  236. * @param string
  237. * @return void
  238. */
  239. public function setSignatureMethod($method)
  240. {
  241. $this->_signatureMethod = strtoupper($method);
  242. }
  243. /**
  244. * Generic accessor for an option value recognised in this class.
  245. *
  246. * @return string
  247. */
  248. public function getSignatureMethod()
  249. {
  250. return $this->_signatureMethod;
  251. }
  252. /**
  253. * Generic mutator for an option value recognised in this class.
  254. *
  255. * @param string
  256. * @return void
  257. */
  258. public function setRequestScheme($scheme)
  259. {
  260. $scheme = strtolower($scheme);
  261. if (!in_array($scheme, array(
  262. Zend_Oauth::REQUEST_SCHEME_HEADER,
  263. Zend_Oauth::REQUEST_SCHEME_POSTBODY,
  264. Zend_Oauth::REQUEST_SCHEME_QUERYSTRING
  265. ))) {
  266. require_once 'Zend/Oauth/Exception.php';
  267. throw new Zend_Oauth_Exception(
  268. '\'' . $scheme . '\' is an unsupported request scheme'
  269. );
  270. }
  271. if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY
  272. && $this->getRequestMethod() == Zend_Oauth::GET) {
  273. require_once 'Zend/Oauth/Exception.php';
  274. throw new Zend_Oauth_Exception(
  275. 'Cannot set POSTBODY request method if HTTP method set to GET'
  276. );
  277. }
  278. $this->_requestScheme = $scheme;
  279. }
  280. /**
  281. * Generic accessor for an option value recognised in this class.
  282. *
  283. * @return string
  284. */
  285. public function getRequestScheme()
  286. {
  287. return $this->_requestScheme;
  288. }
  289. /**
  290. * Generic mutator for an option value recognised in this class.
  291. *
  292. * @param string
  293. * @return void
  294. */
  295. public function setVersion($version)
  296. {
  297. $this->_version = $version;
  298. }
  299. /**
  300. * Generic accessor for an option value recognised in this class.
  301. *
  302. * @return string
  303. */
  304. public function getVersion()
  305. {
  306. return $this->_version;
  307. }
  308. /**
  309. * Generic mutator for an option value recognised in this class.
  310. *
  311. * @param string
  312. * @return void
  313. */
  314. public function setCallbackUrl($url)
  315. {
  316. if (!Zend_Uri::check($url)) {
  317. require_once 'Zend/Oauth/Exception.php';
  318. throw new Zend_Oauth_Exception(
  319. '\'' . $url . '\' is not a valid URI'
  320. );
  321. }
  322. $this->_callbackUrl = $url;
  323. }
  324. /**
  325. * Generic accessor for an option value recognised in this class.
  326. *
  327. * @return string
  328. */
  329. public function getCallbackUrl()
  330. {
  331. return $this->_callbackUrl;
  332. }
  333. /**
  334. * Generic mutator for an option value recognised in this class.
  335. *
  336. * @param string
  337. * @return void
  338. */
  339. public function setLocalUrl($url)
  340. {
  341. $this->setCallbackUrl($url);
  342. }
  343. /**
  344. * Generic accessor for an option value recognised in this class.
  345. *
  346. * @return string
  347. */
  348. public function getLocalUrl()
  349. {
  350. return $this->getCallbackUrl();
  351. }
  352. /**
  353. * Generic mutator for an option value recognised in this class.
  354. *
  355. * @param string
  356. * @return void
  357. */
  358. public function setRequestTokenUrl($url)
  359. {
  360. if (!Zend_Uri::check($url)) {
  361. require_once 'Zend/Oauth/Exception.php';
  362. throw new Zend_Oauth_Exception(
  363. '\'' . $url . '\' is not a valid URI'
  364. );
  365. }
  366. $this->_requestTokenUrl = $url;
  367. }
  368. /**
  369. * Generic accessor for an option value recognised in this class.
  370. *
  371. * @return string
  372. */
  373. public function getRequestTokenUrl()
  374. {
  375. return $this->_requestTokenUrl;
  376. }
  377. /**
  378. * Generic mutator for an option value recognised in this class.
  379. *
  380. * @param string
  381. * @return void
  382. */
  383. public function setAccessTokenUrl($url)
  384. {
  385. if (!Zend_Uri::check($url)) {
  386. require_once 'Zend/Oauth/Exception.php';
  387. throw new Zend_Oauth_Exception(
  388. '\'' . $url . '\' is not a valid URI'
  389. );
  390. }
  391. $this->_accessTokenUrl = $url;
  392. }
  393. /**
  394. * Generic accessor for an option value recognised in this class.
  395. *
  396. * @return string
  397. */
  398. public function getAccessTokenUrl()
  399. {
  400. return $this->_accessTokenUrl;
  401. }
  402. /**
  403. * Generic mutator for an option value recognised in this class.
  404. *
  405. * @param string
  406. * @return void
  407. */
  408. public function setUserAuthorisationUrl($url)
  409. {
  410. if (!Zend_Uri::check($url)) {
  411. require_once 'Zend/Oauth/Exception.php';
  412. throw new Zend_Oauth_Exception(
  413. '\'' . $url . '\' is not a valid URI'
  414. );
  415. }
  416. $this->_userAuthorisationUrl = $url;
  417. }
  418. /**
  419. * Generic accessor for an option value recognised in this class.
  420. *
  421. * @return string
  422. */
  423. public function getUserAuthorisationUrl()
  424. {
  425. return $this->_userAuthorisationUrl;
  426. }
  427. /**
  428. * Generic mutator for an option value recognised in this class.
  429. *
  430. * @param string
  431. * @return void
  432. */
  433. public function setRequestMethod($method)
  434. {
  435. if (!in_array($method, array(Zend_Oauth::GET, Zend_Oauth::POST))) {
  436. require_once 'Zend/Oauth/Exception.php';
  437. throw new Zend_Oauth_Exception('Invalid method: '.$method);
  438. }
  439. $this->_requestMethod = $method;
  440. }
  441. /**
  442. * Generic accessor for an option value recognised in this class.
  443. *
  444. * @return string
  445. */
  446. public function getRequestMethod()
  447. {
  448. return $this->_requestMethod;
  449. }
  450. /**
  451. * Generic mutator for an option value recognised in this class.
  452. *
  453. * @param Zend_Crypt_Rsa_Key_Public
  454. * @return void
  455. */
  456. public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key)
  457. {
  458. $this->_rsaPublicKey = $key;
  459. }
  460. /**
  461. * Generic accessor for an option value recognised in this class.
  462. *
  463. * @return Zend_Crypt_Rsa_Key_Public
  464. */
  465. public function getRsaPublicKey()
  466. {
  467. return $this->_rsaPublicKey;
  468. }
  469. /**
  470. * Generic mutator for an option value recognised in this class.
  471. *
  472. * @param Zend_Crypt_Rsa_Key_Private
  473. * @return void
  474. */
  475. public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key)
  476. {
  477. $this->_rsaPrivateKey = $key;
  478. }
  479. /**
  480. * Generic accessor for an option value recognised in this class.
  481. *
  482. * @return Zend_Crypt_Rsa_Key_Private
  483. */
  484. public function getRsaPrivateKey()
  485. {
  486. return $this->_rsaPrivateKey;
  487. }
  488. /**
  489. * Generic mutator for an option value recognised in this class.
  490. *
  491. * @param string
  492. * @return void
  493. */
  494. public function setToken(Zend_Oauth_Token $token)
  495. {
  496. $this->_token = $token;
  497. }
  498. /**
  499. * Generic accessor for an option value recognised in this class.
  500. *
  501. * @return Zend_Oauth_Token
  502. */
  503. public function getToken()
  504. {
  505. return $this->_token;
  506. }
  507. }