revocation.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. """
  2. oauthlib.oauth2.rfc6749.endpoint.revocation
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. An implementation of the OAuth 2 `Token Revocation`_ spec (draft 11).
  5. .. _`Token Revocation`: https://tools.ietf.org/html/draft-ietf-oauth-revocation-11
  6. """
  7. import logging
  8. from oauthlib.common import Request
  9. from ..errors import OAuth2Error
  10. from .base import BaseEndpoint, catch_errors_and_unavailability
  11. log = logging.getLogger(__name__)
  12. class RevocationEndpoint(BaseEndpoint):
  13. """Token revocation endpoint.
  14. Endpoint used by authenticated clients to revoke access and refresh tokens.
  15. Commonly this will be part of the Authorization Endpoint.
  16. """
  17. valid_token_types = ('access_token', 'refresh_token')
  18. valid_request_methods = ('POST',)
  19. def __init__(self, request_validator, supported_token_types=None,
  20. enable_jsonp=False):
  21. BaseEndpoint.__init__(self)
  22. self.request_validator = request_validator
  23. self.supported_token_types = (
  24. supported_token_types or self.valid_token_types)
  25. self.enable_jsonp = enable_jsonp
  26. @catch_errors_and_unavailability
  27. def create_revocation_response(self, uri, http_method='POST', body=None,
  28. headers=None):
  29. """Revoke supplied access or refresh token.
  30. The authorization server responds with HTTP status code 200 if the
  31. token has been revoked successfully or if the client submitted an
  32. invalid token.
  33. Note: invalid tokens do not cause an error response since the client
  34. cannot handle such an error in a reasonable way. Moreover, the purpose
  35. of the revocation request, invalidating the particular token, is
  36. already achieved.
  37. The content of the response body is ignored by the client as all
  38. necessary information is conveyed in the response code.
  39. An invalid token type hint value is ignored by the authorization server
  40. and does not influence the revocation response.
  41. """
  42. resp_headers = {
  43. 'Content-Type': 'application/json',
  44. 'Cache-Control': 'no-store',
  45. 'Pragma': 'no-cache',
  46. }
  47. request = Request(
  48. uri, http_method=http_method, body=body, headers=headers)
  49. try:
  50. self.validate_revocation_request(request)
  51. log.debug('Token revocation valid for %r.', request)
  52. except OAuth2Error as e:
  53. log.debug('Client error during validation of %r. %r.', request, e)
  54. response_body = e.json
  55. if self.enable_jsonp and request.callback:
  56. response_body = '{}({});'.format(request.callback, response_body)
  57. resp_headers.update(e.headers)
  58. return resp_headers, response_body, e.status_code
  59. self.request_validator.revoke_token(request.token,
  60. request.token_type_hint, request)
  61. response_body = ''
  62. if self.enable_jsonp and request.callback:
  63. response_body = request.callback + '();'
  64. return {}, response_body, 200
  65. def validate_revocation_request(self, request):
  66. """Ensure the request is valid.
  67. The client constructs the request by including the following parameters
  68. using the "application/x-www-form-urlencoded" format in the HTTP
  69. request entity-body:
  70. token (REQUIRED). The token that the client wants to get revoked.
  71. token_type_hint (OPTIONAL). A hint about the type of the token
  72. submitted for revocation. Clients MAY pass this parameter in order to
  73. help the authorization server to optimize the token lookup. If the
  74. server is unable to locate the token using the given hint, it MUST
  75. extend its search across all of its supported token types. An
  76. authorization server MAY ignore this parameter, particularly if it is
  77. able to detect the token type automatically. This specification
  78. defines two such values:
  79. * access_token: An Access Token as defined in [RFC6749],
  80. `section 1.4`_
  81. * refresh_token: A Refresh Token as defined in [RFC6749],
  82. `section 1.5`_
  83. Specific implementations, profiles, and extensions of this
  84. specification MAY define other values for this parameter using
  85. the registry defined in `Section 4.1.2`_.
  86. The client also includes its authentication credentials as described in
  87. `Section 2.3`_. of [`RFC6749`_].
  88. .. _`section 1.4`: https://tools.ietf.org/html/rfc6749#section-1.4
  89. .. _`section 1.5`: https://tools.ietf.org/html/rfc6749#section-1.5
  90. .. _`section 2.3`: https://tools.ietf.org/html/rfc6749#section-2.3
  91. .. _`Section 4.1.2`: https://tools.ietf.org/html/draft-ietf-oauth-revocation-11#section-4.1.2
  92. .. _`RFC6749`: https://tools.ietf.org/html/rfc6749
  93. """
  94. self._raise_on_bad_method(request)
  95. self._raise_on_bad_post_request(request)
  96. self._raise_on_missing_token(request)
  97. self._raise_on_invalid_client(request)
  98. self._raise_on_unsupported_token(request)