]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apioauthauthorize.php
Workflow for request tokens and authorizing request tokens
[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             /* Use a session token for CSRF protection. */
129             $token = $this->trimmed('token');
130             if (!$token || $token != common_session_token()) {
131                 $this->showForm(_('There was a problem with your session token. '.
132                                   'Try again, please.'));
133                 return;
134             }
135
136             $this->handlePost();
137
138         } else {
139
140             common_debug('ApiOauthAuthorize::handle()');
141
142             if (empty($this->oauth_token)) {
143
144                 common_debug("No request token found.");
145
146                 $this->clientError(_('Bad request.'));
147                 return;
148             }
149
150             if (!$this->getApp()) {
151                 $this->clientError(_('Bad request.'));
152                 return;
153             }
154
155             common_debug("Requesting auth for app: $app->name.");
156
157             $this->showForm();
158         }
159     }
160
161     function handlePost()
162     {
163         /* Use a session token for CSRF protection. */
164
165         $token = $this->trimmed('token');
166
167         if (!$token || $token != common_session_token()) {
168             $this->showForm(_('There was a problem with your session token. '.
169                               'Try again, please.'));
170             return;
171         }
172
173         if (!$this->getApp()) {
174             $this->clientError(_('Bad request.'));
175             return;
176         }
177
178         // is the user already logged in?
179
180         // check creds
181
182         if (!common_logged_in()) {
183             $user = common_check_user($this->nickname, $this->password);
184             if (empty($user)) {
185                 $this->showForm(_("Invalid nickname / password!"));
186                 return;
187             }
188         }
189
190         if ($this->arg('allow')) {
191
192             $this->store->authorize_token($this->oauth_token);
193
194             // if we have a callback redirect and provide the token
195
196             if (!empty($this->callback)) {
197                 $target_url = $this->callback . '?oauth_token=' . $this->oauth_token;
198                 common_redirect($target_url, 303);
199             }
200
201             // otherwise inform the user that the rt was authorized
202
203             $this->elementStart('p');
204
205             // XXX: Do verifier code?
206
207             $this->raw(sprintf(_("The request token %s has been authorized. " .
208                                  'Please exchange it for an access token.'),
209                                $this->oauth_token));
210
211             $this->elementEnd('p');
212
213         } else if ($this->arg('deny')) {
214
215             $this->elementStart('p');
216
217             $this->raw(sprintf(_("The request token %s has been denied."),
218                                $this->oauth_token));
219
220             $this->elementEnd('p');
221         } else {
222             $this->clientError(_('Unexpected form submission.'));
223             return;
224         }
225     }
226
227     function showForm($error=null)
228     {
229         $this->error = $error;
230         $this->showPage();
231     }
232
233     function showScripts()
234     {
235         parent::showScripts();
236       //  $this->autofocus('nickname');
237     }
238
239     /**
240      * Title of the page
241      *
242      * @return string title of the page
243      */
244
245     function title()
246     {
247         return _('An application would like to connect to your account');
248     }
249
250     /**
251      * Show page notice
252      *
253      * Display a notice for how to use the page, or the
254      * error if it exists.
255      *
256      * @return void
257      */
258
259     function showPageNotice()
260     {
261         if ($this->error) {
262             $this->element('p', 'error', $this->error);
263         } else {
264             $instr  = $this->getInstructions();
265             $output = common_markup_to_html($instr);
266
267             $this->raw($output);
268         }
269     }
270
271     /**
272      * Shows the authorization form.
273      *
274      * @return void
275      */
276
277     function showContent()
278     {
279         $this->elementStart('form', array('method' => 'post',
280                                            'id' => 'form_login',
281                                            'class' => 'form_settings',
282                                            'action' => common_local_url('apioauthauthorize')));
283
284         $this->hidden('token', common_session_token());
285         $this->hidden('oauth_token', $this->oauth_token);
286         $this->hidden('oauth_callback', $this->callback);
287
288         $this->elementStart('fieldset');
289
290         $this->elementStart('ul');
291         $this->elementStart('li');
292         if (!empty($this->app->icon)) {
293             $this->element('img', array('src' => $this->app->icon));
294         }
295         $this->elementEnd('li');
296         $this->elementStart('li');
297
298         $access = ($this->app->access_type & Oauth_application::$writeAccess) ?
299           'access and update' : 'access';
300
301         $msg = _("The application <b>%s</b> by <b>%s</b> would like " .
302                  "the ability to <b>%s</b> your account data.");
303
304         $this->raw(sprintf($msg,
305                            $this->app->name,
306                            $this->app->organization,
307                            $access));
308
309         $this->elementEnd('li');
310         $this->elementEnd('ul');
311
312         $this->elementEnd('fieldset');
313
314         if (!common_logged_in()) {
315
316             $this->elementStart('fieldset');
317             $this->element('legend', null, _('Login'));
318             $this->elementStart('ul', 'form_data');
319             $this->elementStart('li');
320             $this->input('nickname', _('Nickname'));
321             $this->elementEnd('li');
322             $this->elementStart('li');
323             $this->password('password', _('Password'));
324             $this->elementEnd('li');
325             $this->elementEnd('ul');
326
327             $this->elementEnd('fieldset');
328
329         }
330
331         $this->element('input', array('id' => 'deny_submit',
332                                       'class' => 'submit',
333                                       'name' => 'deny',
334                                       'type' => 'submit',
335                                       'value' => _('Deny')));
336
337         $this->element('input', array('id' => 'allow_submit',
338                                       'class' => 'submit',
339                                       'name' => 'allow',
340                                       'type' => 'submit',
341                                       'value' => _('Allow')));
342
343         $this->elementEnd('form');
344     }
345
346     /**
347      * Instructions for using the form
348      *
349      * For "remembered" logins, we make the user re-login when they
350      * try to change settings. Different instructions for this case.
351      *
352      * @return void
353      */
354
355     function getInstructions()
356     {
357         return _('Allow or deny access to your account information.');
358
359     }
360
361     /**
362      * A local menu
363      *
364      * Shows different login/register actions.
365      *
366      * @return void
367      */
368
369     function showLocalNav()
370     {
371     }
372
373 }