Dotclear


Ignore:
Timestamp:
12/19/17 17:27:59 (8 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Use PHP 5.5+ new password functions, closes #2182

Warnings:

  • $core->auth->crypt($pwd) doesn't return twice the same result for a single $pwd, so if you need this old behaviour use the $core->auth->cryptLegacy($pwd) instead.
  • $core->auth->checkPassword($pwd) must be used with an uncrypted password string as argument.
  • if you need a unique UID/key, use http::browserUID(DC_MASTER_KEY.$core->auth->userID().$core->auth->cryptLegacy($core->auth->userID())). (may be refined in future)
File:
1 edited

Legend:

Unmodified
Added
Removed
  • inc/core/class.dc.auth.php

    r3535 r3627  
    121121          if ($pwd != '') 
    122122          { 
    123                if ($this->crypt($pwd) != $rs->user_pwd) { 
    124                     sleep(rand(2,5)); 
    125                     return false; 
     123               $rehash = false; 
     124               if (password_verify($pwd,$rs->user_pwd)) { 
     125                    // User password ok 
     126                    if (password_needs_rehash($rs->user_pwd,PASSWORD_DEFAULT)) { 
     127                         $rs->user_pwd = $this->crypt($pwd); 
     128                         $rehash = true; 
     129                    } 
     130               } else { 
     131                    // Check if pwd still stored in old fashion way 
     132                    $ret = password_get_info($rs->user_pwd); 
     133                    if (is_array($ret) && isset($ret['algo']) && $ret['algo'] == 0) { 
     134                         // hash not done with password_hash() function, check by old fashion way 
     135                         if (crypt::hmac(DC_MASTER_KEY,$pwd,DC_CRYPT_ALGO) == $rs->user_pwd) { 
     136                              // Password Ok, need to store it in new fashion way 
     137                              $rs->user_pwd = $this->crypt($pwd); 
     138                              $rehash = true; 
     139                         } else { 
     140                              // Password KO 
     141                              sleep(rand(2,5)); 
     142                              return false; 
     143                         } 
     144                    } else { 
     145                         // Password KO 
     146                         sleep(rand(2,5)); 
     147                         return false; 
     148                    } 
     149               } 
     150               if ($rehash) { 
     151                    // Store new hash in DB 
     152                    $cur = $this->con->openCursor($this->user_table); 
     153                    $cur->user_pwd = (string) $rs->user_pwd; 
     154                    $cur->update("WHERE user_id = '".$rs->user_id."'"); 
    126155               } 
    127156          } 
    128157          elseif ($user_key != '') 
    129158          { 
    130                if (http::browserUID(DC_MASTER_KEY.$rs->user_id.$rs->user_pwd) != $user_key) { 
     159               if (http::browserUID(DC_MASTER_KEY.$rs->user_id.$this->cryptLegacy($rs->user_id)) != $user_key) { 
    131160                    return false; 
    132161               } 
     
    172201     public function crypt($pwd) 
    173202     { 
     203          return password_hash($pwd,PASSWORD_DEFAULT); 
     204     } 
     205 
     206     /** 
     207      * This method crypt given string (password, session_id, …). 
     208      * 
     209      * @param string $pwd string to be crypted 
     210      * @return string crypted value 
     211      */ 
     212     public function cryptLegacy($pwd) 
     213     { 
    174214          return crypt::hmac(DC_MASTER_KEY,$pwd,DC_CRYPT_ALGO); 
    175215     } 
     
    184224     { 
    185225          if (!empty($this->user_info['user_pwd'])) { 
    186                return $pwd == $this->user_info['user_pwd']; 
     226               return password_verify($pwd,$this->user_info['user_pwd']); 
    187227          } 
    188228 
Note: See TracChangeset for help on using the changeset viewer.

Sites map