test_client.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # -*- coding: utf-8 -*-
  2. from oauthlib.common import Request
  3. from oauthlib.oauth1 import (
  4. SIGNATURE_HMAC_SHA1, SIGNATURE_HMAC_SHA256, SIGNATURE_PLAINTEXT,
  5. SIGNATURE_RSA, SIGNATURE_TYPE_BODY, SIGNATURE_TYPE_QUERY,
  6. )
  7. from oauthlib.oauth1.rfc5849 import Client
  8. from tests.unittest import TestCase
  9. class ClientRealmTests(TestCase):
  10. def test_client_no_realm(self):
  11. client = Client("client-key")
  12. uri, header, body = client.sign("http://example-uri")
  13. self.assertTrue(
  14. header["Authorization"].startswith('OAuth oauth_nonce='))
  15. def test_client_realm_sign_with_default_realm(self):
  16. client = Client("client-key", realm="moo-realm")
  17. self.assertEqual(client.realm, "moo-realm")
  18. uri, header, body = client.sign("http://example-uri")
  19. self.assertTrue(
  20. header["Authorization"].startswith('OAuth realm="moo-realm",'))
  21. def test_client_realm_sign_with_additional_realm(self):
  22. client = Client("client-key", realm="moo-realm")
  23. uri, header, body = client.sign("http://example-uri", realm="baa-realm")
  24. self.assertTrue(
  25. header["Authorization"].startswith('OAuth realm="baa-realm",'))
  26. # make sure sign() does not override the default realm
  27. self.assertEqual(client.realm, "moo-realm")
  28. class ClientConstructorTests(TestCase):
  29. def test_convert_to_unicode_resource_owner(self):
  30. client = Client('client-key',
  31. resource_owner_key=b'owner key')
  32. self.assertNotIsInstance(client.resource_owner_key, bytes)
  33. self.assertEqual(client.resource_owner_key, 'owner key')
  34. def test_give_explicit_timestamp(self):
  35. client = Client('client-key', timestamp='1')
  36. params = dict(client.get_oauth_params(Request('http://example.com')))
  37. self.assertEqual(params['oauth_timestamp'], '1')
  38. def test_give_explicit_nonce(self):
  39. client = Client('client-key', nonce='1')
  40. params = dict(client.get_oauth_params(Request('http://example.com')))
  41. self.assertEqual(params['oauth_nonce'], '1')
  42. def test_decoding(self):
  43. client = Client('client_key', decoding='utf-8')
  44. uri, headers, body = client.sign('http://a.b/path?query',
  45. http_method='POST', body='a=b',
  46. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  47. self.assertIsInstance(uri, bytes)
  48. self.assertIsInstance(body, bytes)
  49. for k, v in headers.items():
  50. self.assertIsInstance(k, bytes)
  51. self.assertIsInstance(v, bytes)
  52. def test_hmac_sha1(self):
  53. client = Client('client_key')
  54. # instance is using the correct signer method
  55. self.assertEqual(Client.SIGNATURE_METHODS[SIGNATURE_HMAC_SHA1],
  56. client.SIGNATURE_METHODS[client.signature_method])
  57. def test_hmac_sha256(self):
  58. client = Client('client_key', signature_method=SIGNATURE_HMAC_SHA256)
  59. # instance is using the correct signer method
  60. self.assertEqual(Client.SIGNATURE_METHODS[SIGNATURE_HMAC_SHA256],
  61. client.SIGNATURE_METHODS[client.signature_method])
  62. def test_rsa(self):
  63. client = Client('client_key', signature_method=SIGNATURE_RSA)
  64. # instance is using the correct signer method
  65. self.assertEqual(Client.SIGNATURE_METHODS[SIGNATURE_RSA],
  66. client.SIGNATURE_METHODS[client.signature_method])
  67. # don't need an RSA key to instantiate
  68. self.assertIsNone(client.rsa_key)
  69. class SignatureMethodTest(TestCase):
  70. def test_hmac_sha1_method(self):
  71. client = Client('client_key', timestamp='1234567890', nonce='abc')
  72. u, h, b = client.sign('http://example.com')
  73. correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
  74. 'oauth_version="1.0", oauth_signature_method="HMAC-SHA1", '
  75. 'oauth_consumer_key="client_key", '
  76. 'oauth_signature="hH5BWYVqo7QI4EmPBUUe9owRUUQ%3D"')
  77. self.assertEqual(h['Authorization'], correct)
  78. def test_hmac_sha256_method(self):
  79. client = Client('client_key', signature_method=SIGNATURE_HMAC_SHA256,
  80. timestamp='1234567890', nonce='abc')
  81. u, h, b = client.sign('http://example.com')
  82. correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
  83. 'oauth_version="1.0", oauth_signature_method="HMAC-SHA256", '
  84. 'oauth_consumer_key="client_key", '
  85. 'oauth_signature="JzgJWBxX664OiMW3WE4MEjtYwOjI%2FpaUWHqtdHe68Es%3D"')
  86. self.assertEqual(h['Authorization'], correct)
  87. def test_rsa_method(self):
  88. private_key = (
  89. "-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKBgQDk1/bxy"
  90. "S8Q8jiheHeYYp/4rEKJopeQRRKKpZI4s5i+UPwVpupG\nAlwXWfzXw"
  91. "SMaKPAoKJNdu7tqKRniqst5uoHXw98gj0x7zamu0Ck1LtQ4c7pFMVa"
  92. "h\n5IYGhBi2E9ycNS329W27nJPWNCbESTu7snVlG8V8mfvGGg3xNjT"
  93. "MO7IdrwIDAQAB\nAoGBAOQ2KuH8S5+OrsL4K+wfjoCi6MfxCUyqVU9"
  94. "GxocdM1m30WyWRFMEz2nKJ8fR\np3vTD4w8yplTOhcoXdQZl0kRoaD"
  95. "zrcYkm2VvJtQRrX7dKFT8dR8D/Tr7dNQLOXfC\nDY6xveQczE7qt7V"
  96. "k7lp4FqmxBsaaEuokt78pOOjywZoInjZhAkEA9wz3zoZNT0/i\nrf6"
  97. "qv2qTIeieUB035N3dyw6f1BGSWYaXSuerDCD/J1qZbAPKKhyHZbVaw"
  98. "Ft3UMhe\n542UftBaxQJBAO0iJy1I8GQjGnS7B3yvyH3CcLYGy296+"
  99. "XO/2xKp/d/ty1OIeovx\nC60pLNwuFNF3z9d2GVQAdoQ89hUkOtjZL"
  100. "eMCQQD0JO6oPHUeUjYT+T7ImAv7UKVT\nSuy30sKjLzqoGw1kR+wv7"
  101. "C5PeDRvscs4wa4CW9s6mjSrMDkDrmCLuJDtmf55AkEA\nkmaMg2PNr"
  102. "jUR51F0zOEFycaaqXbGcFwe1/xx9zLmHzMDXd4bsnwt9kk+fe0hQzV"
  103. "S\nJzatanQit3+feev1PN3QewJAWv4RZeavEUhKv+kLe95Yd0su7lT"
  104. "LVduVgh4v5yLT\nGa6FHdjGPcfajt+nrpB1n8UQBEH9ZxniokR/IPv"
  105. "dMlxqXA==\n-----END RSA PRIVATE KEY-----"
  106. )
  107. client = Client('client_key', signature_method=SIGNATURE_RSA,
  108. rsa_key=private_key, timestamp='1234567890', nonce='abc')
  109. u, h, b = client.sign('http://example.com')
  110. correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
  111. 'oauth_version="1.0", oauth_signature_method="RSA-SHA1", '
  112. 'oauth_consumer_key="client_key", '
  113. 'oauth_signature="ktvzkUhtrIawBcq21DRJrAyysTc3E1Zq5GdGu8EzH'
  114. 'OtbeaCmOBDLGHAcqlm92mj7xp5E1Z6i2vbExPimYAJL7FzkLnkRE5YEJR4'
  115. 'rNtIgAf1OZbYsIUmmBO%2BCLuStuu5Lg3tAluwC7XkkgoXCBaRKT1mUXzP'
  116. 'HJILzZ8iFOvS6w5E%3D"')
  117. self.assertEqual(h['Authorization'], correct)
  118. def test_plaintext_method(self):
  119. client = Client('client_key',
  120. signature_method=SIGNATURE_PLAINTEXT,
  121. timestamp='1234567890',
  122. nonce='abc',
  123. client_secret='foo',
  124. resource_owner_secret='bar')
  125. u, h, b = client.sign('http://example.com')
  126. correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
  127. 'oauth_version="1.0", oauth_signature_method="PLAINTEXT", '
  128. 'oauth_consumer_key="client_key", '
  129. 'oauth_signature="foo%26bar"')
  130. self.assertEqual(h['Authorization'], correct)
  131. def test_invalid_method(self):
  132. client = Client('client_key', signature_method='invalid')
  133. self.assertRaises(ValueError, client.sign, 'http://example.com')
  134. def test_rsa_no_key(self):
  135. client = Client('client_key', signature_method=SIGNATURE_RSA)
  136. self.assertRaises(ValueError, client.sign, 'http://example.com')
  137. def test_register_method(self):
  138. Client.register_signature_method('PIZZA',
  139. lambda base_string, client: 'PIZZA')
  140. self.assertIn('PIZZA', Client.SIGNATURE_METHODS)
  141. client = Client('client_key', signature_method='PIZZA',
  142. timestamp='1234567890', nonce='abc')
  143. u, h, b = client.sign('http://example.com')
  144. self.assertEqual(h['Authorization'], (
  145. 'OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
  146. 'oauth_version="1.0", oauth_signature_method="PIZZA", '
  147. 'oauth_consumer_key="client_key", '
  148. 'oauth_signature="PIZZA"'
  149. ))
  150. class SignatureTypeTest(TestCase):
  151. def test_params_in_body(self):
  152. client = Client('client_key', signature_type=SIGNATURE_TYPE_BODY,
  153. timestamp='1378988215', nonce='14205877133089081931378988215')
  154. _, h, b = client.sign('http://i.b/path', http_method='POST', body='a=b',
  155. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  156. self.assertEqual(h['Content-Type'], 'application/x-www-form-urlencoded')
  157. correct = ('a=b&oauth_nonce=14205877133089081931378988215&'
  158. 'oauth_timestamp=1378988215&'
  159. 'oauth_version=1.0&'
  160. 'oauth_signature_method=HMAC-SHA1&'
  161. 'oauth_consumer_key=client_key&'
  162. 'oauth_signature=2JAQomgbShqoscqKWBiYQZwWq94%3D')
  163. self.assertEqual(b, correct)
  164. def test_params_in_query(self):
  165. client = Client('client_key', signature_type=SIGNATURE_TYPE_QUERY,
  166. timestamp='1378988215', nonce='14205877133089081931378988215')
  167. u, _, _ = client.sign('http://i.b/path', http_method='POST')
  168. correct = ('http://i.b/path?oauth_nonce=14205877133089081931378988215&'
  169. 'oauth_timestamp=1378988215&'
  170. 'oauth_version=1.0&'
  171. 'oauth_signature_method=HMAC-SHA1&'
  172. 'oauth_consumer_key=client_key&'
  173. 'oauth_signature=08G5Snvw%2BgDAzBF%2BCmT5KqlrPKo%3D')
  174. self.assertEqual(u, correct)
  175. def test_invalid_signature_type(self):
  176. client = Client('client_key', signature_type='invalid')
  177. self.assertRaises(ValueError, client.sign, 'http://i.b/path')
  178. class SigningTest(TestCase):
  179. def test_case_insensitive_headers(self):
  180. client = Client('client_key')
  181. # Uppercase
  182. _, h, _ = client.sign('http://i.b/path', http_method='POST', body='',
  183. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  184. self.assertEqual(h['Content-Type'], 'application/x-www-form-urlencoded')
  185. # Lowercase
  186. _, h, _ = client.sign('http://i.b/path', http_method='POST', body='',
  187. headers={'content-type': 'application/x-www-form-urlencoded'})
  188. self.assertEqual(h['content-type'], 'application/x-www-form-urlencoded')
  189. # Capitalized
  190. _, h, _ = client.sign('http://i.b/path', http_method='POST', body='',
  191. headers={'Content-type': 'application/x-www-form-urlencoded'})
  192. self.assertEqual(h['Content-type'], 'application/x-www-form-urlencoded')
  193. # Random
  194. _, h, _ = client.sign('http://i.b/path', http_method='POST', body='',
  195. headers={'conTent-tYpe': 'application/x-www-form-urlencoded'})
  196. self.assertEqual(h['conTent-tYpe'], 'application/x-www-form-urlencoded')
  197. def test_sign_no_body(self):
  198. client = Client('client_key', decoding='utf-8')
  199. self.assertRaises(ValueError, client.sign, 'http://i.b/path',
  200. http_method='POST', body=None,
  201. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  202. def test_sign_body(self):
  203. client = Client('client_key')
  204. _, h, b = client.sign('http://i.b/path', http_method='POST', body='',
  205. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  206. self.assertEqual(h['Content-Type'], 'application/x-www-form-urlencoded')
  207. def test_sign_get_with_body(self):
  208. client = Client('client_key')
  209. for method in ('GET', 'HEAD'):
  210. self.assertRaises(ValueError, client.sign, 'http://a.b/path?query',
  211. http_method=method, body='a=b',
  212. headers={
  213. 'Content-Type': 'application/x-www-form-urlencoded'
  214. })
  215. def test_sign_unicode(self):
  216. client = Client('client_key', nonce='abc', timestamp='abc')
  217. _, h, b = client.sign('http://i.b/path', http_method='POST',
  218. body='status=%E5%95%A6%E5%95%A6',
  219. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  220. self.assertEqual(b, 'status=%E5%95%A6%E5%95%A6')
  221. self.assertIn('oauth_signature="yrtSqp88m%2Fc5UDaucI8BXK4oEtk%3D"', h['Authorization'])
  222. _, h, b = client.sign('http://i.b/path', http_method='POST',
  223. body='status=%C3%A6%C3%A5%C3%B8',
  224. headers={'Content-Type': 'application/x-www-form-urlencoded'})
  225. self.assertEqual(b, 'status=%C3%A6%C3%A5%C3%B8')
  226. self.assertIn('oauth_signature="oG5t3Eg%2FXO5FfQgUUlTtUeeZzvk%3D"', h['Authorization'])