fetch_temp_creds.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * StatusNet - a distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
  21. require_once INSTALLDIR . '/scripts/commandline.inc';
  22. require_once INSTALLDIR . '/extlib/OAuth.php';
  23. $ini = parse_ini_file("oauth.ini");
  24. // Check to make sure we have everything we need from the ini file
  25. foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'request_token_url') as $inikey) {
  26. if (empty($ini[$inikey])) {
  27. print "You forgot to specify a $inikey in your oauth.ini file.\n";
  28. exit(1);
  29. }
  30. }
  31. $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
  32. $endpoint = $ini['apiroot'] . $ini['request_token_url'];
  33. $parsed = parse_url($endpoint);
  34. $params = array();
  35. parse_str($parsed['query'], $params);
  36. $params['oauth_callback'] = 'oob'; // out-of-band
  37. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  38. try {
  39. $req = OAuthRequest::from_consumer_and_token(
  40. $consumer,
  41. null,
  42. "POST",
  43. $endpoint,
  44. $params
  45. );
  46. $req->sign_request($hmac_method, $consumer, NULL);
  47. $r = httpRequest($endpoint, $req->to_postdata());
  48. } catch (Exception $e) {
  49. // oh noez
  50. print $e->getMessage();
  51. print "\nOAuth Request:\n";
  52. var_dump($req);
  53. exit(1);
  54. }
  55. $body = $r->getBody();
  56. $tokenStuff = array();
  57. parse_str($body, $tokenStuff);
  58. $tok = $tokenStuff['oauth_token'];
  59. $confirmed = $tokenStuff['oauth_callback_confirmed'];
  60. if (empty($tokenStuff['oauth_token'])
  61. || empty($tokenStuff['oauth_token_secret'])
  62. || empty($confirmed)
  63. || $confirmed != 'true')
  64. {
  65. print "Error! HTTP response body: $body\n";
  66. exit(1);
  67. }
  68. $authurl = $ini['apiroot'] . $ini['authorize_url'] . '?oauth_token=' . $tok;
  69. print "Request Token\n";
  70. print ' - oauth_token = ' . $tokenStuff['oauth_token'] . "\n";
  71. print ' - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
  72. print "Authorize URL\n $authurl\n\n";
  73. print "Now paste the Authorize URL into your browser and authorize your temporary credentials.\n";
  74. function httpRequest($endpoint, $poststr)
  75. {
  76. $request = HTTPClient::start();
  77. $request->setConfig(
  78. array(
  79. 'follow_redirects' => true,
  80. 'connect_timeout' => 120,
  81. 'timeout' => 120,
  82. 'ssl_verify_peer' => false,
  83. 'ssl_verify_host' => false
  84. )
  85. );
  86. // Turn signed request query string back into an array
  87. parse_str($poststr, $postdata);
  88. return $request->post($endpoint, null, $postdata);
  89. }