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;
    }

}

No comments:

Post a Comment