]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthauthorize.php
Better token revocation
[quix0rs-gnu-social.git] / actions / apioauthauthorize.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Authorize an OAuth 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  * Authorize an OAuth 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
46 class ApiOauthAuthorizeAction extends ApiOauthAction
47 {
48     var $oauth_token;
49     var $callback;
50     var $app;
51     var $nickname;
52     var $password;
53     var $store;
54
55     /**
56      * Is this a read-only action?
57      *
58      * @return boolean false
59      */
60
61     function isReadOnly($args)
62     {
63         return false;
64     }
65
66     function prepare($args)
67     {
68         parent::prepare($args);
69
70         common_debug("apioauthauthorize");
71
72         $this->nickname    = $this->trimmed('nickname');
73         $this->password    = $this->arg('password');
74         $this->oauth_token = $this->arg('oauth_token');
75         $this->callback    = $this->arg('oauth_callback');
76         $this->store       = new ApiStatusNetOAuthDataStore();
77         $this->app         = $this->store->getAppByRequestToken($this->oauth_token);
78
79         return true;
80     }
81
82     /**
83      * Handle input, produce output
84      *
85      * Switches on request method; either shows the form or handles its input.
86      *
87      * @param array $args $_REQUEST data
88      *
89      * @return void
90      */
91
92     function handle($args)
93     {
94         parent::handle($args);
95
96         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
97
98             $this->handlePost();
99
100         } else {
101
102             if (empty($this->oauth_token)) {
103                 $this->clientError(_('No oauth_token parameter provided.'));
104                 return;
105             }
106
107             if (empty($this->app)) {
108                 $this->clientError(_('Invalid token.'));
109                 return;
110             }
111
112             $name = $this->app->name;
113
114             $this->showForm();
115         }
116     }
117
118     function handlePost()
119     {
120         // check session token for CSRF protection.
121
122         $token = $this->trimmed('token');
123
124         if (!$token || $token != common_session_token()) {
125             $this->showForm(_('There was a problem with your session token. '.
126                               'Try again, please.'));
127             return;
128         }
129
130         // check creds
131
132         $user = null;
133
134         if (!common_logged_in()) {
135             $user = common_check_user($this->nickname, $this->password);
136             if (empty($user)) {
137                 $this->showForm(_("Invalid nickname / password!"));
138                 return;
139             }
140         } else {
141             $user = common_current_user();
142         }
143
144         if ($this->arg('allow')) {
145
146             // mark the req token as authorized
147
148             $this->store->authorize_token($this->oauth_token);
149
150             // Check to see if there was a previous token associated
151             // with this user/app and kill it. If the user is doing this she
152             // probably doesn't want any old tokens anyway.
153
154             $appUser = Oauth_application_user::getByKeys($user, $this->app);
155
156             if (!empty($appUser)) {
157                 $result = $appUser->delete();
158
159                 if (!$result) {
160                     common_log_db_error($appUser, 'DELETE', __FILE__);
161                     throw new ServerException(_('DB error deleting OAuth app user.'));
162                     return;
163                 }
164             }
165
166             // associated the authorized req token with the user and the app
167
168             $appUser = new Oauth_application_user();
169
170             $appUser->profile_id     = $user->id;
171             $appUser->application_id = $this->app->id;
172
173             // Note: do not copy the access type from the application.
174             // The access type should always be 0 when the OAuth app
175             // user record has a request token associated with it.
176             // Access type gets assigned once an access token has been
177             // granted.  The OAuth app user record then gets updated
178             // with the new access token and access type.
179
180             $appUser->token          = $this->oauth_token;
181             $appUser->created        = common_sql_now();
182
183             $result = $appUser->insert();
184
185             if (!$result) {
186                 common_log_db_error($appUser, 'INSERT', __FILE__);
187                 throw new ServerException(_('DB error inserting OAuth app user.'));
188                 return;
189             }
190
191             // if we have a callback redirect and provide the token
192
193             // A callback specified in the app setup overrides whatever
194             // is passed in with the request.
195
196             common_debug("Req token is authorized - doing callback");
197
198             if (!empty($this->app->callback_url)) {
199                 $this->callback = $this->app->callback_url;
200             }
201
202             if (!empty($this->callback)) {
203
204                 $target_url = $this->getCallback($this->callback,
205                                                  array('oauth_token' => $this->oauth_token));
206
207                 common_redirect($target_url, 303);
208             } else {
209                 common_debug("callback was empty!");
210             }
211
212             // otherwise inform the user that the rt was authorized
213
214             $this->elementStart('p');
215
216             // XXX: Do OAuth 1.0a verifier code
217
218             $this->raw(sprintf(_("The request token %s has been authorized. " .
219                                  'Please exchange it for an access token.'),
220                                $this->oauth_token));
221
222             $this->elementEnd('p');
223
224         } else if ($this->arg('deny')) {
225
226             $datastore = new ApiStatusNetOAuthDataStore();
227             $datastore->revoke_token($this->oauth_token, 0);
228
229             $this->elementStart('p');
230
231             $this->raw(sprintf(_("The request token %s has been denied and revoked."),
232                                $this->oauth_token));
233
234             $this->elementEnd('p');
235         } else {
236             $this->clientError(_('Unexpected form submission.'));
237             return;
238         }
239     }
240
241     function showForm($error=null)
242     {
243         $this->error = $error;
244         $this->showPage();
245     }
246
247     function showScripts()
248     {
249         parent::showScripts();
250         if (!common_logged_in()) {
251             $this->autofocus('nickname');
252         }
253     }
254
255     /**
256      * Title of the page
257      *
258      * @return string title of the page
259      */
260
261     function title()
262     {
263         return _('An application would like to connect to your account');
264     }
265
266     /**
267      * Shows the authorization form.
268      *
269      * @return void
270      */
271
272     function showContent()
273     {
274         $this->elementStart('form', array('method' => 'post',
275                                           'id' => 'form_apioauthauthorize',
276                                           'class' => 'form_settings',
277                                           'action' => common_local_url('apioauthauthorize')));
278         $this->elementStart('fieldset');
279         $this->element('legend', array('id' => 'apioauthauthorize_allowdeny'),
280                                  _('Allow or deny access'));
281
282         $this->hidden('token', common_session_token());
283         $this->hidden('oauth_token', $this->oauth_token);
284         $this->hidden('oauth_callback', $this->callback);
285
286         $this->elementStart('ul', 'form_data');
287         $this->elementStart('li');
288         $this->elementStart('p');
289         if (!empty($this->app->icon)) {
290             $this->element('img', array('src' => $this->app->icon));
291         }
292
293         $access = ($this->app->access_type & Oauth_application::$writeAccess) ?
294           'access and update' : 'access';
295
296         $msg = _('The application <strong>%1$s</strong> by ' .
297                  '<strong>%2$s</strong> would like the ability ' .
298                  'to <strong>%3$s</strong> your account data.');
299
300         $this->raw(sprintf($msg,
301                            $this->app->name,
302                            $this->app->organization,
303                            $access));
304         $this->elementEnd('p');
305         $this->elementEnd('li');
306         $this->elementEnd('ul');
307
308         if (!common_logged_in()) {
309
310             $this->elementStart('fieldset');
311             $this->element('legend', null, _('Account'));
312             $this->elementStart('ul', 'form_data');
313             $this->elementStart('li');
314             $this->input('nickname', _('Nickname'));
315             $this->elementEnd('li');
316             $this->elementStart('li');
317             $this->password('password', _('Password'));
318             $this->elementEnd('li');
319             $this->elementEnd('ul');
320
321             $this->elementEnd('fieldset');
322
323         }
324
325         $this->element('input', array('id' => 'deny_submit',
326                                       'class' => 'submit submit form_action-primary',
327                                       'name' => 'deny',
328                                       'type' => 'submit',
329                                       'value' => _('Deny')));
330
331         $this->element('input', array('id' => 'allow_submit',
332                                       'class' => 'submit submit form_action-secondary',
333                                       'name' => 'allow',
334                                       'type' => 'submit',
335                                       'value' => _('Allow')));
336
337         $this->elementEnd('fieldset');
338         $this->elementEnd('form');
339     }
340
341     /**
342      * Instructions for using the form
343      *
344      * For "remembered" logins, we make the user re-login when they
345      * try to change settings. Different instructions for this case.
346      *
347      * @return void
348      */
349
350     function getInstructions()
351     {
352         return _('Allow or deny access to your account information.');
353     }
354
355     /**
356      * A local menu
357      *
358      * Shows different login/register actions.
359      *
360      * @return void
361      */
362
363     function showLocalNav()
364     {
365     }
366
367 }