Modify oauth calls to expect urlencoded responses

Previously, the server side would send jsonwrapped urlencoded
responses. Now we are removing the jsonwrapped portion so that
the responses for oauth calls are just urlencoded.

Related-Bug: #1336910
Change-Id: I0431a581a5c3fca38e22751ef9bde034f0a89a30
This commit is contained in:
Steve Martinelli
2014-07-02 15:22:52 -04:00
parent 548c15f7f7
commit b47f6a2702
4 changed files with 15 additions and 11 deletions

View File

@@ -16,9 +16,9 @@ import uuid
import httpretty import httpretty
import mock import mock
import six import six
from six.moves.urllib import parse as urlparse
from testtools import matchers from testtools import matchers
from keystoneclient.openstack.common import jsonutils
from keystoneclient.openstack.common import timeutils from keystoneclient.openstack.common import timeutils
from keystoneclient import session from keystoneclient import session
from keystoneclient.tests.v3 import client_fixtures from keystoneclient.tests.v3 import client_fixtures
@@ -87,13 +87,17 @@ class TokenTests(BaseTest):
def _new_oauth_token(self): def _new_oauth_token(self):
key = uuid.uuid4().hex key = uuid.uuid4().hex
secret = uuid.uuid4().hex secret = uuid.uuid4().hex
token = 'oauth_token=%s&oauth_token_secret=%s' % (key, secret) params = {'oauth_token': key, 'oauth_token_secret': secret}
token = urlparse.urlencode(params)
return (key, secret, token) return (key, secret, token)
def _new_oauth_token_with_expires_at(self): def _new_oauth_token_with_expires_at(self):
key, secret, token = self._new_oauth_token() key, secret, token = self._new_oauth_token()
expires_at = timeutils.strtime() expires_at = timeutils.strtime()
token += '&oauth_expires_at=%s' % expires_at params = {'oauth_token': key,
'oauth_token_secret': secret,
'oauth_expires_at': expires_at}
token = urlparse.urlencode(params)
return (key, secret, expires_at, token) return (key, secret, expires_at, token)
def _validate_oauth_headers(self, auth_header, oauth_client): def _validate_oauth_headers(self, auth_header, oauth_client):
@@ -171,10 +175,8 @@ class RequestTokenTests(TokenTests):
request_key, request_secret, resp_ref = self._new_oauth_token() request_key, request_secret, resp_ref = self._new_oauth_token()
# NOTE(stevemar) The server expects the body to be JSON. Even though
# the resp_ref is a string it is not a JSON string.
self.stub_url(httpretty.POST, [self.path_prefix, 'request_token'], self.stub_url(httpretty.POST, [self.path_prefix, 'request_token'],
status=201, body=jsonutils.dumps(resp_ref), status=201, body=resp_ref,
content_type='application/x-www-form-urlencoded') content_type='application/x-www-form-urlencoded')
# Assert the manager is returning request token object # Assert the manager is returning request token object
@@ -214,10 +216,8 @@ class AccessTokenTests(TokenTests):
t = self._new_oauth_token_with_expires_at() t = self._new_oauth_token_with_expires_at()
access_key, access_secret, expires_at, resp_ref = t access_key, access_secret, expires_at, resp_ref = t
# NOTE(stevemar) The server expects the body to be JSON. Even though
# the resp_ref is a string it is not a JSON string.
self.stub_url(httpretty.POST, [self.path_prefix, 'access_token'], self.stub_url(httpretty.POST, [self.path_prefix, 'access_token'],
status=201, body=jsonutils.dumps(resp_ref), status=201, body=resp_ref,
content_type='application/x-www-form-urlencoded') content_type='application/x-www-form-urlencoded')
# Assert that the manager creates an access token object # Assert that the manager creates an access token object

View File

@@ -42,5 +42,5 @@ class AccessTokenManager(base.CrudManager):
url = self.client.auth_url.rstrip("/") + endpoint url = self.client.auth_url.rstrip("/") + endpoint
url, headers, body = oauth_client.sign(url, http_method='POST') url, headers, body = oauth_client.sign(url, http_method='POST')
resp, body = self.client.post(endpoint, headers=headers) resp, body = self.client.post(endpoint, headers=headers)
token = utils.get_oauth_token_from_body(body) token = utils.get_oauth_token_from_body(resp.content)
return self.resource_class(self, token) return self.resource_class(self, token)

View File

@@ -66,5 +66,5 @@ class RequestTokenManager(base.CrudManager):
url, headers, body = oauth_client.sign(url, http_method='POST', url, headers, body = oauth_client.sign(url, http_method='POST',
headers=headers) headers=headers)
resp, body = self.client.post(endpoint, headers=headers) resp, body = self.client.post(endpoint, headers=headers)
token = utils.get_oauth_token_from_body(body) token = utils.get_oauth_token_from_body(resp.content)
return self.resource_class(self, token) return self.resource_class(self, token)

View File

@@ -11,6 +11,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import six
from six.moves.urllib import parse as urlparse from six.moves.urllib import parse as urlparse
@@ -25,6 +26,9 @@ def get_oauth_token_from_body(body):
'oauth_expires_at=2013-03-30T05:27:19.463201' possibly there, too. 'oauth_expires_at=2013-03-30T05:27:19.463201' possibly there, too.
""" """
if six.PY3:
body = body.decode('utf-8')
credentials = urlparse.parse_qs(body) credentials = urlparse.parse_qs(body)
key = credentials['oauth_token'][0] key = credentials['oauth_token'][0]
secret = credentials['oauth_token_secret'][0] secret = credentials['oauth_token_secret'][0]