errors.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. oauthlib.oauth1.rfc5849.errors
  3. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. Error used both by OAuth 1 clients and provicers to represent the spec
  5. defined error responses for all four core grant types.
  6. """
  7. from oauthlib.common import add_params_to_uri, urlencode
  8. class OAuth1Error(Exception):
  9. error = None
  10. description = ''
  11. def __init__(self, description=None, uri=None, status_code=400,
  12. request=None):
  13. """
  14. description: A human-readable ASCII [USASCII] text providing
  15. additional information, used to assist the client
  16. developer in understanding the error that occurred.
  17. Values for the "error_description" parameter MUST NOT
  18. include characters outside the set
  19. x20-21 / x23-5B / x5D-7E.
  20. uri: A URI identifying a human-readable web page with information
  21. about the error, used to provide the client developer with
  22. additional information about the error. Values for the
  23. "error_uri" parameter MUST conform to the URI- Reference
  24. syntax, and thus MUST NOT include characters outside the set
  25. x21 / x23-5B / x5D-7E.
  26. state: A CSRF protection value received from the client.
  27. request: Oauthlib Request object
  28. """
  29. self.description = description or self.description
  30. message = '({}) {}'.format(self.error, self.description)
  31. if request:
  32. message += ' ' + repr(request)
  33. super().__init__(message)
  34. self.uri = uri
  35. self.status_code = status_code
  36. def in_uri(self, uri):
  37. return add_params_to_uri(uri, self.twotuples)
  38. @property
  39. def twotuples(self):
  40. error = [('error', self.error)]
  41. if self.description:
  42. error.append(('error_description', self.description))
  43. if self.uri:
  44. error.append(('error_uri', self.uri))
  45. return error
  46. @property
  47. def urlencoded(self):
  48. return urlencode(self.twotuples)
  49. class InsecureTransportError(OAuth1Error):
  50. error = 'insecure_transport_protocol'
  51. description = 'Only HTTPS connections are permitted.'
  52. class InvalidSignatureMethodError(OAuth1Error):
  53. error = 'invalid_signature_method'
  54. class InvalidRequestError(OAuth1Error):
  55. error = 'invalid_request'
  56. class InvalidClientError(OAuth1Error):
  57. error = 'invalid_client'