Showing posts with label Zend_Auth_Adapter_Interface. Show all posts
Showing posts with label Zend_Auth_Adapter_Interface. Show all posts

Thursday, November 19, 2009

Authentication with a custom storage


In this example, we implement a simple authentication with a custom adapter. The user names and passwords are stored inside. And the persistence mechanism is implemented with a custom storage.

Components used in this example
Implementation of the authentication

class MyAuthentication
{
The authentication process
  • We get the user name and password from the GET request. Or we get the request to sign out.
  • We instantiate the authentication object.
  • We pass the storage to the authentication object.
  • If the user requested to sign out, we erase the identity of the user.
  • If the user is already authenticated, we return the identity of the user.
  • Or we attempt to authenticate the user. We return a message stating if the user was identified successfully or not.

    public function process()
    {
        
// We get the user name and password from the GET request.
        // Or we get the request to sign out.
        
list($username$password$clear) = $this->_getParameters();
        
// We instantiate the authentication object.
        
$auth Zend_Auth::getInstance();
        
// We pass the storage to the authentication object.
        
$auth->setStorage(new MyStorage());

        if (
$clear) {
            
// If the user requested to sign out, we erase the identity of the user.
            
$auth->clearIdentity();
            
$message 'The identity is cleared';
        } else if (
$auth->hasIdentity()) {
            
// If the user is already authenticated, we return the identity of the user.
            
$identity $auth->getIdentity();
            
$message "$username is already authenticated and identified as $identity!";
        } else if (
$username) {
            
// Or we attempt to authenticate the user.
            // We return a message stating if the user was identified successfully or not.
            
$message $this->_authenticate($username$password);
        } else {
            
$message '';
        }

        return array(
$username$password$message);
    }
Extraction of the parameters from the GET request

    private function _getParameters()
    {
        
$username = isset($_GET['username'])? $_GET['username'] : '';
        
$password = isset($_GET['password'])? $_GET['password'] : '';
        
$clear = !empty($_GET['clear']);

        return array(
$username$password$clear);
    }
Authentication of the user
  • We instantiate the authentication adapter by passing the user name and password.
  • We instantiate the authentication object.
  • We attempt to authenticate the user.
  • If the user is authenticated, we return the identity of the user. The identity of the user is stored in the custom storage.
  • If the authentication failed, we return an error message.

    private function _authenticate($username$password)
    {
        
// We instantiate the authentication adapter by passing the user name and password.
        
$authAdapter = new MyAuthAdapter($username$password);
        
// We instantiate the authentication object.
        
$auth Zend_Auth::getInstance();
        
// We attempt to authenticate the user.
        
$result $auth->authenticate($authAdapter);

        if (
$result->isValid()) {
            
// If the user is authenticated, we return the identity of the user.
            // The identity of the user is stored in the custom storage.
            
$identity $result->getIdentity();
            
$message "$identity is now authenticated!";
        } else if (
$username) {
            
// If the authentication failed, we return an error message.
            
$message $result->getMessages();
        }

        return 
$message;
    }

}

Authentication with a custom adapter


In this example, we implement a simple authentication with a custom adapter. The user names and passwords are stored inside. And we use the default persistence mechanism.

Components used in this example
Implementation of the authentication adapter
  • User names and passwords are hard coded for the purpose of this example.

class MyAuthAdapter implements Zend_Auth_Adapter_Interface
{
    
// User names and passwords are hard coded for the purpose of this example.
    
private $_userPasswords = array('john' => '123''jane' => '456');
    private 
$_username;
    private 
$_password;
Construction of the adapter
  • The user name and password are passed to the adapter.

    public function __construct($username$password)
    {
        
// The user name and password are passed to the adapter.
        
$this->_username $username;
        
$this->_password $password;
    }
Authentication of the user
  • We check if the user exists. We prepare the error message if not.
  • We check if the password is valid. We prepare the error message if not.
  • We mark the result as successful if the user name and password are valid.
  • We return the authentication result.

    public function authenticate()
    {
        if (!isset(
$this->_userPasswords[$this->_username])) {
            
// We check if the user exists. We prepare the error message if not.
            
$result = new Zend_Auth_Result(
                
Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND,
                
$this->_username,
                array(
'Unknown user!'));
        } else if (
$this->_userPasswords[$this->_username] != $this->_password) {
            
// We check if the password is valid. We prepare the error message if not.
            
$result =  new Zend_Auth_Result(
                
Zend_Auth_Result::FAILURE_CREDENTIAL_INVALID,
                
$this->_username,
                array(
'Bad password!'));
        } else {
            
// We mark the result as successful if the user name and password are valid.
            
$result =  new Zend_Auth_Result(
                
Zend_Auth_Result::SUCCESS,
                
$this->_username);
        }

        
// We return the authentication result.
        
return $result;
    }

}