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 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
49 * Initializer for the plugin.
52 function __construct()
54 parent::__construct();
58 * Add OpenID-related paths to the router table
60 * Hook for RouterInitialized event.
62 * @param Net_URL_Mapper $m URL mapper
64 * @return boolean hook return
67 function onStartInitializeRouter($m)
69 $m->connect('main/openid', array('action' => 'openidlogin'));
70 $m->connect('main/openidtrust', array('action' => 'openidtrust'));
71 $m->connect('settings/openid', array('action' => 'openidsettings'));
72 $m->connect('index.php?action=finishopenidlogin',
73 array('action' => 'finishopenidlogin'));
74 $m->connect('index.php?action=finishaddopenid',
75 array('action' => 'finishaddopenid'));
76 $m->connect('main/openidserver', array('action' => 'openidserver'));
82 * Public XRDS output hook
84 * Puts the bits of code needed by some OpenID providers to show
85 * we're good citizens.
87 * @param Action $action Action being executed
88 * @param XMLOutputter &$xrdsOutputter Output channel
90 * @return boolean hook return
93 function onEndPublicXRDS($action, &$xrdsOutputter)
95 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
96 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
98 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
100 foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
101 $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
102 common_local_url($finish));
105 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
106 common_local_url('openidserver'),
109 'http://specs.openid.net/auth/2.0/identifier_select');
110 $xrdsOutputter->elementEnd('XRD');
114 * User XRDS output hook
116 * Puts the bits of code needed to discover OpenID endpoints.
118 * @param Action $action Action being executed
119 * @param XMLOutputter &$xrdsOutputter Output channel
121 * @return boolean hook return
124 function onEndUserXRDS($action, &$xrdsOutputter)
126 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
127 'xml:id' => 'openid',
128 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
129 'version' => '2.0'));
130 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
133 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
134 common_local_url('finishopenidlogin'));
137 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
138 common_local_url('openidserver'),
141 common_profile_url($action->user->nickname));
142 $xrdsOutputter->elementEnd('XRD');
146 * Menu item for login
148 * @param Action &$action Action being executed
150 * @return boolean hook return
153 function onEndLoginGroupNav(&$action)
155 $action_name = $action->trimmed('action');
157 $action->menuItem(common_local_url('openidlogin'),
159 _m('Login or register with OpenID'),
160 $action_name === 'openidlogin');
166 * Menu item for OpenID admin
168 * @param Action &$action Action being executed
170 * @return boolean hook return
173 function onEndAccountSettingsNav(&$action)
175 $action_name = $action->trimmed('action');
177 $action->menuItem(common_local_url('openidsettings'),
179 _m('Add or remove OpenIDs'),
180 $action_name === 'openidsettings');
188 * Loads our classes if they're requested.
190 * @param string $cls Class requested
192 * @return boolean hook return
195 function onAutoload($cls)
199 case 'OpenidloginAction':
200 case 'FinishopenidloginAction':
201 case 'FinishaddopenidAction':
203 case 'PublicxrdsAction':
204 case 'OpenidsettingsAction':
205 case 'OpenidserverAction':
206 case 'OpenidtrustAction':
207 require_once INSTALLDIR.'/plugins/OpenID/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
210 require_once INSTALLDIR.'/plugins/OpenID/User_openid.php';
212 case 'User_openid_trustroot':
213 require_once INSTALLDIR.'/plugins/OpenID/User_openid_trustroot.php';
223 * These actions should use https when SSL support is 'sometimes'
225 * @param Action $action Action to form an URL for
226 * @param boolean &$ssl Whether to mark it for SSL
228 * @return boolean hook return
231 function onSensitiveAction($action, &$ssl)
235 case 'finishopenidlogin':
236 case 'finishaddopenid':
247 * These actions should be visible even when the site is marked private
249 * @param Action $action Action to show
250 * @param boolean &$login Whether it's a login action
252 * @return boolean hook return
255 function onLoginAction($action, &$login)
260 case 'finishopenidlogin':
270 * We include a <meta> element linking to the userxrds page, for OpenID
271 * client-side authentication.
273 * @param Action $action Action being shown
278 function onEndShowHeadElements($action)
280 if ($action instanceof ShowstreamAction) {
281 $action->element('link', array('rel' => 'openid2.provider',
282 'href' => common_local_url('openidserver')));
283 $action->element('link', array('rel' => 'openid2.local_id',
284 'href' => $action->profile->profileurl));
285 $action->element('link', array('rel' => 'openid.server',
286 'href' => common_local_url('openidserver')));
287 $action->element('link', array('rel' => 'openid.delegate',
288 'href' => $action->profile->profileurl));
294 * Redirect to OpenID login if they have an OpenID
296 * @param Action $action Action being executed
297 * @param User $user User doing the action
299 * @return boolean whether to continue
302 function onRedirectToLogin($action, $user)
304 if (!empty($user) && User_openid::hasOpenID($user->id)) {
305 common_redirect(common_local_url('openidlogin'), 303);
312 * Show some extra instructions for using OpenID
314 * @param Action $action Action being executed
316 * @return boolean hook value
319 function onEndShowPageNotice($action)
321 $name = $action->trimmed('action');
326 if (common_logged_in()) {
327 $instr = '(Have an [OpenID](http://openid.net/)? ' .
328 '[Add an OpenID to your account](%%action.openidsettings%%)!';
330 $instr = '(Have an [OpenID](http://openid.net/)? ' .
331 'Try our [OpenID registration]'.
332 '(%%action.openidlogin%%)!)';
336 $instr = '(Have an [OpenID](http://openid.net/)? ' .
337 'Try our [OpenID login]'.
338 '(%%action.openidlogin%%)!)';
344 $output = common_markup_to_html($instr);
345 $action->raw($output);
350 * Load our document if requested
352 * @param string &$title Title to fetch
353 * @param string &$output HTML to output
355 * @return boolean hook value
358 function onStartLoadDoc(&$title, &$output)
360 if ($title == 'openid') {
361 $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
363 $c = file_get_contents($filename);
364 $output = common_markup_to_html($c);
365 return false; // success!
372 * Add our document to the global menu
374 * @param string $title Title being fetched
375 * @param string &$output HTML being output
377 * @return boolean hook value
380 function onEndLoadDoc($title, &$output)
382 if ($title == 'help') {
383 $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
385 $output .= common_markup_to_html($menuitem);
394 * Assure that our data objects are available in the DB
396 * @return boolean hook value
399 function onCheckSchema()
401 $schema = Schema::get();
402 $schema->ensureTable('user_openid',
403 array(new ColumnDef('canonical', 'varchar',
404 '255', false, 'PRI'),
405 new ColumnDef('display', 'varchar',
406 '255', false, 'UNI'),
407 new ColumnDef('user_id', 'integer',
409 new ColumnDef('created', 'datetime',
411 new ColumnDef('modified', 'timestamp')));
412 $schema->ensureTable('user_openid_trustroot',
413 array(new ColumnDef('trustroot', 'varchar',
414 '255', false, 'PRI'),
415 new ColumnDef('user_id', 'integer',
417 new ColumnDef('created', 'datetime',
419 new ColumnDef('modified', 'timestamp')));
424 * Add our tables to be deleted when a user is deleted
426 * @param User $user User being deleted
427 * @param array &$tables Array of table names
429 * @return boolean hook value
432 function onUserDeleteRelated($user, &$tables)
434 $tables[] = 'User_openid';
435 $tables[] = 'User_openid_trustroot';
440 * Add our version information to output
442 * @param array &$versions Array of version-data arrays
444 * @return boolean hook value
447 function onPluginVersion(&$versions)
449 $versions[] = array('name' => 'OpenID',
450 'version' => STATUSNET_VERSION,
451 'author' => 'Evan Prodromou, Craig Andrews',
452 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
454 _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));