test_revocation_endpoint.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # -*- coding: utf-8 -*-
  2. from json import loads
  3. from unittest.mock import MagicMock
  4. from oauthlib.common import urlencode
  5. from oauthlib.oauth2 import RequestValidator, RevocationEndpoint
  6. from tests.unittest import TestCase
  7. class RevocationEndpointTest(TestCase):
  8. def setUp(self):
  9. self.validator = MagicMock(wraps=RequestValidator())
  10. self.validator.client_authentication_required.return_value = True
  11. self.validator.authenticate_client.return_value = True
  12. self.validator.revoke_token.return_value = True
  13. self.endpoint = RevocationEndpoint(self.validator)
  14. self.uri = 'https://example.com/revoke_token'
  15. self.headers = {
  16. 'Content-Type': 'application/x-www-form-urlencoded',
  17. }
  18. self.resp_h = {
  19. 'Cache-Control': 'no-store',
  20. 'Content-Type': 'application/json',
  21. 'Pragma': 'no-cache'
  22. }
  23. def test_revoke_token(self):
  24. for token_type in ('access_token', 'refresh_token', 'invalid'):
  25. body = urlencode([('token', 'foo'),
  26. ('token_type_hint', token_type)])
  27. h, b, s = self.endpoint.create_revocation_response(self.uri,
  28. headers=self.headers, body=body)
  29. self.assertEqual(h, {})
  30. self.assertEqual(b, '')
  31. self.assertEqual(s, 200)
  32. # don't specify token_type_hint
  33. body = urlencode([('token', 'foo')])
  34. h, b, s = self.endpoint.create_revocation_response(self.uri,
  35. headers=self.headers, body=body)
  36. self.assertEqual(h, {})
  37. self.assertEqual(b, '')
  38. self.assertEqual(s, 200)
  39. def test_revoke_token_client_authentication_failed(self):
  40. self.validator.authenticate_client.return_value = False
  41. body = urlencode([('token', 'foo'),
  42. ('token_type_hint', 'access_token')])
  43. h, b, s = self.endpoint.create_revocation_response(self.uri,
  44. headers=self.headers, body=body)
  45. self.assertEqual(h, {
  46. 'Content-Type': 'application/json',
  47. 'Cache-Control': 'no-store',
  48. 'Pragma': 'no-cache',
  49. "WWW-Authenticate": 'Bearer error="invalid_client"'
  50. })
  51. self.assertEqual(loads(b)['error'], 'invalid_client')
  52. self.assertEqual(s, 401)
  53. def test_revoke_token_public_client_authentication(self):
  54. self.validator.client_authentication_required.return_value = False
  55. self.validator.authenticate_client_id.return_value = True
  56. for token_type in ('access_token', 'refresh_token', 'invalid'):
  57. body = urlencode([('token', 'foo'),
  58. ('token_type_hint', token_type)])
  59. h, b, s = self.endpoint.create_revocation_response(self.uri,
  60. headers=self.headers, body=body)
  61. self.assertEqual(h, {})
  62. self.assertEqual(b, '')
  63. self.assertEqual(s, 200)
  64. def test_revoke_token_public_client_authentication_failed(self):
  65. self.validator.client_authentication_required.return_value = False
  66. self.validator.authenticate_client_id.return_value = False
  67. body = urlencode([('token', 'foo'),
  68. ('token_type_hint', 'access_token')])
  69. h, b, s = self.endpoint.create_revocation_response(self.uri,
  70. headers=self.headers, body=body)
  71. self.assertEqual(h, {
  72. 'Content-Type': 'application/json',
  73. 'Cache-Control': 'no-store',
  74. 'Pragma': 'no-cache',
  75. "WWW-Authenticate": 'Bearer error="invalid_client"'
  76. })
  77. self.assertEqual(loads(b)['error'], 'invalid_client')
  78. self.assertEqual(s, 401)
  79. def test_revoke_with_callback(self):
  80. endpoint = RevocationEndpoint(self.validator, enable_jsonp=True)
  81. callback = 'package.hello_world'
  82. for token_type in ('access_token', 'refresh_token', 'invalid'):
  83. body = urlencode([('token', 'foo'),
  84. ('token_type_hint', token_type),
  85. ('callback', callback)])
  86. h, b, s = endpoint.create_revocation_response(self.uri,
  87. headers=self.headers, body=body)
  88. self.assertEqual(h, {})
  89. self.assertEqual(b, callback + '();')
  90. self.assertEqual(s, 200)
  91. def test_revoke_unsupported_token(self):
  92. endpoint = RevocationEndpoint(self.validator,
  93. supported_token_types=['access_token'])
  94. body = urlencode([('token', 'foo'),
  95. ('token_type_hint', 'refresh_token')])
  96. h, b, s = endpoint.create_revocation_response(self.uri,
  97. headers=self.headers, body=body)
  98. self.assertEqual(h, self.resp_h)
  99. self.assertEqual(loads(b)['error'], 'unsupported_token_type')
  100. self.assertEqual(s, 400)
  101. h, b, s = endpoint.create_revocation_response(self.uri,
  102. headers=self.headers, body='')
  103. self.assertEqual(h, self.resp_h)
  104. self.assertEqual(loads(b)['error'], 'invalid_request')
  105. self.assertEqual(s, 400)
  106. def test_revoke_invalid_request_method(self):
  107. endpoint = RevocationEndpoint(self.validator,
  108. supported_token_types=['access_token'])
  109. test_methods = ['GET', 'pUt', 'dEleTe', 'paTcH']
  110. test_methods = test_methods + [x.lower() for x in test_methods] + [x.upper() for x in test_methods]
  111. for method in test_methods:
  112. body = urlencode([('token', 'foo'),
  113. ('token_type_hint', 'refresh_token')])
  114. h, b, s = endpoint.create_revocation_response(self.uri,
  115. http_method = method, headers=self.headers, body=body)
  116. self.assertEqual(h, self.resp_h)
  117. self.assertEqual(loads(b)['error'], 'invalid_request')
  118. self.assertIn('Unsupported request method', loads(b)['error_description'])
  119. self.assertEqual(s, 400)
  120. def test_revoke_bad_post_request(self):
  121. endpoint = RevocationEndpoint(self.validator,
  122. supported_token_types=['access_token'])
  123. for param in ['token', 'secret', 'code', 'foo']:
  124. uri = 'http://some.endpoint?' + urlencode([(param, 'secret')])
  125. body = urlencode([('token', 'foo'),
  126. ('token_type_hint', 'access_token')])
  127. h, b, s = endpoint.create_revocation_response(uri,
  128. headers=self.headers, body=body)
  129. self.assertEqual(h, self.resp_h)
  130. self.assertEqual(loads(b)['error'], 'invalid_request')
  131. self.assertIn('query parameters are not allowed', loads(b)['error_description'])
  132. self.assertEqual(s, 400)