| 1 |
<?php |
<?php |
| 2 |
// $Id: openid.module,v 1.2.2.5 2007/10/14 20:19:21 walkah Exp $ |
// $Id: openid.module,v 1.2.2.6 2007/10/14 20:29:27 walkah Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 69 |
* Implementation of hook_user(). |
* Implementation of hook_user(). |
| 70 |
*/ |
*/ |
| 71 |
function openid_user($op, &$edit, &$account, $category = NULL) { |
function openid_user($op, &$edit, &$account, $category = NULL) { |
| 72 |
if ($op == 'insert' && isset($_SESSION['openid'])) { |
if ($op == 'insert' && isset($_SESSION['openid']['values'])) { |
| 73 |
// The user has registered after trying to login via OpenID. |
// The user has registered after trying to login via OpenID. |
| 74 |
if (variable_get('user_email_verification', TRUE)) { |
if (variable_get('user_email_verification', TRUE)) { |
| 75 |
drupal_set_message(t('Once you have verified your email address, you may log in via OpenID.')); |
drupal_set_message(t('Once you have verified your email address, you may log in via OpenID.')); |
| 86 |
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module'); |
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module'); |
| 87 |
drupal_add_js(drupal_get_path('module', 'openid') .'/openid.js'); |
drupal_add_js(drupal_get_path('module', 'openid') .'/openid.js'); |
| 88 |
|
|
| 89 |
if (!empty($form['#post']['openid_url'])) { |
if (!empty($form['#post']['openid_identifier'])) { |
| 90 |
$form['name']['#required'] = FALSE; |
$form['name']['#required'] = FALSE; |
| 91 |
$form['pass']['#required'] = FALSE; |
$form['pass']['#required'] = FALSE; |
| 92 |
unset($form['#submit']); |
unset($form['#submit']); |
| 96 |
$form['openid_link'] = array('#value' => l(t('Log in using OpenID'), '#', array('class' => 'openid-link'))); |
$form['openid_link'] = array('#value' => l(t('Log in using OpenID'), '#', array('class' => 'openid-link'))); |
| 97 |
$form['user_link'] = array('#value' => l(t('Cancel OpenID login'), '#', array('class' => 'user-link'))); |
$form['user_link'] = array('#value' => l(t('Cancel OpenID login'), '#', array('class' => 'user-link'))); |
| 98 |
|
|
| 99 |
$form['openid_url'] = array( |
$form['openid_identifier'] = array( |
| 100 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 101 |
'#title' => t('Log in using OpenID'), |
'#title' => t('Log in using OpenID'), |
| 102 |
'#size' => ($form_id == 'user_login') ? 58 : 13, |
'#size' => ($form_id == 'user_login') ? 58 : 13, |
| 103 |
'#maxlength' => 255, |
'#maxlength' => 255, |
| 104 |
'#weight' => -1, |
'#weight' => -1, |
| 105 |
'#description' => l(t('What is OpenID?'), 'http://openid.net/'), |
'#description' => l(t('What is OpenID?'), 'http://openid.net/'), |
| 106 |
'#prefix' => '<div id="edit-openid-url-wrapper">', |
'#prefix' => '<div id="edit-openid-identifier-wrapper">', |
| 107 |
'#suffix' => '</div>' |
'#suffix' => '</div>' |
| 108 |
); |
); |
| 109 |
$form['openid.return_to'] = array('#type' => 'hidden', '#value' => url('openid/authenticate', drupal_get_destination(), NULL, TRUE)); |
$form['openid.return_to'] = array('#type' => 'hidden', '#value' => url('openid/authenticate', drupal_get_destination(), NULL, TRUE)); |
| 115 |
elseif($form_id == 'user_register' && isset($_SESSION['openid'])) { |
elseif($form_id == 'user_register' && isset($_SESSION['openid'])) { |
| 116 |
// We were unable to auto-register a new user. Prefill the registration |
// We were unable to auto-register a new user. Prefill the registration |
| 117 |
// form with the values we have. |
// form with the values we have. |
| 118 |
$form['name']['#default_value'] = $_SESSION['openid']['name']; |
$form['name']['#default_value'] = $_SESSION['openid']['values']['name']; |
| 119 |
$form['mail']['#default_value'] = $_SESSION['openid']['mail']; |
$form['mail']['#default_value'] = $_SESSION['openid']['values']['mail']; |
| 120 |
// If user_email_verification is off, hide the password field and just fill |
// If user_email_verification is off, hide the password field and just fill |
| 121 |
// with random password to avoid confusion. |
// with random password to avoid confusion. |
| 122 |
if (!variable_get('user_email_verification', TRUE)) { |
if (!variable_get('user_email_verification', TRUE)) { |
| 123 |
$form['pass']['#type'] = 'hidden'; |
$form['pass']['#type'] = 'hidden'; |
| 124 |
$form['pass']['#value'] = user_password(); |
$form['pass']['#value'] = user_password(); |
| 125 |
} |
} |
| 126 |
$form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['auth_openid']); |
$form['auth_openid'] = array('#type' => 'hidden', '#value' => $_SESSION['openid']['values']['auth_openid']); |
| 127 |
} |
} |
| 128 |
return $form; |
return $form; |
| 129 |
} |
} |
| 136 |
if (empty($return_to)) { |
if (empty($return_to)) { |
| 137 |
$return_to = url('', NULL, NULL, TRUE); |
$return_to = url('', NULL, NULL, TRUE); |
| 138 |
} |
} |
| 139 |
return openid_begin($form_values['openid_url'], $return_to); |
return openid_begin($form_values['openid_identifier'], $return_to); |
| 140 |
} |
} |
| 141 |
|
|
| 142 |
function openid_authentication_page() { |
function openid_authentication_page() { |
| 143 |
$result = openid_complete($_REQUEST); |
$result = openid_complete(); |
| 144 |
switch ($result['status']) { |
switch ($result['status']) { |
| 145 |
case 'success': |
case 'success': |
| 146 |
return openid_authentication($result); |
return openid_authentication($result); |
| 158 |
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module'); |
drupal_add_css(drupal_get_path('module', 'openid') .'/openid.css', 'module'); |
| 159 |
|
|
| 160 |
// Check to see if we got a response |
// Check to see if we got a response |
| 161 |
$result = openid_complete($_REQUEST); |
$result = openid_complete(); |
| 162 |
if ($result['status'] == 'success') { |
if ($result['status'] == 'success') { |
| 163 |
db_query("INSERT INTO {authmap} (uid, authname, module) VALUES (%d, '%s','openid')", $account->uid, $result['openid.identity']); |
$identity = $result['openid.claimed_id']; |
| 164 |
drupal_set_message(t('Successfully added %identity', array('%identity' => $result['openid.identity']))); |
db_query("INSERT INTO {authmap} (uid, authname, module) VALUES (%d, '%s','openid')", $account->uid, $identity); |
| 165 |
|
drupal_set_message(t('Successfully added %identity', array('%identity' => $identity))); |
| 166 |
} |
} |
| 167 |
|
|
| 168 |
$header = array(t('OpenID'), t('Operations')); |
$header = array(t('OpenID'), t('Operations')); |
| 179 |
} |
} |
| 180 |
|
|
| 181 |
function openid_user_add() { |
function openid_user_add() { |
| 182 |
$form['openid_url'] = array( |
$form['openid_identifier'] = array( |
| 183 |
'#type' => 'textfield', |
'#type' => 'textfield', |
| 184 |
'#title' => t('Add an OpenID') |
'#title' => t('Add an OpenID') |
| 185 |
); |
); |
| 189 |
|
|
| 190 |
function openid_user_add_validate($form_id, $form_values) { |
function openid_user_add_validate($form_id, $form_values) { |
| 191 |
// Check for existing entries. |
// Check for existing entries. |
| 192 |
$claimed_id = _openid_normalize($form_values['openid_url']); |
$claimed_id = _openid_normalize($form_values['openid_identifier']); |
| 193 |
if (db_result(db_query("SELECT authname FROM {authmap} WHERE authname='%s'", $claimed_id))) { |
if (db_result(db_query("SELECT authname FROM {authmap} WHERE authname='%s'", $claimed_id))) { |
| 194 |
form_set_error('openid_url', t('That OpenID is already in use on this site.')); |
form_set_error('openid_identifier', t('That OpenID is already in use on this site.')); |
| 195 |
} |
} |
| 196 |
else { |
else { |
| 197 |
$return_to = url('user/'. arg(1) .'/openid', NULL, NULL, TRUE); |
$return_to = url('user/'. arg(1) .'/openid', NULL, NULL, TRUE); |
| 198 |
openid_begin($form_values['openid_url'], $return_to); |
openid_begin($form_values['openid_identifier'], $return_to); |
| 199 |
} |
} |
| 200 |
} |
} |
| 201 |
|
|
| 224 |
|
|
| 225 |
$services = openid_discovery($claimed_id); |
$services = openid_discovery($claimed_id); |
| 226 |
if (count($services) == 0) { |
if (count($services) == 0) { |
| 227 |
form_set_error('openid_url', t('Sorry, that is not a valid OpenID. Please ensure you have spelled your ID correctly.')); |
form_set_error('openid_identifier', t('Sorry, that is not a valid OpenID. Please ensure you have spelled your ID correctly.')); |
| 228 |
return; |
return; |
| 229 |
} |
} |
| 230 |
|
|
| 231 |
$op_endpoint = $services[0]['uri']; |
$op_endpoint = $services[0]['uri']; |
| 232 |
// Store the discovered endpoint in the session (so we don't have to rediscover). |
// Store discovered information in the users' session so we don't have to rediscover. |
| 233 |
$_SESSION['openid_op_endpoint'] = $op_endpoint; |
$_SESSION['openid']['service'] = $services[0]; |
| 234 |
// Store the claimed_id in the session (for handling delegation). |
// Store the claimed id |
| 235 |
$_SESSION['openid_claimed_id'] = $claimed_id; |
$_SESSION['openid']['claimed_id'] = $claimed_id; |
| 236 |
|
|
| 237 |
// If bcmath is present, then create an association |
// If bcmath is present, then create an association |
| 238 |
$assoc_handle = ''; |
$assoc_handle = ''; |
| 239 |
if (function_exists('bcadd')) { |
if (function_exists('bcadd')) { |
| 255 |
} |
} |
| 256 |
|
|
| 257 |
if (isset($services[0]['types']) && is_array($services[0]['types']) && in_array(OPENID_NS_2_0 .'/server', $services[0]['types'])) { |
if (isset($services[0]['types']) && is_array($services[0]['types']) && in_array(OPENID_NS_2_0 .'/server', $services[0]['types'])) { |
| 258 |
$identity = 'http://openid.net/identifier_select/2.0'; |
$identity = 'http://specs.openid.net/auth/2.0/identifier_select'; |
| 259 |
} |
} |
| 260 |
$authn_request = openid_authentication_request($claimed_id, $identity, $return_to, $assoc_handle, $services[0]['version']); |
$authn_request = openid_authentication_request($claimed_id, $identity, $return_to, $assoc_handle, $services[0]['version']); |
| 261 |
|
|
| 276 |
* @return $response Response values for further processing with |
* @return $response Response values for further processing with |
| 277 |
* $response['status'] set to one of 'success', 'failed' or 'cancel'. |
* $response['status'] set to one of 'success', 'failed' or 'cancel'. |
| 278 |
*/ |
*/ |
| 279 |
function openid_complete($response) { |
function openid_complete($response = array()) { |
| 280 |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
| 281 |
|
|
| 282 |
|
if (count($response) == 0) { |
| 283 |
|
$response = _openid_response(); |
| 284 |
|
} |
| 285 |
|
|
| 286 |
// Default to failed response |
// Default to failed response |
| 287 |
$response['status'] = 'failed'; |
$response['status'] = 'failed'; |
| 288 |
if (isset($_SESSION['openid_op_endpoint']) && isset($_SESSION['openid_claimed_id'])) { |
if (isset($_SESSION['openid']['service']['uri']) && isset($_SESSION['openid']['claimed_id'])) { |
| 289 |
_openid_fix_post($response); |
$service = $_SESSION['openid']['service']; |
| 290 |
$op_endpoint = $_SESSION['openid_op_endpoint']; |
$claimed_id = $_SESSION['openid']['claimed_id']; |
| 291 |
$claimed_id = $_SESSION['openid_claimed_id']; |
unset($_SESSION['openid']['service']); |
| 292 |
unset($_SESSION['openid_op_endpoint']); |
unset($_SESSION['openid']['claimed_id']); |
|
unset($_SESSION['openid_claimed_id']); |
|
| 293 |
if (isset($response['openid.mode'])) { |
if (isset($response['openid.mode'])) { |
| 294 |
if ($response['openid.mode'] == 'cancel') { |
if ($response['openid.mode'] == 'cancel') { |
| 295 |
$response['status'] = 'cancel'; |
$response['status'] = 'cancel'; |
| 296 |
} |
} |
| 297 |
else { |
else { |
| 298 |
if (openid_verify_assertion($op_endpoint, $response)) { |
if (openid_verify_assertion($service['uri'], $response)) { |
| 299 |
$response['openid.identity'] = $claimed_id; |
// If the returned claimed_id is different from the session claimed_id, |
| 300 |
|
// then we need to do discovery and make sure the op_endpoint matches. |
| 301 |
|
if ($service['version'] == 2 && $response['openid.claimed_id'] != $claimed_id) { |
| 302 |
|
$disco = openid_discovery($response['openid.claimed_id']); |
| 303 |
|
if ($disco[0]['uri'] != $service['uri']) { |
| 304 |
|
return $response; |
| 305 |
|
} |
| 306 |
|
} |
| 307 |
|
else { |
| 308 |
|
$response['openid.claimed_id'] = $claimed_id; |
| 309 |
|
} |
| 310 |
$response['status'] = 'success'; |
$response['status'] = 'success'; |
| 311 |
} |
} |
| 312 |
} |
} |
| 443 |
function openid_authentication($response) { |
function openid_authentication($response) { |
| 444 |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
| 445 |
|
|
| 446 |
$identity = $response['openid.identity']; |
$identity = $response['openid.claimed_id']; |
| 447 |
|
|
| 448 |
$account = user_external_load($identity); |
$account = user_external_load($identity); |
| 449 |
if (isset($account->uid)) { |
if (isset($account->uid)) { |
| 472 |
// We were unable to register a valid new user, redirect to standard |
// We were unable to register a valid new user, redirect to standard |
| 473 |
// user/register and prefill with the values we received. |
// user/register and prefill with the values we received. |
| 474 |
drupal_set_message(t('OpenID registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and add your OpenID under "My Account"', array('@login' => url('user/login'))), 'error'); |
drupal_set_message(t('OpenID registration failed for the reasons listed. You may register now, or if you already have an account you can <a href="@login">log in</a> now and add your OpenID under "My Account"', array('@login' => url('user/login'))), 'error'); |
| 475 |
$_SESSION['openid'] = $edit; |
$_SESSION['openid']['values'] = $edit; |
| 476 |
// We'll want to redirect back to the same place. |
// We'll want to redirect back to the same place. |
| 477 |
$destination = drupal_get_destination(); |
$destination = drupal_get_destination(); |
| 478 |
unset($_REQUEST['destination']); |
unset($_REQUEST['destination']); |
| 514 |
global $base_url; |
global $base_url; |
| 515 |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
include_once drupal_get_path('module', 'openid') .'/openid.inc'; |
| 516 |
|
|
|
$realm = ''; |
|
|
if ($return_to != '') { |
|
|
$realm = $base_url; |
|
|
} |
|
|
|
|
| 517 |
$ns = ($version == 2) ? OPENID_NS_2_0 : OPENID_NS_1_0; |
$ns = ($version == 2) ? OPENID_NS_2_0 : OPENID_NS_1_0; |
| 518 |
$request = array( |
$request = array( |
| 519 |
'openid.ns' => $ns, |
'openid.ns' => $ns, |
| 525 |
); |
); |
| 526 |
|
|
| 527 |
if ($version == 2) { |
if ($version == 2) { |
| 528 |
$request['openid.realm'] = $realm; |
$request['openid.realm'] = url('', NULL, NULL, TRUE); |
| 529 |
} |
} |
| 530 |
else { |
else { |
| 531 |
$request['openid.trust_root'] = $realm; |
$request['openid.trust_root'] = $realm; |