3 oauthlib.oauth2.rfc6749.errors
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6 Error used both by OAuth 2 clients and provicers 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 InvalidRedirectURIError(FatalClientError):
126 error = 'invalid_redirect_uri'
129 class MissingRedirectURIError(FatalClientError):
130 error = 'missing_redirect_uri'
133 class MismatchingRedirectURIError(FatalClientError):
134 error = 'mismatching_redirect_uri'
137 class MissingClientIdError(FatalClientError):
138 error = 'invalid_client_id'
141 class InvalidClientIdError(FatalClientError):
142 error = 'invalid_client_id'
145 class InvalidRequestError(OAuth2Error):
147 """The request is missing a required parameter, includes an invalid
148 parameter value, includes a parameter more than once, or is
151 error = 'invalid_request'
154 class AccessDeniedError(OAuth2Error):
156 """The resource owner or authorization server denied the request."""
157 error = 'access_denied'
161 class UnsupportedResponseTypeError(OAuth2Error):
163 """The authorization server does not support obtaining an authorization
164 code using this method.
166 error = 'unsupported_response_type'
169 class InvalidScopeError(OAuth2Error):
171 """The requested scope is invalid, unknown, or malformed."""
172 error = 'invalid_scope'
176 class ServerError(OAuth2Error):
178 """The authorization server encountered an unexpected condition that
179 prevented it from fulfilling the request. (This error code is needed
180 because a 500 Internal Server Error HTTP status code cannot be returned
181 to the client via a HTTP redirect.)
183 error = 'server_error'
186 class TemporarilyUnavailableError(OAuth2Error):
188 """The authorization server is currently unable to handle the request
189 due to a temporary overloading or maintenance of the server.
190 (This error code is needed because a 503 Service Unavailable HTTP
191 status code cannot be returned to the client via a HTTP redirect.)
193 error = 'temporarily_unavailable'
196 class InvalidClientError(OAuth2Error):
198 """Client authentication failed (e.g. unknown client, no client
199 authentication included, or unsupported authentication method).
200 The authorization server MAY return an HTTP 401 (Unauthorized) status
201 code to indicate which HTTP authentication schemes are supported.
202 If the client attempted to authenticate via the "Authorization" request
203 header field, the authorization server MUST respond with an
204 HTTP 401 (Unauthorized) status code, and include the "WWW-Authenticate"
205 response header field matching the authentication scheme used by the
208 error = 'invalid_client'
212 class InvalidGrantError(OAuth2Error):
214 """The provided authorization grant (e.g. authorization code, resource
215 owner credentials) or refresh token is invalid, expired, revoked, does
216 not match the redirection URI used in the authorization request, or was
217 issued to another client.
219 error = 'invalid_grant'
223 class UnauthorizedClientError(OAuth2Error):
225 """The authenticated client is not authorized to use this authorization
228 error = 'unauthorized_client'
232 class UnsupportedGrantTypeError(OAuth2Error):
234 """The authorization grant type is not supported by the authorization
237 error = 'unsupported_grant_type'
240 class UnsupportedTokenTypeError(OAuth2Error):
242 """The authorization server does not support the revocation of the
243 presented token type. I.e. the client tried to revoke an access token
244 on a server not supporting this feature.
246 error = 'unsupported_token_type'
249 def raise_from_error(error, params=None):
253 'description': params.get('error_description'),
254 'uri': params.get('error_uri'),
255 'state': params.get('state')
257 for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
258 if cls.error == error: