DrupalOAuthToken.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class DrupalOAuthToken extends OAuthToken {
  3. public $tid = 0;
  4. public $expires = 0;
  5. public $type = OAUTH_COMMON_TOKEN_TYPE_REQUEST;
  6. public $uid = 0;
  7. public $created = 0;
  8. public $changed = 0;
  9. public $services = array();
  10. public $authorized = 0;
  11. public $in_database = FALSE;
  12. public function __construct($key, $secret, $consumer, $params = array()) {
  13. foreach ($params as $param_key => $value) {
  14. if (isset($this->$param_key)) {
  15. $this->$param_key = $value;
  16. }
  17. }
  18. // Backwards compatibility with 6.x-3.0-beta3
  19. if (empty($consumer) || is_array($consumer)) {
  20. if (is_array($consumer)) {
  21. $params = $consumer;
  22. }
  23. if (!empty($params['csid'])) {
  24. $consumer = DrupalOAuthConsumer::loadById($params['csid'], isset($params['services']));
  25. }
  26. }
  27. if (!is_object($consumer)) {
  28. throw new OAuthException("Needs an associated consumer");
  29. }
  30. else {
  31. $this->consumer = $consumer;
  32. }
  33. parent::__construct($key, $secret);
  34. }
  35. /**
  36. * Writes the token to the database
  37. *
  38. * @return void
  39. */
  40. public function write() {
  41. $update = !empty($this->tid);
  42. $primary = $update ? array('tid') : array();
  43. if ($this->consumer->provider_consumer) {
  44. $this->changed = REQUEST_TIME;
  45. $values = array(
  46. 'token_key' => $this->key,
  47. 'changed' => $this->changed,
  48. 'services' => json_encode($this->services),
  49. 'authorized' => $this->authorized,
  50. );
  51. if ($update) {
  52. $values['tid'] = $this->tid;
  53. }
  54. else {
  55. $this->created = REQUEST_TIME;
  56. $values['created'] = $this->created;
  57. }
  58. $ready = drupal_write_record('oauth_common_provider_token', $values, $primary);
  59. if (!$ready) {
  60. throw new OAuthException("Couldn't save token");
  61. }
  62. }
  63. $values = array(
  64. 'csid' => $this->consumer->csid,
  65. 'key_hash' => sha1($this->key),
  66. 'token_key' => $this->key,
  67. 'secret' => $this->secret,
  68. 'expires' => $this->expires,
  69. 'type' => $this->type,
  70. 'uid' => $this->uid,
  71. );
  72. if ($update) {
  73. $values['tid'] = $this->tid;
  74. }
  75. drupal_write_record('oauth_common_token', $values, $primary);
  76. $this->tid = $values['tid'];
  77. $this->in_database = TRUE;
  78. if (!$update) {
  79. $values = array(
  80. 'tid' => $this->tid,
  81. 'token_key' => $this->key,
  82. );
  83. drupal_write_record('oauth_common_provider_token', $values, array('token_key'));
  84. }
  85. }
  86. /**
  87. * Deletes the token from the database
  88. *
  89. * @return void
  90. */
  91. public function delete() {
  92. self::deleteToken($this->key, $this->consumer);
  93. }
  94. /**
  95. * Deletes the token with the key from the database
  96. *
  97. * @param string $key
  98. * The key of the token to delete.
  99. * @param object $consumer
  100. * The consumer for which to fetch a token
  101. * @return void
  102. */
  103. public static function deleteToken($key, $consumer) {
  104. //TODO: Ensure backwards compatibility
  105. $condition = db_and()->condition('key_hash', sha1($key))->condition('csid', $consumer->csid);
  106. db_delete('oauth_common_provider_token')
  107. ->condition('tid', db_select('oauth_common_token', 't')->condition($condition)->fields('t', array('tid')), 'IN')
  108. ->execute();
  109. db_delete('oauth_common_token')
  110. ->condition($condition)
  111. ->execute();
  112. }
  113. /**
  114. * Deprecated - Gets the token with the specified key
  115. *
  116. * @param string $key
  117. * The key of the token to get
  118. * @param bool $provider_token
  119. * Whether the token to load is a provider token.
  120. * @return DrupalOAuthToken
  121. * The loaded token object or FALSE if load failed
  122. */
  123. public static function load($key, $provider_token = TRUE) {
  124. return DrupalOAuthToken::loadByKey($key, !$provider_token, FALSE);
  125. }
  126. /**
  127. * Gets the token with the specified key
  128. *
  129. * @param string $key
  130. * The key of the token to get
  131. * @param boolean|object $consumer
  132. * The consumer for which to fetch a token or FALSE to fetch a provider token
  133. * @param int $type
  134. * Used internally for backwards compatibility with ::load()
  135. * @return DrupalOAuthToken
  136. * The loaded token object or FALSE if load failed
  137. */
  138. public static function loadByKey($key, $consumer = FALSE, $type = OAUTH_COMMON_TOKEN_TYPE_ACCESS) {
  139. $query = db_select('oauth_common_token', 't');
  140. $query
  141. ->condition('t.key_hash', sha1($key))
  142. ->fields('t');
  143. // Only add if defined - needed for backwards compatibility with deprecated DrupalOAuthToken::load() from 6.x-3.0-beta3
  144. if ($type !== FALSE) {
  145. $query->condition('t.type', $type);
  146. }
  147. if (!$consumer || is_object($consumer) && $consumer->provider_consumer) {
  148. $query->join('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
  149. $query->fields('pt', array('created', 'changed', 'services', 'authorized'));
  150. }
  151. // Only fetch non-provider tokens - needed for backwards compatibility with deprecated DrupalOAuthToken::load() from 6.x-3.0-beta3
  152. if ($consumer === TRUE) {
  153. $query->leftJoin('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
  154. $query->isNull('pt.tid');
  155. }
  156. else if ($consumer) {
  157. $query->condition('t.csid', $consumer->csid);
  158. }
  159. return self::fromResult($query->execute(), $consumer);
  160. }
  161. /**
  162. * Gets the token with the specified id
  163. *
  164. * @param int $id
  165. * The id of the token to get
  166. * @param boolean $load_provider_data
  167. * Whether to load provider related data or not
  168. * @return DrupalOAuthToken
  169. * The loaded token object or FALSE if load failed
  170. */
  171. public static function loadById($tid, $load_provider_data = TRUE) {
  172. $query = db_select('oauth_common_token', 't');
  173. $query
  174. ->condition('t.tid', $tid)
  175. ->fields('t');
  176. if ($load_provider_data) {
  177. $query->join('oauth_common_provider_token', 'pt', 'pt.tid = t.tid');
  178. $query->fields('pt', array('created', 'changed', 'services', 'authorized'));
  179. }
  180. return self::fromResult($query->execute());
  181. }
  182. /**
  183. * Constructs a token from a db-result resource
  184. *
  185. * @param resource $res
  186. * A database result resource
  187. * @return DrupalOAuthToken
  188. * The constructed token object or NULL if no rows could be read or construction failed
  189. */
  190. public static function fromResult($res, $consumer = FALSE) {
  191. //TODO: Ensure this works with old inputs?
  192. if ($data = $res->fetchAssoc()) {
  193. if (isset($data['services'])) {
  194. $data['services'] = json_decode($data['services']);
  195. }
  196. $data['in_database'] = TRUE;
  197. if (is_object($consumer) && $consumer->csid == $data['csid']) {
  198. $token_consumer = $consumer;
  199. }
  200. else {
  201. $token_consumer = DrupalOAuthConsumer::loadById($data['csid'], isset($data['services']));
  202. }
  203. return new DrupalOAuthToken($data['token_key'], $data['secret'], $token_consumer, $data);
  204. }
  205. return NULL;
  206. }
  207. }