Config.php 17 KB

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