3 oauthlib.oauth2.rfc6749.errors
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 Error used both by OAuth 2 clients and providers to represent the spec
7 defined error responses for all four core grant types.
9 from __future__ import unicode_literals
11 from oauthlib.common import urlencode, add_params_to_uri
14 class OAuth2Error(Exception):
19 def __init__(self, description=None, uri=None, state=None, status_code=None,
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.
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.
36 state: A CSRF protection value received from the client.
38 request: Oauthlib Request object
40 self.description = description or self.description
41 message = '(%s) %s' % (self.error, self.description)
43 message += ' ' + repr(request)
44 super(OAuth2Error, self).__init__(message)
50 self.status_code = status_code
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
59 self.state = request.state
61 def in_uri(self, uri):
62 return add_params_to_uri(uri, self.twotuples)
66 error = [('error', self.error)]
68 error.append(('error_description', self.description))
70 error.append(('error_uri', self.uri))
72 error.append(('state', self.state))
77 return urlencode(self.twotuples)
81 return json.dumps(dict(self.twotuples))
84 class TokenExpiredError(OAuth2Error):
85 error = 'token_expired'
88 class InsecureTransportError(OAuth2Error):
89 error = 'insecure_transport'
90 description = 'OAuth 2 MUST utilize https.'
93 class MismatchingStateError(OAuth2Error):
94 error = 'mismatching_state'
95 description = 'CSRF Warning! State not equal in request and response.'
98 class MissingCodeError(OAuth2Error):
99 error = 'missing_code'
102 class MissingTokenError(OAuth2Error):
103 error = 'missing_token'
106 class MissingTokenTypeError(OAuth2Error):
107 error = 'missing_token_type'
110 class FatalClientError(OAuth2Error):
112 """Errors during authorization where user should not be redirected back.
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.
120 Instead the user should be informed of the error by the provider itself.
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
130 error = 'invalid_request'
133 class InvalidRedirectURIError(InvalidRequestFatalError):
134 description = 'Invalid redirect URI.'
137 class MissingRedirectURIError(InvalidRequestFatalError):
138 description = 'Missing redirect URI.'
141 class MismatchingRedirectURIError(InvalidRequestFatalError):
142 description = 'Mismatching redirect URI.'
145 class InvalidClientIdError(InvalidRequestFatalError):
146 description = 'Invalid client_id parameter value.'
149 class MissingClientIdError(InvalidRequestFatalError):
150 description = 'Missing client_id parameter.'
153 class InvalidRequestError(OAuth2Error):
155 """The request is missing a required parameter, includes an invalid
156 parameter value, includes a parameter more than once, or is
159 error = 'invalid_request'
162 class MissingResponseTypeError(InvalidRequestError):
163 description = 'Missing response_type parameter.'
166 class AccessDeniedError(OAuth2Error):
168 """The resource owner or authorization server denied the request."""
169 error = 'access_denied'
173 class UnsupportedResponseTypeError(OAuth2Error):
175 """The authorization server does not support obtaining an authorization
176 code using this method.
178 error = 'unsupported_response_type'
181 class InvalidScopeError(OAuth2Error):
183 """The requested scope is invalid, unknown, or malformed."""
184 error = 'invalid_scope'
188 class ServerError(OAuth2Error):
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.)
195 error = 'server_error'
198 class TemporarilyUnavailableError(OAuth2Error):
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.)
205 error = 'temporarily_unavailable'
208 class InvalidClientError(OAuth2Error):
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
220 error = 'invalid_client'
224 class InvalidGrantError(OAuth2Error):
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.
231 error = 'invalid_grant'
235 class UnauthorizedClientError(OAuth2Error):
237 """The authenticated client is not authorized to use this authorization
240 error = 'unauthorized_client'
244 class UnsupportedGrantTypeError(OAuth2Error):
246 """The authorization grant type is not supported by the authorization
249 error = 'unsupported_grant_type'
252 class UnsupportedTokenTypeError(OAuth2Error):
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.
258 error = 'unsupported_token_type'
261 def raise_from_error(error, params=None):
265 'description': params.get('error_description'),
266 'uri': params.get('error_uri'),
267 'state': params.get('state')
269 for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
270 if cls.error == error: