oauth_common.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /**
  3. * Return information about consumers related to a user.
  4. *
  5. * @param int $uid
  6. * User ID to retrieve consumer info for.
  7. * @return array
  8. * An array of consumer info
  9. */
  10. function oauth_common_user_consumers($uid) {
  11. $result = db_query('SELECT c.secret, c.configuration, pc.*
  12. FROM {oauth_common_consumer} c
  13. INNER JOIN {oauth_common_provider_consumer} pc ON pc.csid = c.csid
  14. WHERE pc.uid = :uid', array(':uid' => $uid));
  15. $consumers = array();
  16. while ($consumer = DrupalOAuthConsumer::fromResult($result)) {
  17. $consumers[] = $consumer;
  18. }
  19. return $consumers;
  20. }
  21. /**
  22. * Return information about access tokens related to a user.
  23. *
  24. * @param int $uid
  25. * User ID to retrieve consumer info for.
  26. * @return array
  27. * An array of consumer info
  28. */
  29. function oauth_common_user_access_tokens($uid) {
  30. $result = db_query("SELECT * FROM {oauth_common_token} WHERE uid = :uid AND type = :type", array(
  31. ':uid' => $uid,
  32. ':type' => OAUTH_COMMON_TOKEN_TYPE_ACCESS,
  33. ));
  34. $tokens = array();
  35. while ($token = DrupalOAuthToken::fromResult($result)) {
  36. $tokens[] = $token;
  37. }
  38. return $tokens;
  39. }
  40. /**
  41. * Verifies the request
  42. *
  43. * @return array
  44. * An array containing three elements. The first is TRUE if the request was
  45. * signed, otherwise FALSE. Then comes the validated consumer and token objects.
  46. */
  47. function oauth_common_verify_request() {
  48. $req = DrupalOAuthRequest::from_request();
  49. // Verify
  50. $consumer_key = $req->get_parameter('oauth_consumer_key');
  51. if (!empty($consumer_key)) {
  52. $consumer = DrupalOAuthConsumer::loadProviderByKey($consumer_key);
  53. if ($consumer) {
  54. $context = oauth_common_context_load($consumer->context);
  55. if (!$context) {
  56. throw new Exception('No OAuth context found');
  57. }
  58. _oauth_common_verify_body_hash($req);
  59. // Only verify request if we got a signature
  60. $signature = $req->get_parameter('oauth_signature');
  61. if (!empty($signature)) {
  62. $server = new DrupalOAuthServer($context);
  63. return array_merge(array(TRUE), $server->verify_request($req));
  64. }
  65. else {
  66. $token_key = $req->get_parameter('oauth_token');
  67. if (empty($token_key) || !($token = DrupalOAuthToken::loadbyKey($token_key, $consumer))) {
  68. $token = NULL;
  69. }
  70. return array(FALSE, $consumer, $token);
  71. }
  72. }
  73. }
  74. return array(FALSE, NULL, NULL);
  75. }
  76. function _oauth_common_verify_body_hash($req) {
  77. $body_hash = $req->get_parameter('oauth_body_hash');
  78. if ($body_hash && module_exists('inputstream')) {
  79. $hres = hash_init('sha1');
  80. $stream = fopen('drupal://input', 'r');
  81. hash_update_stream($hres, $stream);
  82. fclose($stream);
  83. $sha1 = hash_final($hres, TRUE);
  84. if ($sha1 !== base64_decode($body_hash)) {
  85. throw new OAuthException("Invalid body hash");
  86. }
  87. }
  88. }