Search code examples
zend-frameworkdrupalzend-auth

Using Zend Auth with external authentication mechanism


I have a Drupal site and a Zend application. The main thing is the Drupal site, where the users are stored & everything.

I want my users to be automatically logged in to the Zend app when they log in on Drupal. The problem is that Drupal changes the session cookie to SESS* where * is some random (EDIT: not random, but based on protocol and domain) string.

Is there any way I can tell Zend to use this cookie as a session identifier and to log the user automatically?


Solution

  • You have to write your own authentication adapter:

    class YourApp_Auth_Adapter_DrupalBridge implements Zend_Auth_Adapter_Interface
    {
        /**
         * @return Zend_Auth_Result
         */
        public function authenticate()
        {
            // Check if the Drupal session is set by reading the cookie.
            // ...
    
            // Read the current user's login into $username.
            // ...
    
            // Create the authentication result object.
    
            // Failure
            if (null === $username) {
                return new Zend_Auth_Result(Zend_Auth_Result::FAILURE_IDENTITY_NOT_FOUND, null);
            }
    
            // Success
            return new Zend_Auth_Result(Zend_Auth_Result::SUCCESS, $username);
        }
    }
    

    Then process your authentication:

    $adapter = new YourApp_Auth_Adapter_DrupalBridge();
    $result = Zend_Auth::getInstance()->authenticate($adapter);
    
    if ($result->isValid()) {
        // User is logged in
    }