OAuth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Dropbox OAuth
  4. *
  5. * @package Dropbox
  6. * @copyright Copyright (C) 2010 Rooftop Solutions. All rights reserved.
  7. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  8. * @license http://code.google.com/p/dropbox-php/wiki/License MIT
  9. */
  10. /**
  11. * This class is an abstract OAuth class.
  12. *
  13. * It must be extended by classes who wish to provide OAuth functionality
  14. * using different libraries.
  15. */
  16. abstract class Dropbox_OAuth {
  17. /**
  18. * After a user has authorized access, dropbox can redirect the user back
  19. * to this url.
  20. *
  21. * @var string
  22. */
  23. public $authorizeCallbackUrl = null;
  24. /**
  25. * Uri used to fetch request tokens
  26. *
  27. * @var string
  28. */
  29. const URI_REQUEST_TOKEN = 'https://api.dropbox.com/1/oauth/request_token';
  30. /**
  31. * Uri used to redirect the user to for authorization.
  32. *
  33. * @var string
  34. */
  35. const URI_AUTHORIZE = 'https://www.dropbox.com/1/oauth/authorize';
  36. /**
  37. * Uri used to
  38. *
  39. * @var string
  40. */
  41. const URI_ACCESS_TOKEN = 'https://api.dropbox.com/1/oauth/access_token';
  42. /**
  43. * An OAuth request token.
  44. *
  45. * @var string
  46. */
  47. protected $oauth_token = null;
  48. /**
  49. * OAuth token secret
  50. *
  51. * @var string
  52. */
  53. protected $oauth_token_secret = null;
  54. /**
  55. * Constructor
  56. *
  57. * @param string $consumerKey
  58. * @param string $consumerSecret
  59. */
  60. abstract public function __construct($consumerKey, $consumerSecret);
  61. /**
  62. * Sets the request token and secret.
  63. *
  64. * The tokens can also be passed as an array into the first argument.
  65. * The array must have the elements token and token_secret.
  66. *
  67. * @param string|array $token
  68. * @param string $token_secret
  69. * @return void
  70. */
  71. public function setToken($token, $token_secret = null) {
  72. if (is_array($token)) {
  73. $this->oauth_token = $token['token'];
  74. $this->oauth_token_secret = $token['token_secret'];
  75. } else {
  76. $this->oauth_token = $token;
  77. $this->oauth_token_secret = $token_secret;
  78. }
  79. }
  80. /**
  81. * Returns the oauth request tokens as an associative array.
  82. *
  83. * The array will contain the elements 'token' and 'token_secret'.
  84. *
  85. * @return array
  86. */
  87. public function getToken() {
  88. return array(
  89. 'token' => $this->oauth_token,
  90. 'token_secret' => $this->oauth_token_secret,
  91. );
  92. }
  93. /**
  94. * Returns the authorization url
  95. *
  96. * @param string $callBack Specify a callback url to automatically redirect the user back
  97. * @return string
  98. */
  99. public function getAuthorizeUrl($callBack = null) {
  100. // Building the redirect uri
  101. $token = $this->getToken();
  102. $uri = self::URI_AUTHORIZE . '?oauth_token=' . $token['token'];
  103. if ($callBack) $uri.='&oauth_callback=' . $callBack;
  104. return $uri;
  105. }
  106. /**
  107. * Fetches a secured oauth url and returns the response body.
  108. *
  109. * @param string $uri
  110. * @param mixed $arguments
  111. * @param string $method
  112. * @param array $httpHeaders
  113. * @return string
  114. */
  115. public abstract function fetch($uri, $arguments = array(), $method = 'GET', $httpHeaders = array());
  116. /**
  117. * Requests the OAuth request token.
  118. *
  119. * @return array
  120. */
  121. abstract public function getRequestToken();
  122. /**
  123. * Requests the OAuth access tokens.
  124. *
  125. * @return array
  126. */
  127. abstract public function getAccessToken();
  128. }