4 * This file is (c) Copyright 2011 by Olivier BERGER, Institut TELECOM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * This program has been developed in the frame of the COCLICO
21 * project with financial support of its funders.
25 // This program helps test the OAuth provider plugin of fusionforge.
26 // See the README for more details.
28 require_once 'OAuth.php';
30 // customize this value which is the address of the FusionForge server
32 $forge = 'https://192.168.122.90/';
35 * Provides invocation details to the user
36 * @param integer $code return code on exit
38 function usage($code=0) {
39 echo "php command-line.php [command] [args]\n";
41 echo "where command in : request_token, ...\n";
43 echo " request_token [consumer_key] [consumer_secret]\n";
45 echo " authorize [request_token]\n";
47 echo " access_token [consumer_key] [consumer_secret] [request_token] [request_token_secret]\n";
49 echo " call_echo [consumer_key] [consumer_secret] [access_token] [access_token_secret] [message]\n";
55 * Makes a requests with the CURL library
57 * @param integer $code HTTP return code (writable)
58 * @param string $url to be called
59 * @param string $params passed to curl_init
60 * @return boolean|mixed response (HTTP return code is in $code)
62 function request_curl(&$code, $url, $params=array()) {
64 $params = http_build_query($params, '', '&');
65 $curl = curl_init($url . ($method == 'GET' && $params ? '?' . $params : ''));
66 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
67 curl_setopt($curl, CURLOPT_HEADER, false);
68 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
69 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
70 curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/xrds+xml, */*'));
72 curl_setopt($curl, CURLOPT_HTTPGET, true);
74 $response = curl_exec($curl);
77 echo 'Curl error: ' . curl_error($curl);
82 $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
88 * Displays a message about the need for the user to go authorize the request token in the forge
90 * @param string $authorize_endpoint
91 * @param string $oauth_token
93 function authorize_request_token_message($authorize_endpoint, $oauth_token) {
94 echo "Go to the following URL in your FusionForge session to authorize the request token:\n";
95 echo ' '. $authorize_endpoint. '?oauth_token='. $oauth_token . "\n";
97 echo "Upon completion, you will be able request access tokens with the authorized token.";
102 * Provides authorize endpoint's URL
105 function fusionforge_authorize_endpoint() {
106 return $forge . 'plugins/oauthprovider/authorize.php';
110 * Retrieves a request token for the consumer
111 * @param string $request_token_endpoint endpoint called on the oauthprovider plugin
112 * @param string $consumer_key
113 * @param string $consumer_secret
115 function retrieve_request_token($request_token_endpoint, $consumer_key, $consumer_secret) {
117 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
119 $test_consumer = new OAuthConsumer($consumer_key, $consumer_secret);
121 //print_r($test_consumer);
125 $req_req = OAuthRequest::from_consumer_and_token($test_consumer, NULL, "GET", $request_token_endpoint, $params);
127 $sig_method = $hmac_method;
129 $req_req->sign_request($sig_method, $test_consumer, NULL);
131 //print "request url: " . $req_req->to_url(). "\n";
135 $response = request_curl($code, $req_req->to_url());
137 // TODO: should be testing HTTP return code in $code
138 //print_r($response);
140 parse_str($response, $params);
143 echo "received request token :\n";
144 echo ' $oauth_token : '. $params['oauth_token'] ."\n";
145 echo ' $oauth_token_secret : '. $params['oauth_token_secret'] ."\n";
148 authorize_request_token_message(fusionforge_authorize_endpoint(), $params['oauth_token']);
152 * Call remote endpoints through OAuth
154 * @param string $endpoint URL
155 * @param string $consumer_key
156 * @param string $consumer_secret
157 * @param string $access_token
158 * @param string $token_secret
159 * @param array $params passed to the endpoint
160 * @return Ambigous <boolean, mixed>
162 function call_remote_endpoint($endpoint, $consumer_key, $consumer_secret, $access_token, $token_secret, $params) {
163 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
165 $test_consumer = new OAuthConsumer($consumer_key, $consumer_secret);
167 $test_token = new OAuthConsumer($access_token, $token_secret);
170 //print_r($test_consumer);
172 $acc_req = OAuthRequest::from_consumer_and_token($test_consumer, $test_token, "GET", $endpoint, $params);
174 $sig_method = $hmac_method;
176 $acc_req->sign_request($sig_method, $test_consumer, $test_token);
178 //print "request url: " . $req_req->to_url(). "\n";
182 $response = request_curl($code, $acc_req->to_url());
184 //print_r($response);
191 echo 'received error code : ' . $code . "\n";
199 * Retrieves an access token in exchange for the request token (that should have been authorized by now)
201 * @param string $access_token_endpoint
202 * @param string $consumer_key
203 * @param string $consumer_secret
204 * @param string $request_token
205 * @param string $token_secret
207 function retrieve_access_token($access_token_endpoint, $consumer_key, $consumer_secret, $request_token, $token_secret) {
210 $response = call_remote_endpoint($access_token_endpoint, $consumer_key, $consumer_secret, $request_token, $token_secret, $params);
213 parse_str($response, $params);
216 $oauth_token = $params['oauth_token'];
217 $oauth_token_secret = $params['oauth_token_secret'];
218 echo "received access token :\n";
219 echo ' $oauth_token : '. $oauth_token ."\n";
220 echo ' $oauth_token_secret : '. $oauth_token_secret ."\n";
222 echo "You may now access endpoints with this token, for instance as in :\n";
223 echo " php command-line.php call_echo $consumer_key $consumer_secret $oauth_token $oauth_token_secret\n";
234 case 'request_token':
235 if($argc < 4) usage(1);
236 $consumer_key = $argv[2];
237 $consumer_secret = $argv[3];
238 $request_token_endpoint = $forge . 'plugins/oauthprovider/request_token.php';
239 retrieve_request_token($request_token_endpoint, $consumer_key, $consumer_secret);
242 if($argc < 3) usage(1);
243 $oauth_token = $argv[2];
244 $authorize_endpoint = fusionforge_authorize_endpoint();
245 authorize_request_token_message($authorize_endpoint, $oauth_token);
248 if($argc < 6) usage(1);
249 $consumer_key = $argv[2];
250 $consumer_secret = $argv[3];
251 $request_token = $argv[4];
252 $token_secret = $argv[5];
253 $access_token_endpoint = $forge . 'plugins/oauthprovider/access_token.php';
254 retrieve_access_token($access_token_endpoint, $consumer_key, $consumer_secret, $request_token, $token_secret);
257 if($argc < 7) usage(1);
258 $consumer_key = $argv[2];
259 $consumer_secret = $argv[3];
260 $access_token = $argv[4];
261 $token_secret = $argv[5];
262 $echo_endpoint = $forge . 'plugins/oauthprovider/echo.php';
263 $params = array('message' => $argv[6]);
264 $response = call_remote_endpoint($echo_endpoint, $consumer_key, $consumer_secret, $access_token, $token_secret, $params);