<?php

namespace BNETDocs\Libraries;

use \CarlBennett\MVC\Libraries\Common;

class CSRF {

  const TTL = 300;

  private function __construct() {}

  public static function generate($id, $ttl = self::TTL) {
    $id = (int) $id;
    $t  = microtime(true);
    $s  = mt_rand();
    $v  = hash("sha256", $t * $s * $id);
    Common::$cache->set("bnetdocs-csrf-" . $id, $v, $ttl);
    return $v;
  }

  public static function invalidate($id) {
    $key = "bnetdocs-csrf-" . (int) $id;
    return Common::$cache->set($key, "", 1);
  }

  public static function validate($id, $token) {
    $key = "bnetdocs-csrf-" . (int) $id;
    $val = Common::$cache->get($key);
    if (is_null($val)) return false;
    return ($val === $token);
  }

}

<?php

namespace BNETDocs\Libraries;

use \CarlBennett\MVC\Libraries\Common;

class CSRF {

  const TTL = 900; // 15 minutes

  private function __construct() {}

  public static function generate($id, $ttl = self::TTL) {
    $id = (int) $id;
    $t  = microtime(true);
    $s  = mt_rand();
    $v  = hash("sha256", $t * $s * $id);
    Common::$cache->set("bnetdocs-csrf-" . $id, $v, $ttl);
    return $v;
  }

  public static function invalidate($id) {
    $key = "bnetdocs-csrf-" . (int) $id;
    return Common::$cache->set($key, "", 1);
  }

  public static function validate($id, $token) {
    $key = "bnetdocs-csrf-" . (int) $id;
    $val = Common::$cache->get($key);
    if (is_null($val)) return false;
    return ($val === $token);
  }

}

Adjust default anti-csrf token ttl to 15 minutes
Adjust default anti-csrf token ttl to 15 minutes
