]> projects.mako.cc - twitter-api-cdsw/blob - oauthlib/oauth2/rfc6749/errors.py
Handle content-type header charset value for streaming API
[twitter-api-cdsw] / oauthlib / oauth2 / rfc6749 / errors.py
1 # coding=utf-8
2 """
3 oauthlib.oauth2.rfc6749.errors
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5
6 Error used both by OAuth 2 clients and providers to represent the spec
7 defined error responses for all four core grant types.
8 """
9 from __future__ import unicode_literals
10 import json
11 from oauthlib.common import urlencode, add_params_to_uri
12
13
14 class OAuth2Error(Exception):
15     error = None
16     status_code = 400
17     description = ''
18
19     def __init__(self, description=None, uri=None, state=None, status_code=None,
20                  request=None):
21         """
22         description:    A human-readable ASCII [USASCII] text providing
23                         additional information, used to assist the client
24                         developer in understanding the error that occurred.
25                         Values for the "error_description" parameter MUST NOT
26                         include characters outside the set
27                         x20-21 / x23-5B / x5D-7E.
28
29         uri:    A URI identifying a human-readable web page with information
30                 about the error, used to provide the client developer with
31                 additional information about the error.  Values for the
32                 "error_uri" parameter MUST conform to the URI- Reference
33                 syntax, and thus MUST NOT include characters outside the set
34                 x21 / x23-5B / x5D-7E.
35
36         state:  A CSRF protection value received from the client.
37
38         request:  Oauthlib Request object
39         """
40         self.description = description or self.description
41         message = '(%s) %s' % (self.error, self.description)
42         if request:
43             message += ' ' + repr(request)
44         super(OAuth2Error, self).__init__(message)
45
46         self.uri = uri
47         self.state = state
48
49         if status_code:
50             self.status_code = status_code
51
52         if request:
53             self.redirect_uri = request.redirect_uri
54             self.client_id = request.client_id
55             self.scopes = request.scopes
56             self.response_type = request.response_type
57             self.grant_type = request.grant_type
58             if not state:
59                 self.state = request.state
60
61     def in_uri(self, uri):
62         return add_params_to_uri(uri, self.twotuples)
63
64     @property
65     def twotuples(self):
66         error = [('error', self.error)]
67         if self.description:
68             error.append(('error_description', self.description))
69         if self.uri:
70             error.append(('error_uri', self.uri))
71         if self.state:
72             error.append(('state', self.state))
73         return error
74
75     @property
76     def urlencoded(self):
77         return urlencode(self.twotuples)
78
79     @property
80     def json(self):
81         return json.dumps(dict(self.twotuples))
82
83
84 class TokenExpiredError(OAuth2Error):
85     error = 'token_expired'
86
87
88 class InsecureTransportError(OAuth2Error):
89     error = 'insecure_transport'
90     description = 'OAuth 2 MUST utilize https.'
91
92
93 class MismatchingStateError(OAuth2Error):
94     error = 'mismatching_state'
95     description = 'CSRF Warning! State not equal in request and response.'
96
97
98 class MissingCodeError(OAuth2Error):
99     error = 'missing_code'
100
101
102 class MissingTokenError(OAuth2Error):
103     error = 'missing_token'
104
105
106 class MissingTokenTypeError(OAuth2Error):
107     error = 'missing_token_type'
108
109
110 class FatalClientError(OAuth2Error):
111
112     """Errors during authorization where user should not be redirected back.
113
114     If the request fails due to a missing, invalid, or mismatching
115     redirection URI, or if the client identifier is missing or invalid,
116     the authorization server SHOULD inform the resource owner of the
117     error and MUST NOT automatically redirect the user-agent to the
118     invalid redirection URI.
119
120     Instead the user should be informed of the error by the provider itself.
121     """
122     pass
123
124
125 class InvalidRequestFatalError(FatalClientError):
126     """For fatal errors, the request is missing a required parameter, includes
127     an invalid parameter value, includes a parameter more than once, or is
128     otherwise malformed.
129     """
130     error = 'invalid_request'
131
132
133 class InvalidRedirectURIError(InvalidRequestFatalError):
134     description = 'Invalid redirect URI.'
135
136
137 class MissingRedirectURIError(InvalidRequestFatalError):
138     description = 'Missing redirect URI.'
139
140
141 class MismatchingRedirectURIError(InvalidRequestFatalError):
142     description = 'Mismatching redirect URI.'
143
144
145 class InvalidClientIdError(InvalidRequestFatalError):
146     description = 'Invalid client_id parameter value.'
147
148
149 class MissingClientIdError(InvalidRequestFatalError):
150     description = 'Missing client_id parameter.'
151
152
153 class InvalidRequestError(OAuth2Error):
154
155     """The request is missing a required parameter, includes an invalid
156     parameter value, includes a parameter more than once, or is
157     otherwise malformed.
158     """
159     error = 'invalid_request'
160
161
162 class MissingResponseTypeError(InvalidRequestError):
163     description = 'Missing response_type parameter.'
164
165
166 class AccessDeniedError(OAuth2Error):
167
168     """The resource owner or authorization server denied the request."""
169     error = 'access_denied'
170     status_code = 401
171
172
173 class UnsupportedResponseTypeError(OAuth2Error):
174
175     """The authorization server does not support obtaining an authorization
176     code using this method.
177     """
178     error = 'unsupported_response_type'
179
180
181 class InvalidScopeError(OAuth2Error):
182
183     """The requested scope is invalid, unknown, or malformed."""
184     error = 'invalid_scope'
185     status_code = 401
186
187
188 class ServerError(OAuth2Error):
189
190     """The authorization server encountered an unexpected condition that
191     prevented it from fulfilling the request.  (This error code is needed
192     because a 500 Internal Server Error HTTP status code cannot be returned
193     to the client via a HTTP redirect.)
194     """
195     error = 'server_error'
196
197
198 class TemporarilyUnavailableError(OAuth2Error):
199
200     """The authorization server is currently unable to handle the request
201     due to a temporary overloading or maintenance of the server.
202     (This error code is needed because a 503 Service Unavailable HTTP
203     status code cannot be returned to the client via a HTTP redirect.)
204     """
205     error = 'temporarily_unavailable'
206
207
208 class InvalidClientError(OAuth2Error):
209
210     """Client authentication failed (e.g. unknown client, no client
211     authentication included, or unsupported authentication method).
212     The authorization server MAY return an HTTP 401 (Unauthorized) status
213     code to indicate which HTTP authentication schemes are supported.
214     If the client attempted to authenticate via the "Authorization" request
215     header field, the authorization server MUST respond with an
216     HTTP 401 (Unauthorized) status code, and include the "WWW-Authenticate"
217     response header field matching the authentication scheme used by the
218     client.
219     """
220     error = 'invalid_client'
221     status_code = 401
222
223
224 class InvalidGrantError(OAuth2Error):
225
226     """The provided authorization grant (e.g. authorization code, resource
227     owner credentials) or refresh token is invalid, expired, revoked, does
228     not match the redirection URI used in the authorization request, or was
229     issued to another client.
230     """
231     error = 'invalid_grant'
232     status_code = 401
233
234
235 class UnauthorizedClientError(OAuth2Error):
236
237     """The authenticated client is not authorized to use this authorization
238     grant type.
239     """
240     error = 'unauthorized_client'
241     status_code = 401
242
243
244 class UnsupportedGrantTypeError(OAuth2Error):
245
246     """The authorization grant type is not supported by the authorization
247     server.
248     """
249     error = 'unsupported_grant_type'
250
251
252 class UnsupportedTokenTypeError(OAuth2Error):
253
254     """The authorization server does not support the revocation of the
255     presented token type.  I.e. the client tried to revoke an access token
256     on a server not supporting this feature.
257     """
258     error = 'unsupported_token_type'
259
260
261 def raise_from_error(error, params=None):
262     import inspect
263     import sys
264     kwargs = {
265         'description': params.get('error_description'),
266         'uri': params.get('error_uri'),
267         'state': params.get('state')
268     }
269     for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
270         if cls.error == error:
271             raise cls(**kwargs)

Benjamin Mako Hill || Want to submit a patch?