fetch_temp_creds.php 3.18 KB
Newer Older
1 2 3 4
#!/usr/bin/env php
<?php
/*
 * StatusNet - a distributed open-source microblogging tool
5
 * Copyright (C) 2010, StatusNet, Inc.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));

require_once INSTALLDIR . '/scripts/commandline.inc';
require_once INSTALLDIR . '/extlib/OAuth.php';

$ini = parse_ini_file("oauth.ini");
27 28 29 30 31 32 33 34 35

// Check to make sure we have everything we need from the ini file
foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'request_token_url') as $inikey) {
    if (empty($ini[$inikey])) {
        print "You forgot to specify a $inikey in your oauth.ini file.\n";
        exit(1);
    }
}

36 37 38 39
$consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
$endpoint = $ini['apiroot'] . $ini['request_token_url'];
$parsed   = parse_url($endpoint);
$params   = array();
Zach Copley's avatar
Zach Copley committed
40

41
parse_str($parsed['query'], $params);
Zach Copley's avatar
Zach Copley committed
42
$params['oauth_callback'] = 'oob'; // out-of-band
43 44 45

$hmac_method = new OAuthSignatureMethod_HMAC_SHA1();

46
try {
47
    $req = OAuthRequest::from_consumer_and_token(
48
        $consumer,
49
        null,
50
        "POST",
51
        $endpoint,
52 53
        $params
    );
54 55
    $req->sign_request($hmac_method, $consumer, NULL);
    $r = httpRequest($endpoint, $req->to_postdata());
56
} catch (Exception $e) {
57
    // oh noez
58
    print $e->getMessage();
59
    print "\nOAuth Request:\n";
60
    var_dump($req);
61 62
    exit(1);
}
63

Zach Copley's avatar
Zach Copley committed
64
$body       = $r->getBody();
65
$tokenStuff = array();
Zach Copley's avatar
Zach Copley committed
66

67
parse_str($body, $tokenStuff);
68

Zach Copley's avatar
Zach Copley committed
69 70 71
$tok       = $tokenStuff['oauth_token'];
$confirmed = $tokenStuff['oauth_callback_confirmed'];

72 73 74 75 76 77
if (empty($tokenStuff['oauth_token'])
    || empty($tokenStuff['oauth_token_secret'])
    || empty($confirmed)
    || $confirmed != 'true')
{
    print "Error! HTTP response body: $body\n";
78 79
    exit(1);
}
80

Zach Copley's avatar
Zach Copley committed
81
$authurl = $ini['apiroot'] . $ini['authorize_url'] . '?oauth_token=' . $tok;
82

83 84 85 86
print "Request Token\n";
print '   - oauth_token        = ' . $tokenStuff['oauth_token'] . "\n";
print '   - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
print "Authorize URL\n    $authurl\n\n";
Zach Copley's avatar
Zach Copley committed
87
print "Now paste the Authorize URL into your browser and authorize your temporary credentials.\n";
88

89
function httpRequest($endpoint, $poststr)
90 91
{
    $request = HTTPClient::start();
92

93 94 95 96 97 98 99 100 101
    $request->setConfig(
        array(
            'follow_redirects' => true,
	    'connect_timeout' => 120,
	    'timeout' => 120,
	    'ssl_verify_peer' => false,
	    'ssl_verify_host' => false
        )
    );
102

103 104 105
    // Turn signed request query string back into an array
    parse_str($poststr, $postdata);
    return $request->post($endpoint, null, $postdata);
106
}