Skip to content
This repository has been archived by the owner on Mar 30, 2023. It is now read-only.

Commit

Permalink
Fix issue oauthlib#86 by adding nonce and timestamp arguments to …
Browse files Browse the repository at this point in the history
…Client

constructor.  In get_oauth_params(), use the explicitly provided nonce and
timestamp if given, otherwise generate them.
  • Loading branch information
warsaw committed Jan 4, 2013
1 parent a9deb08 commit 6d56259
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 14 additions & 3 deletions oauthlib/oauth1/rfc5849/__init__.py
Expand Up @@ -48,7 +48,8 @@ def __init__(self, client_key,
signature_method=SIGNATURE_HMAC,
signature_type=SIGNATURE_TYPE_AUTH_HEADER,
rsa_key=None, verifier=None, realm=None,
convert_to_unicode=False, encoding='utf-8'):
convert_to_unicode=False, encoding='utf-8',
nonce=None, timestamp=None):
if convert_to_unicode:
if isinstance(client_key, bytes_type):
client_key = client_key.decode(encoding)
Expand All @@ -70,6 +71,10 @@ def __init__(self, client_key,
verifier = verifier.decode(encoding)
if isinstance(realm, bytes_type):
realm = realm.decode(encoding)
if isinstance(nonce, bytes_type):
nonce = nonce.decode(encoding)
if isinstance(timestamp, bytes_type):
timestamp = timestamp.decode(encoding)

self.client_key = client_key
self.client_secret = client_secret
Expand All @@ -83,6 +88,8 @@ def __init__(self, client_key,
self.realm = realm
self.convert_to_unicode = convert_to_unicode
self.encoding = encoding
self.nonce = nonce
self.timestamp = timestamp

if self.signature_method == SIGNATURE_RSA and self.rsa_key is None:
raise ValueError('rsa_key is required when using RSA signature method.')
Expand Down Expand Up @@ -128,9 +135,13 @@ def get_oauth_signature(self, request):
def get_oauth_params(self):
"""Get the basic OAuth parameters to be used in generating a signature.
"""
nonce = (generate_nonce()
if self.nonce is None else self.nonce)
timestamp = (generate_timestamp()
if self.timestamp is None else self.timestamp)
params = [
('oauth_nonce', generate_nonce()),
('oauth_timestamp', generate_timestamp()),
('oauth_nonce', nonce),
('oauth_timestamp', timestamp),
('oauth_version', '1.0'),
('oauth_signature_method', self.signature_method),
('oauth_consumer_key', self.client_key),
Expand Down
10 changes: 10 additions & 0 deletions tests/oauth1/rfc5849/test_client.py
Expand Up @@ -37,3 +37,13 @@ def test_convert_to_unicode_resource_owner(self):
convert_to_unicode=True)
self.assertFalse(isinstance(client.resource_owner_key, bytes_type))
self.assertEqual(client.resource_owner_key, 'owner key')

def test_give_explicit_timestamp(self):
client = Client('client-key', timestamp='1')
params = dict(client.get_oauth_params())
self.assertEqual(params['oauth_timestamp'], '1')

def test_give_explicit_nonce(self):
client = Client('client-key', nonce='1')
params = dict(client.get_oauth_params())
self.assertEqual(params['oauth_nonce'], '1')

0 comments on commit 6d56259

Please sign in to comment.