]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthrequesttoken.php
Merge branch 'master' into 0.9.x
[quix0rs-gnu-social.git] / actions / apioauthrequesttoken.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Issue temporary OAuth credentials (a request token)
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2010 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/apioauth.php';
35
36 /**
37  * Issue temporary OAuth credentials (a request token)
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45 class ApiOauthRequestTokenAction extends ApiOauthAction
46 {
47     /**
48      * Take arguments for running
49      *
50      * @param array $args $_REQUEST args
51      *
52      * @return boolean success flag
53      */
54     function prepare($args)
55     {
56         parent::prepare($args);
57
58         // XXX: support "force_login" parameter like Twitter? (Forces the user to enter
59         // their credentials to ensure the correct users account is authorized.)
60
61         return true;
62     }
63
64     /**
65      * Handle a request for temporary OAuth credentials
66      *
67      * Make sure the request is kosher, then emit a set of temporary
68      * credentials -- AKA an unauthorized request token.
69      *
70      * @param array $args array of arguments
71      *
72      * @return void
73      */
74     function handle($args)
75     {
76         parent::handle($args);
77
78         $datastore   = new ApiStatusNetOAuthDataStore();
79         $server      = new OAuthServer($datastore);
80         $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
81
82         $server->add_signature_method($hmac_method);
83
84         try {
85
86             $req = OAuthRequest::from_request();
87
88             // verify callback
89             if (!$this->verifyCallback($req->get_parameter('oauth_callback'))) {
90                 throw new OAuthException(
91                     "You must provide a valid URL or 'oob' in oauth_callback.",
92                     400
93                 );
94             }
95
96             // check signature and issue a new request token
97             $token = $server->fetch_request_token($req);
98
99             common_log(
100                 LOG_INFO,
101                 sprintf(
102                     "API OAuth - Issued request token %s for consumer %s with oauth_callback %s",
103                     $token->key,
104                     $req->get_parameter('oauth_consumer_key'),
105                     "'" . $req->get_parameter('oauth_callback') ."'"
106                 )
107             );
108
109             // return token to the client
110             $this->showRequestToken($token);
111
112         } catch (OAuthException $e) {
113             common_log(LOG_WARNING, 'API OAuthException - ' . $e->getMessage());
114
115             // Return 401 for for bad credentials or signature problems,
116             // and 400 for missing or unsupported parameters
117
118             $code = $e->getCode();
119             $this->clientError($e->getMessage(), empty($code) ? 401 : $code, 'text');
120         }
121     }
122
123     /*
124      * Display temporary OAuth credentials
125      */
126     function showRequestToken($token)
127     {
128         header('Content-Type: application/x-www-form-urlencoded');
129         print $token;
130         print '&oauth_callback_confirmed=true';
131     }
132
133     /* Make sure the callback parameter contains either a real URL
134      * or the string 'oob'.
135      *
136      * @todo Check for evil/banned URLs here
137      *
138      * @return boolean true or false
139      */
140     function verifyCallback($callback)
141     {
142         if ($callback == "oob") {
143             common_debug("OAuth request token requested for out of band client.");
144
145             // XXX: Should we throw an error if a client is registered as a
146             // web application but requests the pin based workflow? For now I'm
147             // allowing the workflow to proceed and issuing a pin. --Zach
148
149             return true;
150         } else {
151             return Validate::uri($callback);
152         }
153     }
154 }