Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/requests_oauthlib/oauth1_auth.py: 39%
44 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:22 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:22 +0000
1# -*- coding: utf-8 -*-
2from __future__ import unicode_literals
4import logging
6from oauthlib.common import extract_params
7from oauthlib.oauth1 import Client, SIGNATURE_HMAC, SIGNATURE_TYPE_AUTH_HEADER
8from oauthlib.oauth1 import SIGNATURE_TYPE_BODY
9from requests.compat import is_py3
10from requests.utils import to_native_string
11from requests.auth import AuthBase
13CONTENT_TYPE_FORM_URLENCODED = "application/x-www-form-urlencoded"
14CONTENT_TYPE_MULTI_PART = "multipart/form-data"
16if is_py3:
17 unicode = str
19log = logging.getLogger(__name__)
21# OBS!: Correct signing of requests are conditional on invoking OAuth1
22# as the last step of preparing a request, or at least having the
23# content-type set properly.
24class OAuth1(AuthBase):
25 """Signs the request using OAuth 1 (RFC5849)"""
27 client_class = Client
29 def __init__(
30 self,
31 client_key,
32 client_secret=None,
33 resource_owner_key=None,
34 resource_owner_secret=None,
35 callback_uri=None,
36 signature_method=SIGNATURE_HMAC,
37 signature_type=SIGNATURE_TYPE_AUTH_HEADER,
38 rsa_key=None,
39 verifier=None,
40 decoding="utf-8",
41 client_class=None,
42 force_include_body=False,
43 **kwargs
44 ):
46 try:
47 signature_type = signature_type.upper()
48 except AttributeError:
49 pass
51 client_class = client_class or self.client_class
53 self.force_include_body = force_include_body
55 self.client = client_class(
56 client_key,
57 client_secret,
58 resource_owner_key,
59 resource_owner_secret,
60 callback_uri,
61 signature_method,
62 signature_type,
63 rsa_key,
64 verifier,
65 decoding=decoding,
66 **kwargs
67 )
69 def __call__(self, r):
70 """Add OAuth parameters to the request.
72 Parameters may be included from the body if the content-type is
73 urlencoded, if no content type is set a guess is made.
74 """
75 # Overwriting url is safe here as request will not modify it past
76 # this point.
77 log.debug("Signing request %s using client %s", r, self.client)
79 content_type = r.headers.get("Content-Type", "")
80 if (
81 not content_type
82 and extract_params(r.body)
83 or self.client.signature_type == SIGNATURE_TYPE_BODY
84 ):
85 content_type = CONTENT_TYPE_FORM_URLENCODED
86 if not isinstance(content_type, unicode):
87 content_type = content_type.decode("utf-8")
89 is_form_encoded = CONTENT_TYPE_FORM_URLENCODED in content_type
91 log.debug(
92 "Including body in call to sign: %s",
93 is_form_encoded or self.force_include_body,
94 )
96 if is_form_encoded:
97 r.headers["Content-Type"] = CONTENT_TYPE_FORM_URLENCODED
98 r.url, headers, r.body = self.client.sign(
99 unicode(r.url), unicode(r.method), r.body or "", r.headers
100 )
101 elif self.force_include_body:
102 # To allow custom clients to work on non form encoded bodies.
103 r.url, headers, r.body = self.client.sign(
104 unicode(r.url), unicode(r.method), r.body or "", r.headers
105 )
106 else:
107 # Omit body data in the signing of non form-encoded requests
108 r.url, headers, _ = self.client.sign(
109 unicode(r.url), unicode(r.method), None, r.headers
110 )
112 r.prepare_headers(headers)
113 r.url = to_native_string(r.url)
114 log.debug("Updated url: %s", r.url)
115 log.debug("Updated headers: %s", headers)
116 log.debug("Updated body: %r", r.body)
117 return r