]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/oauth/fetch_token_creds.php
XSS vulnerability when remote-subscribing
[quix0rs-gnu-social.git] / tests / oauth / fetch_token_creds.php
1 #!/usr/bin/env php
2 <?php
3 /*
4  * StatusNet - a distributed open-source microblogging tool
5  * Copyright (C) 2008, 2009, 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
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../..'));
22
23 require_once INSTALLDIR . '/extlib/OAuth.php';
24
25 $ini = parse_ini_file("oauth.ini");
26
27 // Check to make sure we have everything we need from the ini file
28 foreach(array('consumer_key', 'consumer_secret', 'apiroot', 'access_token_url') as $inikey) {
29     if (empty($ini[$inikey])) {
30         print "You forgot to specify a $inikey in your oauth.ini file.\n";
31         exit(1);
32     }
33 }
34
35 $consumer = new OAuthConsumer($ini['consumer_key'], $ini['consumer_secret']);
36
37 $endpoint = $ini['apiroot'] . $ini['access_token_url'];
38
39 $shortoptions = 't:s:v:';
40 $longoptions = array('oauth_token=', 'oauth_token_secret=', 'oauth_verifier=');
41
42 $helptext = <<<END_OF_ETOKENS_HELP
43   fetch_token_creds.php [options]
44
45   Exchange authorized OAuth temporary credentials for token credentials
46   (an authorized request token for an access token)
47
48     -t --oauth_token        authorized request token
49     -s --oauth_token_secret authorized request token secret
50     -v --oauth_verifier     authorized request token verifier
51
52
53 END_OF_ETOKENS_HELP;
54
55 require_once INSTALLDIR . '/scripts/commandline.inc';
56
57 $token = $secret = $verifier = null;
58
59 if (have_option('t', 'oauth_token')) {
60     $token = get_option_value('t', 'oauth_token');
61 }
62
63 if (have_option('s', 'oauth_token_secret')) {
64     $secret = get_option_value('s', 'oauth_token_secret');
65 }
66
67 if (have_option('v', 'oauth_verifier')) {
68     $verifier = get_option_value('v', 'oauth_verifier');
69 }
70
71 if (empty($token)) {
72     print "Please specify the request token (--help for help).\n";
73     exit(1);
74 }
75
76 if (empty($secret)) {
77     print "Please specify the request token secret (--help for help).\n";
78     exit(1);
79 }
80
81 if (empty($verifier)) {
82     print "Please specify the request token verifier (--help for help).\n";
83     exit(1);
84 }
85
86 $rtok   = new OAuthToken($token, $secret);
87 $parsed = parse_url($endpoint);
88 parse_str($parsed['query'], $params);
89
90 $params['oauth_verifier'] = $verifier; // 1.0a
91
92 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
93
94 try {
95
96     $oauthReq = OAuthRequest::from_consumer_and_token(
97         $consumer,
98         $rtok,
99         "POST",
100         $endpoint,
101         $params
102     );
103
104     $oauthReq->sign_request($hmac_method, $consumer, $rtok);
105
106     $httpReq    = httpRequest($endpoint, $oauthReq->to_postdata());
107     $body       = $httpReq->getBody();
108
109 } catch (Exception $e) {
110     // oh noez
111     print $e->getMessage();
112     print "\nOAuth Request:\n";
113     var_dump($oauthReq);
114     exit(1);
115 }
116
117 $tokenStuff = array();
118 parse_str($body, $tokenStuff);
119
120 if (empty($tokenStuff['oauth_token']) || empty($tokenStuff['oauth_token_secret'])) {
121     print "Error! HTTP response body: $body\n";
122     exit(1);
123 }
124
125 print "Access Token\n";
126 print '   - oauth_token        = ' . $tokenStuff['oauth_token'] . "\n";
127 print '   - oauth_token_secret = ' . $tokenStuff['oauth_token_secret'] . "\n";
128
129 function httpRequest($endpoint, $poststr)
130 {
131     $request = HTTPClient::start();
132
133     $request->setConfig(
134         array(
135             'follow_redirects' => true,
136             'connect_timeout'  => 120,
137             'timeout'          => 120,
138             'ssl_verify_peer'  => false,
139             'ssl_verify_host'  => false
140         )
141     );
142
143     parse_str($poststr, $postdata);
144     return $request->post($endpoint, null, $postdata);
145 }
146