Overview

Packages

  • Auth
  • Config
  • Controller
  • Date
  • Db
  • Feed
    • Abstract
    • Writers
  • File
    • Decorator
  • Form
    • Element
  • Image
  • Log
    • Writer
  • Net
    • Exception
  • None
  • PHP
  • PHPMailer
  • Session
  • Util
  • Validate
    • Validator
  • Zend
    • Registry

Classes

  • Util_Bench
  • Util_ErrorHandler
  • Util_Pagination
  • Util_String
  • Util_Token
  • Util_UserAgent

Exceptions

  • Util_ExceptionHandler
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  * @version $Revision: 276 $
 12:  */
 13: 
 14: /**
 15:  * Classe de génération de token pour se prémunire des attaques CSRF
 16:  * 
 17:  * @category Pry
 18:  * @package Util
 19:  * @version 1.1.0
 20:  * @author Olivier ROGER <oroger.fr>
 21:  *       
 22:  */
 23: class Util_Token
 24: {
 25: 
 26:     /**
 27:      * Type d'erreur retournée à la vérification du token
 28:      * 1 = Token non passé en paramètre
 29:      * 2 = token recu != token généré
 30:      * 3 = token expiré
 31:      * @var int
 32:      */
 33:     static public $error = 0;
 34: 
 35:     /**
 36:      * Génère un token et le stocke en session
 37:      *
 38:      * @param int $ttl Durée de vie du token en minute
 39:      * @return string
 40:      */
 41:     static public function genToken($ttl = 15)
 42:     {
 43:         if (!isset($_SESSION))
 44:             session_start();
 45: 
 46:         $token  = hash('sha1', uniqid(rand(), true));
 47:         $rand   = rand(1, 20);
 48:         //Sha1  = 40 caractères => 20 de longeur max
 49:         $token  = substr($token, $rand, 20);
 50:         $ttl    *=60;
 51:         $_SESSION['csrf_protect']           = array();
 52:         $_SESSION['csrf_protect']['ttl']    = time() + $ttl;
 53:         $_SESSION['csrf_protect']['token']  = $token;
 54:         return $token;
 55:     }
 56: 
 57:     /**
 58:      * Récupère le token
 59:      *
 60:      * @return string
 61:      */
 62:     static public function getToken()
 63:     {
 64:         if (isset($_SESSION['csrf_protect']) && !empty($_SESSION['csrf_protect']))
 65:             return $_SESSION['csrf_protect']['token'];
 66:         else
 67:             throw new UnexpectedValueException('No token available');
 68:     }
 69: 
 70:     /**
 71:      * Récupère le timestamp de durée de vie
 72:      *
 73:      * @return int
 74:      */
 75:     static public function getTTL()
 76:     {
 77:         if (isset($_SESSION['csrf_protect']) && !empty($_SESSION['csrf_protect']))
 78:             return $_SESSION['csrf_protect']['ttl'];
 79:         else
 80:             throw new UnexpectedValueException('No token available');
 81:     }
 82: 
 83:     /**
 84:      * Vérifie la validité du token
 85:      *
 86:      * @return boolean
 87:      */
 88:     static public function checkToken()
 89:     {
 90:         if (!isset($_SESSION))
 91:             throw new Exception('Can\'t check token if there is no session available');
 92: 
 93:         if (isset($_REQUEST['csrf_protect']) && !empty($_REQUEST['csrf_protect']))
 94:         {
 95:             if ($_REQUEST['csrf_protect'] == $_SESSION['csrf_protect']['token'])
 96:             {
 97:                 if ($_SESSION['csrf_protect']['ttl'] - time() > 0)
 98:                 {
 99:                     return true;
100:                 }
101:                 else
102:                 {
103:                     self::$error = 3;
104:                 }
105:             }
106:             else
107:             {
108:                 self::$error = 2;
109:             }
110:         }
111:         else
112:         {
113:             self::$error = 1;
114:         }
115:         return false;
116:     }
117: 
118:     /**
119:      * Retourn le code erreur
120:      *
121:      * @return int
122:      */
123:     static public function getError()
124:     {
125:         return self::$error;
126:     }
127: 
128: }
129: 
130: ?>
Pry Framework API documentation generated by ApiGen 2.6.1