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 * @return boolean hook return
65 function onStartInitializeRouter($m)
67 $m->connect('main/openid', array('action' => 'openidlogin'));
68 $m->connect('main/openidtrust', array('action' => 'openidtrust'));
69 $m->connect('settings/openid', array('action' => 'openidsettings'));
70 $m->connect('index.php?action=finishopenidlogin', array('action' => 'finishopenidlogin'));
71 $m->connect('index.php?action=finishaddopenid', array('action' => 'finishaddopenid'));
72 $m->connect('main/openidserver', array('action' => 'openidserver'));
77 function onEndPublicXRDS($action, &$xrdsOutputter)
79 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
80 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
82 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
84 foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
85 $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
86 common_local_url($finish));
89 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
90 common_local_url('openidserver'),
93 'http://specs.openid.net/auth/2.0/identifier_select');
94 $xrdsOutputter->elementEnd('XRD');
97 function onEndUserXRDS($action, &$xrdsOutputter)
99 $xrdsOutputter->elementStart('XRD', array('xmlns' => 'xri://$xrd*($v*2.0)',
100 'xml:id' => 'openid',
101 'xmlns:simple' => 'http://xrds-simple.net/core/1.0',
102 'version' => '2.0'));
103 $xrdsOutputter->element('Type', null, 'xri://$xrds*simple');
106 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
107 common_local_url('finishopenidlogin'));
110 $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
111 common_local_url('openidserver'),
114 common_profile_url($action->user->nickname));
115 $xrdsOutputter->elementEnd('XRD');
118 function onEndLoginGroupNav(&$action)
120 $action_name = $action->trimmed('action');
122 $action->menuItem(common_local_url('openidlogin'),
124 _m('Login or register with OpenID'),
125 $action_name === 'openidlogin');
130 function onEndAccountSettingsNav(&$action)
132 $action_name = $action->trimmed('action');
134 $action->menuItem(common_local_url('openidsettings'),
136 _m('Add or remove OpenIDs'),
137 $action_name === 'openidsettings');
142 function onAutoload($cls)
146 case 'OpenidloginAction':
147 case 'FinishopenidloginAction':
148 case 'FinishaddopenidAction':
150 case 'PublicxrdsAction':
151 case 'OpenidsettingsAction':
152 case 'OpenidserverAction':
153 case 'OpenidtrustAction':
154 require_once(INSTALLDIR.'/plugins/OpenID/' . strtolower(mb_substr($cls, 0, -6)) . '.php');
157 require_once(INSTALLDIR.'/plugins/OpenID/User_openid.php');
159 case 'User_openid_trustroot':
160 require_once(INSTALLDIR.'/plugins/OpenID/User_openid_trustroot.php');
167 function onSensitiveAction($action, &$ssl)
171 case 'finishopenidlogin':
172 case 'finishaddopenid':
180 function onLoginAction($action, &$login)
185 case 'finishopenidlogin':
195 * We include a <meta> element linking to the publicxrds page, for OpenID
196 * client-side authentication.
201 function onEndShowHeadElements($action)
203 if($action instanceof ShowstreamAction){
204 $action->element('link', array('rel' => 'openid2.provider',
205 'href' => common_local_url('openidserver')));
206 $action->element('link', array('rel' => 'openid2.local_id',
207 'href' => $action->profile->profileurl));
208 $action->element('link', array('rel' => 'openid.server',
209 'href' => common_local_url('openidserver')));
210 $action->element('link', array('rel' => 'openid.delegate',
211 'href' => $action->profile->profileurl));
217 * Redirect to OpenID login if they have an OpenID
219 * @return boolean whether to continue
222 function onRedirectToLogin($action, $user)
224 if (!empty($user) && User_openid::hasOpenID($user->id)) {
225 common_redirect(common_local_url('openidlogin'), 303);
231 function onEndShowPageNotice($action)
233 $name = $action->trimmed('action');
238 $instr = '(Have an [OpenID](http://openid.net/)? ' .
239 'Try our [OpenID registration]'.
240 '(%%action.openidlogin%%)!)';
243 $instr = '(Have an [OpenID](http://openid.net/)? ' .
244 'Try our [OpenID login]'.
245 '(%%action.openidlogin%%)!)';
251 $output = common_markup_to_html($instr);
252 $action->raw($output);
256 function onStartLoadDoc(&$title, &$output)
258 if ($title == 'openid')
260 $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
262 $c = file_get_contents($filename);
263 $output = common_markup_to_html($c);
264 return false; // success!
270 function onEndLoadDoc($title, &$output)
272 if ($title == 'help')
274 $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
276 $output .= common_markup_to_html($menuitem);
282 function onCheckSchema() {
283 $schema = Schema::get();
284 $schema->ensureTable('user_openid',
285 array(new ColumnDef('canonical', 'varchar',
286 '255', false, 'PRI'),
287 new ColumnDef('display', 'varchar',
288 '255', false, 'UNI'),
289 new ColumnDef('user_id', 'integer',
291 new ColumnDef('created', 'datetime',
293 new ColumnDef('modified', 'timestamp')));
294 $schema->ensureTable('user_openid_trustroot',
295 array(new ColumnDef('trustroot', 'varchar',
296 '255', false, 'PRI'),
297 new ColumnDef('user_id', 'integer',
299 new ColumnDef('created', 'datetime',
301 new ColumnDef('modified', 'timestamp')));
305 function onUserDeleteRelated($user, &$tables)
307 $tables[] = 'User_openid';
308 $tables[] = 'User_openid_trustroot';
312 function onPluginVersion(&$versions)
314 $versions[] = array('name' => 'OpenID',
315 'version' => STATUSNET_VERSION,
316 'author' => 'Evan Prodromou, Craig Andrews',
317 'homepage' => 'http://status.net/wiki/Plugin:OpenID',
319 _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));