Browse Source

Change Csrf\verify() method + token rule in Validator to be able to set the token lifetime

Visman 2 years ago
parent
commit
4c78b07a95
2 changed files with 10 additions and 3 deletions
  1. 2 2
      app/Core/Csrf.php
  2. 8 1
      app/Core/Validator.php

+ 2 - 2
app/Core/Csrf.php

@@ -77,7 +77,7 @@ class Csrf
     /**
      * Проверка токена/хэша
      */
-    public function verify($token, string $marker, array $args = []): bool
+    public function verify($token, string $marker, array $args = [], int $lifetime = null): bool
     {
         $this->error = 'Bad token';
         $now         = \time();
@@ -90,7 +90,7 @@ class Csrf
             switch ($matches[1]) {
                 // токен
                 case 's':
-                    if ($matches[2] + self::TOKEN_LIFETIME < $now) {
+                    if ($matches[2] + ($lifetime ?? self::TOKEN_LIFETIME) < $now) {
                         // просрочен
                         $this->error = 'Expired token';
                     } elseif (

+ 8 - 1
app/Core/Validator.php

@@ -772,9 +772,16 @@ class Validator
             $args = [];
         }
 
+        if (\preg_match('%^([1-9]\d+):(.+)$%', $attr, $matches)) {
+            $lifetime = (int) $matches[1];
+            $attr     = $matches[2];
+        } else {
+            $lifetime = null;
+        }
+
         if (
             ! \is_string($value)
-            || ! $this->c->Csrf->verify($value, $attr, $args)
+            || ! $this->c->Csrf->verify($value, $attr, $args, $lifetime)
         ) {
             $this->addError($this->c->Csrf->getError() ?? 'Bad token', FORK_MESS_ERR);