]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthauthorize.php
Associate request tokens with OAuth apps and app users
[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/apioauthstore.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 Action
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(var_export($_REQUEST, true));
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
78         return true;
79     }
80
81     function getApp()
82     {
83         // Look up the full req token
84
85         $req_token = $this->store->lookup_token(null,
86                                                 'request',
87                                                 $this->oauth_token);
88
89         if (empty($req_token)) {
90
91             common_debug("Couldn't find request token!");
92
93             $this->clientError(_('Bad request.'));
94             return;
95         }
96
97         // Look up the app
98
99         $app = new Oauth_application();
100         $app->consumer_key = $req_token->consumer_key;
101         $result = $app->find(true);
102
103         if (!empty($result)) {
104             $this->app = $app;
105             return true;
106
107         } else {
108             common_debug("couldn't find the app!");
109             return false;
110         }
111     }
112
113     /**
114      * Handle input, produce output
115      *
116      * Switches on request method; either shows the form or handles its input.
117      *
118      * @param array $args $_REQUEST data
119      *
120      * @return void
121      */
122
123     function handle($args)
124     {
125         parent::handle($args);
126
127         if ($_SERVER['REQUEST_METHOD'] == 'POST') {
128
129             $this->handlePost();
130
131         } else {
132
133             // XXX: make better error messages
134
135             if (empty($this->oauth_token)) {
136
137                 common_debug("No request token found.");
138
139                 $this->clientError(_('Bad request.'));
140                 return;
141             }
142
143             if (!$this->getApp()) {
144                 $this->clientError(_('Bad request.'));
145                 return;
146             }
147
148             common_debug("Requesting auth for app: $app->name.");
149
150             $this->showForm();
151         }
152     }
153
154     function handlePost()
155     {
156         // check session token for CSRF protection.
157
158         $token = $this->trimmed('token');
159
160         if (!$token || $token != common_session_token()) {
161             $this->showForm(_('There was a problem with your session token. '.
162                               'Try again, please.'));
163             return;
164         }
165
166         if (!$this->getApp()) {
167             $this->clientError(_('Bad request.'));
168             return;
169         }
170
171         // check creds
172
173         $user = null;
174
175         if (!common_logged_in()) {
176             $user = common_check_user($this->nickname, $this->password);
177             if (empty($user)) {
178                 $this->showForm(_("Invalid nickname / password!"));
179                 return;
180             }
181         } else {
182             $user = common_current_user();
183         }
184
185         if ($this->arg('allow')) {
186
187             // mark the req token as authorized
188
189             $this->store->authorize_token($this->oauth_token);
190
191             // Check to see if there was a previous token associated
192             // with this user/app and kill it. If you're doing this you
193             // probably don't want any old tokens anyway.
194
195             $appUser = Oauth_application_user::getByKeys($user, $this->app);
196
197             if (!empty($appUser)) {
198                 $result = $appUser->delete();
199
200                 if (!$result) {
201                     common_log_db_error($appUser, 'DELETE', __FILE__);
202                     throw new ServerException(_('DB error deleting OAuth app user.'));
203                     return;
204                 }
205             }
206
207             // associated the new req token with the user and the app
208
209             $appUser = new Oauth_application_user();
210
211             $appUser->profile_id     = $user->id;
212             $appUser->application_id = $this->app->id;
213             $appUser->access_type    = $this->app->access_type;
214             $appUser->token          = $this->oauth_token;
215             $appUser->created        = common_sql_now();
216
217             $result = $appUser->insert();
218
219             if (!$result) {
220                 common_log_db_error($appUser, 'INSERT', __FILE__);
221                 throw new ServerException(_('DB error inserting OAuth app user.'));
222                 return;
223             }
224
225             // if we have a callback redirect and provide the token
226
227             if (!empty($this->callback)) {
228
229                 // XXX: Need better way to build this redirect url.
230
231                 $target_url = $this->callback . '?oauth_token=' . $this->oauth_token;
232                 common_redirect($target_url, 303);
233             }
234
235             // otherwise inform the user that the rt was authorized
236
237             $this->elementStart('p');
238
239             // XXX: Do OAuth 1.0a verifier code?
240
241             $this->raw(sprintf(_("The request token %s has been authorized. " .
242                                  'Please exchange it for an access token.'),
243                                $this->oauth_token));
244
245             $this->elementEnd('p');
246
247         } else if ($this->arg('deny')) {
248
249             $this->elementStart('p');
250
251             $this->raw(sprintf(_("The request token %s has been denied."),
252                                $this->oauth_token));
253
254             $this->elementEnd('p');
255         } else {
256             $this->clientError(_('Unexpected form submission.'));
257             return;
258         }
259     }
260
261     function showForm($error=null)
262     {
263         $this->error = $error;
264         $this->showPage();
265     }
266
267     function showScripts()
268     {
269         parent::showScripts();
270         if (!common_logged_in()) {
271             $this->autofocus('nickname');
272         }
273     }
274
275     /**
276      * Title of the page
277      *
278      * @return string title of the page
279      */
280
281     function title()
282     {
283         return _('An application would like to connect to your account');
284     }
285
286     /**
287      * Show page notice
288      *
289      * Display a notice for how to use the page, or the
290      * error if it exists.
291      *
292      * @return void
293      */
294
295     function showPageNotice()
296     {
297         if ($this->error) {
298             $this->element('p', 'error', $this->error);
299         } else {
300             $instr  = $this->getInstructions();
301             $output = common_markup_to_html($instr);
302
303             $this->raw($output);
304         }
305     }
306
307     /**
308      * Shows the authorization form.
309      *
310      * @return void
311      */
312
313     function showContent()
314     {
315         $this->elementStart('form', array('method' => 'post',
316                                            'id' => 'form_login',
317                                            'class' => 'form_settings',
318                                            'action' => common_local_url('apioauthauthorize')));
319
320         $this->hidden('token', common_session_token());
321         $this->hidden('oauth_token', $this->oauth_token);
322         $this->hidden('oauth_callback', $this->callback);
323
324         $this->elementStart('fieldset');
325
326         $this->elementStart('ul');
327         $this->elementStart('li');
328         if (!empty($this->app->icon)) {
329             $this->element('img', array('src' => $this->app->icon));
330         }
331         $this->elementEnd('li');
332         $this->elementStart('li');
333
334         $access = ($this->app->access_type & Oauth_application::$writeAccess) ?
335           'access and update' : 'access';
336
337         $msg = _("The application <b>%s</b> by <b>%s</b> would like " .
338                  "the ability to <b>%s</b> your account data.");
339
340         $this->raw(sprintf($msg,
341                            $this->app->name,
342                            $this->app->organization,
343                            $access));
344
345         $this->elementEnd('li');
346         $this->elementEnd('ul');
347
348         $this->elementEnd('fieldset');
349
350         if (!common_logged_in()) {
351
352             $this->elementStart('fieldset');
353             $this->element('legend', null, _('Login'));
354             $this->elementStart('ul', 'form_data');
355             $this->elementStart('li');
356             $this->input('nickname', _('Nickname'));
357             $this->elementEnd('li');
358             $this->elementStart('li');
359             $this->password('password', _('Password'));
360             $this->elementEnd('li');
361             $this->elementEnd('ul');
362
363             $this->elementEnd('fieldset');
364
365         }
366
367         $this->element('input', array('id' => 'deny_submit',
368                                       'class' => 'submit',
369                                       'name' => 'deny',
370                                       'type' => 'submit',
371                                       'value' => _('Deny')));
372
373         $this->element('input', array('id' => 'allow_submit',
374                                       'class' => 'submit',
375                                       'name' => 'allow',
376                                       'type' => 'submit',
377                                       'value' => _('Allow')));
378
379         $this->elementEnd('form');
380     }
381
382     /**
383      * Instructions for using the form
384      *
385      * For "remembered" logins, we make the user re-login when they
386      * try to change settings. Different instructions for this case.
387      *
388      * @return void
389      */
390
391     function getInstructions()
392     {
393         return _('Allow or deny access to your account information.');
394
395     }
396
397     /**
398      * A local menu
399      *
400      * Shows different login/register actions.
401      *
402      * @return void
403      */
404
405     function showLocalNav()
406     {
407     }
408
409 }