3 * StatusNet, the distributed open-source microblogging tool
5 * Action for getting OAuth token credentials (exchange an authorized
6 * request token for an access token)
10 * LICENCE: This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU Affero General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Affero General Public License for more details.
20 * You should have received a copy of the GNU Affero General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
35 require_once INSTALLDIR . '/lib/apioauth.php';
38 * Action for getting OAuth token credentials (exchange an authorized
39 * request token for an access token)
43 * @author Zach Copley <zach@status.net>
44 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45 * @link http://status.net/
48 class ApiOauthAccessTokenAction extends ApiOauthAction
50 protected $reqToken = null;
51 protected $verifier = null;
56 * @param array $args array of arguments
60 function handle($args)
62 parent::handle($args);
64 $datastore = new ApiStatusNetOAuthDataStore();
65 $server = new OAuthServer($datastore);
66 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
68 $server->add_signature_method($hmac_method);
72 // XXX: Insist that oauth_token and oauth_verifier be populated?
73 // Spec doesn't say they MUST be.
77 $req = OAuthRequest::from_request();
79 $this->reqToken = $req->get_parameter('oauth_token');
80 $this->verifier = $req->get_parameter('oauth_verifier');
81 $app = $datastore->getAppByRequestToken($this->reqToken);
82 $atok = $server->fetch_access_token($req);
84 } catch (OAuthException $e) {
85 common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
86 common_debug(var_export($req, true));
87 $code = $e->getCode();
88 $this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
93 // Token exchange failed -- log it
96 'API OAuth - Failure exchanging OAuth request token for access token, '
97 . 'request token = %s, verifier = %s',
102 common_log(LOG_WARNIGN, $msg);
103 $this->clientError(_("Invalid request token or verifier.", 400, 'text'));
109 "Issued now access token '%s' for application %d (%s).",
115 $this->showAccessToken($atok);
120 * Display OAuth token credentials
122 * @param OAuthToken token the access token
125 function showAccessToken($token)
127 header('Content-Type: application/x-www-form-urlencoded');