]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthaccesstoken.php
Merge remote-tracking branch 'upstream/master' into social-master
[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('GNUSOCIAL')) { exit(1); }
32
33 /**
34  * Action for getting OAuth token credentials (exchange an authorized
35  * request token for an access token)
36  *
37  * @category API
38  * @package  StatusNet
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/
42  */
43 class ApiOAuthAccessTokenAction extends ApiOAuthAction
44 {
45     protected $reqToken = null;
46     protected $verifier = null;
47
48     /**
49      * Class handler.
50      *
51      * @param array $args array of arguments
52      *
53      * @return void
54      */
55     function handle(array $args=array())
56     {
57         parent::handle($args);
58
59         $datastore   = new ApiGNUsocialOAuthDataStore();
60         $server      = new OAuthServer($datastore);
61         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
62
63         $server->add_signature_method($hmac_method);
64
65         $atok = $app = null;
66
67         // XXX: Insist that oauth_token and oauth_verifier be populated?
68         // Spec doesn't say they MUST be.
69
70         try {
71             $req  = OAuthRequest::from_request();
72
73             $this->reqToken = $req->get_parameter('oauth_token');
74             $this->verifier = $req->get_parameter('oauth_verifier');
75
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');
83         }
84
85         if (empty($atok)) {
86             // Token exchange failed -- log it
87
88             $msg = sprintf(
89                 'API OAuth - Failure exchanging OAuth request token for access token, '
90                     . 'request token = %s, verifier = %s',
91                 $this->reqToken,
92                 $this->verifier
93             );
94
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');
98         } else {
99             common_log(
100                 LOG_INFO,
101                 sprintf(
102                     "Issued access token '%s' for application %d (%s).",
103                     $atok->key,
104                     $app->id,
105                     $app->name
106                 )
107             );
108             $this->showAccessToken($atok);
109         }
110     }
111
112     /*
113      * Display OAuth token credentials
114      *
115      * @param OAuthToken token the access token
116      */
117     function showAccessToken($token)
118     {
119         header('Content-Type: application/x-www-form-urlencoded');
120         print $token;
121     }
122 }