Updated packages and code to python3. Won't work with python 2
[twitter-api-cdsw] / oauthlib / oauth2 / rfc6749 / endpoints / revocation.py
1 # -*- coding: utf-8 -*-
2 """
3 oauthlib.oauth2.rfc6749.endpoint.revocation
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 An implementation of the OAuth 2 `Token Revocation`_ spec (draft 11).
7
8 .. _`Token Revocation`: http://tools.ietf.org/html/draft-ietf-oauth-revocation-11
9 """
10 from __future__ import absolute_import, unicode_literals
11
12 import logging
13
14 from oauthlib.common import Request
15
16 from .base import BaseEndpoint, catch_errors_and_unavailability
17 from ..errors import InvalidClientError, UnsupportedTokenTypeError
18 from ..errors import InvalidRequestError, OAuth2Error
19
20 log = logging.getLogger(__name__)
21
22
23 class RevocationEndpoint(BaseEndpoint):
24
25     """Token revocation endpoint.
26
27     Endpoint used by authenticated clients to revoke access and refresh tokens.
28     Commonly this will be part of the Authorization Endpoint.
29     """
30
31     valid_token_types = ('access_token', 'refresh_token')
32
33     def __init__(self, request_validator, supported_token_types=None,
34             enable_jsonp=False):
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
40
41     @catch_errors_and_unavailability
42     def create_revocation_response(self, uri, http_method='POST', body=None,
43                                    headers=None):
44         """Revoke supplied access or refresh token.
45
46
47         The authorization server responds with HTTP status code 200 if the
48         token has been revoked sucessfully or if the client submitted an
49         invalid token.
50
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
54         already achieved.
55
56         The content of the response body is ignored by the client as all
57         necessary information is conveyed in the response code.
58
59         An invalid token type hint value is ignored by the authorization server
60         and does not influence the revocation response.
61         """
62         request = Request(
63             uri, http_method=http_method, body=body, headers=headers)
64         try:
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
73
74         self.request_validator.revoke_token(request.token,
75                                             request.token_type_hint, request)
76
77         response_body = None
78         if self.enable_jsonp and request.callback:
79             response_body = request.callback + '();'
80         return {}, response_body, 200
81
82     def validate_revocation_request(self, request):
83         """Ensure the request is valid.
84
85         The client constructs the request by including the following parameters
86         using the "application/x-www-form-urlencoded" format in the HTTP
87         request entity-body:
88
89         token (REQUIRED).  The token that the client wants to get revoked.
90
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:
99
100                 *  access_token: An Access Token as defined in [RFC6749],
101                     `section 1.4`_
102
103                 *  refresh_token: A Refresh Token as defined in [RFC6749],
104                     `section 1.5`_
105
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`_.
109
110         The client also includes its authentication credentials as described in
111         `Section 2.3`_. of [`RFC6749`_].
112
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
118         """
119         if not request.token:
120             raise InvalidRequestError(request=request,
121                                       description='Missing token parameter.')
122
123         if not self.request_validator.authenticate_client(request):
124             raise InvalidClientError(request=request)
125
126         if (request.token_type_hint and
127                 request.token_type_hint in self.valid_token_types and
128                 request.token_type_hint not in self.supported_token_types):
129             raise UnsupportedTokenTypeError(request=request)

Benjamin Mako Hill || Want to submit a patch?