3 * StatusNet, the distributed open-source microblogging tool
7 * LICENCE: This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * @author Evan Prodromou <evan@status.net>
23 * @copyright 2009-2010 StatusNet, Inc.
24 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
25 * @link http://status.net/
28 if (!defined('STATUSNET')) {
33 * Plugin for OpenID authentication and identity
35 * This class enables consumer support for OpenID, the distributed authentication
36 * and identity system.
40 * @author Evan Prodromou <evan@status.net>
41 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42 * @link http://status.net/
43 * @link http://openid.net/
46 class OpenIDPlugin extends Plugin
48 // Plugin parameter: set true to disallow non-OpenID logins
49 // If set, overrides the setting in database or $config['site']['openidonly']
50 public $openidOnly = null;
55 if ($this->openidOnly !== null) {
57 $config['site']['openidonly'] = (bool)$this->openidOnly;
63 * Add OpenID-related paths to the router table
65 * Hook for RouterInitialized event.
67 * @param Net_URL_Mapper $m URL mapper
69 * @return boolean hook return
72 function onStartInitializeRouter($m)
74 $m->connect('main/openid', array('action' => 'openidlogin'));
75 $m->connect('main/openidtrust', array('action' => 'openidtrust'));
76 $m->connect('settings/openid', array('action' => 'openidsettings'));
77 $m->connect('index.php?action=finishopenidlogin',
78 array('action' => 'finishopenidlogin'));
79 $m->connect('index.php?action=finishaddopenid',
80 array('action' => 'finishaddopenid'));
81 $m->connect('main/openidserver', array('action' => 'openidserver'));
82 $m->connect('admin/openid', array('action' => 'openidadminpanel'));
88 * In OpenID-only mode, disable paths for password stuff
90 * @param string $path path to connect
91 * @param array $defaults path defaults
92 * @param array $rules path rules
93 * @param array $result unused
95 * @return boolean hook return
98 function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
100 if (common_config('site', 'openidonly')) {
101 static $block = array('main/login',
103 'main/recoverpassword',
104 'settings/password');
106 if (in_array($path, $block)) {
115 * If we've been hit with password-login args, redirect
117 * @param array $args args (URL, Get, post)
119 * @return boolean hook return
122 function onArgsInitialize($args)
124 if (common_config('site', 'openidonly')) {
125 if (array_key_exists('action', $args)) {
126 $action = trim($args['action']);
127 if (in_array($action, array('login', 'register'))) {
128 common_redirect(common_local_url('openidlogin'));
130 } else if ($action == 'passwordsettings') {
131 common_redirect(common_local_url('openidsettings'));
133 } else if ($action == 'recoverpassword') {
134 throw new ClientException('Unavailable action');
142 * Public XRDS output hook
144 * Puts the bits of code needed by some OpenID providers to show
145 * we're good citizens.
147 * @param Action $action Action being executed
148 * @param XMLOutputter &$xrdsOutputter Output channel
150 * @return boolean hook return
153 function onEndPublicXRDS($action, &$xrdsOutputter)
155 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
156 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
157 'version' => '2.0'));
158 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
160 foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
161 $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
162 common_local_url($finish));
165 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
166 common_local_url('openidserver'),
169 'http://specs.openid.net/auth/2.0/identifier_select');
170 $xrdsOutputter->elementEnd('XRD');
174 * User XRDS output hook
176 * Puts the bits of code needed to discover OpenID endpoints.
178 * @param Action $action Action being executed
179 * @param XMLOutputter &$xrdsOutputter Output channel
181 * @return boolean hook return
184 function onEndUserXRDS($action, &$xrdsOutputter)
186 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
187 'xml:id' => 'openid',
188 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
189 'version' => '2.0'));
190 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
193 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
194 common_local_url('finishopenidlogin'));
197 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
198 common_local_url('openidserver'),
201 common_profile_url($action->user->nickname));
202 $xrdsOutputter->elementEnd('XRD');
206 * If we're in OpenID-only mode, hide all the main menu except OpenID login.
208 * @param Action $action Action being run
210 * @return boolean hook return
213 function onStartPrimaryNav($action)
215 if (common_config('site', 'openidonly') && !common_logged_in()) {
216 // TRANS: Tooltip for main menu option "Login"
217 $tooltip = _m('TOOLTIP', 'Login to the site');
218 $action->menuItem(common_local_url('openidlogin'),
219 // TRANS: Main menu option when not logged in to log in
224 // TRANS: Tooltip for main menu option "Help"
225 $tooltip = _m('TOOLTIP', 'Help me!');
226 $action->menuItem(common_local_url('doc', array('title' => 'help')),
227 // TRANS: Main menu option for help on the StatusNet site
232 if (!common_config('site', 'private')) {
233 // TRANS: Tooltip for main menu option "Search"
234 $tooltip = _m('TOOLTIP', 'Search for people or text');
235 $action->menuItem(common_local_url('peoplesearch'),
236 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
237 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
239 Event::handle('EndPrimaryNav', array($action));
248 * If we're in openidOnly mode, we disable the menu for all other login.
250 * @param Action &$action Action being executed
252 * @return boolean hook return
255 function onStartLoginGroupNav(&$action)
257 if (common_config('site', 'openidonly')) {
258 $this->showOpenIDLoginTab($action);
259 // Even though we replace this code, we
260 // DON'T run the End* hook, to keep others from
261 // adding tabs. Not nice, but.
269 * Menu item for login
271 * @param Action &$action Action being executed
273 * @return boolean hook return
276 function onEndLoginGroupNav(&$action)
278 $this->showOpenIDLoginTab($action);
284 * Show menu item for login
286 * @param Action $action Action being executed
291 function showOpenIDLoginTab($action)
293 $action_name = $action->trimmed('action');
295 $action->menuItem(common_local_url('openidlogin'),
296 // TRANS: OpenID plugin menu item on site logon page.
297 _m('MENU', 'OpenID'),
298 // TRANS: OpenID plugin tooltip for logon menu item.
299 _m('Login or register with OpenID'),
300 $action_name === 'openidlogin');
304 * Show menu item for password
306 * We hide it in openID-only mode
308 * @param Action $menu Widget for menu
309 * @param void &$unused Unused value
314 function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
315 if (common_config('site', 'openidonly')) {
322 * Menu item for OpenID settings
324 * @param Action &$action Action being executed
326 * @return boolean hook return
329 function onEndAccountSettingsNav(&$action)
331 $action_name = $action->trimmed('action');
333 $action->menuItem(common_local_url('openidsettings'),
334 // TRANS: OpenID plugin menu item on user settings page.
335 _m('MENU', 'OpenID'),
336 // TRANS: OpenID plugin tooltip for user settings menu item.
337 _m('Add or remove OpenIDs'),
338 $action_name === 'openidsettings');
346 * Loads our classes if they're requested.
348 * @param string $cls Class requested
350 * @return boolean hook return
353 function onAutoload($cls)
357 case 'OpenidloginAction':
358 case 'FinishopenidloginAction':
359 case 'FinishaddopenidAction':
361 case 'PublicxrdsAction':
362 case 'OpenidsettingsAction':
363 case 'OpenidserverAction':
364 case 'OpenidtrustAction':
365 case 'OpenidadminpanelAction':
366 require_once dirname(__FILE__) . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
369 require_once dirname(__FILE__) . '/User_openid.php';
371 case 'User_openid_trustroot':
372 require_once dirname(__FILE__) . '/User_openid_trustroot.php';
374 case 'Auth_OpenID_TeamsExtension':
375 case 'Auth_OpenID_TeamsRequest':
376 case 'Auth_OpenID_TeamsResponse':
377 require_once dirname(__FILE__) . '/extlib/teams-extension.php';
387 * These actions should use https when SSL support is 'sometimes'
389 * @param Action $action Action to form an URL for
390 * @param boolean &$ssl Whether to mark it for SSL
392 * @return boolean hook return
395 function onSensitiveAction($action, &$ssl)
399 case 'finishopenidlogin':
400 case 'finishaddopenid':
411 * These actions should be visible even when the site is marked private
413 * @param Action $action Action to show
414 * @param boolean &$login Whether it's a login action
416 * @return boolean hook return
419 function onLoginAction($action, &$login)
424 case 'finishopenidlogin':
434 * We include a <meta> element linking to the userxrds page, for OpenID
435 * client-side authentication.
437 * @param Action $action Action being shown
442 function onEndShowHeadElements($action)
444 if ($action instanceof ShowstreamAction) {
445 $action->element('link', array('rel' => 'openid2.provider',
446 'href' => common_local_url('openidserver')));
447 $action->element('link', array('rel' => 'openid2.local_id',
448 'href' => $action->profile->profileurl));
449 $action->element('link', array('rel' => 'openid.server',
450 'href' => common_local_url('openidserver')));
451 $action->element('link', array('rel' => 'openid.delegate',
452 'href' => $action->profile->profileurl));
458 * Redirect to OpenID login if they have an OpenID
460 * @param Action $action Action being executed
461 * @param User $user User doing the action
463 * @return boolean whether to continue
466 function onRedirectToLogin($action, $user)
468 if (common_config('site', 'openid_only') || (!empty($user) && User_openid::hasOpenID($user->id))) {
469 common_redirect(common_local_url('openidlogin'), 303);
476 * Show some extra instructions for using OpenID
478 * @param Action $action Action being executed
480 * @return boolean hook value
483 function onEndShowPageNotice($action)
485 $name = $action->trimmed('action');
490 if (common_logged_in()) {
491 $instr = '(Have an [OpenID](http://openid.net/)? ' .
492 '[Add an OpenID to your account](%%action.openidsettings%%)!';
494 $instr = '(Have an [OpenID](http://openid.net/)? ' .
495 'Try our [OpenID registration]'.
496 '(%%action.openidlogin%%)!)';
500 $instr = '(Have an [OpenID](http://openid.net/)? ' .
501 'Try our [OpenID login]'.
502 '(%%action.openidlogin%%)!)';
508 $output = common_markup_to_html($instr);
509 $action->raw($output);
514 * Load our document if requested
516 * @param string &$title Title to fetch
517 * @param string &$output HTML to output
519 * @return boolean hook value
522 function onStartLoadDoc(&$title, &$output)
524 if ($title == 'openid') {
525 $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
527 $c = file_get_contents($filename);
528 $output = common_markup_to_html($c);
529 return false; // success!
536 * Add our document to the global menu
538 * @param string $title Title being fetched
539 * @param string &$output HTML being output
541 * @return boolean hook value
544 function onEndLoadDoc($title, &$output)
546 if ($title == 'help') {
547 $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
549 $output .= common_markup_to_html($menuitem);
558 * Assure that our data objects are available in the DB
560 * @return boolean hook value
563 function onCheckSchema()
565 $schema = Schema::get();
566 $schema->ensureTable('user_openid',
567 array(new ColumnDef('canonical', 'varchar',
568 '255', false, 'PRI'),
569 new ColumnDef('display', 'varchar',
570 '255', false, 'UNI'),
571 new ColumnDef('user_id', 'integer',
573 new ColumnDef('created', 'datetime',
575 new ColumnDef('modified', 'timestamp')));
576 $schema->ensureTable('user_openid_trustroot',
577 array(new ColumnDef('trustroot', 'varchar',
578 '255', false, 'PRI'),
579 new ColumnDef('user_id', 'integer',
581 new ColumnDef('created', 'datetime',
583 new ColumnDef('modified', 'timestamp')));
588 * Add our tables to be deleted when a user is deleted
590 * @param User $user User being deleted
591 * @param array &$tables Array of table names
593 * @return boolean hook value
596 function onUserDeleteRelated($user, &$tables)
598 $tables[] = 'User_openid';
599 $tables[] = 'User_openid_trustroot';
604 * Add an OpenID tab to the admin panel
606 * @param Widget $nav Admin panel nav
608 * @return boolean hook value
611 function onEndAdminPanelNav($nav)
613 if (AdminPanelAction::canAdmin('openid')) {
615 $action_name = $nav->action->trimmed('action');
618 common_local_url('openidadminpanel'),
620 _m('OpenID configuration'),
621 $action_name == 'openidadminpanel',
622 'nav_openid_admin_panel'
630 * Add our version information to output
632 * @param array &$versions Array of version-data arrays
634 * @return boolean hook value
637 function onPluginVersion(&$versions)
639 $versions[] = array('name' => 'OpenID',
640 'version' => STATUSNET_VERSION,
641 'author' => 'Evan Prodromou, Craig Andrews',
642 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
644 // TRANS: OpenID plugin description.
645 _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));