]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OpenID/openidlogin.php
OpenID access control options: trusted provider URL, Launchpad team restrictions...
[quix0rs-gnu-social.git] / plugins / OpenID / openidlogin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
23
24 class OpenidloginAction extends Action
25 {
26     function handle($args)
27     {
28         parent::handle($args);
29         if (common_is_real_login()) {
30             $this->clientError(_m('Already logged in.'));
31         } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
32             $provider = common_config('openid', 'trusted_provider');
33             if ($provider) {
34                 $openid_url = $provider;
35             } else {
36                 $openid_url = $this->trimmed('openid_url');
37             }
38
39             oid_assert_allowed($openid_url);
40
41             # CSRF protection
42             $token = $this->trimmed('token');
43             if (!$token || $token != common_session_token()) {
44                 $this->showForm(_m('There was a problem with your session token. Try again, please.'), $openid_url);
45                 return;
46             }
47
48             $rememberme = $this->boolean('rememberme');
49
50             common_ensure_session();
51
52             $_SESSION['openid_rememberme'] = $rememberme;
53
54             $result = oid_authenticate($openid_url,
55                                        'finishopenidlogin');
56
57             if (is_string($result)) { # error message
58                 unset($_SESSION['openid_rememberme']);
59                 $this->showForm($result, $openid_url);
60             }
61         } else {
62             $openid_url = oid_get_last();
63             $this->showForm(null, $openid_url);
64         }
65     }
66
67     function getInstructions()
68     {
69         if (common_logged_in() && !common_is_real_login() &&
70             common_get_returnto()) {
71             // rememberme logins have to reauthenticate before
72             // changing any profile settings (cookie-stealing protection)
73             return _m('For security reasons, please re-login with your ' .
74                      '[OpenID](%%doc.openid%%) ' .
75                      'before changing your settings.');
76         } else {
77             return _m('Login with an [OpenID](%%doc.openid%%) account.');
78         }
79     }
80
81     function showPageNotice()
82     {
83         if ($this->error) {
84             $this->element('div', array('class' => 'error'), $this->error);
85         } else {
86             $instr = $this->getInstructions();
87             $output = common_markup_to_html($instr);
88             $this->elementStart('div', 'instructions');
89             $this->raw($output);
90             $this->elementEnd('div');
91         }
92     }
93
94     function showScripts()
95     {
96         parent::showScripts();
97         $this->autofocus('openid_url');
98     }
99
100     function title()
101     {
102         return _m('OpenID Login');
103     }
104
105     function showForm($error=null, $openid_url)
106     {
107         $this->error = $error;
108         $this->openid_url = $openid_url;
109         $this->showPage();
110     }
111
112     function showContent() {
113         $formaction = common_local_url('openidlogin');
114         $this->elementStart('form', array('method' => 'post',
115                                            'id' => 'form_openid_login',
116                                            'class' => 'form_settings',
117                                            'action' => $formaction));
118         $this->elementStart('fieldset');
119         $this->element('legend', null, _m('OpenID login'));
120         $this->hidden('token', common_session_token());
121
122         $this->elementStart('ul', 'form_data');
123         $this->elementStart('li');
124         $provider = common_config('openid', 'trusted_provider');
125         if ($provider) {
126             $this->element('label', array(), _m('OpenID provider'));
127             $this->element('span', array(), $provider);
128             $this->element('p', 'form_guide',
129                            _m('You will be sent to the provider\'s site for authentication.'));
130             $this->hidden('openid_url', $provider);
131         } else {
132             $this->input('openid_url', _m('OpenID URL'),
133                          $this->openid_url,
134                          _m('Your OpenID URL'));
135         }
136         $this->elementEnd('li');
137         $this->elementStart('li', array('id' => 'settings_rememberme'));
138         $this->checkbox('rememberme', _m('Remember me'), false,
139                         _m('Automatically login in the future; ' .
140                            'not for shared computers!'));
141         $this->elementEnd('li');
142         $this->elementEnd('ul');
143         $this->submit('submit', _m('Login'));
144         $this->elementEnd('fieldset');
145         $this->elementEnd('form');
146     }
147
148     function showLocalNav()
149     {
150         $nav = new LoginGroupNav($this);
151         $nav->show();
152     }
153 }