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