]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/OpenIDPlugin.php
Do not allow blank passwords when authenticating against LDAP.
[quix0rs-gnu-social.git] / plugins / OpenID / OpenIDPlugin.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * PHP version 5
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  * @category  Plugin
21  * @package   StatusNet
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/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 /**
35  * Plugin for OpenID authentication and identity
36  *
37  * This class enables consumer support for OpenID, the distributed authentication
38  * and identity system.
39  *
40  * @category Plugin
41  * @package  StatusNet
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/
48  */
49
50 class OpenIDPlugin extends Plugin
51 {
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;
55
56     function initialize()
57     {
58         parent::initialize();
59         if ($this->openidOnly !== null) {
60             global $config;
61             $config['site']['openidonly'] = (bool)$this->openidOnly;
62         }
63
64     }
65
66     /**
67      * Add OpenID-related paths to the router table
68      *
69      * Hook for RouterInitialized event.
70      *
71      * @param Net_URL_Mapper $m URL mapper
72      *
73      * @return boolean hook return
74      */
75
76     function onStartInitializeRouter($m)
77     {
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'));
87
88         return true;
89     }
90
91     /**
92      * In OpenID-only mode, disable paths for password stuff
93      *
94      * @param string $path     path to connect
95      * @param array  $defaults path defaults
96      * @param array  $rules    path rules
97      * @param array  $result   unused
98      *
99      * @return boolean hook return
100      */
101
102     function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
103     {
104         if (common_config('site', 'openidonly')) {
105             static $block = array('main/login',
106                                   'main/register',
107                                   'main/recoverpassword',
108                                   'settings/password');
109
110             if (in_array($path, $block)) {
111                 return false;
112             }
113         }
114
115         return true;
116     }
117
118     /**
119      * If we've been hit with password-login args, redirect
120      *
121      * @param array $args args (URL, Get, post)
122      *
123      * @return boolean hook return
124      */
125
126     function onArgsInitialize($args)
127     {
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'));
133                     exit(0);
134                 } else if ($action == 'passwordsettings') {
135                     common_redirect(common_local_url('openidsettings'));
136                     exit(0);
137                 } else if ($action == 'recoverpassword') {
138                     throw new ClientException('Unavailable action');
139                 }
140             }
141         }
142         return true;
143     }
144
145     /**
146      * Public XRDS output hook
147      *
148      * Puts the bits of code needed by some OpenID providers to show
149      * we're good citizens.
150      *
151      * @param Action       $action         Action being executed
152      * @param XMLOutputter &$xrdsOutputter Output channel
153      *
154      * @return boolean hook return
155      */
156
157     function onEndPublicXRDS($action, &$xrdsOutputter)
158     {
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');
163         //consumer
164         foreach (array('finishopenidlogin', 'finishaddopenid') as $finish) {
165             $xrdsOutputter->showXrdsService(Auth_OpenID_RP_RETURN_TO_URL_TYPE,
166                                             common_local_url($finish));
167         }
168         //provider
169         $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/server',
170                                         common_local_url('openidserver'),
171                                         null,
172                                         null,
173                                         'http://specs.openid.net/auth/2.0/identifier_select');
174         $xrdsOutputter->elementEnd('XRD');
175     }
176
177     /**
178      * User XRDS output hook
179      *
180      * Puts the bits of code needed to discover OpenID endpoints.
181      *
182      * @param Action       $action         Action being executed
183      * @param XMLOutputter &$xrdsOutputter Output channel
184      *
185      * @return boolean hook return
186      */
187
188     function onEndUserXRDS($action, &$xrdsOutputter)
189     {
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');
195
196         //consumer
197         $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/return_to',
198                                         common_local_url('finishopenidlogin'));
199
200         //provider
201         $xrdsOutputter->showXrdsService('http://specs.openid.net/auth/2.0/signon',
202                                         common_local_url('openidserver'),
203                                         null,
204                                         null,
205                                         common_profile_url($action->user->nickname));
206         $xrdsOutputter->elementEnd('XRD');
207     }
208
209     /**
210      * If we're in OpenID-only mode, hide all the main menu except OpenID login.
211      *
212      * @param Action $action Action being run
213      *
214      * @return boolean hook return
215      */
216
217     function onStartPrimaryNav($action)
218     {
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
224                               _m('MENU', 'Login'),
225                               $tooltip,
226                               false,
227                               'nav_login');
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
232                               _m('MENU', 'Help'),
233                               $tooltip,
234                               false,
235                               'nav_help');
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');
242             }
243             Event::handle('EndPrimaryNav', array($action));
244             return false;
245         }
246         return true;
247     }
248
249     /**
250      * Menu for login
251      *
252      * If we're in openidOnly mode, we disable the menu for all other login.
253      *
254      * @param Action &$action Action being executed
255      *
256      * @return boolean hook return
257      */
258
259     function onStartLoginGroupNav(&$action)
260     {
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.
266             return false;
267         }
268
269         return true;
270     }
271
272     /**
273      * Menu item for login
274      *
275      * @param Action &$action Action being executed
276      *
277      * @return boolean hook return
278      */
279
280     function onEndLoginGroupNav(&$action)
281     {
282         $this->showOpenIDLoginTab($action);
283
284         return true;
285     }
286
287     /**
288      * Show menu item for login
289      *
290      * @param Action $action Action being executed
291      *
292      * @return void
293      */
294
295     function showOpenIDLoginTab($action)
296     {
297         $action_name = $action->trimmed('action');
298
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');
305     }
306
307     /**
308      * Show menu item for password
309      *
310      * We hide it in openID-only mode
311      *
312      * @param Action $menu    Widget for menu
313      * @param void   &$unused Unused value
314      *
315      * @return void
316      */
317
318     function onStartAccountSettingsPasswordMenuItem($menu, &$unused) {
319         if (common_config('site', 'openidonly')) {
320             return false;
321         }
322         return true;
323     }
324
325     /**
326      * Menu item for OpenID settings
327      *
328      * @param Action &$action Action being executed
329      *
330      * @return boolean hook return
331      */
332
333     function onEndAccountSettingsNav(&$action)
334     {
335         $action_name = $action->trimmed('action');
336
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');
343
344         return true;
345     }
346
347     /**
348      * Autoloader
349      *
350      * Loads our classes if they're requested.
351      *
352      * @param string $cls Class requested
353      *
354      * @return boolean hook return
355      */
356
357     function onAutoload($cls)
358     {
359         switch ($cls)
360         {
361         case 'OpenidloginAction':
362         case 'FinishopenidloginAction':
363         case 'FinishaddopenidAction':
364         case 'XrdsAction':
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';
371             return false;
372         case 'User_openid':
373             require_once dirname(__FILE__) . '/User_openid.php';
374             return false;
375         case 'User_openid_trustroot':
376             require_once dirname(__FILE__) . '/User_openid_trustroot.php';
377             return false;
378         case 'Auth_OpenID_TeamsExtension':
379         case 'Auth_OpenID_TeamsRequest':
380         case 'Auth_OpenID_TeamsResponse':
381             require_once dirname(__FILE__) . '/extlib/teams-extension.php';
382             return false;
383         default:
384             return true;
385         }
386     }
387
388     /**
389      * Sensitive actions
390      *
391      * These actions should use https when SSL support is 'sometimes'
392      *
393      * @param Action  $action Action to form an URL for
394      * @param boolean &$ssl   Whether to mark it for SSL
395      *
396      * @return boolean hook return
397      */
398
399     function onSensitiveAction($action, &$ssl)
400     {
401         switch ($action)
402         {
403         case 'finishopenidlogin':
404         case 'finishaddopenid':
405             $ssl = true;
406             return false;
407         default:
408             return true;
409         }
410     }
411
412     /**
413      * Login actions
414      *
415      * These actions should be visible even when the site is marked private
416      *
417      * @param Action  $action Action to show
418      * @param boolean &$login Whether it's a login action
419      *
420      * @return boolean hook return
421      */
422
423     function onLoginAction($action, &$login)
424     {
425         switch ($action)
426         {
427         case 'openidlogin':
428         case 'finishopenidlogin':
429         case 'openidserver':
430             $login = true;
431             return false;
432         default:
433             return true;
434         }
435     }
436
437     /**
438      * We include a <meta> element linking to the userxrds page, for OpenID
439      * client-side authentication.
440      *
441      * @param Action $action Action being shown
442      *
443      * @return void
444      */
445
446     function onEndShowHeadElements($action)
447     {
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));
457         }
458         return true;
459     }
460
461     /**
462      * Redirect to OpenID login if they have an OpenID
463      *
464      * @param Action $action Action being executed
465      * @param User   $user   User doing the action
466      *
467      * @return boolean whether to continue
468      */
469
470     function onRedirectToLogin($action, $user)
471     {
472         if (common_config('site', 'openid_only') || (!empty($user) && User_openid::hasOpenID($user->id))) {
473             common_redirect(common_local_url('openidlogin'), 303);
474             return false;
475         }
476         return true;
477     }
478
479     /**
480      * Show some extra instructions for using OpenID
481      *
482      * @param Action $action Action being executed
483      *
484      * @return boolean hook value
485      */
486
487     function onEndShowPageNotice($action)
488     {
489         $name = $action->trimmed('action');
490
491         switch ($name)
492         {
493         case 'register':
494             if (common_logged_in()) {
495                 $instr = '(Have an [OpenID](http://openid.net/)? ' .
496                   '[Add an OpenID to your account](%%action.openidsettings%%)!';
497             } else {
498                 $instr = '(Have an [OpenID](http://openid.net/)? ' .
499                   'Try our [OpenID registration]'.
500                   '(%%action.openidlogin%%)!)';
501             }
502             break;
503         case 'login':
504             $instr = '(Have an [OpenID](http://openid.net/)? ' .
505               'Try our [OpenID login]'.
506               '(%%action.openidlogin%%)!)';
507             break;
508         default:
509             return true;
510         }
511
512         $output = common_markup_to_html($instr);
513         $action->raw($output);
514         return true;
515     }
516
517     /**
518      * Load our document if requested
519      *
520      * @param string &$title  Title to fetch
521      * @param string &$output HTML to output
522      *
523      * @return boolean hook value
524      */
525
526     function onStartLoadDoc(&$title, &$output)
527     {
528         if ($title == 'openid') {
529             $filename = INSTALLDIR.'/plugins/OpenID/doc-src/openid';
530
531             $c      = file_get_contents($filename);
532             $output = common_markup_to_html($c);
533             return false; // success!
534         }
535
536         return true;
537     }
538
539     /**
540      * Add our document to the global menu
541      *
542      * @param string $title   Title being fetched
543      * @param string &$output HTML being output
544      *
545      * @return boolean hook value
546      */
547
548     function onEndLoadDoc($title, &$output)
549     {
550         if ($title == 'help') {
551             $menuitem = '* [OpenID](%%doc.openid%%) - what OpenID is and how to use it with this service';
552
553             $output .= common_markup_to_html($menuitem);
554         }
555
556         return true;
557     }
558
559     /**
560      * Data definitions
561      *
562      * Assure that our data objects are available in the DB
563      *
564      * @return boolean hook value
565      */
566
567     function onCheckSchema()
568     {
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',
576                                                  null, false, 'MUL'),
577                                    new ColumnDef('created', 'datetime',
578                                                  null, false),
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',
584                                                  null, false, 'PRI'),
585                                    new ColumnDef('created', 'datetime',
586                                                  null, false),
587                                    new ColumnDef('modified', 'timestamp')));
588         return true;
589     }
590
591     /**
592      * Add our tables to be deleted when a user is deleted
593      *
594      * @param User  $user    User being deleted
595      * @param array &$tables Array of table names
596      *
597      * @return boolean hook value
598      */
599
600     function onUserDeleteRelated($user, &$tables)
601     {
602         $tables[] = 'User_openid';
603         $tables[] = 'User_openid_trustroot';
604         return true;
605     }
606
607     /**
608      * Add an OpenID tab to the admin panel
609      *
610      * @param Widget $nav Admin panel nav
611      *
612      * @return boolean hook value
613      */
614
615     function onEndAdminPanelNav($nav)
616     {
617         if (AdminPanelAction::canAdmin('openid')) {
618
619             $action_name = $nav->action->trimmed('action');
620
621             $nav->out->menuItem(
622                 common_local_url('openidadminpanel'),
623                 _m('OpenID'),
624                 _m('OpenID configuration'),
625                 $action_name == 'openidadminpanel',
626                 'nav_openid_admin_panel'
627             );
628         }
629
630         return true;
631     }
632
633     /**
634      * Add OpenID information to the Account Management Control Document
635      * Event supplied by the Account Manager plugin
636      *
637      * @param array &$amcd Array that expresses the AMCD
638      *
639      * @return boolean hook value
640      */
641
642     function onEndAccountManagementControlDocument(&$amcd)
643     {
644         $amcd['auth-methods']['openid'] = array(
645             'connect' => array(
646                 'method' => 'POST',
647                 'path' => common_local_url('openidlogin'),
648                 'params' => array(
649                     'identity' => 'openid_url'
650                 )
651             )
652         );
653     }
654
655     /**
656      * Add our version information to output
657      *
658      * @param array &$versions Array of version-data arrays
659      *
660      * @return boolean hook value
661      */
662
663     function onPluginVersion(&$versions)
664     {
665         $versions[] = array('name' => 'OpenID',
666                             'version' => STATUSNET_VERSION,
667                             'author' => 'Evan Prodromou, Craig Andrews',
668                             'homepage' => 'http://status.net/wiki/Plugin:OpenID',
669                             'rawdescription' =>
670                             // TRANS: OpenID plugin description.
671                             _m('Use <a href="http://openid.net/">OpenID</a> to login to the site.'));
672         return true;
673     }
674 }