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