summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gouldin <david@gould.in>2012-05-01 22:24:15 -0700
committerDavid Gouldin <david@gould.in>2012-05-01 23:30:01 -0700
commit772d48de9a285d9f95dcac49be4542576c38c267 (patch)
tree7c930e7d25a416ee0b3b4672dfeecf2c01849065
parent5806535de38a552fd0bb68af928cd19a9e8727bd (diff)
downloadoauthlib-772d48de9a285d9f95dcac49be4542576c38c267.tar.gz
Half implemented solution which passes around Request instances to parameters instead of raw headers/body/params
-rw-r--r--oauthlib/common.py7
-rw-r--r--oauthlib/oauth1/rfc5849/__init__.py15
-rw-r--r--oauthlib/oauth1/rfc5849/constants.py23
-rw-r--r--oauthlib/oauth1/rfc5849/parameters.py47
4 files changed, 65 insertions, 27 deletions
diff --git a/oauthlib/common.py b/oauthlib/common.py
index 4cdfd0d..a8bbde1 100644
--- a/oauthlib/common.py
+++ b/oauthlib/common.py
@@ -9,6 +9,7 @@ This module provides data structures and utilities common
to all implementations of OAuth.
"""
+import copy
import re
import urllib
import urlparse
@@ -153,3 +154,9 @@ class Request(object):
def uri_query_params(self):
return urlparse.parse_qsl(self.uri_query, keep_blank_values=True,
strict_parsing=True)
+
+ def clone(self):
+ return Request(self.uri, http_method=self.http_method,
+ body=self.body,
+ headers=copy.copy(self.headers))
+
diff --git a/oauthlib/oauth1/rfc5849/__init__.py b/oauthlib/oauth1/rfc5849/__init__.py
index 327bae0..d5fccad 100644
--- a/oauthlib/oauth1/rfc5849/__init__.py
+++ b/oauthlib/oauth1/rfc5849/__init__.py
@@ -13,18 +13,7 @@ import logging
import urlparse
from oauthlib.common import Request, urlencode
-from . import parameters, signature, utils
-
-SIGNATURE_HMAC = u"HMAC-SHA1"
-SIGNATURE_RSA = u"RSA-SHA1"
-SIGNATURE_PLAINTEXT = u"PLAINTEXT"
-SIGNATURE_METHODS = (SIGNATURE_HMAC, SIGNATURE_RSA, SIGNATURE_PLAINTEXT)
-
-SIGNATURE_TYPE_AUTH_HEADER = u'AUTH_HEADER'
-SIGNATURE_TYPE_QUERY = u'QUERY'
-SIGNATURE_TYPE_BODY = u'BODY'
-
-CONTENT_TYPE_FORM_URLENCODED = u'application/x-www-form-urlencoded'
+from . import parameters, signature, utils, constants
class Client(object):
@@ -127,7 +116,7 @@ class Client(object):
# like the spec requires. This would be a fundamental change though, and
# I'm not sure how I feel about it.
if self.signature_type == SIGNATURE_TYPE_AUTH_HEADER:
- headers = parameters.prepare_headers(request.oauth_params, request.headers)
+ request = parameters.prepare_headers(request)
elif self.signature_type == SIGNATURE_TYPE_BODY and request.decoded_body is not None:
body = parameters.prepare_form_encoded_body(request.oauth_params, request.decoded_body)
if formencode:
diff --git a/oauthlib/oauth1/rfc5849/constants.py b/oauthlib/oauth1/rfc5849/constants.py
new file mode 100644
index 0000000..0feb227
--- /dev/null
+++ b/oauthlib/oauth1/rfc5849/constants.py
@@ -0,0 +1,23 @@
+SIGNATURE_METHOD_HMAC = u"HMAC-SHA1"
+SIGNATURE_METHOD_RSA = u"RSA-SHA1"
+SIGNATURE_METHOD_PLAINTEXT = u"PLAINTEXT"
+SIGNATURE_METHODS = (
+ SIGNATURE_METHOD_HMAC,
+ SIGNATURE_METHOD_RSA,
+ SIGNATURE_METHOD_PLAINTEXT,
+)
+
+SIGNATURE_TYPE_AUTH_HEADER = u'AUTH_HEADER'
+SIGNATURE_TYPE_QUERY = u'QUERY'
+SIGNATURE_TYPE_BODY = u'BODY'
+SIGNATURE_TYPES = (
+ SIGNATURE_TYPE_AUTH_HEADER,
+ SIGNATURE_TYPE_QUERY,
+ SIGNATURE_TYPE_BODY,
+)
+
+CONTENT_TYPE_FORM_URLENCODED = u'application/x-www-form-urlencoded'
+CONTENT_TYPES = (
+ CONTENT_TYPE_FORM_URLENCODED,
+)
+
diff --git a/oauthlib/oauth1/rfc5849/parameters.py b/oauthlib/oauth1/rfc5849/parameters.py
index dee23a4..e36dd28 100644
--- a/oauthlib/oauth1/rfc5849/parameters.py
+++ b/oauthlib/oauth1/rfc5849/parameters.py
@@ -11,14 +11,13 @@ This module contains methods related to `section 3.5`_ of the OAuth 1.0a spec.
"""
from urlparse import urlparse, urlunparse
-from . import utils
+from . import constants, utils
from oauthlib.common import extract_params, urlencode
# TODO: do we need filter_params now that oauth_params are handled by Request?
# We can easily pass in just oauth protocol params.
-@utils.filter_params
-def prepare_headers(oauth_params, headers=None, realm=None):
+def prepare_headers(request, realm=None):
"""**Prepare the Authorization header.**
Per `section 3.5.1`_ of the spec.
@@ -41,7 +40,7 @@ def prepare_headers(oauth_params, headers=None, realm=None):
.. _`section 3.5.1`: http://tools.ietf.org/html/rfc5849#section-3.5.1
.. _`RFC2617`: http://tools.ietf.org/html/rfc2617
"""
- headers = headers or {}
+ new_request = request.clone()
# Protocol parameters SHALL be included in the "Authorization" header
# field as follows:
@@ -81,10 +80,8 @@ def prepare_headers(oauth_params, headers=None, realm=None):
authorization_header = u'OAuth %s' % authorization_header_parameters
# contribute the Authorization header to the given headers
- full_headers = {}
- full_headers.update(headers)
- full_headers[u'Authorization'] = authorization_header
- return full_headers
+ new_request.headers[u'Authorization'] = authorization_header
+ return new_request
def _append_params(oauth_params, params):
@@ -108,7 +105,7 @@ def _append_params(oauth_params, params):
return merged
-def prepare_form_encoded_body(oauth_params, body):
+def prepare_form_encoded_body(request, formencode=False):
"""Prepare the Form-Encoded Body.
Per `section 3.5.2`_ of the spec.
@@ -117,10 +114,16 @@ def prepare_form_encoded_body(oauth_params, body):
"""
# append OAuth params to the existing body
- return _append_params(oauth_params, body)
+ new_request = request.clone()
+ body = _append_params(new_request.oauth_params, new_request.body)
+ if formencode:
+ body = urlencode(body)
+ new_request.body = body
+ new_request.headers['Content-Type'] = u'application/x-www-form-urlencoded'
+ return new_request
-def prepare_request_uri_query(oauth_params, uri):
+def prepare_request_uri_query(request):
"""Prepare the Request URI Query.
Per `section 3.5.3`_ of the spec.
@@ -128,7 +131,23 @@ def prepare_request_uri_query(oauth_params, uri):
.. _`section 3.5.3`: http://tools.ietf.org/html/rfc5849#section-3.5.3
"""
+ new_request = request.clone()
+
# append OAuth params to the existing set of query components
- sch, net, path, par, query, fra = urlparse(uri)
- query = urlencode(_append_params(oauth_params, extract_params(query) or []))
- return urlunparse((sch, net, path, par, query, fra))
+ sch, net, path, par, query, fra = urlparse(new_request.uri)
+ query = urlencode(_append_params(new_request.oauth_params,
+ extract_params(query) or []))
+ new_request.uri = urlunparse((sch, net, path, par, query, fra))
+ return new_request
+
+
+PREPARE_BY_SIGNATURE_TYPE = {
+ constants.SIGNATURE_TYPE_AUTH_HEADER: prepare_header,
+ constants.SIGNATURE_TYPE_QUERY: prepare_form_encoded_body,
+ constants.SIGNATURE_TYPE_BODY: prepare_request_uri_query,
+}
+
+
+def prepare_request(request, signature_type):
+ return PREPARE_BY_SIGNATURE_TYPE[signature_type](request)
+