Modules

Gleez_CSRF

Cross-Site Request Forgery helper.

package
Gleez
category
CSRF
author
Sandeep Sangamreddi - Gleez
copyright
© 2012 Gleez Technologies
license
http://gleezcms.org/license

Class declared in MODPATH/gleez/classes/gleez/csrf.php on line 11.

Constants

  • None

Properties

Properties

public static integer $csrf_ttl

Token time to live in seconds, 30 minutes

integer 1800

Methods

public static key( ) (defined in Gleez_CSRF)

User specefic key used to generate unqiue tokens.

The user specefic private key.

Return Values

Source Code

static function key()
{
        $token  = Session::instance()->id();
        $secret = self::_private_key();
        return sha1($secret . $token);
}

public static token( [ string $id = string(0) "" , string $action = string(0) "" , integer $time = integer 0 ] ) (defined in Gleez_CSRF)

Get CSRF token

Parameters

  • string $id = string(0) "" - Custom token id, e.g. uid
  • string $action = string(0) "" - Optional action
  • integer $time = integer 0 - Used only internally

Return Values

  • string

Source Code

public static function token($id = '', $action = '', $time = 0)
{
        // Get id string for token, could be uid or ip etc
        if (!$id) $id = Request::$client_ip;

        // Get time to live
        if (!$time) $time = ceil(time() / self::$csrf_ttl);

        return sha1($time . self::key() . $id . $action);
}

public static valid( [ string $token = bool FALSE , string $action = string(0) "" , string $id = string(0) "" ] ) (defined in Gleez_CSRF)

Validate CSRF token

Parameters

  • string $token = bool FALSE - $token
  • string $action = string(0) "" - $id Custom token id, e.g. uid
  • string $id = string(0) "" - $action Optional action

Return Values

  • boolean

Source Code

public static function valid($token = false, $action = '', $id = '')
{
token and action from Form POST
        if (!$token)  $token  = Arr::get($_REQUEST, '_token');
        if (!$action) $action = Arr::get($_REQUEST, '_action');

        // Get time to live
        $time = ceil(time() / self::$csrf_ttl);

        // Check token validity
        return ($token === self::token($id, $action, $time) || $token === self::token($id, $action, $time - 1));
}

private static _private_key( ) (defined in Gleez_CSRF)

Ensure the private key variable used to generate tokens is set.

The private key.

Return Values

Source Code

private static function _private_key()
{
g = Kohana::$config->load('site');

        if ( !($key = $config->get('gleez_private_key')) )
        {
                $key = sha1(uniqid(mt_rand(), true)) . md5(uniqid(mt_rand(), true));
                $config->set('gleez_private_key', $key);
        }
        
        return $key;
}
Documentation comments powered by Disqus