title: Mediawiki authenication replacement showing "Login Required" instead of signing user into wiki
body: <p>I'm fairly to MediaWiki and needed a way to automatically log users in after they authenticated to a central server (which creates a session and cookie for applications to use).</p>
  
  <p>I wrote a custom authentication extension based off of the <a href="http://www.mediawiki.org/wiki/Extension%3aLDAP_Authentication" rel="nofollow">LDAP Authentication</a> extension and a <a href="http://www.mediawiki.org/wiki/Category%3aUser_identity_extensions" rel="nofollow">few others</a>. The extension simply needs to read some session data to create or update a user and then log them in automatically. All the authentication is handled externally. A user would not be able to even access the wiki website without logging in externally.</p>
  
  <p>This extension was placed into production which replaced the old standard MediaWiki authentication system. I also merged user accounts to prepare for the change. By default, a user must be logged in to view, edit, or otherwise do anything in the wiki.</p>
  
  <p>My problem is that I found if a user had previously used the built-in MediaWiki authentication system and returned to the wiki, my extension would attempt to auto-login the user, however, they would see a "Login Required" page instead of the page they requested like they were an anonymous user. If the user then refreshed the page, they would be able to navigate, edit, etc. </p>
  
  <p>From what I can tell, this issue resolves itself after the UserID cookie is reset or created fresh (but has been known to strangely come up sometimes). To replicate, if there is an older User ID in the "USERID" cookie, the user is shown the "Login Required" page which is a poor user experience. Another way of showing this page is by removing the user account from the database and refreshing the wiki page. As a result, the user will again see the "Login Required" page.</p>
  
  <p>Does anyone know how I can use debugging to find out why MediaWiki thinks the user is not signed in when the cookies are set properly and all it takes is a page refresh?</p>
  
  <p>Here is my extension (simplified a little for this post):</p>
  
  <pre><code>&lt;?php
  
  $wgExtensionCredits['parserhook'][] = array (
      'name' =&gt; 'MyExtension',
      'author' =&gt; '',
  );
  
  if (!class_exists('AuthPlugin')) {
      require_once ( 'AuthPlugin.php' );
  }
  
  class MyExtensionPlugin extends AuthPlugin {
  
      function userExists($username) {
          return true;
      }
  
      function authenticate($username, $password) {
  
          $id = $_SESSION['id'];
          if($username = $id) {
            return true;
          } else {
            return false;
          }
  
      }
  
      function updateUser(&amp; $user) {
          $name = $user-&gt;getName();
          $user-&gt;load();
  
          $user-&gt;mPassword = ''; 
          $user-&gt;mNewpassword = '';
          $user-&gt;mNewpassTime = null;
  
          $user-&gt;setRealName($_SESSION['name']);
          $user-&gt;setEmail($_SESSION['email']);
          $user-&gt;mEmailAuthenticated = wfTimestampNow();
          $user-&gt;saveSettings();
  
          return true;
      }
  
      function modifyUITemplate(&amp; $template) { 
          $template-&gt;set('useemail', false); 
          $template-&gt;set('remember', false); 
          $template-&gt;set('create', false); 
          $template-&gt;set('domain', false); 
          $template-&gt;set('usedomain', false); 
      } 
  
      function autoCreate() {
          return true;
      }
  
      function disallowPrefsEditByUser() {
          return array (
              'wpRealName' =&gt; true,
              'wpUserEmail' =&gt; true,
              'wpNick' =&gt; true
          );
      }
  
      function allowPasswordChange() {
          return false;
      }
  
  
      function setPassword( $user, $password ) {
          return false;
      }
  
  
      function strict() {
          return true;
      }
  
      function initUser( &amp; $user ) {
      }
  
      function updateExternalDB( $user ) {
          return false;
      }
  
      function canCreateAccounts() {
          return false;
      }
  
      function addUser( $user, $password ) {
          return false;
      }
  
      function getCanonicalName( $username ) {
          return $username;
      }
  }
  
  
  function SetupAuthMyExtension() {
      global $wgHooks;
      global $wgAuth;
  
      $wgHooks['UserLoadFromSession'][] = 'Auth_MyExtension_autologin_hook';
      $wgHooks['UserLogoutComplete'][] = 'Auth_MyExtension_UserLogoutComplete';
      $wgHooks['PersonalUrls'][] = 'Auth_MyExtension_personalURL_hook';
      $wgAuth = new MyExtensionPlugin();
  }
  
  function Auth_MyExtension_autologin_hook($user, &amp;$return_user ) {
      global $wgUser;
      global $wgAuth;
      global $wgContLang;
  
      wfSetupSession();
  
      // Give us a user, see if we're around
      $tmpuser = new User() ;
      $rc = $tmpuser-&gt;newFromSession();
      $rc = $tmpuser-&gt;load();
  
      if( $rc &amp;&amp; $rc-&gt;isLoggedIn() ) {
          if ( $rc-&gt;authenticate($rc-&gt;getName(), '') ) {
              return true;
          } else {
              $rc-&gt;logout();
          }
      }
  
      $id = trim($_SESSION['id']);
      $name = ucfirst(trim($_SESSION['name']));
  
      if (empty($dsid)) {
          $result = false;    // Deny access
          return true;
      }
  
      $user = User::newFromName($dsid);
  
      if (0 == $user-&gt;getID() ) {
          // we have a new user to add...     
          $user-&gt;setName( $id);
          $user-&gt;addToDatabase();
          $user-&gt;setToken();
              $user-&gt;saveSettings();
  
          $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
          $ssUpdate-&gt;doUpdate();
  
      } else {
          $user-&gt;saveToCache();
      }
  
      // update email, real name, etc.
      $wgAuth-&gt;updateUser( $user );
  
      $result = true;
      // Go ahead and log 'em in
      $user-&gt;setToken();
      $user-&gt;saveSettings();
      $user-&gt;setupSession();
      $user-&gt;setCookies();    
      return true;
  
  }
  
  function Auth_MyExtension_personalURL_hook(&amp; $personal_urls, &amp; $title) {
      global $wgUser;
      unset( $personal_urls['mytalk'] );
      unset($personal_urls['Userlogin']);
      $personal_urls['userpage']['text'] = $wgUser-&gt;getRealName();
  
      foreach (array('login', 'anonlogin') as $k) {
          if (array_key_exists($k, $personal_urls)) {
              unset($personal_urls[$k]);
          }
      }
      return true;
  }
  
  
  function Auth_MyExtension_UserLogoutComplete(&amp;$user, &amp;$inject_html, $old_name) { 
      setcookie( $GLOBALS['wgCookiePrefix'] . '_session', '', time() - 3600, $GLOBALS['wgCookiePath']);
      setcookie( $GLOBALS['wgCookiePrefix'] . 'UserName', '', time() - 3600, $GLOBALS['wgCookiePath']);
      setcookie( $GLOBALS['wgCookiePrefix'] . 'UserID', '', time() - 3600, $GLOBALS['wgCookiePath']);
      setcookie( $GLOBALS['wgCookiePrefix'] . 'Token', '', time() - 3600, $GLOBALS['wgCookiePath']);
      return true;
  }
  
  
  ?&gt;
  </code></pre>
  
  <p>Here is part of my LocalSettings.php file:</p>
  
  <pre><code>#############################
  # Disallow Anonymous Access
  #############################
  $wgGroupPermissions['*']['read']            = false;
  $wgGroupPermissions['*']['edit']            = false;
  $wgGroupPermissions['*']['createpage']      = false;
  $wgGroupPermissions['*']['createtalk']      = false;
  $wgGroupPermissions['*']['createaccount']   = false;
  $wgShowIPinHeader = false; # For non-logged in users
  
  #############################
  # Extension: MyExtension
  #############################
  require_once("$IP/extensions/MyExtension.php");
  $wgAutoLogin = true;
  SetupAuthMyExtension();
  $wgDisableCookieCheck = true;
  </code></pre>