errors.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # coding=utf-8
  2. """
  3. oauthlib.oauth1.rfc5849.errors
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. Error used both by OAuth 1 clients and provicers to represent the spec
  6. defined error responses for all four core grant types.
  7. """
  8. from __future__ import unicode_literals
  9. from oauthlib.common import urlencode, add_params_to_uri
  10. class OAuth1Error(Exception):
  11. error = None
  12. description = ''
  13. def __init__(self, description=None, uri=None, status_code=400,
  14. request=None):
  15. """
  16. 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 MUST NOT
  20. include characters outside the set
  21. x20-21 / x23-5B / x5D-7E.
  22. 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. state: A CSRF protection value received from the client.
  29. request: Oauthlib Request object
  30. """
  31. self.description = description or self.description
  32. message = '(%s) %s' % (self.error, self.description)
  33. if request:
  34. message += ' ' + repr(request)
  35. super(OAuth1Error, self).__init__(message)
  36. self.uri = uri
  37. self.status_code = status_code
  38. def in_uri(self, uri):
  39. return add_params_to_uri(uri, self.twotuples)
  40. @property
  41. def twotuples(self):
  42. error = [('error', self.error)]
  43. if self.description:
  44. error.append(('error_description', self.description))
  45. if self.uri:
  46. error.append(('error_uri', self.uri))
  47. return error
  48. @property
  49. def urlencoded(self):
  50. return urlencode(self.twotuples)
  51. class InsecureTransportError(OAuth1Error):
  52. error = 'insecure_transport_protocol'
  53. description = 'Only HTTPS connections are permitted.'
  54. class InvalidSignatureMethodError(OAuth1Error):
  55. error = 'invalid_signature_method'
  56. class InvalidRequestError(OAuth1Error):
  57. error = 'invalid_request'
  58. class InvalidClientError(OAuth1Error):
  59. error = 'invalid_client'