style: auto-fix 1340 phpcs PSR-12 violations via phpcbf; exclude MissingNamespace and SideEffects
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* CSRF Protection Middleware
|
||||
* Generates and validates CSRF tokens for all state-changing operations
|
||||
*/
|
||||
class CsrfMiddleware {
|
||||
class CsrfMiddleware
|
||||
{
|
||||
private static string $tokenName = 'csrf_token';
|
||||
private static string $tokenTime = 'csrf_token_time';
|
||||
private static int $tokenLifetime = 3600; // 1 hour
|
||||
@@ -11,7 +13,8 @@ class CsrfMiddleware {
|
||||
/**
|
||||
* Generate a new CSRF token
|
||||
*/
|
||||
public static function generateToken(): string {
|
||||
public static function generateToken(): string
|
||||
{
|
||||
$_SESSION[self::$tokenName] = bin2hex(random_bytes(32));
|
||||
$_SESSION[self::$tokenTime] = time();
|
||||
return $_SESSION[self::$tokenName];
|
||||
@@ -20,7 +23,8 @@ class CsrfMiddleware {
|
||||
/**
|
||||
* Get current CSRF token, regenerate if expired
|
||||
*/
|
||||
public static function getToken(): string {
|
||||
public static function getToken(): string
|
||||
{
|
||||
if (!isset($_SESSION[self::$tokenName]) || self::isTokenExpired()) {
|
||||
return self::generateToken();
|
||||
}
|
||||
@@ -30,7 +34,8 @@ class CsrfMiddleware {
|
||||
/**
|
||||
* Validate CSRF token (constant-time comparison)
|
||||
*/
|
||||
public static function validateToken(string $token): bool {
|
||||
public static function validateToken(string $token): bool
|
||||
{
|
||||
if (!isset($_SESSION[self::$tokenName])) {
|
||||
return false;
|
||||
}
|
||||
@@ -52,14 +57,16 @@ class CsrfMiddleware {
|
||||
*
|
||||
* @return string The new token
|
||||
*/
|
||||
public static function rotateToken(): string {
|
||||
public static function rotateToken(): string
|
||||
{
|
||||
return self::generateToken();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if token is expired
|
||||
*/
|
||||
private static function isTokenExpired(): bool {
|
||||
private static function isTokenExpired(): bool
|
||||
{
|
||||
return !isset($_SESSION[self::$tokenTime]) ||
|
||||
(time() - $_SESSION[self::$tokenTime]) > self::$tokenLifetime;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user