]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthaccesstoken.php
a3ce9edbf64e1ef2d287c2af19555c4c7c25e3bb
[quix0rs-gnu-social.git] / actions / apioauthaccesstoken.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Action for getting OAuth token credentials (exchange an authorized
6  * request token for an access token)
7  *
8  * PHP version 5
9  *
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.
14  *
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.
19  *
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/>.
22  *
23  * @category  API
24  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR . '/lib/apioauth.php';
36
37 /**
38  * Action for getting OAuth token credentials (exchange an authorized
39  * request token for an access token)
40  *
41  * @category API
42  * @package  StatusNet
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/
46  */
47 class ApiOauthAccessTokenAction extends ApiOauthAction
48 {
49     protected $reqToken = null;
50     protected $verifier = null;
51
52     /**
53      * Class handler.
54      *
55      * @param array $args array of arguments
56      *
57      * @return void
58      */
59     function handle($args)
60     {
61         parent::handle($args);
62
63         $datastore   = new ApiStatusNetOAuthDataStore();
64         $server      = new OAuthServer($datastore);
65         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
66
67         $server->add_signature_method($hmac_method);
68
69         $atok = $app = null;
70
71         // XXX: Insist that oauth_token and oauth_verifier be populated?
72         // Spec doesn't say they MUST be.
73
74         try {
75             $req  = OAuthRequest::from_request();
76
77             $this->reqToken = $req->get_parameter('oauth_token');
78             $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) {
83             common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
84             common_debug(var_export($req, true));
85             $code = $e->getCode();
86             $this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
87         }
88
89         if (empty($atok)) {
90             // Token exchange failed -- log it
91
92             $msg = sprintf(
93                 'API OAuth - Failure exchanging OAuth request token for access token, '
94                     . 'request token = %s, verifier = %s',
95                 $this->reqToken,
96                 $this->verifier
97             );
98
99             common_log(LOG_WARNING, $msg);
100             // TRANS: Client error given from the OAuth API when the request token or verifier is invalid.
101             $this->clientError(_("Invalid request token or verifier."), 400, 'text');
102         } else {
103             common_log(
104                 LOG_INFO,
105                 sprintf(
106                     "Issued access token '%s' for application %d (%s).",
107                     $atok->key,
108                     $app->id,
109                     $app->name
110                 )
111             );
112             $this->showAccessToken($atok);
113         }
114     }
115
116     /*
117      * Display OAuth token credentials
118      *
119      * @param OAuthToken token the access token
120      */
121     function showAccessToken($token)
122     {
123         header('Content-Type: application/x-www-form-urlencoded');
124         print $token;
125     }
126 }