Skip to content

Commit

Permalink
Makes signed oauth_signature str rather than bytes
Browse files Browse the repository at this point in the history
When using header auth in py3k (maybe even 2.7, didn't verify), the
oauth_signature was encoded with repr of the bytestring rather than the
string value.

e.g. "b%27somesignature%27"

closes #130
  • Loading branch information
mitgr81 authored and maxcountryman committed Oct 2, 2013
1 parent 8369419 commit 27f331e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
7 changes: 4 additions & 3 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
This project was concieved of with the help of the full software team at Litl.

Special thanks to Kenneth Reitz for the amazing [Requests](https://github.com/kennethreitz/requests) library. It made
this project sane.
this project sane.

Also kudos to Miguel Araujo for utilizing the pre-request hook of Requests in his OAuth 1.0/a library
[requests-oauth](https://github.com/maraujop/requests-oauth) which is used as a model herein for parts of the
Also kudos to Miguel Araujo for utilizing the pre-request hook of Requests in his OAuth 1.0/a library
[requests-oauth](https://github.com/maraujop/requests-oauth) which is used as a model herein for parts of the
OAuth 1.0/a hook; thanks Miguel!


Expand All @@ -32,4 +32,5 @@ Add yourself as a contributor!
* [Marek Rei](https://github.com/marekrei)
* [Kieron Briggs](https://github.com/kbriggs)
* [Tatsuji Tsuchiya](https://github.com/ta2xeo)
* [Chris McGraw](https://github.com/mitgr81)
* (your name here)
4 changes: 2 additions & 2 deletions rauth/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def sign(self,
hashed = hmac.new(key, signature_base_string, sha1)

# return the signature
return base64.b64encode(hashed.digest())
return base64.b64encode(hashed.digest()).decode()


class RsaSha1Signature(SignatureMethod):
Expand Down Expand Up @@ -215,7 +215,7 @@ def sign(self,
hashed = s.sign(h)

# return the signature
return base64.b64encode(hashed)
return base64.b64encode(hashed).decode()


class PlaintextSignature(SignatureMethod):
Expand Down
23 changes: 14 additions & 9 deletions tests/test_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
except ImportError:
raise RuntimeError('PyCrypto is required to run the rauth test suite')

try:
stringtype = unicode # python 2
except NameError:
stringtype = str # python 3

assert RSA


Expand All @@ -36,8 +41,8 @@ def test_hmacsha1_signature(self):
self.oauth_params,
self.req_kwargs)
self.assertIsNotNone(oauth_signature)
self.assertIsInstance(oauth_signature, bytes)
self.assertEqual(oauth_signature, b'cYzjVXCOk62KoYmJ+iCvcAcgfp8=')
self.assertIsInstance(oauth_signature, stringtype)
self.assertEqual(oauth_signature, 'cYzjVXCOk62KoYmJ+iCvcAcgfp8=')

def test_normalize_request_parameters_params(self):
# params as a dict
Expand Down Expand Up @@ -98,7 +103,7 @@ def test_sign_utf8_encoded_string(self):
self.url,
self.oauth_params,
req_kwargs)
self.assertEqual(b'cYzjVXCOk62KoYmJ+iCvcAcgfp8=', sig)
self.assertEqual('cYzjVXCOk62KoYmJ+iCvcAcgfp8=', sig)

def test_sign_with_data(self):
# in the event a string is already UTF-8
Expand All @@ -110,7 +115,7 @@ def test_sign_with_data(self):
self.url,
self.oauth_params,
req_kwargs)
self.assertEqual(b'JzmJUmqjdNYBJsJWbtQKXnc0W8w=', sig)
self.assertEqual('JzmJUmqjdNYBJsJWbtQKXnc0W8w=', sig)

def test_remove_query_string(self):
# can't sign the URL with the query string so
Expand Down Expand Up @@ -186,12 +191,12 @@ def test_rsasha1_signature(self):
self.oauth_params,
self.req_kwargs)
self.assertIsNotNone(oauth_signature)
self.assertIsInstance(oauth_signature, bytes)
self.assertIsInstance(oauth_signature, stringtype)
self.assertEqual(oauth_signature,
b'MEnbOKBw0lWi5NvGyrABQ6tPygWiNOjGz47y8d+SQfXYrzsvK'
b'kzcMgt2VGBRgKsKSdFho36TuCuP75Qe1uou6/rhHrZoSppQ+6'
b'vdPSKkriGzSK3azqBacg9ZIIVy/atHPTm6BAvo+0v4ysiI9ci'
b'7hJbRkXL0NJVz/p0ZQKO/Jds=')
'MEnbOKBw0lWi5NvGyrABQ6tPygWiNOjGz47y8d+SQfXYrzsvK'
'kzcMgt2VGBRgKsKSdFho36TuCuP75Qe1uou6/rhHrZoSppQ+6'
'vdPSKkriGzSK3azqBacg9ZIIVy/atHPTm6BAvo+0v4ysiI9ci'
'7hJbRkXL0NJVz/p0ZQKO/Jds=')

def test_rsasha1_badargument(self):
self.assertRaises(ValueError, RsaSha1Signature().sign,
Expand Down

0 comments on commit 27f331e

Please sign in to comment.