OAuthBody.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. require_once("OAuth.php");
  3. require_once("TrivialOAuthDataStore.php");
  4. function getLastOAuthBodyBaseString() {
  5. global $LastOAuthBodyBaseString;
  6. return $LastOAuthBodyBaseString;
  7. }
  8. function getOAuthKeyFromHeaders()
  9. {
  10. $request_headers = OAuthUtil::get_headers();
  11. // print_r($request_headers);
  12. if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
  13. $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
  14. // echo("HEADER PARMS=\n");
  15. // print_r($header_parameters);
  16. return $header_parameters['oauth_consumer_key'];
  17. }
  18. return false;
  19. }
  20. function handleOAuthBodyPOST($oauth_consumer_key, $oauth_consumer_secret)
  21. {
  22. $request_headers = OAuthUtil::get_headers();
  23. // print_r($request_headers);
  24. // Must reject application/x-www-form-urlencoded
  25. $hdr = $request_headers['Content-type'];
  26. if ( !isset($hdr) ) $hdr = $request_headers['Content-Type'];
  27. if ($hdr == 'application/x-www-form-urlencoded' ) {
  28. throw new Exception("OAuth request body signing must not use application/x-www-form-urlencoded");
  29. }
  30. if (@substr($request_headers['Authorization'], 0, 6) == "OAuth ") {
  31. $header_parameters = OAuthUtil::split_header($request_headers['Authorization']);
  32. // echo("HEADER PARMS=\n");
  33. // print_r($header_parameters);
  34. $oauth_body_hash = $header_parameters['oauth_body_hash'];
  35. // echo("OBH=".$oauth_body_hash."\n");
  36. }
  37. if ( ! isset($oauth_body_hash) ) {
  38. throw new Exception("OAuth request body signing requires oauth_body_hash body");
  39. }
  40. // Verify the message signature
  41. $store = new TrivialOAuthDataStore();
  42. $store->add_consumer($oauth_consumer_key, $oauth_consumer_secret);
  43. $server = new OAuthServer($store);
  44. $method = new OAuthSignatureMethod_HMAC_SHA1();
  45. $server->add_signature_method($method);
  46. $request = OAuthRequest::from_request();
  47. global $LastOAuthBodyBaseString;
  48. $LastOAuthBodyBaseString = $request->get_signature_base_string();
  49. // echo($LastOAuthBodyBaseString."\n");
  50. try {
  51. $server->verify_request($request);
  52. } catch (Exception $e) {
  53. $message = $e->getMessage();
  54. throw new Exception("OAuth signature failed: " . $message);
  55. }
  56. $postdata = file_get_contents('php://input');
  57. // echo($postdata);
  58. $hash = base64_encode(sha1($postdata, TRUE));
  59. if ( $hash != $oauth_body_hash ) {
  60. throw new Exception("OAuth oauth_body_hash mismatch");
  61. }
  62. return $postdata;
  63. }
  64. function sendOAuthBodyPOST($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $body)
  65. {
  66. $hash = base64_encode(sha1($body, TRUE));
  67. $parms = array('oauth_body_hash' => $hash);
  68. $test_token = '';
  69. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  70. $test_consumer = new OAuthConsumer($oauth_consumer_key, $oauth_consumer_secret, NULL);
  71. $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, $method, $endpoint, $parms);
  72. $acc_req->sign_request($hmac_method, $test_consumer, $test_token);
  73. // Pass this back up "out of band" for debugging
  74. global $LastOAuthBodyBaseString;
  75. $LastOAuthBodyBaseString = $acc_req->get_signature_base_string();
  76. // echo($LastOAuthBodyBaseString."\m");
  77. $header = $acc_req->to_header();
  78. $header = $header . "\r\nContent-type: " . $content_type . "\r\n";
  79. $params = array('http' => array(
  80. 'method' => 'POST',
  81. 'content' => $body,
  82. 'header' => $header
  83. ));
  84. try {
  85. $ctx = stream_context_create($params);
  86. $fp = @fopen($endpoint, 'rb', false, $ctx);
  87. } catch (Exception $e) {
  88. $fp = false;
  89. }
  90. if ($fp) {
  91. $response = @stream_get_contents($fp);
  92. } else { // Try CURL
  93. $headers = explode("\r\n",$header);
  94. $response = sendXmlOverPost($endpoint, $body, $headers);
  95. }
  96. if ($response === false) {
  97. throw new Exception("Problem reading data from $endpoint, $php_errormsg");
  98. }
  99. return $response;
  100. }
  101. function sendXmlOverPost($url, $xml, $header) {
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_URL, $url);
  104. // For xml, change the content-type.
  105. curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);
  106. curl_setopt($ch, CURLOPT_POST, 1);
  107. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
  108. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // ask for results to be returned
  109. /*
  110. if(CurlHelper::checkHttpsURL($url)) {
  111. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  112. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  113. }
  114. */
  115. // Send to remote and return data to caller.
  116. $result = curl_exec($ch);
  117. curl_close($ch);
  118. return $result;
  119. }