3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR.'/plugins/OpenID/openid.php';
24 class FinishopenidloginAction extends Action
30 function handle($args)
32 parent::handle($args);
33 if (common_is_real_login()) {
34 $this->clientError(_m('Already logged in.'));
35 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
36 $token = $this->trimmed('token');
37 if (!$token || $token != common_session_token()) {
38 $this->showForm(_m('There was a problem with your session token. Try again, please.'));
41 if ($this->arg('create')) {
42 if (!$this->boolean('license')) {
43 $this->showForm(_m('You can\'t register if you don\'t agree to the license.'),
44 $this->trimmed('newname'));
47 $this->createNewUser();
48 } else if ($this->arg('connect')) {
51 $this->showForm(_m('Something weird happened.'),
52 $this->trimmed('newname'));
59 function showPageNotice()
62 $this->element('div', array('class' => 'error'), $this->error);
64 $this->element('div', 'instructions',
65 sprintf(_m('This is the first time you\'ve logged into %s so we must connect your OpenID to a local account. You can either create a new account, or connect with your existing account, if you have one.'), common_config('site', 'name')));
71 return _m('OpenID Account Setup');
74 function showForm($error=null, $username=null)
76 $this->error = $error;
77 $this->username = $username;
82 function showContent()
84 if (!empty($this->message_text)) {
85 $this->element('div', array('class' => 'error'), $this->message_text);
89 $this->elementStart('form', array('method' => 'post',
90 'id' => 'account_connect',
91 'class' => 'form_settings',
92 'action' => common_local_url('finishopenidlogin')));
93 $this->hidden('token', common_session_token());
94 $this->elementStart('fieldset', array('id' => 'form_openid_createaccount'));
95 $this->element('legend', null,
96 _m('Create new account'));
97 $this->element('p', null,
98 _m('Create a new user with this nickname.'));
99 $this->elementStart('ul', 'form_data');
100 $this->elementStart('li');
101 $this->input('newname', _m('New nickname'),
102 ($this->username) ? $this->username : '',
103 _m('1-64 lowercase letters or numbers, no punctuation or spaces'));
104 $this->elementEnd('li');
105 $this->elementStart('li');
106 $this->element('input', array('type' => 'checkbox',
108 'class' => 'checkbox',
111 $this->elementStart('label', array('for' => 'license',
112 'class' => 'checkbox'));
113 $this->text(_m('My text and files are available under '));
114 $this->element('a', array('href' => common_config('license', 'url')),
115 common_config('license', 'title'));
116 $this->text(_m(' except this private data: password, email address, IM address, phone number.'));
117 $this->elementEnd('label');
118 $this->elementEnd('li');
119 $this->elementEnd('ul');
120 $this->submit('create', _m('Create'));
121 $this->elementEnd('fieldset');
123 $this->elementStart('fieldset', array('id' => 'form_openid_createaccount'));
124 $this->element('legend', null,
125 _m('Connect existing account'));
126 $this->element('p', null,
127 _m('If you already have an account, login with your username and password to connect it to your OpenID.'));
128 $this->elementStart('ul', 'form_data');
129 $this->elementStart('li');
130 $this->input('nickname', _m('Existing nickname'));
131 $this->elementEnd('li');
132 $this->elementStart('li');
133 $this->password('password', _m('Password'));
134 $this->elementEnd('li');
135 $this->elementEnd('ul');
136 $this->submit('connect', _m('Connect'));
137 $this->elementEnd('fieldset');
138 $this->elementEnd('form');
143 $consumer = oid_consumer();
145 $response = $consumer->complete(common_local_url('finishopenidlogin'));
147 if ($response->status == Auth_OpenID_CANCEL) {
148 $this->message(_m('OpenID authentication cancelled.'));
150 } else if ($response->status == Auth_OpenID_FAILURE) {
151 // Authentication failed; display the error message.
152 $this->message(sprintf(_m('OpenID authentication failed: %s'), $response->message));
153 } else if ($response->status == Auth_OpenID_SUCCESS) {
154 // This means the authentication succeeded; extract the
155 // identity URL and Simple Registration data (if it was
157 $display = $response->getDisplayIdentifier();
158 $canonical = ($response->endpoint->canonicalID) ?
159 $response->endpoint->canonicalID : $response->getDisplayIdentifier();
161 oid_assert_allowed($display);
162 oid_assert_allowed($canonical);
164 $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);
167 $sreg = $sreg_resp->contents();
170 $user = oid_get_user($canonical);
173 oid_set_last($display);
174 # XXX: commented out at @edd's request until better
175 # control over how data flows from OpenID provider.
176 # oid_update_user($user, $sreg);
177 common_set_user($user);
178 common_real_login(true);
179 if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
180 common_rememberme($user);
182 unset($_SESSION['openid_rememberme']);
183 $this->goHome($user->nickname);
185 $this->saveValues($display, $canonical, $sreg);
186 $this->showForm(null, $this->bestNewNickname($display, $sreg));
191 function message($msg)
193 $this->message_text = $msg;
197 function saveValues($display, $canonical, $sreg)
199 common_ensure_session();
200 $_SESSION['openid_display'] = $display;
201 $_SESSION['openid_canonical'] = $canonical;
202 $_SESSION['openid_sreg'] = $sreg;
205 function getSavedValues()
207 return array($_SESSION['openid_display'],
208 $_SESSION['openid_canonical'],
209 $_SESSION['openid_sreg']);
212 function createNewUser()
214 # FIXME: save invite code before redirect, and check here
216 if (common_config('site', 'closed')) {
217 $this->clientError(_m('Registration not allowed.'));
223 if (common_config('site', 'inviteonly')) {
224 $code = $_SESSION['invitecode'];
226 $this->clientError(_m('Registration not allowed.'));
230 $invite = Invitation::staticGet($code);
232 if (empty($invite)) {
233 $this->clientError(_m('Not a valid invitation code.'));
238 $nickname = $this->trimmed('newname');
240 if (!Validate::string($nickname, array('min_length' => 1,
242 'format' => NICKNAME_FMT))) {
243 $this->showForm(_m('Nickname must have only lowercase letters and numbers and no spaces.'));
247 if (!User::allowed_nickname($nickname)) {
248 $this->showForm(_m('Nickname not allowed.'));
252 if (User::staticGet('nickname', $nickname)) {
253 $this->showForm(_m('Nickname already in use. Try another one.'));
257 list($display, $canonical, $sreg) = $this->getSavedValues();
259 if (!$display || !$canonical) {
260 $this->serverError(_m('Stored OpenID not found.'));
264 # Possible race condition... let's be paranoid
266 $other = oid_get_user($canonical);
269 $this->serverError(_m('Creating new account for OpenID that already has a user.'));
274 if (!empty($sreg['country'])) {
275 if ($sreg['postcode']) {
276 # XXX: use postcode to get city and region
277 # XXX: also, store postcode somewhere -- it's valuable!
278 $location = $sreg['postcode'] . ', ' . $sreg['country'];
280 $location = $sreg['country'];
284 if (!empty($sreg['fullname']) && mb_strlen($sreg['fullname']) <= 255) {
285 $fullname = $sreg['fullname'];
290 if (!empty($sreg['email']) && Validate::email($sreg['email'], common_config('email', 'check_domain'))) {
291 $email = $sreg['email'];
299 $args = array('nickname' => $nickname,
301 'fullname' => $fullname,
302 'location' => $location);
304 if (!empty($invite)) {
305 $args['code'] = $invite->code;
308 $user = User::register($args);
310 $result = oid_link_user($user->id, $canonical, $display);
312 oid_set_last($display);
313 common_set_user($user);
314 common_real_login(true);
315 if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
316 common_rememberme($user);
318 unset($_SESSION['openid_rememberme']);
319 common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)),
323 function connectUser()
325 $nickname = $this->trimmed('nickname');
326 $password = $this->trimmed('password');
328 if (!common_check_user($nickname, $password)) {
329 $this->showForm(_m('Invalid username or password.'));
335 $user = User::staticGet('nickname', $nickname);
337 list($display, $canonical, $sreg) = $this->getSavedValues();
339 if (!$display || !$canonical) {
340 $this->serverError(_m('Stored OpenID not found.'));
344 $result = oid_link_user($user->id, $canonical, $display);
347 $this->serverError(_m('Error connecting user to OpenID.'));
351 oid_update_user($user, $sreg);
352 oid_set_last($display);
353 common_set_user($user);
354 common_real_login(true);
355 if (isset($_SESSION['openid_rememberme']) && $_SESSION['openid_rememberme']) {
356 common_rememberme($user);
358 unset($_SESSION['openid_rememberme']);
359 $this->goHome($user->nickname);
362 function goHome($nickname)
364 $url = common_get_returnto();
366 # We don't have to return to it again
367 common_set_returnto(null);
368 $url = common_inject_session($url);
370 $url = common_local_url('all',
374 common_redirect($url, 303);
377 function bestNewNickname($display, $sreg)
380 # Try the passed-in nickname
382 if (!empty($sreg['nickname'])) {
383 $nickname = $this->nicknamize($sreg['nickname']);
384 if ($this->isNewNickname($nickname)) {
391 if (!empty($sreg['fullname'])) {
392 $fullname = $this->nicknamize($sreg['fullname']);
393 if ($this->isNewNickname($fullname)) {
400 $from_url = $this->openidToNickname($display);
402 if ($from_url && $this->isNewNickname($from_url)) {
411 function isNewNickname($str)
413 if (!Validate::string($str, array('min_length' => 1,
415 'format' => NICKNAME_FMT))) {
418 if (!User::allowed_nickname($str)) {
421 if (User::staticGet('nickname', $str)) {
427 function openidToNickname($openid)
429 if (Auth_Yadis_identifierScheme($openid) == 'XRI') {
430 return $this->xriToNickname($openid);
432 return $this->urlToNickname($openid);
436 # We try to use an OpenID URL as a legal StatusNet user name in this order
437 # 1. Plain hostname, like http://evanp.myopenid.com/
438 # 2. One element in path, like http://profile.typekey.com/EvanProdromou/
439 # or http://getopenid.com/evanprodromou
441 function urlToNickname($openid)
443 return common_url_to_nickname($openid);
446 function xriToNickname($xri)
448 $base = $this->xriBase($xri);
454 # or @gratis*evan.prodromou
455 $parts = explode('*', substr($base, 1));
456 return $this->nicknamize(array_pop($parts));
460 function xriBase($xri)
462 if (substr($xri, 0, 6) == 'xri://') {
463 return substr($xri, 6);
469 # Given a string, try to make it work as a nickname
471 function nicknamize($str)
473 return common_nicknamize($str);