# success!
$cur->query('COMMIT');
+
+ oid_set_last($display);
common_redirect(common_local_url('openidsettings'));
}
--- /dev/null
+<?php
+/*
+ * Laconica - a distributed open-source microblogging tool
+ * Copyright (C) 2008, Controlez-Vous, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+if (!defined('LACONICA')) { exit(1); }
+
+require_once(INSTALLDIR.'/lib/openid.php');
+
+class FinishimmediateAction extends Action {
+
+ function handle($args) {
+ parent::handle($args);
+
+ $consumer = oid_consumer();
+
+ $response = $consumer->complete(common_local_url('finishimmediate'));
+
+ if ($response->status == Auth_OpenID_SUCCESS) {
+ $display = $response->getDisplayIdentifier();
+ $canonical = ($response->endpoint->canonicalID) ?
+ $response->endpoint->canonicalID : $response->getDisplayIdentifier();
+
+ $user = $this->get_user($canonical);
+
+ if ($user) {
+ $this->update_user($user, $sreg);
+ common_set_user($user->nickname);
+ $this->go_backto();
+ return;
+ }
+ }
+
+ # Failure! Clear openid so we don't try it again
+
+ oid_clear_last();
+ $this->go_backto();
+ return;
+ }
+
+ function go_backto() {
+ common_ensure_session();
+ $backto = $_SESSION['openid_immediate_backto'];
+ if (!$backto) {
+ # gar. Well, push them to the public page
+ $backto = common_local_url('public');
+ }
+ common_redirect($backto);
+ }
+}
$sreg = $sreg_resp->contents();
}
- $user = $this->get_user($canonical);
+ $user = oid_get_user($canonical);
if ($user) {
+ oid_set_last($display);
$this->update_user($user, $sreg);
common_set_user($user->nickname);
$this->go_home($user->nickname);
common_show_footer();
}
- function get_user($canonical) {
- $user = NULL;
- $oid = User_openid::staticGet('canonical', $canonical);
- if ($oid) {
- $user = User::staticGet('id', $oid->user_id);
- }
- return $user;
- }
-
function update_user($user, $sreg) {
$profile = $user->getProfile();
# Possible race condition... let's be paranoid
- $other = $this->get_user($canonical);
+ $other = oid_get_user($canonical);
if ($other) {
common_server_error(_t('Creating new account for OpenID that already has a user.'));
$profile->delete();
}
+ oid_set_last($display);
common_set_user($user->nickname);
common_redirect(common_local_url('showstream', array('nickname' => $user->nickname)));
}
}
$this->update_user($user, $sreg);
+ oid_set_last($display);
common_set_user($user->nickname);
$this->go_home($user->nickname);
}
if (!defined('LACONICA')) { exit(1); }
+require_once(INSTALLDIR.'/lib/openid.php');
+
class LogoutAction extends Action {
function handle($args) {
parent::handle($args);
common_user_error(_t('Not logged in.'));
} else {
common_set_user(NULL);
+ oid_clear_last();
common_redirect(common_local_url('public'));
}
}
if (common_logged_in()) {
common_user_error(_t('Already logged in.'));
} else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
- $result = oid_authenticate($this->trimmed('openid_url'),
+ $openid_url = $this->trimmed('openid_url');
+ $result = oid_authenticate($openid_url,
'finishopenidlogin');
if (is_string($result)) { # error message
- $this->show_form($result);
+ $this->show_form($result, $openid_url);
}
} else {
- $this->show_form();
+ $openid_url = oid_get_last();
+ $this->show_form(NULL, $openid_url);
}
}
}
}
- function show_form($error=NULL) {
+ function show_form($error=NULL, $openid_url) {
common_show_header(_t('OpenID Login'), NULL, $error, array($this, 'show_top'));
$formaction = common_local_url('openidlogin');
common_element_start('form', array('method' => 'POST',
'id' => 'openidlogin',
'action' => $formaction));
- common_input('openid_url', _t('OpenID URL'));
+ common_input('openid_url', _t('OpenID URL'),
+ $openid_url,
+ _t('Your OpenID URL'));
common_submit('submit', _t('Login'));
common_element_end('form');
common_show_footer();
common_redirect(common_local_url('public'));
}
+# Do an OpenID immediate request if they're not logged in
+# and they have an OpenID cookie
+
+if (!common_logged_in() &&
+ $_SERVER['REQUEST_METHOD'] == 'GET' &&
+ $action != 'finishimmediate')
+{
+ require_once(INSTALLDIR.'/lib/openid.php');
+ $openid_url = oid_get_last();
+ if ($openid_url) {
+ oid_check_immediate($openid_url);
+ return;
+ }
+}
+
$actionfile = INSTALLDIR."/actions/$action.php";
if (file_exists($actionfile)) {
require_once('Auth/OpenID/SReg.php');
require_once('Auth/OpenID/MySQLStore.php');
+# About one year cookie expiry
+
+define('OPENID_COOKIE_EXPIRY', round(365.25 * 24 * 60 * 60));
+define('OPENID_COOKIE_KEY', 'lastusedopenid');
+
function oid_store() {
static $store = NULL;
if (!$store) {
return $consumer;
}
+function oid_clear_last() {
+ if (oid_get_last()) {
+ oid_set_last('');
+ }
+}
+
+function oid_set_last($openid_url) {
+ global $config;
+ setcookie(OPENID_COOKIE_KEY, $openid_url,
+ time() + OPENID_COOKIE_EXPIRY,
+ '/' . $config['site']['path'] . '/',
+ $config['site']['server']);
+}
+
+function oid_get_last() {
+ return $_COOKIE[OPENID_COOKIE_KEY];
+}
+
function oid_link_user($id, $canonical, $display) {
$oid = new User_openid();
return true;
}
-function oid_authenticate($openid_url, $returnto) {
-
+function oid_get_user($openid_url) {
+ $user = NULL;
+ $oid = User_openid::staticGet('canonical', $openid_url);
+ if ($oid) {
+ $user = User::staticGet('id', $oid->user_id);
+ }
+ return $user;
+}
+
+function oid_check_immediate($openid_url, $backto=NULL) {
+ if (!$backto) {
+ $backto = $_SERVER['PHP_SELF'];
+ }
+ common_ensure_session();
+ $_SESSION['openid_immediate_backto'] = $backto;
+ oid_authenticate($openid_url,
+ 'finishimmediate',
+ true);
+}
+
+function oid_authenticate($openid_url, $returnto, $immediate=false) {
+
$consumer = oid_consumer();
if (!$consumer) {
if ($auth_request->shouldSendRedirect()) {
$redirect_url = $auth_request->redirectURL($trust_root,
- $process_url);
+ $process_url,
+ $immediate);
if (!$redirect_url) {
} else if (Auth_OpenID::isFailure($redirect_url)) {
return _t('Could not redirect to server: ') . $redirect_url->message;
// Generate form markup and render it.
$form_id = 'openid_message';
$form_html = $auth_request->formMarkup($trust_root, $process_url,
- false, array('id' => $form_id));
+ $immediate, array('id' => $form_id));
# XXX: This is cheap, but things choke if we don't escape ampersands
# in the HTML attributes