git.gnu.io has moved to IP address 209.51.188.249 -- please double check where you are logging in.

oauthclient.php 7.84 KB
Newer Older
1
<?php
2
/**
3
 * StatusNet, the distributed open-source microblogging tool
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Base class for doing OAuth calls as a consumer
 *
 * PHP version 5
 *
 * LICENCE: 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/>.
 *
 * @category  Action
23
 * @package   StatusNet
24
 * @author    Zach Copley <zach@status.net>
25
 * @copyright 2009 StatusNet, Inc.
26
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27
 * @link      http://status.net/
28 29
 */

30
if (!defined('STATUSNET') && !defined('LACONICA')) {
31 32
    exit(1);
}
33

34 35 36 37 38 39
require_once 'OAuth.php';

/**
 * Exception wrapper for cURL errors
 *
 * @category Integration
40
 * @package  StatusNet
41
 * @author   Zach Copley <zach@status.net>
42
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43
 * @link     http://status.net/
44 45
 *
 */
46
class OAuthClientException extends Exception
47 48
{
}
49

50 51 52 53
/**
 * Base class for doing OAuth calls as a consumer
 *
 * @category Integration
54
 * @package  StatusNet
55
 * @author   Zach Copley <zach@status.net>
56
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57
 * @link     http://status.net/
58 59
 *
 */
60 61 62 63 64
class OAuthClient
{
    var $consumer;
    var $token;

65 66 67 68 69 70 71 72 73 74 75 76 77
    /**
     * Constructor
     *
     * Can be initialized with just consumer key and secret for requesting new
     * tokens or with additional request token or access token
     *
     * @param string $consumer_key       consumer key
     * @param string $consumer_secret    consumer secret
     * @param string $oauth_token        user's token
     * @param string $oauth_token_secret user's secret
     *
     * @return nothing
     */
78 79 80 81
    function __construct($consumer_key, $consumer_secret,
                         $oauth_token = null, $oauth_token_secret = null)
    {
        $this->sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
82 83
        $this->consumer    = new OAuthConsumer($consumer_key, $consumer_secret);
        $this->token       = null;
84 85 86 87 88 89

        if (isset($oauth_token) && isset($oauth_token_secret)) {
            $this->token = new OAuthToken($oauth_token, $oauth_token_secret);
        }
    }

90 91 92
    /**
     * Gets a request token from the given url
     *
93 94
     * @param string $url      OAuth endpoint for grabbing request tokens
     * @param string $callback authorized request token callback
95 96 97
     *
     * @return OAuthToken $token the request token
     */
98
    function getRequestToken($url, $callback = null)
99
    {
100 101 102 103 104 105 106 107
        $params = null;

        if (!is_null($callback)) {
            $params['oauth_callback'] = $callback;
        }

        $response = $this->oAuthGet($url, $params);

108 109
        $arr = array();
        parse_str($response, $arr);
110 111 112 113 114 115 116 117 118 119 120 121 122 123

        $token   = $arr['oauth_token'];
        $secret  = $arr['oauth_token_secret'];
        $confirm = $arr['oauth_callback_confirmed'];

        if (isset($token) && isset($secret)) {

            $token = new OAuthToken($token, $secret);

            if (isset($confirm)) {
                if ($confirm == 'true') {
                    return $token;
                } else {
                    throw new OAuthClientException(
124
                        'Callback was not confirmed by remote OAuth side.'
125 126 127
                    );
                }
            }
128 129
            return $token;
        } else {
130
            throw new OAuthClientException(
131
                'Could not get a request token from remote OAuth side.'
132
            );
133
        }
134 135
    }

136 137 138 139 140 141 142 143 144
    /**
     * Builds a link that can be redirected to in order to
     * authorize a request token.
     *
     * @param string     $url            endpoint for authorizing request tokens
     * @param OAuthToken $request_token  the request token to be authorized
     *
     * @return string $authorize_url the url to redirect to
     */
145
    function getAuthorizeLink($url, $request_token)
146
    {
147
        $authorize_url = $url . '?oauth_token=' .
148 149
            $request_token->key;

150
        return $authorize_url;
151 152
    }

153 154 155
    /**
     * Fetches an access token
     *
156 157 158
     * @param string $url      OAuth endpoint for exchanging authorized request tokens
     *                         for access tokens
     * @param string $verifier 1.0a verifier
159 160 161
     *
     * @return OAuthToken $token the access token
     */
162
    function getAccessToken($url, $verifier = null)
163
    {
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
        $params = array();

        if (!is_null($verifier)) {
            $params['oauth_verifier'] = $verifier;
        }

        $response = $this->oAuthPost($url, $params);

        $arr = array();
        parse_str($response, $arr);

        $token  = $arr['oauth_token'];
        $secret = $arr['oauth_token_secret'];

        if (isset($token) && isset($secret)) {
            $token = new OAuthToken($token, $secret);
            return $token;
        } else {
            throw new OAuthClientException(
183
                'Could not get a access token from remote OAuth side.'
184 185
            );
        }
186 187
    }

188
    /**
189
     * Use HTTP GET to make a signed OAuth requesta
190
     *
191 192
     * @param string $url    OAuth request token endpoint
     * @param array  $params additional parameters
193 194 195
     *
     * @return mixed the request
     */
196
    function oAuthGet($url, $params = null)
197 198
    {
        $request = OAuthRequest::from_consumer_and_token($this->consumer,
199
            $this->token, 'GET', $url, $params);
200 201 202 203 204 205
        $request->sign_request($this->sha1_method,
            $this->consumer, $this->token);

        return $this->httpRequest($request->to_url());
    }

206 207 208 209 210 211 212 213
    /**
     * Use HTTP POST to make a signed OAuth request
     *
     * @param string $url    OAuth endpoint
     * @param array  $params additional post parameters
     *
     * @return mixed the request
     */
214 215 216 217 218 219 220 221 222 223 224
    function oAuthPost($url, $params = null)
    {
        $request = OAuthRequest::from_consumer_and_token($this->consumer,
            $this->token, 'POST', $url, $params);
        $request->sign_request($this->sha1_method,
            $this->consumer, $this->token);

        return $this->httpRequest($request->get_normalized_http_url(),
            $request->to_postdata());
    }

225
    /**
226
     * Make a HTTP request.
227 228 229 230 231 232
     *
     * @param string $url    Where to make the
     * @param array  $params post parameters
     *
     * @return mixed the request
     */
233 234
    function httpRequest($url, $params = null)
    {
235 236 237 238 239 240
        $request = new HTTPClient($url);
        $request->setConfig(array(
            'connect_timeout' => 120,
            'timeout' => 120,
            'follow_redirects' => true,
            'ssl_verify_peer' => false,
241
            'ssl_verify_host' => false
242 243
        ));

244 245 246
        // Twitter was strict about accepting invalid "Expect" headers
        // between 2008ish and October 2012. Caused "417 Expectation failed"
        //$request->setHeader('Expect', '');
247 248

        if (isset($params)) {
249 250
            $request->setMethod(HTTP_Request2::METHOD_POST);
            $request->setBody($params);
251 252
        }

253 254 255 256 257 258 259 260 261
        try {
            $response = $request->send();
            $code = $response->getStatus();
            if ($code < 200 || $code >= 400) {
                throw new OAuthClientException($response->getBody(), $code);
            }
            return $response->getBody();
        } catch (Exception $e) {
            throw new OAuthClientException($e->getMessage(), $e->getCode());
262 263 264 265
        }
    }

}