Authentication 使用香草论坛通过cookie验证用户

Authentication 使用香草论坛通过cookie验证用户,authentication,cookies,Authentication,Cookies,我在我的网站mydomain.com/forums上运行一个论坛,它使用香草框架 但是,我希望在该论坛上注册的用户能够在我网站的其他页面上留言,网址是mydomain.com/blog。因此,我需要一种检查用户是否登录的方法,或者如果没有,则需要一种向用户提供普通登录框的方法 我的普通代码根本不在我的博客页面上,所以我需要一些额外的脚本。经过大量挖掘,我在网上找到了这个,如果用户登录,它会给出用户id,如果未登录,它会给出0。因此,它似乎工作得很好,因为我不必包含整个香草框架 脚本似乎正在检查用

我在我的网站mydomain.com/forums上运行一个论坛,它使用香草框架

但是,我希望在该论坛上注册的用户能够在我网站的其他页面上留言,网址是mydomain.com/blog。因此,我需要一种检查用户是否登录的方法,或者如果没有,则需要一种向用户提供普通登录框的方法

我的普通代码根本不在我的博客页面上,所以我需要一些额外的脚本。经过大量挖掘,我在网上找到了这个,如果用户登录,它会给出用户id,如果未登录,它会给出0。因此,它似乎工作得很好,因为我不必包含整个香草框架

脚本似乎正在检查用户浏览器上cookie的值。有人可以伪造cookie的价值并访问某人的帐户吗

作为一个相对的新手,有人能解释一下这是否是验证用户登录的安全方法吗?我花了很长时间才找到这个,而且它似乎被隐藏起来/没有公开。此外,该脚本并非100%完美,因为它在静态函数中使用$this

谢谢

<?php
/**
 * @copyright Vanilla Forums Inc.
 * @license GNU GPL2
 */

/**
 * Instantiating this class will store current user's ID from cookie as $this->UserID.
 */
class VanillaIdentity {

   # Copy these from Vanilla config
   public $CookieName = 'Vanilla';
   public $CookieSalt = ''; 
   public $CookieHashMethod = 'md5';
   public $UserID = 0;

   /**
    * Returns the unique id assigned to the user in the database (retrieved
    * from the session cookie if the cookie authenticates) or FALSE if not
    * found or authentication fails.
    *
    * @return int
    */
   public function __construct() {         
      if (!$this->_CheckCookie($this->CookieName)) return 0;

      list($UserID, $Expiration) = $this->GetCookiePayload($this->CookieName);

      if (!is_numeric($UserID) || $UserID < -2) // allow for handshake special id
         $this->UserID = 0;
      else
         $this->UserID = $UserID;
   }

   public static function GetCookiePayload($CookieName) {
      if (!self::CheckCookie($CookieName)) return FALSE;

      $Payload = explode('|', $_COOKIE[$CookieName]);

      // Get rid of check fields like HashKey, HMAC and Time
      array_shift($Payload);
      array_shift($Payload);
      array_shift($Payload);

      return $Payload;
   }

   protected function _CheckCookie($CookieName) {
      return self::CheckCookie($CookieName);
   }

   public static function CheckCookie($CookieName) {

      if (empty($_COOKIE[$CookieName])) {
         return FALSE;
      }

      $CookieHashMethod = $this->CookieHashMethod;
      $CookieSalt = $this->CookieSalt;

      $CookieData = explode('|', $_COOKIE[$CookieName]);
      if (count($CookieData) < 5) {
         return FALSE;
      }

      list($HashKey, $CookieHash, $Time, $UserID, $Expiration) = $CookieData;
      if ($Expiration < time() && $Expiration != 0) {
         return FALSE;
      }

      $Key = self::_Hash($HashKey, $CookieHashMethod, $CookieSalt);
      $GeneratedHash = self::_HashHMAC($CookieHashMethod, $HashKey, $Key);

      if ($CookieHash != $GeneratedHash) {
         return FALSE;
      }

      return TRUE;
   }

   /**
    * Returns $this->_HashHMAC with the provided data, the default hashing method
    * (md5), and the server's COOKIE.SALT string as the key.
    *
    * @param string $Data The data to place in the hash.
    */
   protected static function _Hash($Data, $CookieHashMethod, $CookieSalt) {
      return Gdn_CookieIdentity::_HashHMAC($CookieHashMethod, $Data, $CookieSalt);
   }

   /**
    * Returns the provided data hashed with the specified method using the
    * specified key.
    *
    * @param string $HashMethod The hashing method to use on $Data. Options are MD5 or SHA1.
    * @param string $Data The data to place in the hash.
    * @param string $Key The key to use when hashing the data.
    */
   protected static function _HashHMAC($HashMethod, $Data, $Key) {
      $PackFormats = array('md5' => 'H32', 'sha1' => 'H40');

      if (!isset($PackFormats[$HashMethod]))
         return false;

      $PackFormat = $PackFormats[$HashMethod];
      // this is the equivalent of "strlen($Key) > 64":
      if (isset($Key[63]))
         $Key = pack($PackFormat, $HashMethod($Key));
      else
         $Key = str_pad($Key, 64, chr(0));

      $InnerPad = (substr($Key, 0, 64) ^ str_repeat(chr(0x36), 64));
      $OuterPad = (substr($Key, 0, 64) ^ str_repeat(chr(0x5C), 64));

      return $HashMethod($OuterPad . pack($PackFormat, $HashMethod($InnerPad . $Data)));
   }

}

我不是普通的JavaScript专家。我非常了解HTTP身份验证和SSO,我将尝试澄清您的两个问题

首先,在HTTP身份验证之后,应用程序将用户cookie设置为浏览器。 在下一次请求时,浏览器向服务器发送cookie。请注意,设置cookie期间使用的路径很重要。如果路径为“/”,浏览器会将cookie发送到服务器上的所有应用程序

cookie可能被劫持并用于访问应用程序。请注意,黑客可能不会解密cookie–只需使用它。有关详细信息,请参阅以下链接:。要防止会话劫持,可以使用HTTPS

为了解决你的问题,你需要 1) 对mydomain.com/forums和mydomain.com/blog使用相同的用户存储库 在这种情况下,两个应用程序的用户“bob”将是同一个用户。 2) 要确保将cookie设置为路径“/” 在这种情况下,浏览器会向mydomain.com/forums和mydomain.com/blog发送cookie

我发现以下香草插件-它可能完全解决您的问题:


希望有帮助。如果您需要任何其他澄清,请发表评论。

您使用什么框架来验证mydomain.com/blog?这基本上是问题的关键。我目前没有任何博客身份验证,但我想使用Vanilla的用户管理软件,所以我想使用Vanilla来验证mydomain.com/blog的用户同意。此外,即使您可以在不解密cookie的情况下劫持会话,也必须对其进行加密以防止其他攻击。VanillaIdentity和SingleSingOn(jsConnect)都使用md5(默认)或者sha1。两者都是。因此,您可以调整库以使用scrypt或bcrypt,或者将它们配置为使用sha1,并希望达到最佳状态。感谢您的回答。使用scrpt或bcrypt劫持cookie安全吗?保护cookie劫持的最安全的方法是使用HTTPS