Explorar o código

ZF-10851
Zend_Oauth
Update component to allow setting oauth realm


git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23946 44c647ce-9c0f-0410-b52a-842ac1e357ba

adamlundrigan %!s(int64=14) %!d(string=hai) anos
pai
achega
ce2a8e9ab3

+ 2 - 1
library/Zend/Oauth/Client.php

@@ -246,7 +246,8 @@ class Zend_Oauth_Client extends Zend_Http_Client
             $oauthHeaderValue = $this->getToken()->toHeader(
                 $this->getUri(true),
                 $this->_config,
-                $this->_getSignableParametersAsQueryString()
+                $this->_getSignableParametersAsQueryString(),
+                $this->getRealm()
             );
             $this->setHeaders('Authorization', $oauthHeaderValue);
         } elseif ($requestScheme == Zend_Oauth::REQUEST_SCHEME_POSTBODY) {

+ 32 - 0
library/Zend/Oauth/Config.php

@@ -146,6 +146,13 @@ class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
      * @var Zend_Oauth_Token
      */
     protected $_token = null;
+    
+    /**
+     * Define the OAuth realm
+     * 
+     * @var string
+     */
+    protected $_realm = null;
 
     /**
      * Constructor; create a new object with an optional array|Zend_Config
@@ -214,6 +221,9 @@ class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
                 case 'rsaPublicKey':
                     $this->setRsaPublicKey($value);
                     break;
+                case 'realm':
+                    $this->setRealm($value);
+                    break;
             }
         }
         if (isset($options['requestScheme'])) {
@@ -655,4 +665,26 @@ class Zend_Oauth_Config implements Zend_Oauth_Config_ConfigInterface
     {
         return $this->_token;
     }
+
+    /**
+     * Set OAuth realm
+     *
+     * @param  string $realm
+     * @return Zend_Oauth_Config
+     */
+    public function setRealm($realm)
+    {
+        $this->_realm = $realm;
+        return $this;
+    }
+
+    /**
+     * Get OAuth realm
+     *
+     * @return string
+     */
+    public function getRealm()
+    {
+        return $this->_realm;
+    }
 }

+ 4 - 0
library/Zend/Oauth/Config/ConfigInterface.php

@@ -72,4 +72,8 @@ interface Zend_Oauth_Config_ConfigInterface
     public function setToken(Zend_Oauth_Token $token);
 
     public function getToken();
+
+    public function setRealm($realm);
+
+    public function getRealm();
 }

+ 40 - 0
tests/Zend/Oauth/OauthTest.php

@@ -98,4 +98,44 @@ class Zend_OauthTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('GET', $client->getRequestMethod());
         $this->assertEquals('http://www.example.com', $client->getSiteUrl());
     }
+
+    /**
+     * @group ZF-10851
+     */
+    public function testOauthClientAcceptsRealmConfigurationOption()
+    {
+        $options = array(
+            'realm'			=> 'http://www.example.com'
+        );
+
+        require_once 'Zend/Oauth/Client.php';
+        $client = new Zend_Oauth_Client($options);
+        $this->assertEquals('http://www.example.com', $client->getRealm());
+    }
+
+    /**
+     * @group ZF-10851
+     */
+    public function testOauthClientPreparationWithRealmConfigurationOption()
+    {
+        require_once "Zend/Oauth/Token/Access.php";
+        
+        $options = array(
+            'requestMethod' => 'GET',
+            'siteUrl'       => 'http://www.example.com',
+            'realm'			=> 'someRealm'
+        );
+        $token = new Zend_Oauth_Token_Access();
+
+        require_once 'Zend/Oauth/Client.php';
+        $client = new Zend_Oauth_Client($options);
+        $this->assertEquals(NULL,$client->getHeader('Authorization'));
+        
+        $client->setToken($token);
+        $client->setUri('http://oauth.example.com');
+        $client->prepareOauth();
+        
+        $this->assertNotContains('realm=""',$client->getHeader('Authorization'));
+        $this->assertContains('realm="someRealm"',$client->getHeader('Authorization'));
+    }
 }