exceptions.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from django.utils.translation import gettext
  2. class SocialAuthBaseException(ValueError):
  3. """Base class for pipeline exceptions."""
  4. class BackendError(SocialAuthBaseException):
  5. def __str__(self):
  6. return gettext("Backend error: %s") % super().__str__()
  7. class WrongBackend(BackendError):
  8. def __init__(self, backend_name):
  9. self.backend_name = backend_name
  10. def __str__(self):
  11. return gettext('Incorrect authentication service "%s"') % self.backend_name
  12. class StopPipeline(SocialAuthBaseException):
  13. """Stop pipeline process exception.
  14. Raise this exception to stop the rest of the pipeline process.
  15. """
  16. def __str__(self):
  17. return gettext("Stop pipeline")
  18. class AuthException(SocialAuthBaseException):
  19. """Auth process exception."""
  20. def __init__(self, backend, *args, **kwargs):
  21. self.backend = backend
  22. super().__init__(*args, **kwargs)
  23. class AuthFailed(AuthException):
  24. """Auth process failed for some reason."""
  25. def __str__(self):
  26. if self.args == ("access_denied",):
  27. return gettext("Authentication process was cancelled")
  28. else:
  29. return gettext("Authentication failed: %s") % super().__str__()
  30. class AuthCanceled(AuthException):
  31. """Auth process was canceled by user."""
  32. def __str__(self):
  33. return gettext("Authentication process canceled")
  34. class AuthUnknownError(AuthException):
  35. """Unknown auth process error."""
  36. def __str__(self):
  37. err = "An unknown error happened while authenticating %s"
  38. return gettext(err) % super().__str__()
  39. class AuthTokenError(AuthException):
  40. """Auth token error."""
  41. def __str__(self):
  42. msg = super().__str__()
  43. return gettext("Token error: %s") % msg
  44. class AuthMissingParameter(AuthException):
  45. """Missing parameter needed to start or complete the process."""
  46. def __init__(self, backend, parameter, *args, **kwargs):
  47. self.parameter = parameter
  48. super().__init__(backend, *args, **kwargs)
  49. def __str__(self):
  50. return gettext("Missing needed parameter %s") % self.parameter
  51. class AuthStateMissing(AuthException):
  52. """State parameter is incorrect."""
  53. def __str__(self):
  54. return gettext("Session value state missing.")
  55. class AuthStateForbidden(AuthException):
  56. """State parameter is incorrect."""
  57. def __str__(self):
  58. return gettext("Wrong state parameter given.")
  59. class AuthTokenRevoked(AuthException):
  60. """User revoked the access_token in the provider."""
  61. def __str__(self):
  62. return gettext("User revoke access to the token")