test_tokens.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. from unittest import mock
  2. from oauthlib.common import Request
  3. from oauthlib.oauth2.rfc6749.tokens import (
  4. BearerToken, prepare_bearer_body, prepare_bearer_headers,
  5. prepare_bearer_uri, prepare_mac_header,
  6. )
  7. from tests.unittest import TestCase
  8. class TokenTest(TestCase):
  9. # MAC without body/payload or extension
  10. mac_plain = {
  11. 'token': 'h480djs93hd8',
  12. 'uri': 'http://example.com/resource/1?b=1&a=2',
  13. 'key': '489dks293j39',
  14. 'http_method': 'GET',
  15. 'nonce': '264095:dj83hs9s',
  16. 'hash_algorithm': 'hmac-sha-1'
  17. }
  18. auth_plain = {
  19. 'Authorization': 'MAC id="h480djs93hd8", nonce="264095:dj83hs9s",'
  20. ' mac="SLDJd4mg43cjQfElUs3Qub4L6xE="'
  21. }
  22. # MAC with body/payload, no extension
  23. mac_body = {
  24. 'token': 'jd93dh9dh39D',
  25. 'uri': 'http://example.com/request',
  26. 'key': '8yfrufh348h',
  27. 'http_method': 'POST',
  28. 'nonce': '273156:di3hvdf8',
  29. 'hash_algorithm': 'hmac-sha-1',
  30. 'body': 'hello=world%21'
  31. }
  32. auth_body = {
  33. 'Authorization': 'MAC id="jd93dh9dh39D", nonce="273156:di3hvdf8",'
  34. ' bodyhash="k9kbtCIy0CkI3/FEfpS/oIDjk6k=", mac="W7bdMZbv9UWOTadASIQHagZyirA="'
  35. }
  36. # MAC with body/payload and extension
  37. mac_both = {
  38. 'token': 'h480djs93hd8',
  39. 'uri': 'http://example.com/request?b5=%3D%253D&a3=a&c%40=&a2=r%20b&c2&a3=2+q',
  40. 'key': '489dks293j39',
  41. 'http_method': 'GET',
  42. 'nonce': '264095:7d8f3e4a',
  43. 'hash_algorithm': 'hmac-sha-1',
  44. 'body': 'Hello World!',
  45. 'ext': 'a,b,c'
  46. }
  47. auth_both = {
  48. 'Authorization': 'MAC id="h480djs93hd8", nonce="264095:7d8f3e4a",'
  49. ' bodyhash="Lve95gjOVATpfV8EL5X4nxwjKHE=", ext="a,b,c",'
  50. ' mac="Z3C2DojEopRDIC88/imW8Ez853g="'
  51. }
  52. # Bearer
  53. token = 'vF9dft4qmT'
  54. uri = 'http://server.example.com/resource'
  55. bearer_headers = {
  56. 'Authorization': 'Bearer vF9dft4qmT'
  57. }
  58. valid_bearer_header_lowercase = {"Authorization": "bearer vF9dft4qmT"}
  59. fake_bearer_headers = [
  60. {'Authorization': 'Beaver vF9dft4qmT'},
  61. {'Authorization': 'BeavervF9dft4qmT'},
  62. {'Authorization': 'Beaver vF9dft4qmT'},
  63. {'Authorization': 'BearerF9dft4qmT'},
  64. {'Authorization': 'Bearer vF9d ft4qmT'},
  65. ]
  66. valid_header_with_multiple_spaces = {'Authorization': 'Bearer vF9dft4qmT'}
  67. bearer_body = 'access_token=vF9dft4qmT'
  68. bearer_uri = 'http://server.example.com/resource?access_token=vF9dft4qmT'
  69. def _mocked_validate_bearer_token(self, token, scopes, request):
  70. if not token:
  71. return False
  72. return True
  73. def test_prepare_mac_header(self):
  74. """Verify mac signatures correctness
  75. TODO: verify hmac-sha-256
  76. """
  77. self.assertEqual(prepare_mac_header(**self.mac_plain), self.auth_plain)
  78. self.assertEqual(prepare_mac_header(**self.mac_body), self.auth_body)
  79. self.assertEqual(prepare_mac_header(**self.mac_both), self.auth_both)
  80. def test_prepare_bearer_request(self):
  81. """Verify proper addition of bearer tokens to requests.
  82. They may be represented as query components in body or URI or
  83. in a Bearer authorization header.
  84. """
  85. self.assertEqual(prepare_bearer_headers(self.token), self.bearer_headers)
  86. self.assertEqual(prepare_bearer_body(self.token), self.bearer_body)
  87. self.assertEqual(prepare_bearer_uri(self.token, uri=self.uri), self.bearer_uri)
  88. def test_valid_bearer_is_validated(self):
  89. request_validator = mock.MagicMock()
  90. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  91. request = Request("/", headers=self.bearer_headers)
  92. result = BearerToken(request_validator=request_validator).validate_request(
  93. request
  94. )
  95. self.assertTrue(result)
  96. def test_lowercase_bearer_is_validated(self):
  97. request_validator = mock.MagicMock()
  98. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  99. request = Request("/", headers=self.valid_bearer_header_lowercase)
  100. result = BearerToken(request_validator=request_validator).validate_request(
  101. request
  102. )
  103. self.assertTrue(result)
  104. def test_fake_bearer_is_not_validated(self):
  105. request_validator = mock.MagicMock()
  106. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  107. for fake_header in self.fake_bearer_headers:
  108. request = Request("/", headers=fake_header)
  109. result = BearerToken(request_validator=request_validator).validate_request(
  110. request
  111. )
  112. self.assertFalse(result)
  113. def test_header_with_multispaces_is_validated(self):
  114. request_validator = mock.MagicMock()
  115. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  116. request = Request("/", headers=self.valid_header_with_multiple_spaces)
  117. result = BearerToken(request_validator=request_validator).validate_request(
  118. request
  119. )
  120. self.assertTrue(result)
  121. def test_estimate_type(self):
  122. request_validator = mock.MagicMock()
  123. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  124. request = Request("/", headers=self.bearer_headers)
  125. result = BearerToken(request_validator=request_validator).estimate_type(request)
  126. self.assertEqual(result, 9)
  127. def test_estimate_type_with_fake_header_returns_type_0(self):
  128. request_validator = mock.MagicMock()
  129. request_validator.validate_bearer_token = self._mocked_validate_bearer_token
  130. for fake_header in self.fake_bearer_headers:
  131. request = Request("/", headers=fake_header)
  132. result = BearerToken(request_validator=request_validator).estimate_type(
  133. request
  134. )
  135. if (
  136. fake_header["Authorization"].count(" ") == 2
  137. and fake_header["Authorization"].split()[0] == "Bearer"
  138. ):
  139. # If we're dealing with the header containing 2 spaces, it will be recognized
  140. # as a Bearer valid header, the token itself will be invalid by the way.
  141. self.assertEqual(result, 9)
  142. else:
  143. self.assertEqual(result, 0)