Browse Source

ZF-11528: authenticate if credentials are provided and auth adapter present

- Moved authentication out of the request bodies loop in _handle()
  - Test for credentials header, and userid/password keys in it
    - when present, call _handleAuth() with the values
      - if passes, add the authentication header to the response
      - if fails, create an error body
  - In loop, test for an error body
    - if present, add it to the current response body, and loop to the
      next
- Use a switch statement in the request bodies loop to test for encoding
  type

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24203 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 14 years ago
parent
commit
601be8f909
1 changed files with 76 additions and 60 deletions
  1. 76 60
      library/Zend/Amf/Server.php

+ 76 - 60
library/Zend/Amf/Server.php

@@ -494,72 +494,60 @@ class Zend_Amf_Server implements Zend_Server_Interface
         // set response encoding
         $response->setObjectEncoding($objectEncoding);
 
-        $requestBodies = $request->getAmfBodies();
-
-        $handleAuth = false;
-        if ($this->_auth) {
-            $headers = $request->getAmfHeaders();
-            if (isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]) 
-                && isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid)
-            ) {
-                $handleAuth = true;
+        // Authenticate, if we have credential headers
+        $error   = false;
+        $headers = $request->getAmfHeaders();
+        if (isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]) 
+            && isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid)
+            && isset($headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->password)
+        ) {
+            try {
+                if ($this->_handleAuth(
+                        $headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid,
+                        $headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->password
+                )) {
+                    // use RequestPersistentHeader to clear credentials
+                    $response->addAmfHeader(
+                        new Zend_Amf_Value_MessageHeader(
+                            Zend_Amf_Constants::PERSISTENT_HEADER,
+                            false,
+                            new Zend_Amf_Value_MessageHeader(
+                                Zend_Amf_Constants::CREDENTIALS_HEADER,
+                                false, null
+                            )
+                        )
+                    );
+                }
+            } catch (Exception $e) {
+                // Error during authentication; report it
+                $error = $this->_errorMessage(
+                    $objectEncoding, 
+                    '', 
+                    $e->getMessage(),
+                    $e->getTraceAsString(),
+                    $e->getCode(),
+                    $e->getLine()
+                );
+                $responseType = Zend_AMF_Constants::STATUS_METHOD;
             }
         }
 
         // Iterate through each of the service calls in the AMF request
-        foreach($requestBodies as $body)
+        foreach($request->getAmfBodies() as $body)
         {
+            if ($error) {
+                // Error during authentication; just report it and be done
+                $responseURI = $body->getResponseURI() . $responseType;
+                $newBody     = new Zend_Amf_Value_MessageBody($responseURI, null, $error);
+                $response->addAmfBody($newBody);
+                continue;
+            }
             try {
-                if ($handleAuth) {
-                    $message = '';
-                    if ($this->_handleAuth(
-                            $headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->userid,
-                            $headers[Zend_Amf_Constants::CREDENTIALS_HEADER]->password
-                    )) {
-                        // use RequestPersistentHeader to clear credentials
-                        $response->addAmfHeader(
-                            new Zend_Amf_Value_MessageHeader(
-                                Zend_Amf_Constants::PERSISTENT_HEADER,
-                                false,
-                                new Zend_Amf_Value_MessageHeader(
-                                    Zend_Amf_Constants::CREDENTIALS_HEADER,
-                                    false, null
-                                )
-                            )
-                        );
-                        $handleAuth = false;
-                    }
-                }
-
-                if ($objectEncoding == Zend_Amf_Constants::AMF0_OBJECT_ENCODING) {
-                    // AMF0 Object Encoding
-                    $targetURI = $body->getTargetURI();
-                    $message = '';
-
-                    // Split the target string into its values.
-                    $source = substr($targetURI, 0, strrpos($targetURI, '.'));
-
-                    if ($source) {
-                        // Break off method name from namespace into source
-                        $method = substr(strrchr($targetURI, '.'), 1);
-                        $return = $this->_dispatch($method, $body->getData(), $source);
-                    } else {
-                        // Just have a method name.
-                        $return = $this->_dispatch($targetURI, $body->getData());
-                    }
-                } else {
-                    // AMF3 read message type
-                    $message = $body->getData();
-                    if ($message instanceof Zend_Amf_Value_Messaging_CommandMessage) {
-                        // async call with command message
-                        $return = $this->_loadCommandMessage($message);
-                    } elseif ($message instanceof Zend_Amf_Value_Messaging_RemotingMessage) {
-                        require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
-                        $return = new Zend_Amf_Value_Messaging_AcknowledgeMessage($message);
-                        $return->body = $this->_dispatch($message->operation, $message->body, $message->source);
-                    } else {
-                        // Amf3 message sent with netConnection
+                switch ($objectEncoding) {
+                    case Zend_Amf_Constants::AMF0_OBJECT_ENCODING:
+                        // AMF0 Object Encoding
                         $targetURI = $body->getTargetURI();
+                        $message = '';
 
                         // Split the target string into its values.
                         $source = substr($targetURI, 0, strrpos($targetURI, '.'));
@@ -572,7 +560,35 @@ class Zend_Amf_Server implements Zend_Server_Interface
                             // Just have a method name.
                             $return = $this->_dispatch($targetURI, $body->getData());
                         }
-                    }
+                        break;
+                    case Zend_Amf_Constants::AMF3_OBJECT_ENCODING:
+                    default:
+                        // AMF3 read message type
+                        $message = $body->getData();
+                        if ($message instanceof Zend_Amf_Value_Messaging_CommandMessage) {
+                            // async call with command message
+                            $return = $this->_loadCommandMessage($message);
+                        } elseif ($message instanceof Zend_Amf_Value_Messaging_RemotingMessage) {
+                            require_once 'Zend/Amf/Value/Messaging/AcknowledgeMessage.php';
+                            $return = new Zend_Amf_Value_Messaging_AcknowledgeMessage($message);
+                            $return->body = $this->_dispatch($message->operation, $message->body, $message->source);
+                        } else {
+                            // Amf3 message sent with netConnection
+                            $targetURI = $body->getTargetURI();
+
+                            // Split the target string into its values.
+                            $source = substr($targetURI, 0, strrpos($targetURI, '.'));
+
+                            if ($source) {
+                                // Break off method name from namespace into source
+                                $method = substr(strrchr($targetURI, '.'), 1);
+                                $return = $this->_dispatch($method, $body->getData(), $source);
+                            } else {
+                                // Just have a method name.
+                                $return = $this->_dispatch($targetURI, $body->getData());
+                            }
+                        }
+                        break;
                 }
                 $responseType = Zend_AMF_Constants::RESULT_METHOD;
             } catch (Exception $e) {