我在我的网站mydomain.com/forums上运行了一个论坛,该论坛使用了Vanilla Framework。使用Vanilla论坛对Cookie进行身份验证
但是,我希望在该论坛上注册的用户能够在我的网站的其他页面mydomain.com/blog上发表评论。所以我需要一种检查用户是否登录的方法,或者如果没有,则提供一种向他们提供Vanilla登录框的方法。
我的香草代码根本不在我的博客页面上,所以我需要一些额外的脚本。在大量的挖掘之后,我在网上找到了这个,https://gist.github.com/lincolnwebs/700805,如果登录的话,它给出了user_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)));
}
}
你使用什么框架来认证mydomain.com/blog? – Michael 2013-03-12 17:12:49
这基本上是问题的关键。我目前没有对博客进行任何身份验证,但我想使用Vanilla的用户管理软件,所以我想使用Vanilla在我的论坛之外对用户进行身份验证。 – Lars 2013-03-12 17:30:21