• home
  • forum
  • my
  • kt
  • download
  • Authenticate Users Using an LDAP Database

    Author: 2007-08-24 16:13:13 From:

    The following program was written for my company's time keeping system which utilizes an LDAP database for authentication and a MySQL database for time-keeping, account access control, etc.

    If a user has >= 5 unsuccessful logins, it locks the account for 5 minutes. Once a successful LDAP bind has occured, the $_SESSION vars are set and the authorization process is bypassed until the user logs out or the session expires.

    <?
    // file:        authorization.php
    // purpose: authorization modules
    class authorize {

            var $issuccessfulLogin;
            var $manuallyLocked;
            var $timeLocked;

            // purpose: constructor
            function authorize() {  }

            // purpose: bind with LDAP to determine LDAP authorization status
            function LDAP_authenticate( $username, $password, $domain) {
                    require('conf/ldap.conf.php');
                    $ds = @ldap_connect( $ldapconfig[ 'host' ], $ldapconfig[ 'port' ] );

                    if( ! $ds ) return false;

                    ldap_set_option( $ds, LDAP_OPT_PROTOCOL_VERSION, 3 );

                    $dn = $ldapconfig[ 'prepend' ] . $username . "," . $ldapconfig[ 'basedn' ];

                    // bind with ldap to determine LDAP authentication status
                    $LDAP_bind_status =
                            ldap_bind($ds,$ldapconfig[ 'prepend' ].$username.",".$ldapconfig['basedn'],$password);

                    ldap_close();

                    if( $LDAP_bind_status )
                            return true;
                    else
                            return false;
            }

            // purpose: determine whether or not the user is authorized or their account is locked
            //                      lock or unlock the account if necessary
            function getAuthStatus( $username, $password, $domain ) {
                    if( ! $username || $username == '' )
                            $error  =       'must enter username';
                    else if( ! $password || $password == '' )
                            $error  =       'must enter password';
                    else if( ! $domain || $domain == '' )
                            $error  =       'must enter domain';

                    if( ! $_SESSION['logged_in'] )
                    {
                            if( $this->getLockStatus( $username ) ) {
                                    $this->unsuccessfulLogin( $username );
                                    return false;
                            }
                            else if( ! $this->LDAP_authenticate( $username, $password, $domain) ) {
                                    $this->unsuccessfulLogin( $username );
                                    return false;
                            }
                            else {
                                    $this->successfulLogin( $username );
                                    return true;
                            }
                    }
                    session_start();
                    return true;
            }

            // purpose: determine whether or not the user is locked out
            //                      returns true if account is locked, false if unlocked
            function getLockStatus( $username ) {
                    if( ! $username || $username == '' )
                            return false;

                    $current_time   =       mktime( date('h'), date('i'), date('s'), date('m'), date('d'), date('Y') );
                    $five_minutes   =       300; // number of seconds in five minutes

                    // query the database for lock status and time locked
                    require('conf/mysql.conf.php');
                    $query          =       "SELECT is_timesheet_locked, lock_time FROM employee WHERE uid='$username';";
                    $result         =       mysql_query( $query );
                    $lock_hash      =       mysql_fetch_assoc( $result );

                    // if a manual account lock is enabled, account status is LOCKED
                    if( $lock_hash['is_timesheet_locked'] == 1 )
                    {       $this->manuallyLocked   =       true;   return true;    }
                    else
                    {       $this->manuallyLocked   =       false;                                  }

                    // if an automatic time lock is set, deny access
                    if( isset( $lock_hash['lock_time'] ) ) {

                            // has 10 minutes elapsed since locking?
                            if( $lock_hash['lock_time'] + $five_minutes > $current_time )
                            {       $this->timeLocked       =       true;   return true;    }       // account is LOCKED
                            else
                            {       $this->timeLocked       =       falsereturn false;   }       // account is UNLOCKED

                    }
                    // if no time lock is enabled, account status is UNLOCKED
                    else
                    {       $this->timeLocked       =       falsereturn false;           }
            }

            // purpose: lock the account of an employee
            function lockAccount( $username ) {
                    if( ! $username || $username == '' )
                            return false;


                    $current_time   =       mktime( date('h'), date('i'), date('s'), date('m'), date('d'), date('Y') );
                    $query  =       "UPDATE employee SET lock_time = $current_time " .
                                            "WHERE uid = '$username' AND lock_time IS NULL;";
                    mysql_query( $query );
            }

            // purpose: add unsuccessful login attempt to the tally for this user, lock the account if necessary
            function unsuccessfulLogin( $username ) {
                    if( ! $username || $username == '' )
                            return false;

                    $this->isSuccessfulLogin        =       false;

                    require('conf/mysql.conf.php');
                    $query  =       "UPDATE employee SET unsuccessful_attempts = unsuccessful_attempts + 1 " .
                                            "WHERE uid = '$username';";
                    mysql_query( $query );

                    $query  =       "SELECT unsuccessful_attempts FROM employee WHERE uid = '$username';";
                    $result =       mysql_query( $query );
                    $row    =       mysql_fetch_row( $result );
                    $num_attempts   =       $row[0];

                    if( $num_attempts >= 3 )
                            $this->lockAccount( $username );
            }

            // purpose: reset the tally for unsuccessful login attempts for this user
            function successfulLogin( $username ) {
                    if( ! $username || $username == '' )
                            return false;

                    $this->isSuccessfulLogin        =       true;

                    require('employee_data.php');
                    $employee_data  =       new employee_data($username);
                    $employee_data->saveAll_toSession();

                    require('conf/mysql.conf.php');
                    $query  =       "UPDATE employee SET unsuccessful_attempts = 0, lock_time=NULL " .
                                            "WHERE uid='$username';";
                    mysql_query( $query );
            }

            // purpose: get the current status message for this account, if any
            function getMessage( $username ) {
                    if( ! $username || $username == '' )
                            return NULL;

                    if( $this->manuallyLocked == true )
                    {       return "This account is locked, contact administrator for assistance"}
                    else if( $this->timeLocked == true )
                    {       return "Due to excessive login attempts, this account is temporarily locked. Try again in a few minutes."; }
                    else if( $this->isSuccessfulLogin == false ) {
                            return "Invalid username or password.";
                    }
            }

            // purpose: remove all session data for this session
            function logout() {
                    $_SESSION['logged_in'] = false;
                    session_destroy();
            }
    }
    ?>
     

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot