3 * StatusNet, the distributed open-source microblogging tool
5 * Authorize an OAuth request token
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.
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.
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/>.
24 * @author Zach Copley <zach@status.net>
25 * @copyright 2010-2011 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/
30 if (!defined('STATUSNET')) {
34 require_once INSTALLDIR . '/lib/apioauth.php';
35 require_once INSTALLDIR . '/lib/info.php';
38 * Authorize an OAuth request token
42 * @author Zach Copley <zach@status.net>
43 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
44 * @link http://status.net/
46 class ApiOauthAuthorizeAction extends Action
57 * Is this a read-only action?
59 * @return boolean false
61 function isReadOnly($args)
66 function prepare($args)
68 parent::prepare($args);
70 $this->nickname = $this->trimmed('nickname');
71 $this->password = $this->arg('password');
72 $this->oauthTokenParam = $this->arg('oauth_token');
73 $this->mode = $this->arg('mode');
74 $this->store = new ApiStatusNetOAuthDataStore();
77 $this->app = $this->store->getAppByRequestToken($this->oauthTokenParam);
78 } catch (Exception $e) {
79 $this->clientError($e->getMessage());
86 * Handle input, produce output
88 * Switches on request method; either shows the form or handles its input.
90 * @param array $args $_REQUEST data
94 function handle($args)
96 parent::handle($args);
98 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
104 // Make sure a oauth_token parameter was provided
105 if (empty($this->oauthTokenParam)) {
106 // TRANS: Client error given when no oauth_token was passed to the OAuth API.
107 $this->clientError(_('No oauth_token parameter provided.'));
110 // Check to make sure the token exists
111 $this->reqToken = $this->store->getTokenByKey($this->oauthTokenParam);
113 if (empty($this->reqToken)) {
114 // TRANS: Client error given when an invalid request token was passed to the OAuth API.
115 $this->clientError(_('Invalid request token.'));
118 // Check to make sure we haven't already authorized the token
119 if ($this->reqToken->state != 0) {
120 // TRANS: Client error given when an invalid request token was passed to the OAuth API.
121 $this->clientError(_('Request token already authorized.'));
126 // make sure there's an app associated with this token
127 if (empty($this->app)) {
128 // TRANS: Client error given when an invalid request token was passed to the OAuth API.
129 $this->clientError(_('Invalid request token.'));
132 $name = $this->app->name;
138 function handlePost()
140 // check session token for CSRF protection.
142 $token = $this->trimmed('token');
144 if (!$token || $token != common_session_token()) {
146 // TRANS: Form validation error in API OAuth authorisation because of an invalid session token.
147 _('There was a problem with your session token. Try again, please.'));
155 if (!common_logged_in()) {
157 // XXX Force credentials check?
159 // @fixme this should probably use a unified login form handler
161 if (Event::handle('StartOAuthLoginCheck', array($this, &$user))) {
162 $user = common_check_user($this->nickname, $this->password);
164 Event::handle('EndOAuthLoginCheck', array($this, &$user));
167 // TRANS: Form validation error given when an invalid username and/or password was passed to the OAuth API.
168 $this->showForm(_("Invalid nickname / password!"));
172 $user = common_current_user();
176 $this->reqToken = $this->store->getTokenByKey($this->oauthTokenParam);
177 assert(!empty($this->reqToken));
179 if ($this->arg('allow')) {
180 // mark the req token as authorized
182 $this->store->authorize_token($this->oauthTokenParam);
183 } catch (Exception $e) {
184 $this->serverError($e->getMessage());
190 "API OAuth - User %d (%s) has authorized request token %s for OAuth application %d (%s).",
193 $this->reqToken->tok,
199 $tokenAssoc = new Oauth_token_association();
201 $tokenAssoc->profile_id = $user->id;
202 $tokenAssoc->application_id = $this->app->id;
203 $tokenAssoc->token = $this->oauthTokenParam;
204 $tokenAssoc->created = common_sql_now();
206 $result = $tokenAssoc->insert();
209 common_log_db_error($tokenAssoc, 'INSERT', __FILE__);
210 // TRANS: Server error displayed when a database action fails.
211 $this->serverError(_('Database error inserting oauth_token_association.'));
214 $callback = $this->getCallback();
216 if (!empty($callback) && $this->reqToken->verified_callback != 'oob') {
217 $targetUrl = $this->buildCallbackUrl(
220 'oauth_token' => $this->oauthTokenParam,
221 'oauth_verifier' => $this->reqToken->verifier // 1.0a
225 common_log(LOG_INFO, "Redirecting to callback: $targetUrl");
227 // Redirect the user to the provided OAuth callback
228 common_redirect($targetUrl, 303);
230 } elseif ($this->app->type == 2) {
231 // Strangely, a web application seems to want to do the OOB
232 // workflow. Because no callback was specified anywhere.
236 "API OAuth - No callback provided for OAuth web client ID %s (%s) "
237 . "during authorization step. Falling back to OOB workflow.",
244 // Otherwise, inform the user that the rt was authorized
245 $this->showAuthorized();
246 } else if ($this->arg('cancel')) {
250 "API OAuth - User %d (%s) refused to authorize request token %s for OAuth application %d (%s).",
253 $this->reqToken->tok,
260 $this->store->revoke_token($this->oauthTokenParam, 0);
261 } catch (Exception $e) {
262 $this->ServerError($e->getMessage());
265 $callback = $this->getCallback();
267 // If there's a callback available, inform the consumer the user
268 // has refused authorization
269 if (!empty($callback) && $this->reqToken->verified_callback != 'oob') {
270 $targetUrl = $this->buildCallbackUrl(
273 'oauth_problem' => 'user_refused',
277 common_log(LOG_INFO, "Redirecting to callback: $targetUrl");
279 // Redirect the user to the provided OAuth callback
280 common_redirect($targetUrl, 303);
283 // otherwise inform the user that authorization for the rt was declined
284 $this->showCanceled();
287 // TRANS: Client error given on when invalid data was passed through a form in the OAuth API.
288 $this->clientError(_('Unexpected form submission.'));
293 * Show body - override to add a special CSS class for the authorize
294 * page's "desktop mode" (minimal display)
296 * Calls template methods
302 $bodyClasses = array();
304 if ($this->desktopMode()) {
305 $bodyClasses[] = 'oauth-desktop-mode';
308 if (common_current_user()) {
309 $bodyClasses[] = 'user_in';
312 $attrs = array('id' => strtolower($this->trimmed('action')));
314 if (!empty($bodyClasses)) {
315 $attrs['class'] = implode(' ', $bodyClasses);
318 $this->elementStart('body', $attrs);
320 $this->elementStart('div', array('id' => 'wrap'));
321 if (Event::handle('StartShowHeader', array($this))) {
323 Event::handle('EndShowHeader', array($this));
326 if (Event::handle('StartShowFooter', array($this))) {
328 Event::handle('EndShowFooter', array($this));
330 $this->elementEnd('div');
331 $this->showScripts();
332 $this->elementEnd('body');
335 function showForm($error=null)
337 $this->error = $error;
341 function showScripts()
343 parent::showScripts();
344 if (!common_logged_in()) {
345 $this->autofocus('nickname');
352 * @return string title of the page
356 // TRANS: Title for a page where a user can confirm/deny account access by an external application.
357 return _('An application would like to connect to your account');
361 * Shows the authorization form.
365 function showContent()
367 $this->elementStart('form', array('method' => 'post',
368 'id' => 'form_apioauthauthorize',
369 'class' => 'form_settings',
370 'action' => common_local_url('ApiOauthAuthorize')));
371 $this->elementStart('fieldset');
372 $this->element('legend', array('id' => 'apioauthauthorize_allowdeny'),
373 // TRANS: Fieldset legend.
374 _('Allow or deny access'));
376 $this->hidden('token', common_session_token());
377 $this->hidden('mode', $this->mode);
378 $this->hidden('oauth_token', $this->oauthTokenParam);
379 $this->hidden('oauth_callback', $this->callback);
381 $this->elementStart('ul', 'form_data');
382 $this->elementStart('li');
383 $this->elementStart('p');
384 if (!empty($this->app->icon) && $this->app->name != 'anonymous') {
385 $this->element('img', array('src' => $this->app->icon));
388 $access = ($this->app->access_type & Oauth_application::$writeAccess) ?
389 'access and update' : 'access';
391 if ($this->app->name == 'anonymous') {
392 // Special message for the anonymous app and consumer.
393 // TRANS: User notification of external application requesting account access.
394 // TRANS: %3$s is the access type requested (read-write or read-only), %4$s is the StatusNet sitename.
395 $msg = _('An application would like the ability ' .
396 'to <strong>%3$s</strong> your %4$s account data. ' .
397 'You should only give access to your %4$s account ' .
398 'to third parties you trust.');
400 // TRANS: User notification of external application requesting account access.
401 // TRANS: %1$s is the application name requesting access, %2$s is the organisation behind the application,
402 // TRANS: %3$s is the access type requested, %4$s is the StatusNet sitename.
403 $msg = _('The application <strong>%1$s</strong> by ' .
404 '<strong>%2$s</strong> would like the ability ' .
405 'to <strong>%3$s</strong> your %4$s account data. ' .
406 'You should only give access to your %4$s account ' .
407 'to third parties you trust.');
410 $this->raw(sprintf($msg,
412 $this->app->organization,
414 common_config('site', 'name')));
415 $this->elementEnd('p');
416 $this->elementEnd('li');
417 $this->elementEnd('ul');
421 if (!common_logged_in()) {
422 if (Event::handle('StartOAuthLoginForm', array($this, &$button))) {
423 $this->elementStart('fieldset');
424 // TRANS: Fieldset legend.
425 $this->element('legend', null, _m('LEGEND','Account'));
426 $this->elementStart('ul', 'form_data');
427 $this->elementStart('li');
428 // TRANS: Field label on OAuth API authorisation form.
429 $this->input('nickname', _('Nickname'));
430 $this->elementEnd('li');
431 $this->elementStart('li');
432 // TRANS: Field label on OAuth API authorisation form.
433 $this->password('password', _('Password'));
434 $this->elementEnd('li');
435 $this->elementEnd('ul');
437 $this->elementEnd('fieldset');
439 Event::handle('EndOAuthLoginForm', array($this, &$button));
442 $this->element('input', array('id' => 'cancel_submit',
443 'class' => 'submit submit form_action-primary',
446 // TRANS: Button text that when clicked will cancel the process of allowing access to an account
447 // TRANS: by an external application.
448 'value' => _m('BUTTON','Cancel')));
450 $this->element('input', array('id' => 'allow_submit',
451 'class' => 'submit submit form_action-secondary',
454 // TRANS: Button text that when clicked will allow access to an account by an external application.
455 'value' => $button ? $button : _m('BUTTON','Allow')));
457 $this->elementEnd('fieldset');
458 $this->elementEnd('form');
462 * Instructions for using the form
464 * For "remembered" logins, we make the user re-login when they
465 * try to change settings. Different instructions for this case.
469 function getInstructions()
471 // TRANS: Form instructions.
472 return _('Authorize access to your account information.');
478 * Shows different login/register actions.
482 function showLocalNav()
488 * Checks to see if a the "mode" parameter is present in the request
489 * and set to "desktop". If it is, the page is meant to be displayed in
490 * a small frame of another application, and we should suppress the
491 * header, aside, and footer.
493 function desktopMode()
495 if (isset($this->mode) && $this->mode == 'desktop') {
503 * Override - suppress output in "desktop" mode
505 function showHeader()
507 if ($this->desktopMode() == false) {
508 parent::showHeader();
513 * Override - suppress output in "desktop" mode
517 if ($this->desktopMode() == false) {
523 * Override - suppress output in "desktop" mode
525 function showFooter()
527 if ($this->desktopMode() == false) {
528 parent::showFooter();
537 function showSiteNotice()
545 * Show the form for posting a new notice
549 function showNoticeForm()
555 * Show a nice message confirming the authorization
556 * operation was canceled.
560 function showCanceled()
562 $info = new InfoAction(
563 // TRANS: Header for user notification after revoking OAuth access to an application.
564 _('Authorization canceled.'),
566 // TRANS: User notification after revoking OAuth access to an application.
567 // TRANS: %s is an OAuth token.
568 _('The request token %s has been revoked.'),
569 $this->oauthTokenParam
577 * Show a nice message that the authorization was successful.
578 * If the operation is out-of-band, show a pin.
582 function showAuthorized()
587 if ($this->app->name == 'anonymous') {
590 // TRANS: Title of the page notifying the user that an anonymous client application was successfully authorized to access the user's account with OAuth.
591 _('You have successfully authorized the application');
594 // TRANS: Message notifying the user that an anonymous client application was successfully authorized to access the user's account with OAuth.
595 _('Please return to the application and enter the following security code to complete the process.');
600 // TRANS: Title of the page notifying the user that the client application was successfully authorized to access the user's account with OAuth.
601 // TRANS: %s is the authorised application name.
602 _('You have successfully authorized %s'),
607 // TRANS: Message notifying the user that the client application was successfully authorized to access the user's account with OAuth.
608 // TRANS: %s is the authorised application name.
609 _('Please return to %s and enter the following security code to complete the process.'),
615 if ($this->reqToken->verified_callback == 'oob') {
616 $pin = new ApiOauthPinAction(
619 $this->reqToken->verifier,
624 // NOTE: This would only happen if an application registered as
625 // a web application but sent in 'oob' for the oauth_callback
626 // parameter. Usually web apps will send in a callback and
627 // not use the pin-based workflow.
629 $info = new InfoAction(
632 $this->oauthTokenParam,
633 $this->reqToken->verifier
641 * Figure out what the callback should be
643 function getCallback()
647 // Return the verified callback if we have one
648 if ($this->reqToken->verified_callback != 'oob') {
650 $callback = $this->reqToken->verified_callback;
652 // Otherwise return the callback that was provided when
653 // registering the app
654 if (empty($callback)) {
657 "No verified callback found for request token, using application callback: "
658 . $this->app->callback_url,
662 $callback = $this->app->callback_url;
670 * Properly format the callback URL and parameters so it's
671 * suitable for a redirect in the OAuth dance
673 * @param string $url the URL
674 * @param array $params an array of parameters
676 * @return string $url a URL to use for redirecting to
678 function buildCallbackUrl($url, $params)
680 foreach ($params as $k => $v) {
681 $url = $this->appendQueryVar(
683 OAuthUtil::urlencode_rfc3986($k),
684 OAuthUtil::urlencode_rfc3986($v)
692 * Append a new query parameter after any existing query
695 * @param string $url the URL
696 * @prarm string $k the parameter name
697 * @param string $v value of the paramter
699 * @return string $url the new URL with added parameter
701 function appendQueryVar($url, $k, $v) {
702 $url = preg_replace('/(.*)(\?|&)' . $k . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
703 $url = substr($url, 0, -1);
704 if (strpos($url, '?') === false) {
705 return ($url . '?' . $k . '=' . $v);
707 return ($url . '&' . $k . '=' . $v);