summaryrefslogtreecommitdiff
path: root/oauthlib
diff options
context:
space:
mode:
authorIdan Gazit <idan@gazit.me>2012-03-12 11:45:48 -0700
committerIdan Gazit <idan@gazit.me>2012-03-12 11:45:48 -0700
commitd7fcb99e79bec37b71e36646507175bf06c28653 (patch)
tree77d08555e20f97f5ec2dd5f2d0801bfb41f1738f /oauthlib
parent55c51c87f4557aee58ec093735f318b5da094cc8 (diff)
downloadoauthlib-d7fcb99e79bec37b71e36646507175bf06c28653.tar.gz
Move utility methods into utils.py
Diffstat (limited to 'oauthlib')
-rw-r--r--oauthlib/oauth.py27
-rw-r--r--oauthlib/utils.py55
2 files changed, 52 insertions, 30 deletions
diff --git a/oauthlib/oauth.py b/oauthlib/oauth.py
index a17acc5..bf71546 100644
--- a/oauthlib/oauth.py
+++ b/oauthlib/oauth.py
@@ -21,33 +21,6 @@ SIGNATURE_RSA = "RSA-SHA1"
SIGNATURE_PLAINTEXT = "PLAINTEXT"
-def escape(s):
- """Escape a string in an OAuth-compatible fashion.
-
- Per `section 3.6`_ of the spec.
-
- .. _`section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
-
- """
- return urllib.quote(s.encode('utf-8'), safe='~')
-
-
-def utf8_str(s):
- """Convert unicode to utf-8."""
- if isinstance(s, unicode):
- return s.encode("utf-8")
- else:
- return str(s)
-
-
-def generate_timestamp():
- """Get seconds since epoch (UTC)."""
- return str(int(time.time()))
-
-
-def generate_nonce():
- """Generate pseudorandom nonce that is unlikely to repeat."""
- return str(getrandbits(64)) + generate_timestamp()
def generate_params(client_key, access_token, signature_method):
diff --git a/oauthlib/utils.py b/oauthlib/utils.py
index bcf8534..561e4e0 100644
--- a/oauthlib/utils.py
+++ b/oauthlib/utils.py
@@ -8,14 +8,63 @@ This module contains utility methods used by various parts of the OAuth
spec.
"""
+import time
+import urllib
+from random import getrandbits
-def filter_oauth(params):
- """Removes all non oauth parameters
- :param target: A method with the first arg being params.
+def filter_oauth_params(params):
+ """Removes all non oauth parameters from a dict or a list of params.
"""
is_oauth = lambda kv: kv[0].startswith("oauth_")
if isinstance(params, dict):
return filter(is_oauth, params.items())
else:
return filter(is_oauth, params)
+
+
+def utf8_str(s):
+ """Convert unicode to utf-8."""
+ if isinstance(s, unicode):
+ return s.encode("utf-8")
+ else:
+ return str(s)
+
+
+def generate_timestamp():
+ """Get seconds since epoch (UTC).
+
+ Per `section 3.3`_ of the spec.
+
+ .. _`section 3.3`: http://tools.ietf.org/html/rfc5849#section-3.3
+ """
+ return str(int(time.time()))
+
+
+def generate_nonce():
+ """Generate pseudorandom nonce that is unlikely to repeat.
+
+ Per `section 3.3`_ of the spec.
+
+ A random 64-bit number is appended to the epoch timestamp for both
+ randomness and to decrease the likelihood of collisions.
+
+ .. _`section 3.3`: http://tools.ietf.org/html/rfc5849#section-3.3
+ """
+ return str(getrandbits(64)) + generate_timestamp()
+
+
+def escape(s):
+ """Escape a string in an OAuth-compatible fashion.
+
+ Per `section 3.6`_ of the spec.
+
+ :param s: The string to be escaped.
+ :return: An url encoded string.
+
+ .. _`section 3.6`: http://tools.ietf.org/html/rfc5849#section-3.6
+
+ """
+ if not isinstance(s, unicode):
+ raise ValueError('Only unicode objects are escapable.')
+ return urllib.quote(s.encode('utf-8'), safe='~')