Index: openid.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openid/openid.inc,v
retrieving revision 1.6
diff -u -r1.6 openid.inc
--- openid.inc	15 Apr 2007 05:10:08 -0000	1.6
+++ openid.inc	5 May 2007 05:01:12 -0000
@@ -71,6 +71,36 @@
   return $form;
 }
 
+
+function _openid_is_xri($identifier) {
+  $firstchar = substr($identifier, 0, 1);
+  if ($firstchar == "@" || $firstchar == "=")
+    return TRUE;
+
+  if (stristr($identifier, 'xri://') !== FALSE)
+    return TRUE;
+
+  return FALSE;
+}
+
+
+function _openid_normalize($identifier) {
+  if (_openid_is_xri($identifier)) {
+    return _openid_normalize_xri($identifier);
+  }
+  else {
+    return _openid_normalize_url($identifier);
+  }
+}
+
+function _openid_normalize_xri($xri) {
+  $normalized_xri = $xri;
+  if (stristr($xri, 'xri://') !== FALSE) {
+    $normalized_xri = substr($xri, 6);
+  }
+  return $normalized_xri;
+}
+
 function _openid_normalize_url($url) {
   $normalized_url = $url;
 
@@ -82,6 +112,10 @@
     $normalized_url .= '/';
   }
 
+  if (strpos(substr(stristr($normalized_url, '://'), 3), '/') === FALSE) {
+    $normalized_url .= '/';
+  }
+
   return $normalized_url;
 }
 
@@ -379,4 +413,4 @@
     }
     return $result;
   }
-}
\ No newline at end of file
+}
Index: openid.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openid/openid.module,v
retrieving revision 1.8
diff -u -r1.8 openid.module
--- openid.module	15 Apr 2007 05:45:38 -0000	1.8
+++ openid.module	5 May 2007 05:01:12 -0000
@@ -51,7 +51,7 @@
 function openid_login_submit($formid, $form_values) {
   include_once drupal_get_path('module', 'openid') .'/openid.inc';
   
-  $claimed_id = _openid_normalize_url($form_values['openid_url']); 
+  $claimed_id = _openid_normalize($form_values['openid_url']); 
 
   $services = openid_discovery($claimed_id);
   if (count($services) == 0) {
@@ -75,7 +75,13 @@
     $return_to = url('', array('absolute' => TRUE));
   }
 
-  $identity = $claimed_id;
+  // remember the claimed_id
+  $_SESSION['openid_claimed_id'] = $claimed_id; // XXX: lifetime management?
+ 
+  // $identity = $claimed_id;
+  $identity = isset($services[0]['delegate'])? $services[0]['delegate'] : $claimed_id;
+  error_log("identity=$identity claimed=$claimed_id");
+
   if (isset($services[0]['types']) && is_array($services[0]['types']) && in_array('http://openid.net/server/2.0', $services[0]['types'])) {
     $identity = 'http://openid.net/identifier_select/2.0';
   }
@@ -92,6 +98,30 @@
   include_once drupal_get_path('module', 'openid') .'/openid.inc';
   include_once drupal_get_path('module', 'openid') .'/xrds.inc';
 
+  error_log("openid_discovery=$claimed_id");
+  if (_openid_is_xri($claimed_id)) {
+    return openid_xri_discovery($claimed_id);
+  }
+  else {
+    return openid_url_discovery($claimed_id);
+  }
+}
+
+
+function openid_xri_discovery($claimed_id) {
+  error_log("openid_xri_discovery=$claimed_id");
+  $xrds_url = "http://beta.xri.net/$claimed_id?_xrd_r=application/xrds+xml;sep=false;ref=true";
+  error_log("xrds url for $claimed_id = ". $xrds_url);
+  $result = drupal_http_request($xrds_url);
+  error_log("xrds for $claimed_id = ". $result->data);
+  $services = xrds_parse($result->data);
+  error_log("services=" . print_r($services,1));
+
+  return $services;
+}
+
+function openid_url_discovery($claimed_id) {
+
   $url = @parse_url($claimed_id);
   if (!$url) {
     return FALSE;
@@ -133,6 +163,7 @@
                             'delegate' => _openid_link_href('openid.delegate', $result->data));
       }
     }
+    error_log("services=" . print_r($services,1));
   }
   return $services;
 }
@@ -219,6 +250,10 @@
   include_once drupal_get_path('module', 'openid') .'/openid.inc';
 
   $identity = $response['openid.identity'];
+  $identity = isset($_SESSION['openid_claimed_id'])?
+    $_SESSION['openid_claimed_id'] : $response['openid.identity'];
+  unset($_SESSION['openid_claimed_id']);
+  // $identity = $response['openid.identity'];
 
   $account = user_external_load($identity);
   if (isset($account->uid)) {
@@ -232,6 +267,7 @@
     $edit['status'] = 1;
     $edit['auth_openid'] = $identity;
     $edit['openid'] = $response;
+    $edit['roles'] = array(DRUPAL_AUTHENTICATED_RID);
     
     // Build up the new user array an then call user_save().
     user_module_invoke('submit', $edit, $account);
Index: xrds.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openid/xrds.inc,v
retrieving revision 1.2
diff -u -r1.2 xrds.inc
--- xrds.inc	25 Mar 2007 06:38:00 -0000	1.2
+++ xrds.inc	5 May 2007 05:01:12 -0000
@@ -36,12 +36,35 @@
 
   $name = _xrds_strip_namespace($name);
   if ($name == 'SERVICE') {
-    $xrds_services[] = $xrds_current_service;
+    if (_xrds_is_openid_srv($xrds_current_service)) {
+      // save it
+      error_log("YES this is an OpenID service!");
+      $xrds_services[] = $xrds_current_service;
+    }
     $xrds_current_service = array();
   }
   array_pop($xrds_open_elements);
 }
 
+function _xrds_is_openid_srv($srv) {
+  if (!isset($srv['types'])) {
+    return FALSE;
+  }
+
+  foreach ($srv['types'] as $t) {
+    if (strcasecmp($t, 'http://openid.net/signon/1.0') == 0) {
+      return TRUE;
+    }
+    if (strcasecmp($t, 'http://openid.net/signon/1.1') == 0) {
+      return TRUE;
+    }
+    if (strcasecmp($t, 'http://openid.net/signon/2.0') == 0) {
+      return TRUE;
+    }
+  }
+  return FALSE;
+}
+
 function _xrds_cdata(&$parser, $data) {
   global $xrds_open_elements, $xrds_services, $xrds_current_service;
   $path = strtoupper(implode('/', $xrds_open_elements));
@@ -53,6 +76,7 @@
       $xrds_current_service['uri'] = $data;
       break;
     case 'XRDS/XRD/SERVICE/DELEGATE':
+      error_log("delegate: $data");
       $xrds_current_service['delegate'] = $data;
       break;
   }
@@ -66,4 +90,4 @@
   }
 
   return $name;
-}
\ No newline at end of file
+}
