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 * @author Craig Andrews <candrews@integralblue.com>
24 * @copyright 2009-2010 StatusNet, Inc.
25 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
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')) {
35 * Plugin for OpenID authentication and identity
37 * This class enables consumer support for OpenID, the distributed authentication
38 * and identity system.
42 * @author Evan Prodromou <evan@status.net>
43 * @author Craig Andrews <candrews@integralblue.com>
44 * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
45 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
46 * @link http://status.net/
47 * @link http://openid.net/
50 class OpenIDPlugin extends Plugin
52 // Plugin parameter: set true to disallow non-OpenID logins
53 // If set, overrides the setting in database or $config['site']['openidonly']
54 public $openidOnly = null;
59 if ($this->openidOnly !== null) {
61 $config['site']['openidonly'] = (bool)$this->openidOnly;
67 * Add OpenID-related paths to the router table
69 * Hook for RouterInitialized event.
71 * @param Net_URL_Mapper $m URL mapper
73 * @return boolean hook return
76 function onStartInitializeRouter($m)
78 $m->connect('main/openid', array('action' => 'openidlogin'));
79 $m->connect('main/openidtrust', array('action' => 'openidtrust'));
80 $m->connect('settings/openid', array('action' => 'openidsettings'));
81 $m->connect('index.php?action=finishopenidlogin',
82 array('action' => 'finishopenidlogin'));
83 $m->connect('index.php?action=finishaddopenid',
84 array('action' => 'finishaddopenid'));
85 $m->connect('main/openidserver', array('action' => 'openidserver'));
86 $m->connect('admin/openid', array('action' => 'openidadminpanel'));
92 * In OpenID-only mode, disable paths for password stuff
94 * @param string $path path to connect
95 * @param array $defaults path defaults
96 * @param array $rules path rules
97 * @param array $result unused
99 * @return boolean hook return
102 function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
104 if (common_config('site', 'openidonly')) {
105 static $block = array('main/login',
107 'main/recoverpassword',
108 'settings/password');
110 if (in_array($path, $block)) {
119 * If we've been hit with password-login args, redirect
121 * @param array $args args (URL, Get, post)
123 * @return boolean hook return
126 function onArgsInitialize($args)
128 if (common_config('site', 'openidonly')) {
129 if (array_key_exists('action', $args)) {
130 $action = trim($args['action']);
131 if (in_array($action, array('login', 'register'))) {
132 common_redirect(common_local_url('openidlogin'));
134 } else if ($action == 'passwordsettings') {
135 common_redirect(common_local_url('openidsettings'));
137 } else if ($action == 'recoverpassword') {
138 throw new ClientException('Unavailable action');
146 * Public XRDS output hook
148 * Puts the bits of code needed by some OpenID providers to show
149 * we're good citizens.
151 * @param Action $action Action being executed
152 * @param XMLOutputter &$xrdsOutputter Output channel
154 * @return boolean hook return
157 function onEndPublicXRDS($action, &$xrdsOutputter)
159 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
160 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
161 'version' => '2.0'));
162 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
164 foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
165 $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
166 common_local_url($finish));
169 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
170 common_local_url('openidserver'),
173 'http://specs.openid.net/auth/2.0/identifier_select');
174 $xrdsOutputter->elementEnd('XRD');
178 * User XRDS output hook
180 * Puts the bits of code needed to discover OpenID endpoints.
182 * @param Action $action Action being executed
183 * @param XMLOutputter &$xrdsOutputter Output channel
185 * @return boolean hook return
188 function onEndUserXRDS($action, &$xrdsOutputter)
190 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
191 'xml:id' => 'openid',
192 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
193 'version' => '2.0'));
194 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
197 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
198 common_local_url('finishopenidlogin'));
201 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
202 common_local_url('openidserver'),
205 common_profile_url($action->user->nickname));
206 $xrdsOutputter->elementEnd('XRD');
210 * If we're in OpenID-only mode, hide all the main menu except OpenID login.
212 * @param Action $action Action being run
214 * @return boolean hook return
217 function onStartPrimaryNav($action)
219 if (common_config('site', 'openidonly') && !common_logged_in()) {
220 // TRANS: Tooltip for main menu option "Login"
221 $tooltip = _m('TOOLTIP', 'Login to the site');
222 $action->menuItem(common_local_url('openidlogin'),
223 // TRANS: Main menu option when not logged in to log in
228 // TRANS: Tooltip for main menu option "Help"
229 $tooltip = _m('TOOLTIP', 'Help me!');
230 $action->menuItem(common_local_url('doc', array('title' => 'help')),
231 // TRANS: Main menu option for help on the StatusNet site
236 if (!common_config('site', 'private')) {
237 // TRANS: Tooltip for main menu option "Search"
238 $tooltip = _m('TOOLTIP', 'Search for people or text');
239 $action->menuItem(common_local_url('peoplesearch'),
240 // TRANS: Main menu option when logged in or when the StatusNet instance is not private
241 _m('MENU', 'Search'), $tooltip, false, 'nav_search');
243 Event::handle('EndPrimaryNav', array($action));
252 * If we're in openidOnly mode, we disable the menu for all other login.
254 * @param Action &$action Action being executed
256 * @return boolean hook return
259 function onStartLoginGroupNav(&$action)
261 if (common_config('site', 'openidonly')) {
262 $this->showOpenIDLoginTab($action);
263 // Even though we replace this code, we
264 // DON'T run the End* hook, to keep others from
265 // adding tabs. Not nice, but.
273 * Menu item for login
275 * @param Action &$action Action being executed
277 * @return boolean hook return
280 function onEndLoginGroupNav(&$action)
282 $this->showOpenIDLoginTab($action);
288 * Show menu item for login
290 * @param Action $action Action being executed
295 function showOpenIDLoginTab($action)
297 $action_name = $action->trimmed('action');
299 $action->menuItem(common_local_url('openidlogin'),
300 // TRANS: OpenID plugin menu item on site logon page.
301 _m('MENU', 'OpenID'),
302 // TRANS: OpenID plugin tooltip for logon menu item.
303 _m('Login or register with OpenID'),
304 $action_name === 'openidlogin');
308 * Show menu item for password
310 * We hide it in openID-only mode
312 * @param Action $menu Widget for menu
313 * @param void &$unused Unused value
318 function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
319 if (common_config('site', 'openidonly')) {
326 * Menu item for OpenID settings
328 * @param Action &$action Action being executed
330 * @return boolean hook return
333 function onEndAccountSettingsNav(&$action)
335 $action_name = $action->trimmed('action');
337 $action->menuItem(common_local_url('openidsettings'),
338 // TRANS: OpenID plugin menu item on user settings page.
339 _m('MENU', 'OpenID'),
340 // TRANS: OpenID plugin tooltip for user settings menu item.
341 _m('Add or remove OpenIDs'),
342 $action_name === 'openidsettings');
350 * Loads our classes if they're requested.
352 * @param string $cls Class requested
354 * @return boolean hook return
357 function onAutoload($cls)
361 case 'OpenidloginAction':
362 case 'FinishopenidloginAction':
363 case 'FinishaddopenidAction':
365 case 'PublicxrdsAction':
366 case 'OpenidsettingsAction':
367 case 'OpenidserverAction':
368 case 'OpenidtrustAction':
369 case 'OpenidadminpanelAction':
370 require_once dirname(__FILE__) . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
373 require_once dirname(__FILE__) . '/User_openid.php';
375 case 'User_openid_trustroot':
376 require_once dirname(__FILE__) . '/User_openid_trustroot.php';
378 case 'Auth_OpenID_TeamsExtension':
379 case 'Auth_OpenID_TeamsRequest':
380 case 'Auth_OpenID_TeamsResponse':
381 require_once dirname(__FILE__) . '/extlib/teams-extension.php';
391 * These actions should use https when SSL support is 'sometimes'
393 * @param Action $action Action to form an URL for
394 * @param boolean &$ssl Whether to mark it for SSL
396 * @return boolean hook return
399 function onSensitiveAction($action, &$ssl)
403 case 'finishopenidlogin':
404 case 'finishaddopenid':
415 * These actions should be visible even when the site is marked private
417 * @param Action $action Action to show
418 * @param boolean &$login Whether it's a login action
420 * @return boolean hook return
423 function onLoginAction($action, &$login)
428 case 'finishopenidlogin':
438 * We include a <meta> element linking to the userxrds page, for OpenID
439 * client-side authentication.
441 * @param Action $action Action being shown
446 function onEndShowHeadElements($action)
448 if ($action instanceof ShowstreamAction) {
449 $action->element('link', array('rel' => 'openid2.provider',
450 'href' => common_local_url('openidserver')));
451 $action->element('link', array('rel' => 'openid2.local_id',
452 'href' => $action->profile->profileurl));
453 $action->element('link', array('rel' => 'openid.server',
454 'href' => common_local_url('openidserver')));
455 $action->element('link', array('rel' => 'openid.delegate',
456 'href' => $action->profile->profileurl));
462 * Redirect to OpenID login if they have an OpenID
464 * @param Action $action Action being executed
465 * @param User $user User doing the action
467 * @return boolean whether to continue
470 function onRedirectToLogin($action, $user)
472 if (common_config('site', 'openid_only') || (!empty($user) && User_openid::hasOpenID($user->id))) {
473 common_redirect(common_local_url('openidlogin'), 303);
480 * Show some extra instructions for using OpenID
482 * @param Action $action Action being executed
484 * @return boolean hook value
487 function onEndShowPageNotice($action)
489 $name = $action->trimmed('action');
494 if (common_logged_in()) {
495 $instr = '(Have an [OpenID](http://openid.net/)? ' .
496 '[Add an OpenID to your account](%%action.openidsettings%%)!';
498 $instr = '(Have an [OpenID](http://openid.net/)? ' .
499 'Try our [OpenID registration]'.
500 '(%%action.openidlogin%%)!)';
504 $instr = '(Have an [OpenID](http://openid.net/)? ' .
505 'Try our [OpenID login]'.
506 '(%%action.openidlogin%%)!)';
512 $output = common_markup_to_html($instr);
513 $action->raw($output);
518 * Load our document if requested
520 * @param string &$title Title to fetch
521 * @param string &$output HTML to output
523 * @return boolean hook value
526 function onStartLoadDoc(&$title, &$output)
528 if ($title == 'openid') {
529 $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
531 $c = file_get_contents($filename);
532 $output = common_markup_to_html($c);
533 return false; // success!
540 * Add our document to the global menu
542 * @param string $title Title being fetched
543 * @param string &$output HTML being output
545 * @return boolean hook value
548 function onEndLoadDoc($title, &$output)
550 if ($title == 'help') {
551 $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
553 $output .= common_markup_to_html($menuitem);
562 * Assure that our data objects are available in the DB
564 * @return boolean hook value
567 function onCheckSchema()
569 $schema = Schema::get();
570 $schema->ensureTable('user_openid',
571 array(new ColumnDef('canonical', 'varchar',
572 '255', false, 'PRI'),
573 new ColumnDef('display', 'varchar',
574 '255', false, 'UNI'),
575 new ColumnDef('user_id', 'integer',
577 new ColumnDef('created', 'datetime',
579 new ColumnDef('modified', 'timestamp')));
580 $schema->ensureTable('user_openid_trustroot',
581 array(new ColumnDef('trustroot', 'varchar',
582 '255', false, 'PRI'),
583 new ColumnDef('user_id', 'integer',
585 new ColumnDef('created', 'datetime',
587 new ColumnDef('modified', 'timestamp')));
592 * Add our tables to be deleted when a user is deleted
594 * @param User $user User being deleted
595 * @param array &$tables Array of table names
597 * @return boolean hook value
600 function onUserDeleteRelated($user, &$tables)
602 $tables[] = 'User_openid';
603 $tables[] = 'User_openid_trustroot';
608 * Add an OpenID tab to the admin panel
610 * @param Widget $nav Admin panel nav
612 * @return boolean hook value
615 function onEndAdminPanelNav($nav)
617 if (AdminPanelAction::canAdmin('openid')) {
619 $action_name = $nav->action->trimmed('action');
622 common_local_url('openidadminpanel'),
624 _m('OpenID configuration'),
625 $action_name == 'openidadminpanel',
626 'nav_openid_admin_panel'
634 * Add our version information to output
636 * @param array &$versions Array of version-data arrays
638 * @return boolean hook value
641 function onPluginVersion(&$versions)
643 $versions[] = array('name' => 'OpenID',
644 'version' => STATUSNET_VERSION,
645 'author' => 'Evan Prodromou, Craig Andrews',
646 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
648 // TRANS: OpenID plugin description.
649 _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));