Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • Bench
  • CommandLineBuilder
  • Pagination
  • Registry
  • Strings
  • Token
  • UserAgent

Exceptions

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