Config.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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-2010 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/ConfigInterface.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Oauth
  30. * @copyright Copyright (c) 2005-2010 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_ConfigInterface
  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; This defaults to 1.0 - Must not be changed!
  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 authorizing 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 root to append default OAuth endpoint paths.
  74. *
  75. * @var string
  76. */
  77. protected $_siteUrl = null;
  78. /**
  79. * The URL to which requests for a Request Token should be directed.
  80. * When absent, assumed siteUrl+'/request_token'
  81. *
  82. * @var string
  83. */
  84. protected $_requestTokenUrl = null;
  85. /**
  86. * The URL to which requests for an Access Token should be directed.
  87. * When absent, assumed siteUrl+'/access_token'
  88. *
  89. * @var string
  90. */
  91. protected $_accessTokenUrl = null;
  92. /**
  93. * The URL to which users should be redirected to authorize a Request Token.
  94. * When absent, assumed siteUrl+'/authorize'
  95. *
  96. * @var string
  97. */
  98. protected $_authorizeUrl = null;
  99. /**
  100. * An OAuth application's Consumer Key.
  101. *
  102. * @var string
  103. */
  104. protected $_consumerKey = null;
  105. /**
  106. * Every Consumer Key has a Consumer Secret unless you're in RSA-land.
  107. *
  108. * @var string
  109. */
  110. protected $_consumerSecret = null;
  111. /**
  112. * If relevant, a PEM encoded RSA private key encapsulated as a
  113. * Zend_Crypt_Rsa Key
  114. *
  115. * @var Zend_Crypt_Rsa_Key_Private
  116. */
  117. protected $_rsaPrivateKey = null;
  118. /**
  119. * If relevant, a PEM encoded RSA public key encapsulated as a
  120. * Zend_Crypt_Rsa Key
  121. *
  122. * @var Zend_Crypt_Rsa_Key_Public
  123. */
  124. protected $_rsaPublicKey = null;
  125. /**
  126. * Generally this will nearly always be an Access Token represented as a
  127. * Zend_Oauth_Token_Access object.
  128. *
  129. * @var Zend_Oauth_Token
  130. */
  131. protected $_token = null;
  132. /**
  133. * Constructor; create a new object with an optional array|Zend_Config
  134. * instance containing initialising options.
  135. *
  136. * @param array|Zend_Config $options
  137. * @return void
  138. */
  139. public function __construct($options = null)
  140. {
  141. if ($options !== null) {
  142. if ($options instanceof Zend_Config) {
  143. $options = $options->toArray();
  144. }
  145. $this->setOptions($options);
  146. }
  147. }
  148. /**
  149. * Parse option array or Zend_Config instance and setup options using their
  150. * relevant mutators.
  151. *
  152. * @param array|Zend_Config $options
  153. * @return Zend_Oauth_Config
  154. */
  155. public function setOptions(array $options)
  156. {
  157. foreach ($options as $key => $value) {
  158. switch ($key) {
  159. case 'consumerKey':
  160. $this->setConsumerKey($value);
  161. break;
  162. case 'consumerSecret':
  163. $this->setConsumerSecret($value);
  164. break;
  165. case 'signatureMethod':
  166. $this->setSignatureMethod($value);
  167. break;
  168. case 'version':
  169. $this->setVersion($value);
  170. break;
  171. case 'callbackUrl':
  172. $this->setCallbackUrl($value);
  173. break;
  174. case 'siteUrl':
  175. $this->setSiteUrl($value);
  176. break;
  177. case 'requestTokenUrl':
  178. $this->setRequestTokenUrl($value);
  179. break;
  180. case 'accessTokenUrl':
  181. $this->setAccessTokenUrl($value);
  182. break;
  183. case 'userAuthorizationUrl':
  184. $this->setUserAuthorizationUrl($value);
  185. break;
  186. case 'authorizeUrl':
  187. $this->setAuthorizeUrl($value);
  188. break;
  189. case 'requestMethod':
  190. $this->setRequestMethod($value);
  191. break;
  192. case 'rsaPrivateKey':
  193. $this->setRsaPrivateKey($value);
  194. break;
  195. case 'rsaPublicKey':
  196. $this->setRsaPublicKey($value);
  197. break;
  198. }
  199. }
  200. if (isset($options['requestScheme'])) {
  201. $this->setRequestScheme($options['requestScheme']);
  202. }
  203. return $this;
  204. }
  205. /**
  206. * Set consumer key
  207. *
  208. * @param string $key
  209. * @return Zend_Oauth_Config
  210. */
  211. public function setConsumerKey($key)
  212. {
  213. $this->_consumerKey = $key;
  214. return $this;
  215. }
  216. /**
  217. * Get consumer key
  218. *
  219. * @return string
  220. */
  221. public function getConsumerKey()
  222. {
  223. return $this->_consumerKey;
  224. }
  225. /**
  226. * Set consumer secret
  227. *
  228. * @param string $secret
  229. * @return Zend_Oauth_Config
  230. */
  231. public function setConsumerSecret($secret)
  232. {
  233. $this->_consumerSecret = $secret;
  234. return $this;
  235. }
  236. /**
  237. * Get consumer secret
  238. *
  239. * Returns RSA private key if set; otherwise, returns any previously set
  240. * consumer secret.
  241. *
  242. * @return string
  243. */
  244. public function getConsumerSecret()
  245. {
  246. if ($this->_rsaPrivateKey !== null) {
  247. return $this->_rsaPrivateKey;
  248. }
  249. return $this->_consumerSecret;
  250. }
  251. /**
  252. * Set signature method
  253. *
  254. * @param string $method
  255. * @return Zend_Oauth_Config
  256. * @throws Zend_Oauth_Exception if unsupported signature method specified
  257. */
  258. public function setSignatureMethod($method)
  259. {
  260. $method = strtoupper($method);
  261. if (!in_array($method, array(
  262. 'HMAC-SHA1', 'HMAC-SHA256', 'RSA-SHA1', 'PLAINTEXT'
  263. ))
  264. ) {
  265. require_once 'Zend/Oauth/Exception.php';
  266. throw new Zend_Oauth_Exception('Unsupported signature method: '
  267. . $method
  268. . '. Supported are HMAC-SHA1, RSA-SHA1, PLAINTEXT and HMAC-SHA256');
  269. }
  270. $this->_signatureMethod = $method;;
  271. return $this;
  272. }
  273. /**
  274. * Get signature method
  275. *
  276. * @return string
  277. */
  278. public function getSignatureMethod()
  279. {
  280. return $this->_signatureMethod;
  281. }
  282. /**
  283. * Set request scheme
  284. *
  285. * @param string $scheme
  286. * @return Zend_Oauth_Config
  287. * @throws Zend_Oauth_Exception if invalid scheme specified, or if POSTBODY set when request method of GET is specified
  288. */
  289. public function setRequestScheme($scheme)
  290. {
  291. $scheme = strtolower($scheme);
  292. if (!in_array($scheme, array(
  293. Zend_Oauth::REQUEST_SCHEME_HEADER,
  294. Zend_Oauth::REQUEST_SCHEME_POSTBODY,
  295. Zend_Oauth::REQUEST_SCHEME_QUERYSTRING,
  296. ))
  297. ) {
  298. require_once 'Zend/Oauth/Exception.php';
  299. throw new Zend_Oauth_Exception(
  300. '\'' . $scheme . '\' is an unsupported request scheme'
  301. );
  302. }
  303. if ($scheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY
  304. && $this->getRequestMethod() == Zend_Oauth::GET
  305. ) {
  306. require_once 'Zend/Oauth/Exception.php';
  307. throw new Zend_Oauth_Exception(
  308. 'Cannot set POSTBODY request method if HTTP method set to GET'
  309. );
  310. }
  311. $this->_requestScheme = $scheme;
  312. return $this;
  313. }
  314. /**
  315. * Get request scheme
  316. *
  317. * @return string
  318. */
  319. public function getRequestScheme()
  320. {
  321. return $this->_requestScheme;
  322. }
  323. /**
  324. * Set version
  325. *
  326. * @param string $version
  327. * @return Zend_Oauth_Config
  328. */
  329. public function setVersion($version)
  330. {
  331. $this->_version = $version;
  332. return $this;
  333. }
  334. /**
  335. * Get version
  336. *
  337. * @return string
  338. */
  339. public function getVersion()
  340. {
  341. return $this->_version;
  342. }
  343. /**
  344. * Set callback URL
  345. *
  346. * @param string $url
  347. * @return Zend_Oauth_Config
  348. * @throws Zend_Oauth_Exception for invalid URLs
  349. */
  350. public function setCallbackUrl($url)
  351. {
  352. if (!Zend_Uri::check($url)) {
  353. require_once 'Zend/Oauth/Exception.php';
  354. throw new Zend_Oauth_Exception(
  355. '\'' . $url . '\' is not a valid URI'
  356. );
  357. }
  358. $this->_callbackUrl = $url;
  359. return $this;
  360. }
  361. /**
  362. * Get callback URL
  363. *
  364. * @return string
  365. */
  366. public function getCallbackUrl()
  367. {
  368. return $this->_callbackUrl;
  369. }
  370. /**
  371. * Set site URL
  372. *
  373. * @param string $url
  374. * @return Zend_Oauth_Config
  375. * @throws Zend_Oauth_Exception for invalid URLs
  376. */
  377. public function setSiteUrl($url)
  378. {
  379. if (!Zend_Uri::check($url)) {
  380. require_once 'Zend/Oauth/Exception.php';
  381. throw new Zend_Oauth_Exception(
  382. '\'' . $url . '\' is not a valid URI'
  383. );
  384. }
  385. $this->_siteUrl = $url;
  386. return $this;
  387. }
  388. /**
  389. * Get site URL
  390. *
  391. * @return string
  392. */
  393. public function getSiteUrl()
  394. {
  395. return $this->_siteUrl;
  396. }
  397. /**
  398. * Set request token URL
  399. *
  400. * @param string $url
  401. * @return Zend_Oauth_Config
  402. * @throws Zend_Oauth_Exception for invalid URLs
  403. */
  404. public function setRequestTokenUrl($url)
  405. {
  406. if (!Zend_Uri::check($url)) {
  407. require_once 'Zend/Oauth/Exception.php';
  408. throw new Zend_Oauth_Exception(
  409. '\'' . $url . '\' is not a valid URI'
  410. );
  411. }
  412. $this->_requestTokenUrl = rtrim($url, '/');
  413. return $this;
  414. }
  415. /**
  416. * Get request token URL
  417. *
  418. * If no request token URL has been set, but a site URL has, returns the
  419. * site URL with the string "/request_token" appended.
  420. *
  421. * @return string
  422. */
  423. public function getRequestTokenUrl()
  424. {
  425. if (!$this->_requestTokenUrl && $this->_siteUrl) {
  426. return $this->_siteUrl . '/request_token';
  427. }
  428. return $this->_requestTokenUrl;
  429. }
  430. /**
  431. * Set access token URL
  432. *
  433. * @param string $url
  434. * @return Zend_Oauth_Config
  435. * @throws Zend_Oauth_Exception for invalid URLs
  436. */
  437. public function setAccessTokenUrl($url)
  438. {
  439. if (!Zend_Uri::check($url)) {
  440. require_once 'Zend/Oauth/Exception.php';
  441. throw new Zend_Oauth_Exception(
  442. '\'' . $url . '\' is not a valid URI'
  443. );
  444. }
  445. $this->_accessTokenUrl = rtrim($url, '/');
  446. return $this;
  447. }
  448. /**
  449. * Get access token URL
  450. *
  451. * If no access token URL has been set, but a site URL has, returns the
  452. * site URL with the string "/access_token" appended.
  453. *
  454. * @return string
  455. */
  456. public function getAccessTokenUrl()
  457. {
  458. if (!$this->_accessTokenUrl && $this->_siteUrl) {
  459. return $this->_siteUrl . '/access_token';
  460. }
  461. return $this->_accessTokenUrl;
  462. }
  463. /**
  464. * Set user authorization URL
  465. *
  466. * @param string $url
  467. * @return Zend_Oauth_Config
  468. * @throws Zend_Oauth_Exception for invalid URLs
  469. */
  470. public function setUserAuthorizationUrl($url)
  471. {
  472. return $this->setAuthorizeUrl($url);
  473. }
  474. /**
  475. * Set authorization URL
  476. *
  477. * @param string $url
  478. * @return Zend_Oauth_Config
  479. * @throws Zend_Oauth_Exception for invalid URLs
  480. */
  481. public function setAuthorizeUrl($url)
  482. {
  483. if (!Zend_Uri::check($url)) {
  484. require_once 'Zend/Oauth/Exception.php';
  485. throw new Zend_Oauth_Exception(
  486. '\'' . $url . '\' is not a valid URI'
  487. );
  488. }
  489. $this->_authorizeUrl = rtrim($url, '/');
  490. return $this;
  491. }
  492. /**
  493. * Get user authorization URL
  494. *
  495. * @return string
  496. */
  497. public function getUserAuthorizationUrl()
  498. {
  499. return $this->getAuthorizeUrl();
  500. }
  501. /**
  502. * Get authorization URL
  503. *
  504. * If no authorization URL has been set, but a site URL has, returns the
  505. * site URL with the string "/authorize" appended.
  506. *
  507. * @return string
  508. */
  509. public function getAuthorizeUrl()
  510. {
  511. if (!$this->_authorizeUrl && $this->_siteUrl) {
  512. return $this->_siteUrl . '/authorize';
  513. }
  514. return $this->_authorizeUrl;
  515. }
  516. /**
  517. * Set request method
  518. *
  519. * @param string $method
  520. * @return Zend_Oauth_Config
  521. * @throws Zend_Oauth_Exception for invalid request methods
  522. */
  523. public function setRequestMethod($method)
  524. {
  525. $method = strtoupper($method);
  526. if (!in_array($method, array(
  527. Zend_Oauth::GET,
  528. Zend_Oauth::POST,
  529. Zend_Oauth::PUT,
  530. Zend_Oauth::DELETE,
  531. ))
  532. ) {
  533. require_once 'Zend/Oauth/Exception.php';
  534. throw new Zend_Oauth_Exception('Invalid method: ' . $method);
  535. }
  536. $this->_requestMethod = $method;
  537. return $this;
  538. }
  539. /**
  540. * Get request method
  541. *
  542. * @return string
  543. */
  544. public function getRequestMethod()
  545. {
  546. return $this->_requestMethod;
  547. }
  548. /**
  549. * Set RSA public key
  550. *
  551. * @param Zend_Crypt_Rsa_Key_Public $key
  552. * @return Zend_Oauth_Config
  553. */
  554. public function setRsaPublicKey(Zend_Crypt_Rsa_Key_Public $key)
  555. {
  556. $this->_rsaPublicKey = $key;
  557. return $this;
  558. }
  559. /**
  560. * Get RSA public key
  561. *
  562. * @return Zend_Crypt_Rsa_Key_Public
  563. */
  564. public function getRsaPublicKey()
  565. {
  566. return $this->_rsaPublicKey;
  567. }
  568. /**
  569. * Set RSA private key
  570. *
  571. * @param Zend_Crypt_Rsa_Key_Private $key
  572. * @return Zend_Oauth_Config
  573. */
  574. public function setRsaPrivateKey(Zend_Crypt_Rsa_Key_Private $key)
  575. {
  576. $this->_rsaPrivateKey = $key;
  577. return $this;
  578. }
  579. /**
  580. * Get RSA private key
  581. *
  582. * @return Zend_Crypt_Rsa_Key_Private
  583. */
  584. public function getRsaPrivateKey()
  585. {
  586. return $this->_rsaPrivateKey;
  587. }
  588. /**
  589. * Set OAuth token
  590. *
  591. * @param Zend_Oauth_Token $token
  592. * @return Zend_Oauth_Config
  593. */
  594. public function setToken(Zend_Oauth_Token $token)
  595. {
  596. $this->_token = $token;
  597. return $this;
  598. }
  599. /**
  600. * Get OAuth token
  601. *
  602. * @return Zend_Oauth_Token
  603. */
  604. public function getToken()
  605. {
  606. return $this->_token;
  607. }
  608. }