DrupalOAuthDataStore.inc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Database abstraction class
  4. */
  5. class DrupalOAuthDataStore extends OAuthDataStore {
  6. private $context;
  7. public function __construct($context) {
  8. $this->context = $context;
  9. }
  10. /**
  11. * Check if consumer exists from a given consumer key.
  12. *
  13. * @param string $consumer_key
  14. * The consumer key.
  15. * @param bool $provider_consumer
  16. * Whether the consumer should be a provider consumer
  17. * @return OAuthConsumer
  18. * The consumer
  19. * @throws OAuthException
  20. * An exception is thrown when the consumer cannot be found
  21. */
  22. public function lookup_consumer($consumer_key, $provider_consumer = TRUE) {
  23. $consumer = DrupalOAuthConsumer::loadProviderByKey($consumer_key);
  24. if (!$consumer) {
  25. throw new OAuthException('Consumer not found');
  26. }
  27. return $consumer;
  28. }
  29. /**
  30. * Check if the token exists.
  31. *
  32. * @param OAuthConsumer $consumer
  33. * The consumer.
  34. * @param string $token_type
  35. * The type of the token: 'request' or 'access'.
  36. * @param string $token
  37. * The token key.
  38. * @return DrupalOauthToken
  39. * The matching token
  40. * @throws OAuthException
  41. * An exception is thrown when the token cannot be found or doesn't match
  42. */
  43. public function lookup_token($consumer, $token_type, $token) {
  44. $type = ($token_type == 'request' ? OAUTH_COMMON_TOKEN_TYPE_REQUEST : OAUTH_COMMON_TOKEN_TYPE_ACCESS);
  45. $token = DrupalOAuthToken::loadByKey($token, $consumer, $type);
  46. if (!$token) {
  47. throw new OAuthException('Token not found');
  48. }
  49. return $token;
  50. }
  51. /**
  52. * Check if the nonce value exists. If not, generate one.
  53. *
  54. * @param OAuthConsumer $consumer
  55. * The service consumer information with both key
  56. * and secret values.
  57. * @param OAuthToken $token
  58. * The current token.
  59. * @param string $nonce
  60. * A new nonce value, in case a one doesn't current exit.
  61. * @param int $timestamp
  62. * The current time.
  63. * @return string
  64. * The existing nonce value or NULL in
  65. * case it doesn't exist.
  66. */
  67. public function lookup_nonce($consumer, $token, $nonce, $timestamp) {
  68. if (strlen($nonce) > 255) {
  69. throw new OAuthException('Nonces may not be longer than 255 characters');
  70. }
  71. $stored_nonce = db_query(
  72. "SELECT nonce FROM {oauth_common_nonce}
  73. WHERE nonce = :nonce AND timestamp <= :timestamp and token_key = :token_key", array(
  74. ':nonce' => $nonce,
  75. ':timestamp' => $timestamp,
  76. ':token_key' => $token ? $token->key : '',
  77. ));
  78. if (!$stored_nonce->rowCount()) {
  79. $values = array(
  80. 'nonce' => $nonce,
  81. 'timestamp' => $timestamp,
  82. 'token_key' => $token ? $token->key : '',
  83. );
  84. drupal_write_record('oauth_common_nonce', $values);
  85. return NULL;
  86. }
  87. return $stored_nonce;
  88. }
  89. /**
  90. * Generate a new request token.
  91. *
  92. * @param OAuthConsumer $consumer
  93. * The consumer to generate a token for.
  94. * @return DrupalOAuthToken
  95. * The request token
  96. */
  97. function new_request_token($consumer, $callback = NULL) {
  98. $token = new DrupalOAuthToken(user_password(32), user_password(32), $consumer, array(
  99. 'type' => OAUTH_COMMON_TOKEN_TYPE_REQUEST,
  100. 'uid' => 0,
  101. 'expires' => REQUEST_TIME + variable_get('oauth_common_request_token_lifetime', 7200),
  102. ));
  103. $token->write();
  104. return $token;
  105. }
  106. /**
  107. * Generate a new access token and delete the old request token.
  108. *
  109. * @param DrupalOAuthToken $token_old
  110. * The old request token.
  111. * @param OAuthConsumer $consumer
  112. * The service consumer information.
  113. */
  114. function new_access_token($token_old, $consumer, $verifier = NULL) {
  115. module_load_include('inc', 'oauth_common');
  116. if ($token_old && $token_old->authorized) {
  117. $token_new = new DrupalOAuthToken(user_password(32), user_password(32), $consumer, array(
  118. 'type' => OAUTH_COMMON_TOKEN_TYPE_ACCESS,
  119. 'uid' => $token_old->uid,
  120. 'services' => isset($token_old->services) ? $token_old->services : NULL,
  121. 'authorized' => 1,
  122. ));
  123. $token_old->delete();
  124. $token_new->write();
  125. return $token_new;
  126. }
  127. throw new OAuthException('Invalid request token');
  128. }
  129. }