1 Replies - 1951 Views - Last Post: 28 July 2011 - 10:49 AM

#1 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

UserAuthentication Singleton

Posted 28 July 2011 - 10:49 AM

Description: //Get Instance
$auth = UserAuthentication::getInstance();

//Authenticate User
$auth->authenticateUser();

//Check for authentication
if($auth->authenticationValid())
//authenticated
else
//not authenticated

//Unauthenticate users
$auth->unauthenticateUser();Authenticate and unauthenticate users quickly.
<?php
/*
* Authenticate and unauthenticate users quickly
* @author Chavez Watkins
*/
class UserAuthentication
{
    /*
    * @var (UserAuthentication) $instance   Instance of UserAuthentication
    * @var (string) $auth_hash              Hash used to make final
    * @var (string) $auth_key               Key used to make final
    * @var (string) $auth_final             Client's session key
    * @const (string) AUTH_SALT             Salt used to make key and final
    * @const (int) KEY_LENGTH		    Length of key used in hashing. Must be less than 62
    */
    private static 
            $instance   = NULL
    ;
    private
            $auth_hash  = NULL,
            $auth_key   = NULL,
            $auth_final = NULL
    ;
    const       
            AUTH_SALT  = '*HlDd*%*afha!s#d%fd++e28^[email protected]#f54w&78ds%kjgr8',
			KEY_LENGTH = 32
    ;
    
    /*
    * Determines whether session was already initialized and if not it is initialized
    * 
    * @return (void)
    */
    private function __construct()
    {
        if(!isset($_SESSION))
        {
            session_start();
            session_regenerate_id();
        }
		if(isset(
            $_COOKIE['auth_hash'], 
            $_SESSION['auth_key'],
            $_SESSION['auth_final']
        ))
        {
            $this->auth_key = $_SESSION['auth_key'];
            $this->auth_hash = $_COOKIE['auth_hash'];
            $this->auth_final = $_SESSION['auth_final'];
        }
        else
        {
            $this->auth_key = $this->generateKey();
            $this->auth_hash = $this->generateHash(); 
        }
    }
	
	private function __clone() { }
    
    /*
    * Returns an instance of UserAuthentication
    * 
    * @return (UserAuthentication)
    */
    public static function getInstance()
    {
        if(!isset(self::$instance))
        {
            self::$instance = new self;
        }
        return self::$instance;
    }
    
	/*
	* Authenticate the user based on the key and hash
	*
	* @var (int) $minutes The amount of minutes till the cookie is to expire
	* @return (bool)
	*/
    public function authenticateUser($minutes = 60)
    {
        if(setcookie('auth_hash', self::$instance->auth_hash, time() + 60 * $minutes, '/'))
        {
            $_SESSION['auth_key'] = $this->auth_key;
            $_SESSION['auth_final'] = $this->generateFinal();
            return true;
        }
        return false;
    }
    
	/*
	* Deletes all data assosiated with the authentication
	* 
	* @return (void)
	*/
    public function unauthenticateUser()
    {
        session_destroy();
        session_regenerate_id(true);
	$_SESSION = array();
        setcookie('auth_hash', '', time() - 3600);
    }
    
	/*
	* Checks whether the user's authentication is valid
	*
	* @return (bool)
	*/
    public function authenticationValid()
    {
        if($this->auth_final === $this->generateFinal())
            return true;
        else
            return false;
    }
    
	/*
	* Generates a random key
	*
	* @return (string)
	*/
    private function generateKey()
    {
        $alpha_numeric = array_merge(
				range(0, 9), 
				range('a', 'z'), 
				range('A', 'Z')
	); 
        shuffle($alpha_numeric);
        return hash_hmac('SHA512', 
			  substr(implode('', $alpha_numeric), 0, UserAuthentication::KEY_LENGTH), 
			  time()
		);
    }
    
	/*
	* Generates hash based on key and salt
	*
	* @return (string)
	*/
    private function generateHash()
    {
        return hash_hmac('SHA512', $this->auth_key, UserAuthentication::AUTH_SALT);
    }
    
	/*
	* Generates final hash
	*
	* @return (string)
	*/
    private function generateFinal()
    {
        $client_ip = (
                isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? 
                      $_SERVER['HTTP_X_FORWARDED_FOR'] : 
                      $_SERVER['REMOTE_ADDR']
        );
        
        $buffer = hash_hmac('SHA512', $client_ip, $this->auth_hash);
        return hash_hmac('SHA512', $this->auth_key, $buffer);
    }
}
?>


Is This A Good Question/Topic? 0
  • +

Replies To: UserAuthentication Singleton

#2 codeprada   User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 963
  • View blog
  • Posts: 2,382
  • Joined: 15-February 11

Re: UserAuthentication Singleton

Posted 28 July 2011 - 10:49 AM

Description: //Get Instance
$auth = UserAuthentication::getInstance();

//Authenticate User
$auth->authenticateUser();

//Check for authentication
if($auth->authenticationValid())
//authenticated
else
//not authenticated

//Unauthenticate users
$auth->unauthenticateUser();Authenticate and unauthenticate users quickly.
<?php
    /*
    * Authenticate and unauthenticate users quickly
    * @author Chavez Watkins
    * @datemodified 1 November, 2011
    */
    class UserAuthentication
    {
        /*
        * @var (UserAuthentication) $instance   Instance of UserAuthentication
        * @var (string) $authenticationHash     Hash used to make final
        * @var (string) $authenticationKey      Key used to make final
        * @var (string) $authenticationFinal    Client's session key
        * @const (string) AUTH_SALT             Salt used to make key and final
        * @const (int) KEY_LENGTH                Length of key used in hashing. Must be less than 62
        */
        private static 
                $instance   = NULL
        ;
        private
                $authenticationHash  = NULL,
                $authenticationKey   = NULL,
                $authenticationFinal = NULL
        ;
        const       
                AUTH_SALT  = '*HlDd*%*ajh765876jh%ds$[email protected]#f54w&78ds%kjgr8',
                KEY_LENGTH = 32
        ;
        
        /*
        * Determines whether session was already initialized and if not it is initialized
        * 
        * @return (void)
        */
        private function __construct()
        {
            if(!isset($_SESSION))
            {
                session_start();
                session_regenerate_id(false);
            }
            if(isset(
                $_COOKIE['auth_hash'], 
                $_SESSION['auth_key'],
                $_SESSION['auth_final']
            ))
            {
                $this->authenticationKey = $_SESSION['auth_key'];
                $this->authenticationHash = $_COOKIE['auth_hash'];
                $this->authenticationFinal = $_SESSION['auth_final'];
            }
        }
        
        private function __clone() { }
        
        /*
        * Returns an instance of UserAuthentication
        * 
        * @return (UserAuthentication)
        */
        public static function getInstance()
        {
            if(!isset(self::$instance))
            {
                self::$instance = new self;
            }
            return self::$instance;
        }
        
        /*
        * Authenticate the user based on the key and hash
        *
        * @var (int) $minutes The amount of minutes till the cookie is to expire
        * @return (bool)
        */
        public function authenticateUser($minutes = 0)
        {
            $this->authenticationKey = $this->generateKey();
            $this->authenticationHash = $this->generateHash();
            $this->authenticationFinal = $this->generateFinal(); 
            
            if(setcookie('auth_hash', $this->authenticationHash, $minutes == 0 ? 0 : time() + 60 * $minutes, '/'))
            {
                $_SESSION['auth_key'] = $this->authenticationKey;
                $_SESSION['auth_final'] = $this->generateFinal();
                return true;
            }
            return false;
        }
        
        /*
        * Deletes all data assosiated with the authentication
        * 
        * @return (void)
        */
        public function unauthenticateUser()
        {
            session_destroy();
            session_regenerate_id(true);
            $_SESSION = array();
            setcookie('auth_hash', '', time() - 3600);
        }
        
        /*
        * Checks whether the user's authentication is valid
        *
        * @return (bool)
        */
        public function authenticationValid()
        {
            if($this->authenticationFinal === $this->generateFinal())
                return true;
            else
                return false;
        }
        
        /*
        * Generates a random key
        *
        * @return (string)
        */
        private function generateKey()
        {
            $alpha_numeric = array_merge(
                                    range(0, 9), 
                                    range('a', 'z'), 
                                    range('A', 'Z')
            ); 
            shuffle($alpha_numeric);
            return hash_hmac('SHA512', 
                                substr(implode('', $alpha_numeric), 0, User_Authentication::KEY_LENGTH), 
                             time()
            );
        }
        
        /*
        * Generates hash based on key and salt
        *
        * @return (string)
        */
        private function generateHash()
        {
            return hash_hmac('SHA512', $this->authenticationKey, User_Authentication::AUTH_SALT);
        }
        
         /*
        * Generates final hash
        *
        * @return (string)
        */
        private function generateFinal()
        {
            $client_ip = (
                    isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? 
                          $_SERVER['HTTP_X_FORWARDED_FOR'] : 
                          $_SERVER['REMOTE_ADDR']
            );
            
            $buffer = hash_hmac('SHA512', $client_ip, $this->authenticationHash);
            return hash_hmac('SHA512', $this->authenticationKey, $buffer);
        }
    }
?>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1