Always redirect to the canonic URL before discovery can occur
authorAymeric Augustin <aymeric.augustin@m4x.org>
Tue, 28 Oct 2008 22:28:59 +0000 (23:28 +0100)
committerAymeric Augustin <aymeric.augustin@m4x.org>
Tue, 28 Oct 2008 22:28:59 +0000 (23:28 +0100)
(this is a good feature)

Add an alias for the openid page based on the melix alias
(this might be a bad thing, because it provides public alias => realname resolution; the feature is inactive)

modules/openid.php
modules/openid/openid.inc.php

index fc935b5..6c3c0e5 100644 (file)
@@ -51,6 +51,7 @@ class OpenidModule extends PLModule
             'openid/trust'      => $this->make_hook('trust', AUTH_COOKIE),
             'openid/idp_xrds'   => $this->make_hook('idp_xrds', AUTH_PUBLIC),
             'openid/user_xrds'  => $this->make_hook('user_xrds', AUTH_PUBLIC),
+//            'openid/melix'      => $this->make_hook('melix', AUTH_PUBLIC),
         );
     }
 
@@ -198,6 +199,16 @@ class OpenidModule extends PLModule
         $page->assign('uri', get_openid_url());
     }
 
+    function handler_melix(&$page, $x = null)
+    {
+        $this->load('openid.inc.php');
+        $user = get_user_by_alias($x);
+
+        // This will redirect to the canonic URL, which was not used
+        // if this hook was triggered
+        return render_discovery_page(&$page, $user);
+    }
+
     //--------------------------------------------------------------------//
 
     function render_discovery_page(&$page, $user)
@@ -208,6 +219,16 @@ class OpenidModule extends PLModule
             pl_redirect('Xorg/OpenId');
         }
 
+        // Redirect to the canonic URL if we are using an alias
+        // There might be a risk of redirection loop here
+        // if $_SERVER was not exactly what we expect
+        $current_url = 'http' . (empty($_SERVER['HTTPS']) ? '' : 's') . '://'
+                       . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
+        $canonic_url = get_user_openid_url($user);
+        if ($current_url != $canonic_url) {
+            http_redirect($canonic_url);
+        }
+
         // Include X-XRDS-Location response-header for Yadis discovery
         header('X-XRDS-Location: ' . get_user_xrds_url($user));
 
@@ -254,4 +275,4 @@ class OpenidModule extends PLModule
 }
 
 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
-?>
+?>
\ No newline at end of file
index 5271684..2ab06ff 100644 (file)
@@ -48,6 +48,24 @@ function get_user($x) {
 
 }
 
+function get_user_by_alias($x) {
+    if (is_null($x)) {
+        return null;
+    }
+    // TODO such a function should probably be provided in the User class
+    // or at least not here
+    $res = XDB::query('SELECT  u.user_id
+                         FROM  auth_user_md5 AS u
+                   INNER JOIN  aliases       AS a ON (a.id = u.user_id AND type != \'homonyme\')
+                        WHERE  a.alias = {?} AND u.perms IN(\'admin\', \'user\')',
+                               $x);
+    if (list($uid) = $res->fetchOneRow()) {
+        $user = User::getSilent($uid);
+    }
+    return $user ? $user : null;
+
+}
+
 function get_user_openid_url($user)
 {
     if (is_null($user)) {