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('GNUSOCIAL')) { exit(1); }
34 * Action for getting OAuth token credentials (exchange an authorized
35 * request token for an access token)
39 * @author Zach Copley <zach@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
43 class ApiOAuthAccessTokenAction extends ApiOAuthAction
45 protected $reqToken = null;
46 protected $verifier = null;
51 * @param array $args array of arguments
55 function handle($args)
57 parent::handle($args);
59 $datastore = new ApiGNUsocialOAuthDataStore();
60 $server = new OAuthServer($datastore);
61 $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
63 $server->add_signature_method($hmac_method);
67 // XXX: Insist that oauth_token and oauth_verifier be populated?
68 // Spec doesn't say they MUST be.
71 $req = OAuthRequest::from_request();
73 $this->reqToken = $req->get_parameter('oauth_token');
74 $this->verifier = $req->get_parameter('oauth_verifier');
76 $app = $datastore->getAppByRequestToken($this->reqToken);
77 $atok = $server->fetch_access_token($req);
78 } catch (Exception $e) {
79 common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
80 common_debug(var_export($req, true));
81 $code = $e->getCode();
82 $this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
86 // Token exchange failed -- log it
89 'API OAuth - Failure exchanging OAuth request token for access token, '
90 . 'request token = %s, verifier = %s',
95 common_log(LOG_WARNING, $msg);
96 // TRANS: Client error given from the OAuth API when the request token or verifier is invalid.
97 $this->clientError(_('Invalid request token or verifier.'), 400, 'text');
102 "Issued access token '%s' for application %d (%s).",
108 $this->showAccessToken($atok);
113 * Display OAuth token credentials
115 * @param OAuthToken token the access token
117 function showAccessToken($token)
119 header('Content-Type: application/x-www-form-urlencoded');