1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
<?php
namespace Icinga\Module\Businessprocess\Web\Form;
class CsrfToken
{
/**
* Check whether the given token is valid
*
* @param string $token Token
*
* @return bool
*/
public static function isValid($token)
{
if (strpos($token, '|') === false) {
return false;
}
list($seed, $token) = explode('|', $token);
if (!is_numeric($seed)) {
return false;
}
return $token === hash('sha256', self::getSessionId() . $seed);
}
/**
* Create a new token
*
* @return string
*/
public static function generate()
{
$seed = mt_rand();
$token = hash('sha256', self::getSessionId() . $seed);
return sprintf('%s|%s', $seed, $token);
}
/**
* Get current session id
*
* TODO: we should do this through our App or Session object
*
* @return string
*/
protected static function getSessionId()
{
return session_id();
}
}
|