123 lines
3.1 KiB
PHP
123 lines
3.1 KiB
PHP
<?php
|
|
|
|
class stSecureToken
|
|
{
|
|
const DB_TOKEN_LIFETIME = 120;
|
|
|
|
protected static $sessionToken = null;
|
|
|
|
/**
|
|
* Usuwa ważny token bezpieczeństwa
|
|
*
|
|
* @param string $tokenHash Token bezpieczeństwa
|
|
* @return void
|
|
*/
|
|
public static function invalidateDBToken(string $tokenHash)
|
|
{
|
|
$token = SecurityTokenPeer::retrieveByPK($tokenHash);
|
|
|
|
if (null !== $token)
|
|
{
|
|
$token->delete();
|
|
}
|
|
}
|
|
/**
|
|
* Weryfikuje czy token bezpieczeństwa jest wciąż ważny
|
|
*
|
|
* @param string $tokenHash Token bezpieczeństwa
|
|
* @param bool $remove Automatycznie usuwa token bezpieczenstwa po weryfikacji
|
|
* @return bool
|
|
*/
|
|
public static function isDBTokenValid(string $tokenHash, bool $remove = true)
|
|
{
|
|
$token = SecurityTokenPeer::retrieveByPK($tokenHash);
|
|
|
|
if (null === $token)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if ($remove)
|
|
{
|
|
$token->delete();
|
|
}
|
|
|
|
return $token->isValid();
|
|
}
|
|
|
|
/**
|
|
* Tworzy nowy token bezpieczeństwa
|
|
*
|
|
* @param null|int $lifeTime Określa czas życia w sekundach (domyślnie 120 sekund). Przekazanie wartości null tworzy token
|
|
* @return string
|
|
*/
|
|
public static function createDBToken(?int $lifeTime = self::DB_TOKEN_LIFETIME): string
|
|
{
|
|
$token = new SecurityToken();
|
|
|
|
if (null !== $lifeTime)
|
|
{
|
|
$token->setExpireAt(time() + $lifeTime);
|
|
}
|
|
|
|
$token->setId(self::createGuid(true));
|
|
$token->save();
|
|
|
|
return $token->getId();
|
|
}
|
|
|
|
/**
|
|
* Weryfikuje czy token jest prawidłowy
|
|
*
|
|
* @param array $parameters Przekazane parametry podczas tworzenia tokenu bezpieczeństwa
|
|
* @param string $token Wygenerowany token bezpieczeństwa
|
|
* @return bool
|
|
*/
|
|
public static function isValidToken(array $parameters, string $token): bool
|
|
{
|
|
return self::generate($parameters) == $token;
|
|
}
|
|
|
|
/**
|
|
* Generuje token bezpieczeństwa na podstawie przekazanych parametrów
|
|
*
|
|
* @param array $parameters
|
|
* @return string
|
|
*/
|
|
public static function generate(array $parameters): string
|
|
{
|
|
$shop_hash = stConfig::getInstance('stRegister')->get('shop_hash');
|
|
|
|
return sha1(self::stringifyTokenParameters($parameters).$shop_hash);
|
|
}
|
|
|
|
public static function createGuid(bool $digitsOnly = false): string
|
|
{
|
|
$bytes = random_bytes(16);
|
|
$bytes[6] = chr(ord($bytes[6]) & 0x0f | 0x40);
|
|
$bytes[8] = chr(ord($bytes[8]) & 0x3f | 0x80);
|
|
|
|
$hex = bin2hex($bytes);
|
|
|
|
return !$digitsOnly ? vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split($hex, 4)) : $hex;
|
|
}
|
|
|
|
private static function stringifyTokenParameters(array $parameters): string
|
|
{
|
|
$results = array();
|
|
|
|
foreach ($parameters as $k => $v)
|
|
{
|
|
if (is_array($v))
|
|
{
|
|
$results[(string)$k] = self::stringify($v);
|
|
}
|
|
else
|
|
{
|
|
$results[(string)$k] = (string)$v;
|
|
}
|
|
}
|
|
|
|
return serialize($results);
|
|
}
|
|
} |