apioauthaccesstoken.php 3.98 KB
Newer Older
1 2 3 4
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
5 6
 * Action for getting OAuth token credentials (exchange an authorized
 * request token for an access token)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *
 * 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  API
 * @package   StatusNet
 * @author    Zach Copley <zach@status.net>
 * @copyright 2010 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET')) {
    exit(1);
}

Zach Copley's avatar
Zach Copley committed
35
require_once INSTALLDIR . '/lib/apioauth.php';
36 37

/**
38 39
 * Action for getting OAuth token credentials (exchange an authorized
 * request token for an access token)
40 41 42 43 44 45 46
 *
 * @category API
 * @package  StatusNet
 * @author   Zach Copley <zach@status.net>
 * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link     http://status.net/
 */
Zach Copley's avatar
Zach Copley committed
47
class ApiOauthAccessTokenAction extends ApiOauthAction
48
{
49 50
    protected $reqToken = null;
    protected $verifier = null;
51

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    /**
     * Class handler.
     *
     * @param array $args array of arguments
     *
     * @return void
     */
    function handle($args)
    {
        parent::handle($args);

        $datastore   = new ApiStatusNetOAuthDataStore();
        $server      = new OAuthServer($datastore);
        $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();

        $server->add_signature_method($hmac_method);

Zach Copley's avatar
Zach Copley committed
69
        $atok = $app = null;
70

71 72 73
        // XXX: Insist that oauth_token and oauth_verifier be populated?
        // Spec doesn't say they MUST be.

74 75
        try {
            $req  = OAuthRequest::from_request();
76 77 78

            $this->reqToken = $req->get_parameter('oauth_token');
            $this->verifier = $req->get_parameter('oauth_verifier');
79 80

            $app  = $datastore->getAppByRequestToken($this->reqToken);
81
            $atok = $server->fetch_access_token($req);
82
        } catch (Exception $e) {
Zach Copley's avatar
Zach Copley committed
83
            common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
Zach Copley's avatar
Zach Copley committed
84
            common_debug(var_export($req, true));
85 86
            $code = $e->getCode();
            $this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
87
            return;
88 89
        }

Zach Copley's avatar
Zach Copley committed
90
        if (empty($atok)) {
91 92 93
            // Token exchange failed -- log it

            $msg = sprintf(
Zach Copley's avatar
Zach Copley committed
94 95
                'API OAuth - Failure exchanging OAuth request token for access token, '
                    . 'request token = %s, verifier = %s',
96
                $this->reqToken,
Zach Copley's avatar
Zach Copley committed
97
                $this->verifier
98 99
            );

100
            common_log(LOG_WARNING, $msg);
Siebrand Mazeland's avatar
Siebrand Mazeland committed
101
            // TRANS: Client error given from the OAuth API when the request token or verifier is invalid.
102
            $this->clientError(_('Invalid request token or verifier.'), 400, 'text');
Zach Copley's avatar
Zach Copley committed
103
        } else {
Zach Copley's avatar
Zach Copley committed
104 105 106
            common_log(
                LOG_INFO,
                sprintf(
107
                    "Issued access token '%s' for application %d (%s).",
Zach Copley's avatar
Zach Copley committed
108 109 110 111 112
                    $atok->key,
                    $app->id,
                    $app->name
                )
            );
113
            $this->showAccessToken($atok);
Zach Copley's avatar
Zach Copley committed
114
        }
115 116
    }

117 118 119 120 121 122
    /*
     * Display OAuth token credentials
     *
     * @param OAuthToken token the access token
     */
    function showAccessToken($token)
123
    {
124 125
        header('Content-Type: application/x-www-form-urlencoded');
        print $token;
126
    }
127
}