errors.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. """
  2. oauthlib.oauth2.rfc6749.errors
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Error used both by OAuth 2 clients and providers to represent the spec
  5. defined error responses for all four core grant types.
  6. """
  7. import json
  8. from oauthlib.common import add_params_to_uri, urlencode
  9. class OAuth2Error(Exception):
  10. error = None
  11. status_code = 400
  12. description = ''
  13. def __init__(self, description=None, uri=None, state=None,
  14. status_code=None, request=None):
  15. """
  16. :param description: A human-readable ASCII [USASCII] text providing
  17. additional information, used to assist the client
  18. developer in understanding the error that occurred.
  19. Values for the "error_description" parameter
  20. MUST NOT include characters outside the set
  21. x20-21 / x23-5B / x5D-7E.
  22. :param uri: A URI identifying a human-readable web page with information
  23. about the error, used to provide the client developer with
  24. additional information about the error. Values for the
  25. "error_uri" parameter MUST conform to the URI- Reference
  26. syntax, and thus MUST NOT include characters outside the set
  27. x21 / x23-5B / x5D-7E.
  28. :param state: A CSRF protection value received from the client.
  29. :param status_code:
  30. :param request: OAuthlib request.
  31. :type request: oauthlib.common.Request
  32. """
  33. if description is not None:
  34. self.description = description
  35. message = '({}) {}'.format(self.error, self.description)
  36. if request:
  37. message += ' ' + repr(request)
  38. super().__init__(message)
  39. self.uri = uri
  40. self.state = state
  41. if status_code:
  42. self.status_code = status_code
  43. if request:
  44. self.redirect_uri = request.redirect_uri
  45. self.client_id = request.client_id
  46. self.scopes = request.scopes
  47. self.response_type = request.response_type
  48. self.response_mode = request.response_mode
  49. self.grant_type = request.grant_type
  50. if not state:
  51. self.state = request.state
  52. else:
  53. self.redirect_uri = None
  54. self.client_id = None
  55. self.scopes = None
  56. self.response_type = None
  57. self.response_mode = None
  58. self.grant_type = None
  59. def in_uri(self, uri):
  60. fragment = self.response_mode == "fragment"
  61. return add_params_to_uri(uri, self.twotuples, fragment)
  62. @property
  63. def twotuples(self):
  64. error = [('error', self.error)]
  65. if self.description:
  66. error.append(('error_description', self.description))
  67. if self.uri:
  68. error.append(('error_uri', self.uri))
  69. if self.state:
  70. error.append(('state', self.state))
  71. return error
  72. @property
  73. def urlencoded(self):
  74. return urlencode(self.twotuples)
  75. @property
  76. def json(self):
  77. return json.dumps(dict(self.twotuples))
  78. @property
  79. def headers(self):
  80. if self.status_code == 401:
  81. """
  82. https://tools.ietf.org/html/rfc6750#section-3
  83. All challenges defined by this specification MUST use the auth-scheme
  84. value "Bearer". This scheme MUST be followed by one or more
  85. auth-param values.
  86. """
  87. authvalues = ['error="{}"'.format(self.error)]
  88. if self.description:
  89. authvalues.append('error_description="{}"'.format(self.description))
  90. if self.uri:
  91. authvalues.append('error_uri="{}"'.format(self.uri))
  92. return {"WWW-Authenticate": "Bearer " + ", ".join(authvalues)}
  93. return {}
  94. class TokenExpiredError(OAuth2Error):
  95. error = 'token_expired'
  96. class InsecureTransportError(OAuth2Error):
  97. error = 'insecure_transport'
  98. description = 'OAuth 2 MUST utilize https.'
  99. class MismatchingStateError(OAuth2Error):
  100. error = 'mismatching_state'
  101. description = 'CSRF Warning! State not equal in request and response.'
  102. class MissingCodeError(OAuth2Error):
  103. error = 'missing_code'
  104. class MissingTokenError(OAuth2Error):
  105. error = 'missing_token'
  106. class MissingTokenTypeError(OAuth2Error):
  107. error = 'missing_token_type'
  108. class FatalClientError(OAuth2Error):
  109. """
  110. Errors during authorization where user should not be redirected back.
  111. If the request fails due to a missing, invalid, or mismatching
  112. redirection URI, or if the client identifier is missing or invalid,
  113. the authorization server SHOULD inform the resource owner of the
  114. error and MUST NOT automatically redirect the user-agent to the
  115. invalid redirection URI.
  116. Instead the user should be informed of the error by the provider itself.
  117. """
  118. pass
  119. class InvalidRequestFatalError(FatalClientError):
  120. """
  121. For fatal errors, the request is missing a required parameter, includes
  122. an invalid parameter value, includes a parameter more than once, or is
  123. otherwise malformed.
  124. """
  125. error = 'invalid_request'
  126. class InvalidRedirectURIError(InvalidRequestFatalError):
  127. description = 'Invalid redirect URI.'
  128. class MissingRedirectURIError(InvalidRequestFatalError):
  129. description = 'Missing redirect URI.'
  130. class MismatchingRedirectURIError(InvalidRequestFatalError):
  131. description = 'Mismatching redirect URI.'
  132. class InvalidClientIdError(InvalidRequestFatalError):
  133. description = 'Invalid client_id parameter value.'
  134. class MissingClientIdError(InvalidRequestFatalError):
  135. description = 'Missing client_id parameter.'
  136. class InvalidRequestError(OAuth2Error):
  137. """
  138. The request is missing a required parameter, includes an invalid
  139. parameter value, includes a parameter more than once, or is
  140. otherwise malformed.
  141. """
  142. error = 'invalid_request'
  143. class MissingResponseTypeError(InvalidRequestError):
  144. description = 'Missing response_type parameter.'
  145. class MissingCodeChallengeError(InvalidRequestError):
  146. """
  147. If the server requires Proof Key for Code Exchange (PKCE) by OAuth
  148. public clients and the client does not send the "code_challenge" in
  149. the request, the authorization endpoint MUST return the authorization
  150. error response with the "error" value set to "invalid_request". The
  151. "error_description" or the response of "error_uri" SHOULD explain the
  152. nature of error, e.g., code challenge required.
  153. """
  154. description = 'Code challenge required.'
  155. class MissingCodeVerifierError(InvalidRequestError):
  156. """
  157. The request to the token endpoint, when PKCE is enabled, has
  158. the parameter `code_verifier` REQUIRED.
  159. """
  160. description = 'Code verifier required.'
  161. class AccessDeniedError(OAuth2Error):
  162. """
  163. The resource owner or authorization server denied the request.
  164. """
  165. error = 'access_denied'
  166. class UnsupportedResponseTypeError(OAuth2Error):
  167. """
  168. The authorization server does not support obtaining an authorization
  169. code using this method.
  170. """
  171. error = 'unsupported_response_type'
  172. class UnsupportedCodeChallengeMethodError(InvalidRequestError):
  173. """
  174. If the server supporting PKCE does not support the requested
  175. transformation, the authorization endpoint MUST return the
  176. authorization error response with "error" value set to
  177. "invalid_request". The "error_description" or the response of
  178. "error_uri" SHOULD explain the nature of error, e.g., transform
  179. algorithm not supported.
  180. """
  181. description = 'Transform algorithm not supported.'
  182. class InvalidScopeError(OAuth2Error):
  183. """
  184. The requested scope is invalid, unknown, or malformed, or
  185. exceeds the scope granted by the resource owner.
  186. https://tools.ietf.org/html/rfc6749#section-5.2
  187. """
  188. error = 'invalid_scope'
  189. class ServerError(OAuth2Error):
  190. """
  191. The authorization server encountered an unexpected condition that
  192. prevented it from fulfilling the request. (This error code is needed
  193. because a 500 Internal Server Error HTTP status code cannot be returned
  194. to the client via a HTTP redirect.)
  195. """
  196. error = 'server_error'
  197. class TemporarilyUnavailableError(OAuth2Error):
  198. """
  199. The authorization server is currently unable to handle the request
  200. due to a temporary overloading or maintenance of the server.
  201. (This error code is needed because a 503 Service Unavailable HTTP
  202. status code cannot be returned to the client via a HTTP redirect.)
  203. """
  204. error = 'temporarily_unavailable'
  205. class InvalidClientError(FatalClientError):
  206. """
  207. Client authentication failed (e.g. unknown client, no client
  208. authentication included, or unsupported authentication method).
  209. The authorization server MAY return an HTTP 401 (Unauthorized) status
  210. code to indicate which HTTP authentication schemes are supported.
  211. If the client attempted to authenticate via the "Authorization" request
  212. header field, the authorization server MUST respond with an
  213. HTTP 401 (Unauthorized) status code, and include the "WWW-Authenticate"
  214. response header field matching the authentication scheme used by the
  215. client.
  216. """
  217. error = 'invalid_client'
  218. status_code = 401
  219. class InvalidGrantError(OAuth2Error):
  220. """
  221. The provided authorization grant (e.g. authorization code, resource
  222. owner credentials) or refresh token is invalid, expired, revoked, does
  223. not match the redirection URI used in the authorization request, or was
  224. issued to another client.
  225. https://tools.ietf.org/html/rfc6749#section-5.2
  226. """
  227. error = 'invalid_grant'
  228. status_code = 400
  229. class UnauthorizedClientError(OAuth2Error):
  230. """
  231. The authenticated client is not authorized to use this authorization
  232. grant type.
  233. """
  234. error = 'unauthorized_client'
  235. class UnsupportedGrantTypeError(OAuth2Error):
  236. """
  237. The authorization grant type is not supported by the authorization
  238. server.
  239. """
  240. error = 'unsupported_grant_type'
  241. class UnsupportedTokenTypeError(OAuth2Error):
  242. """
  243. The authorization server does not support the hint of the
  244. presented token type. I.e. the client tried to revoke an access token
  245. on a server not supporting this feature.
  246. """
  247. error = 'unsupported_token_type'
  248. class InvalidTokenError(OAuth2Error):
  249. """
  250. The access token provided is expired, revoked, malformed, or
  251. invalid for other reasons. The resource SHOULD respond with
  252. the HTTP 401 (Unauthorized) status code. The client MAY
  253. request a new access token and retry the protected resource
  254. request.
  255. """
  256. error = 'invalid_token'
  257. status_code = 401
  258. description = ("The access token provided is expired, revoked, malformed, "
  259. "or invalid for other reasons.")
  260. class InsufficientScopeError(OAuth2Error):
  261. """
  262. The request requires higher privileges than provided by the
  263. access token. The resource server SHOULD respond with the HTTP
  264. 403 (Forbidden) status code and MAY include the "scope"
  265. attribute with the scope necessary to access the protected
  266. resource.
  267. """
  268. error = 'insufficient_scope'
  269. status_code = 403
  270. description = ("The request requires higher privileges than provided by "
  271. "the access token.")
  272. class ConsentRequired(OAuth2Error):
  273. """
  274. The Authorization Server requires End-User consent.
  275. This error MAY be returned when the prompt parameter value in the
  276. Authentication Request is none, but the Authentication Request cannot be
  277. completed without displaying a user interface for End-User consent.
  278. """
  279. error = 'consent_required'
  280. class LoginRequired(OAuth2Error):
  281. """
  282. The Authorization Server requires End-User authentication.
  283. This error MAY be returned when the prompt parameter value in the
  284. Authentication Request is none, but the Authentication Request cannot be
  285. completed without displaying a user interface for End-User authentication.
  286. """
  287. error = 'login_required'
  288. class CustomOAuth2Error(OAuth2Error):
  289. """
  290. This error is a placeholder for all custom errors not described by the RFC.
  291. Some of the popular OAuth2 providers are using custom errors.
  292. """
  293. def __init__(self, error, *args, **kwargs):
  294. self.error = error
  295. super().__init__(*args, **kwargs)
  296. def raise_from_error(error, params=None):
  297. import inspect
  298. import sys
  299. kwargs = {
  300. 'description': params.get('error_description'),
  301. 'uri': params.get('error_uri'),
  302. 'state': params.get('state')
  303. }
  304. for _, cls in inspect.getmembers(sys.modules[__name__], inspect.isclass):
  305. if cls.error == error:
  306. raise cls(**kwargs)
  307. raise CustomOAuth2Error(error=error, **kwargs)