Ignore:
Timestamp:
Feb 15, 2006 3:30:50 AM (18 years ago)
Author:
scdev
Message:

fixed Auth_File

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Auth_File.inc.php

    r64 r65  
    55 *
    66 * @author  Quinn Comendant <quinn@strangecode.com>
    7  * @inspiration  Horde's Auth class <www.horde.org>
    8  * @version 1.0
     7 * @version 1.1
    98 */
     9
     10// Available encryption types for class Auth_SQL.
     11define('AUTH_ENCRYPT_MD5', 'md5');
     12define('AUTH_ENCRYPT_CRYPT', 'crypt');
     13define('AUTH_ENCRYPT_SHA1', 'sha1');
     14define('AUTH_ENCRYPT_PLAINTEXT', 'plaintext');
     15
    1016class Auth_File {
    1117
    1218    var $_params = array(
    13         'encryption_type' => 'des',
     19        'encryption_type' => AUTH_ENCRYPT_CRYPT,
    1420        'htpasswd_file' => null,
    1521        'login_timeout' => 21600, // 6 hours.
    1622        'idle_timeout' => 3600, // 1 hour.
    1723    );
    18     var $auths = array();
     24    var $_users = array();
    1925
    2026    /**
     
    3036
    3137        if (!empty($this->_params['htpasswd_file'])) {
    32             if (false === ($users = @file($this->_params['htpasswd_file']))) {
     38            if (false === ($users = file($this->_params['htpasswd_file']))) {
    3339                App::logMsg(sprintf(_("Could not read htpasswd file: %s"), $this->_params['htpasswd_file']), LOG_ERR, __FILE__, __LINE__);
    3440            }
     
    4349
    4450    /**
     51     * Set the params of an auth object.
     52     *
     53     * @param  array $params   Array of parameter keys and value to set.
     54     * @return bool true on success, false on failure
     55     */
     56    function setParam($params)
     57    {
     58        if (isset($params) && is_array($params)) {
     59            // Merge new parameters with old overriding only those passed.
     60            $this->_params = array_merge($this->_params, $params);
     61        }
     62    }
     63
     64    /**
     65     * Return the value of a parameter, if it exists.
     66     *
     67     * @access public
     68     * @param string $param        Which parameter to return.
     69     * @return mixed               Configured parameter value.
     70     */
     71    function getParam($param)
     72    {
     73        if (isset($this->_params[$param])) {
     74            return $this->_params[$param];
     75        } else {
     76            App::logMsg(sprintf('Parameter is not set: %s', $param), LOG_DEBUG, __FILE__, __LINE__);
     77            return null;
     78        }
     79    }
     80
     81    /**
     82     * Clear any authentication tokens in the current session. A.K.A. logout.
     83     *
     84     * @access public
     85     */
     86    function clearAuth()
     87    {
     88        $_SESSION['_auth_file'] = array('authenticated' => false);
     89    }
     90
     91    /**
    4592     * Find out if a set of login credentials are valid. Only supports
    4693     * htpasswd files with DES passwords right now.
    4794     *
    48      * @access private
    49      *
    50      * @param string $user_id      The user_id to check.
    51      * @param array $password      The password to compare to user_id.
     95     * @access public
     96     *
     97     * @param string $username      The username to check.
     98     * @param array $password      The password to compare to username.
    5299     *
    53100     * @return boolean  Whether or not the credentials are valid.
    54101     */
    55     function authenticate($user_id, $password)
    56     {
    57         if (empty($password)) {
    58             App::logMsg(_("No password provided for htpasswd authentication."), LOG_NOTICE, __FILE__, __LINE__);
     102    function authenticate($username, $password)
     103    {
     104        if ('' == trim($password)) {
     105            App::logMsg(_("No password provided for htpasswd authentication."), LOG_INFO, __FILE__, __LINE__);
    59106            return false;
    60107        }
    61108
    62         if (empty($this->_users[$user_id])) {
    63             App::logMsg(_("User ID provided does not exist."), LOG_NOTICE, __FILE__, __LINE__);
     109        if (!isset($this->_users[$username])) {
     110            App::logMsg(_("User ID provided does not exist."), LOG_INFO, __FILE__, __LINE__);
    64111            return false;
    65112        }
    66113
    67         $hash = $this->_encrypt($password, $this->_salt($user_id));
    68         if ($hash == $this->_users[$user_id]) {
     114        if ($this->_encrypt($password, $password) == $this->_users[$username]) {
    69115            return true;
    70116        } else {
     117            App::logMsg(sprintf('Authentication failed for user %s', $username), LOG_INFO, __FILE__, __LINE__);
    71118            return false;
    72119        }
     
    76123     * If user passes authentication create authenticated session.
    77124     *
    78      * @access private
    79      *
    80      * @param string $user_id     The user_id to check.
    81      * @param array $password     The password to compare to user_id.
     125     * @access public
     126     *
     127     * @param string $username     The username to check.
     128     * @param array $password     The password to compare to username.
    82129     *
    83130     * @return boolean  Whether or not the credentials are valid.
    84131     */
    85     function login($user_id, $password)
    86     {
    87         $user_id = trim($user_id);
     132    function login($username, $password)
     133    {
     134        $username = strtolower(trim($username));
    88135
    89136        $this->clearAuth();
    90137
    91         if ($this->authenticate($user_id, $password)) {
    92             $_SESSION['_auth'] = array(
     138        if ($this->authenticate($username, $password)) {
     139            $_SESSION['_auth_file'] = array(
    93140                'authenticated' => true,
    94                 'user_id' => $user_id,
    95                 'user_type' => 'admin',
    96                 'priv' => 'editor',
     141                'username' => $username,
    97142                'login_datetime' => date('Y-m-d H:i:s'),
    98143                'last_access_datetime' => date('Y-m-d H:i:s'),
     
    105150
    106151    /**
    107      * Clear any authentication tokens in the current session. A.K.A. logout.
    108      *
    109      * @access public
    110      */
    111     function clearAuth()
    112     {
    113         $_SESSION['_auth'] = array();
    114         $_SESSION['_auth']['authenticated'] = false;
    115     }
    116 
    117     /**
    118152     * Test if user has a currently logged-in session.
    119153     *  - authentication flag set to true
    120      *  - user_id not empty
     154     *  - username not empty
    121155     *  - total logged-in time is not greater than login_timeout
    122156     *  - idle time is not greater than idle_timeout
     
    127161    function isLoggedIn()
    128162    {
    129         if (isset($_SESSION['_auth'])) {
    130             if (true === $_SESSION['_auth']['authenticated']
    131             && !empty($_SESSION['_auth']['user_id'])
    132             && strtotime($_SESSION['_auth']['login_datetime']) > time() - $this->_params['login_timeout']
    133             && strtotime($_SESSION['_auth']['last_access_datetime']) > time() - $this->_params['idle_timeout']
    134             && $_SESSION['_auth']['remote_addr'] == getRemoteAddr()
     163        if (isset($_SESSION['_auth_file'])) {
     164            if (true === $_SESSION['_auth_file']['authenticated']
     165            && !empty($_SESSION['_auth_file']['username'])
     166            && strtotime($_SESSION['_auth_file']['login_datetime']) > time() - $this->_params['login_timeout']
     167            && strtotime($_SESSION['_auth_file']['last_access_datetime']) > time() - $this->_params['idle_timeout']
     168            && $_SESSION['_auth_file']['remote_addr'] == getRemoteAddr()
    135169            ) {
    136                 $_SESSION['_auth']['last_access_datetime'] = date('Y-m-d H:i:s');
     170                $_SESSION['_auth_file']['last_access_datetime'] = date('Y-m-d H:i:s');
    137171                return true;
    138             } else if (true === $_SESSION['_auth']['authenticated']) {
     172            } else if (true === $_SESSION['_auth_file']['authenticated']) {
    139173                App::raiseMsg(_("Your session has closed. You need to log-in again."), MSG_NOTICE, __FILE__, __LINE__);
    140174                $this->clearAuth();
     
    145179
    146180    /**
    147      * Test if user is of user_type 'admin'.
    148      *
    149      * @access public
    150      */
    151     function isAdmin()
    152     {
    153         if ($_SESSION['_auth']['user_type'] == 'admin') {
    154             return true;
    155         }
    156         return false;
    157     }
    158 
    159     /**
    160      * Redirect user to login page if they are not logged in.
    161      *
    162      * @access public
    163      */
    164     function requireAdminLogin()
    165     {
    166         if (!$this->isLoggedIn() || !$this->isAdmin()) {
    167             App::setBoomerangURL(absoluteMe());
    168             App::dieURL('/admin/login.php');
    169         }
    170     }
    171 
    172     /**
    173181     * Hash a given password according to the configured encryption
    174182     * type.
    175183     *
    176      * @param string $password  The password to encrypt.
    177      * @param string $salt      The salt to use, if needed.
     184     * @param string $password              The password to encrypt.
     185     * @param string $encrypted_password    The currently encrypted password to use as salt, if needed.
    178186     *
    179187     * @return string  The hashed password.
    180188     */
    181     function _encrypt($password, $salt=null)
     189    function _encrypt($password, $encrypted_password=null)
    182190    {
    183191        switch ($this->_params['encryption_type']) {
    184         case 'des' :
    185             if (isset($salt)) {
    186                 return crypt($password, $salt);
    187             } else {
    188                 return crypt($password);
    189             }
    190             break;
     192        case AUTH_ENCRYPT_PLAINTEXT :
     193            return $password;
     194            break;
     195
     196        case AUTH_ENCRYPT_SHA1 :
     197            return sha1($password);
     198            break;
     199
     200        case AUTH_ENCRYPT_MD5 :
     201            return md5($password);
     202            break;
     203
     204        case AUTH_ENCRYPT_CRYPT :
    191205        default :
    192             App::logMsg('Encryption type not found.', LOG_ERR, __FILE__, __LINE__);
    193         }
    194 
    195         return false;
    196     }
    197 
    198     /**
    199      * Get a salt for $user_id, or generate a new one.
    200      *
    201      * @return string  The salt.
    202      */
    203     function _salt($user_id)
    204     {
    205         switch ($this->_params['encryption_type']) {
    206         case 'des':
    207             if (!empty($this->_users[$user_id])) {
    208                 return substr($this->_users[$user_id], 0, 2);
    209             }
    210             break;
    211         default :
    212             App::logMsg('Encryption type not found.', LOG_ERR, __FILE__, __LINE__);
    213         }
    214 
    215         return '';
    216     }
    217 
    218 }
     206            return crypt($password, $encrypted_password);
     207            break;
     208        }
     209    }
     210
     211} // end class
    219212?>
Note: See TracChangeset for help on using the changeset viewer.