User.class.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /*
  3. @nom: User
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de gestion des utilisateurs
  6. */
  7. class User extends MysqlEntity{
  8. const OTP_INTERVAL = 30;
  9. const OTP_DIGITS = 8;
  10. const OTP_DIGEST = 'sha1';
  11. private $otpControler;
  12. protected $id,$login,$password,$otpSecret;
  13. protected $TABLE_NAME = 'user';
  14. protected $CLASS_NAME = 'User';
  15. protected $object_fields =
  16. array(
  17. 'id'=>'key',
  18. 'login'=>'string',
  19. 'password'=>'string',
  20. 'otpSecret'=>'string',
  21. );
  22. function __construct(){
  23. parent::__construct();
  24. }
  25. function setId($id){
  26. $this->id = $id;
  27. }
  28. function isOtpSecretValid($otpSecret) {
  29. // Teste si la longueur est d'au moins 8 caractères
  30. // et en Base32: [A-Z] + [2-7]
  31. return is_string($otpSecret) && preg_match('/^[a-zA-Z2-7]{8,}$/', $otpSecret);
  32. }
  33. protected function getOtpControler() {
  34. if (empty($this->otpControler))
  35. $this->otpControler = new \OTPHP\TOTP($this->otpSecret, array('interval'=>self::OTP_INTERVAL, 'digits'=>self::OTP_DIGITS, 'digest'=>self::OTP_DIGEST));
  36. return $this->otpControler;
  37. }
  38. function getOtpKey() {
  39. $otp = $this->getOtpControler();
  40. return str_pad($otp->now(), $otp->digits, '0', STR_PAD_LEFT);
  41. }
  42. function exist($login,$password,$salt='',$otpEntered=Null){
  43. $userManager = new User();
  44. $user = $userManager->load(array('login'=>$login,'password'=>User::encrypt($password,$salt)));
  45. if (false!=$user) {
  46. $otpSecret = $user->otpSecret;
  47. global $configurationManager;
  48. switch (True) {
  49. case !$configurationManager->get('otpEnabled'):
  50. case empty($otpSecret) && empty($otpEntered):
  51. // Pas d'OTP s'il est désactivé dans la configuration où s'il n'est pas demandé et fourni.
  52. return $user;
  53. }
  54. $otp = $user->getOtpControler();
  55. if ($otp->verify($otpEntered) || $otp->verify($otpEntered, time()-10)) {
  56. return $user;
  57. }
  58. }
  59. return false;
  60. }
  61. function get($login){
  62. $userManager = new User();
  63. return $userManager->load(array('login'=>$login,));
  64. }
  65. function getToken() {
  66. assert('!empty($this->password)');
  67. assert('!empty($this->login)');
  68. return sha1($this->password.$this->login);
  69. }
  70. static function existAuthToken($auth=null){
  71. $result = false;
  72. $userManager = new User();
  73. $users = $userManager->populate('id');
  74. $phpAuth = strtolower(@$_SERVER['PHP_AUTH_USER']);
  75. if (empty($auth)) $auth = @$_COOKIE['leedStaySignedIn'];
  76. foreach($users as $user){
  77. if ($user->getToken()==$auth || strtolower($user->login)===$phpAuth){
  78. $result = $user;
  79. break;
  80. }
  81. }
  82. return $result;
  83. }
  84. function setStayConnected() {
  85. ///@TODO: set the current web directory, here and on del
  86. setcookie('leedStaySignedIn', $this->getToken(), time()+31536000);
  87. }
  88. static function delStayConnected() {
  89. setcookie('leedStaySignedIn', '', -1);
  90. }
  91. function getId(){
  92. return $this->id;
  93. }
  94. function getLogin(){
  95. return $this->login;
  96. }
  97. function setLogin($login){
  98. $this->login = $login;
  99. }
  100. function getPassword(){
  101. return $this->password;
  102. }
  103. function setPassword($password,$salt=''){
  104. $this->password = User::encrypt($password,$salt);
  105. }
  106. function getOtpSeed(){
  107. return $this->otpSecret;
  108. }
  109. function setOtpSeed($otpSecret){
  110. return $this->otpSecret = $otpSecret;
  111. }
  112. function resetPassword($resetPassword, $salt=''){
  113. $this->setPassword($resetPassword, $salt);
  114. $this->otpSecret = '';
  115. $this->save();
  116. }
  117. static function encrypt($password, $salt=''){
  118. return sha1($password.$salt);
  119. }
  120. static function generateSalt() {
  121. return ''.mt_rand().mt_rand();
  122. }
  123. }
  124. ?>