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 _('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 _('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');
164 function onSensitiveAction($action, &$ssl)
168 case 'finishopenidlogin':
169 case 'finishaddopenid':
177 function onLoginAction($action, &$login)
182 case 'finishopenidlogin':
192 * We include a <meta> element linking to the publicxrds page, for OpenID
193 * client-side authentication.
198 function onEndShowHeadElements($action)
200 if($action instanceof ShowstreamAction){
201 $action->element('link', array('rel' => 'openid2.provider',
202 'href' => common_local_url('openidserver')));
203 $action->element('link', array('rel' => 'openid2.local_id',
204 'href' => $action->profile->profileurl));
205 $action->element('link', array('rel' => 'openid.server',
206 'href' => common_local_url('openidserver')));
207 $action->element('link', array('rel' => 'openid.delegate',
208 'href' => $action->profile->profileurl));
214 * Redirect to OpenID login if they have an OpenID
216 * @return boolean whether to continue
219 function onRedirectToLogin($action, $user)
221 if (!empty($user) && User_openid::hasOpenID($user->id)) {
222 common_redirect(common_local_url('openidlogin'), 303);
228 function onEndShowPageNotice($action)
230 $name = $action->trimmed('action');
235 $instr = '(Have an [OpenID](http://openid.net/)? ' .
236 'Try our [OpenID registration]'.
237 '(%%action.openidlogin%%)!)';
240 $instr = '(Have an [OpenID](http://openid.net/)? ' .
241 'Try our [OpenID login]'.
242 '(%%action.openidlogin%%)!)';
248 $output = common_markup_to_html($instr);
249 $action->raw($output);
253 function onStartLoadDoc(&$title, &$output)
255 if ($title == 'openid')
257 $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
259 $c = file_get_contents($filename);
260 $output = common_markup_to_html($c);
261 return false; // success!
267 function onEndLoadDoc($title, &$output)
269 if ($title == 'help')
271 $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
273 $output .= common_markup_to_html($menuitem);
279 function onCheckSchema() {
280 $schema = Schema::get();
281 $schema->ensureTable('user_openid',
282 array(new ColumnDef('canonical', 'varchar',
283 '255', false, 'PRI'),
284 new ColumnDef('display', 'varchar',
286 new ColumnDef('user_id', 'integer',
288 new ColumnDef('created', 'datetime',
290 new ColumnDef('modified', 'timestamp')));
291 $schema->ensureTable('user_openid_trustroot',
292 array(new ColumnDef('trustroot', 'varchar',
293 '255', false, 'PRI'),
294 new ColumnDef('user_id', 'integer',
296 new ColumnDef('created', 'datetime',
298 new ColumnDef('modified', 'timestamp')));