fetch_temp_creds.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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(__DIR__ . '/../..'));
  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 (['consumer_key', 'consumer_secret', 'apiroot', 'request_token_url'] as $inikey) {
  26. if (empty($ini[$inikey])) {
  27. echo "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 = [];
  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. echo $e->getMessage();
  51. echo "\nOAuth Request:\n";
  52. var_dump($req);
  53. exit(1);
  54. }
  55. $body = $r->getBody();
  56. $tokenStuff = [];
  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. echo "Error! HTTP response body: {$body}\n";
  65. exit(1);
  66. }
  67. $authurl = $ini['apiroot'] . $ini['authorize_url'] . '?oauth_token=' . $tok;
  68. echo "Request Token\n";
  69. echo ' - oauth_token = ' . $tokenStuff['oauth_token'] . "\n";
  70. echo ' - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
  71. echo "Authorize URL\n {$authurl}\n\n";
  72. echo "Now paste the Authorize URL into your browser and authorize your temporary credentials.\n";
  73. function httpRequest($endpoint, $poststr)
  74. {
  75. $request = HTTPClient::start();
  76. $request->setConfig(
  77. [
  78. 'follow_redirects' => true,
  79. 'connect_timeout' => 120,
  80. 'timeout' => 120,
  81. 'ssl_verify_peer' => false,
  82. 'ssl_verify_host' => false,
  83. ]
  84. );
  85. // Turn signed request query string back into an array
  86. parse_str($poststr, $postdata);
  87. return $request->post($endpoint, null, $postdata);
  88. }