1 # -*- coding: utf-8 -*-
3 oauthlib.oauth2.rfc6749.endpoint.revocation
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 An implementation of the OAuth 2 `Token Revocation`_ spec (draft 11).
8 .. _`Token Revocation`: http://tools.ietf.org/html/draft-ietf-oauth-revocation-11
10 from __future__ import absolute_import, unicode_literals
14 from oauthlib.common import Request
16 from .base import BaseEndpoint, catch_errors_and_unavailability
17 from ..errors import InvalidClientError, UnsupportedTokenTypeError
18 from ..errors import InvalidRequestError, OAuth2Error
20 log = logging.getLogger(__name__)
23 class RevocationEndpoint(BaseEndpoint):
25 """Token revocation endpoint.
27 Endpoint used by authenticated clients to revoke access and refresh tokens.
28 Commonly this will be part of the Authorization Endpoint.
31 valid_token_types = ('access_token', 'refresh_token')
33 def __init__(self, request_validator, supported_token_types=None,
35 BaseEndpoint.__init__(self)
36 self.request_validator = request_validator
37 self.supported_token_types = (
38 supported_token_types or self.valid_token_types)
39 self.enable_jsonp = enable_jsonp
41 @catch_errors_and_unavailability
42 def create_revocation_response(self, uri, http_method='POST', body=None,
44 """Revoke supplied access or refresh token.
47 The authorization server responds with HTTP status code 200 if the
48 token has been revoked sucessfully or if the client submitted an
51 Note: invalid tokens do not cause an error response since the client
52 cannot handle such an error in a reasonable way. Moreover, the purpose
53 of the revocation request, invalidating the particular token, is
56 The content of the response body is ignored by the client as all
57 necessary information is conveyed in the response code.
59 An invalid token type hint value is ignored by the authorization server
60 and does not influence the revocation response.
63 uri, http_method=http_method, body=body, headers=headers)
65 self.validate_revocation_request(request)
66 log.debug('Token revocation valid for %r.', request)
67 except OAuth2Error as e:
68 log.debug('Client error during validation of %r. %r.', request, e)
69 response_body = e.json
70 if self.enable_jsonp and request.callback:
71 response_body = '%s(%s);' % (request.callback, response_body)
72 return {}, response_body, e.status_code
74 self.request_validator.revoke_token(request.token,
75 request.token_type_hint, request)
78 if self.enable_jsonp and request.callback:
79 response_body = request.callback + '();'
80 return {}, response_body, 200
82 def validate_revocation_request(self, request):
83 """Ensure the request is valid.
85 The client constructs the request by including the following parameters
86 using the "application/x-www-form-urlencoded" format in the HTTP
89 token (REQUIRED). The token that the client wants to get revoked.
91 token_type_hint (OPTIONAL). A hint about the type of the token
92 submitted for revocation. Clients MAY pass this parameter in order to
93 help the authorization server to optimize the token lookup. If the
94 server is unable to locate the token using the given hint, it MUST
95 extend its search accross all of its supported token types. An
96 authorization server MAY ignore this parameter, particularly if it is
97 able to detect the token type automatically. This specification
98 defines two such values:
100 * access_token: An Access Token as defined in [RFC6749],
103 * refresh_token: A Refresh Token as defined in [RFC6749],
106 Specific implementations, profiles, and extensions of this
107 specification MAY define other values for this parameter using
108 the registry defined in `Section 4.1.2`_.
110 The client also includes its authentication credentials as described in
111 `Section 2.3`_. of [`RFC6749`_].
113 .. _`section 1.4`: http://tools.ietf.org/html/rfc6749#section-1.4
114 .. _`section 1.5`: http://tools.ietf.org/html/rfc6749#section-1.5
115 .. _`section 2.3`: http://tools.ietf.org/html/rfc6749#section-2.3
116 .. _`Section 4.1.2`: http://tools.ietf.org/html/draft-ietf-oauth-revocation-11#section-4.1.2
117 .. _`RFC6749`: http://tools.ietf.org/html/rfc6749
119 if not request.token:
120 raise InvalidRequestError(request=request,
121 description='Missing token parameter.')
123 if self.request_validator.client_authentication_required(request):
124 if not self.request_validator.authenticate_client(request):
125 raise InvalidClientError(request=request)
127 if (request.token_type_hint and
128 request.token_type_hint in self.valid_token_types and
129 request.token_type_hint not in self.supported_token_types):
130 raise UnsupportedTokenTypeError(request=request)