1class SocialAuthBaseException(ValueError):
2    """Base class for pipeline exceptions."""
3    pass
4
5
6class WrongBackend(SocialAuthBaseException):
7    def __init__(self, backend_name):
8        self.backend_name = backend_name
9
10    def __str__(self):
11        return 'Incorrect authentication service "{0}"'.format(
12            self.backend_name
13        )
14
15
16class MissingBackend(WrongBackend):
17    def __str__(self):
18        return 'Missing backend "{0}" entry'.format(self.backend_name)
19
20
21class NotAllowedToDisconnect(SocialAuthBaseException):
22    """User is not allowed to disconnect it's social account."""
23    pass
24
25
26class AuthException(SocialAuthBaseException):
27    """Auth process exception."""
28    def __init__(self, backend, *args, **kwargs):
29        self.backend = backend
30        super(AuthException, self).__init__(*args, **kwargs)
31
32
33class AuthFailed(AuthException):
34    """Auth process failed for some reason."""
35    def __str__(self):
36        msg = super(AuthFailed, self).__str__()
37        if msg == 'access_denied':
38            return 'Authentication process was canceled'
39        return 'Authentication failed: {0}'.format(msg)
40
41
42class AuthCanceled(AuthException):
43    """Auth process was canceled by user."""
44    def __init__(self, *args, **kwargs):
45        self.response = kwargs.pop('response', None)
46        super(AuthCanceled, self).__init__(*args, **kwargs)
47
48    def __str__(self):
49        msg = super(AuthCanceled, self).__str__()
50        if msg:
51            return 'Authentication process canceled: {0}'.format(msg)
52        return 'Authentication process canceled'
53
54
55class AuthUnknownError(AuthException):
56    """Unknown auth process error."""
57    def __str__(self):
58        msg = super(AuthUnknownError, self).__str__()
59        return 'An unknown error happened while authenticating {0}'.format(msg)
60
61
62class AuthTokenError(AuthException):
63    """Auth token error."""
64    def __str__(self):
65        msg = super(AuthTokenError, self).__str__()
66        return 'Token error: {0}'.format(msg)
67
68
69class AuthMissingParameter(AuthException):
70    """Missing parameter needed to start or complete the process."""
71    def __init__(self, backend, parameter, *args, **kwargs):
72        self.parameter = parameter
73        super(AuthMissingParameter, self).__init__(backend, *args, **kwargs)
74
75    def __str__(self):
76        return 'Missing needed parameter {0}'.format(self.parameter)
77
78
79class AuthStateMissing(AuthException):
80    """State parameter is incorrect."""
81    def __str__(self):
82        return 'Session value state missing.'
83
84
85class AuthStateForbidden(AuthException):
86    """State parameter is incorrect."""
87    def __str__(self):
88        return 'Wrong state parameter given.'
89
90
91class AuthAlreadyAssociated(AuthException):
92    """A different user has already associated the target social account"""
93    pass
94
95
96class AuthTokenRevoked(AuthException):
97    """User revoked the access_token in the provider."""
98    def __str__(self):
99        return 'User revoke access to the token'
100
101
102class AuthForbidden(AuthException):
103    """Authentication for this user is forbidden"""
104    def __str__(self):
105        return 'Your credentials aren\'t allowed'
106
107
108class AuthUnreachableProvider(AuthException):
109    """Cannot reach the provider"""
110    def __str__(self):
111        return 'The authentication provider could not be reached'
112
113
114class InvalidEmail(AuthException):
115    def __str__(self):
116        return 'Email couldn\'t be validated'
117