Debian Codegraph @

allauth/socialaccount/providers/oauth/oauth1_auth.py

raw
1 # This file is from the requests-oauthlib library,
2 # https://github.com/requests/requests-oauthlib/blob/416d7382a193d180aac3a2fb7da6f621401ac960/requests_oauthlib/oauth1_auth.py
3 #
4 # The requests-oauthlib library is licensed under the ISC License:
5 #
6 # ISC License
7 #
8 # Copyright (c) 2014 Kenneth Reitz.
9 #
10 # Permission to use, copy, modify, and/or distribute this software for any
11 # purpose with or without fee is hereby granted, provided that the above
12 # copyright notice and this permission notice appear in all copies.
13 #
14 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
22 from __future__ import annotations
23
24 import logging
25 from requests.auth import AuthBase
26 from requests.utils import to_native_string
27
28 from oauthlib.common import extract_params
29 from oauthlib.oauth1 import (
30 SIGNATURE_HMAC,
31 SIGNATURE_TYPE_AUTH_HEADER,
32 SIGNATURE_TYPE_BODY,
33 Client,
34 )
35
36
37 CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded"
38 CONTENT_TYPE_MULTI_PART = "multipart/form-data"
39
40
41 log = logging.getLogger(__name__)
42
43
44 # OBS!: Correct signing of requests are conditional on invoking OAuth1
45 # as the last step of preparing a request, or at least having the
46 # content-type set properly.
47 class OAuth1(AuthBase):
48 """Signs the request using OAuth 1 (RFC5849)"""
49
50 client_class = Client
51
52 def __init__(
53 self,
54 client_key,
55 client_secret=None,
56 resource_owner_key=None,
57 resource_owner_secret=None,
58 callback_uri=None,
59 signature_method=SIGNATURE_HMAC,
60 signature_type=SIGNATURE_TYPE_AUTH_HEADER,
61 rsa_key=None,
62 verifier=None,
63 decoding="utf-8",
64 client_class=None,
65 force_include_body=False,
66 **kwargs,
67 ) -> None:
68
69 try:
70 signature_type = signature_type.upper()
71 except AttributeError:
72 pass
73
74 client_class = client_class or self.client_class
75
76 self.force_include_body = force_include_body
77
78 self.client = client_class(
79 client_key,
80 client_secret,
81 resource_owner_key,
82 resource_owner_secret,
83 callback_uri,
84 signature_method,
85 signature_type,
86 rsa_key,
87 verifier,
88 decoding=decoding,
89 **kwargs,
90 )
91
92 def __call__(self, r):
93 """Add OAuth parameters to the request.
94
95 Parameters may be included from the body if the content-type is
96 urlencoded, if no content type is set a guess is made.
97 """
98 # Overwriting url is safe here as request will not modify it past
99 # this point.
100 log.debug("Signing request %s using client %s", r, self.client)
101
102 content_type = r.headers.get("Content-Type", "")
103 if (
104 not content_type
105 and extract_params(r.body)
106 or self.client.signature_type == SIGNATURE_TYPE_BODY
107 ):
108 content_type = CONTENT_TYPE_FORM_URLENCODED
109 if not isinstance(content_type, str):
110 content_type = content_type.decode("utf-8")
111
112 is_form_encoded = CONTENT_TYPE_FORM_URLENCODED in content_type
113
114 log.debug(
115 "Including body in call to sign: %s",
116 is_form_encoded or self.force_include_body,
117 )
118
119 if is_form_encoded:
120 r.headers["Content-Type"] = CONTENT_TYPE_FORM_URLENCODED
121 r.url, headers, r.body = self.client.sign(
122 str(r.url), str(r.method), r.body or "", r.headers
123 )
124 elif self.force_include_body:
125 # To allow custom clients to work on non form encoded bodies.
126 r.url, headers, r.body = self.client.sign(
127 str(r.url), str(r.method), r.body or "", r.headers
128 )
129 else:
130 # Omit body data in the signing of non form-encoded requests
131 r.url, headers, _ = self.client.sign(
132 str(r.url), str(r.method), None, r.headers
133 )
134
135 r.prepare_headers(headers)
136 r.url = to_native_string(r.url)
137 log.debug("Updated url: %s", r.url)
138 log.debug("Updated headers: %s", headers)
139 log.debug("Updated body: %r", r.body)
140 return r