Skip to content

Commit 7382631

Browse files
author
Jesse
authored
OAuth: don't override auth headers with contents of .netrc file (#122)
Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent f45280d commit 7382631

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Add support for Cloud Fetch
66
- Fix: Revised SQLAlchemy dialect and examples for compatibility with SQLAlchemy==1.3.x
7+
- Fix: oauth would fail if expired credentials appeared in ~/.netrc
78

89
## 2.7.0 (2023-06-26)
910

src/databricks/sql/auth/oauth.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@
1919
logger = logging.getLogger(__name__)
2020

2121

22+
class IgnoreNetrcAuth(requests.auth.AuthBase):
23+
"""This auth method is a no-op.
24+
25+
We use it to force requestslib to not use .netrc to write auth headers
26+
when making .post() requests to the oauth token endpoints, since these
27+
don't require authentication.
28+
29+
In cases where .netrc is outdated or corrupt, these requests will fail.
30+
31+
See issue #121
32+
"""
33+
34+
def __call__(self, r):
35+
return r
36+
37+
2238
class OAuthManager:
2339
def __init__(
2440
self,
@@ -43,7 +59,7 @@ def __fetch_well_known_config(self, hostname: str):
4359
known_config_url = self.idp_endpoint.get_openid_config_url(hostname)
4460

4561
try:
46-
response = requests.get(url=known_config_url)
62+
response = requests.get(url=known_config_url, auth=IgnoreNetrcAuth())
4763
except RequestException as e:
4864
logger.error(
4965
f"Unable to fetch OAuth configuration from {known_config_url}.\n"
@@ -149,7 +165,9 @@ def __send_token_request(token_request_url, data):
149165
"Accept": "application/json",
150166
"Content-Type": "application/x-www-form-urlencoded",
151167
}
152-
response = requests.post(url=token_request_url, data=data, headers=headers)
168+
response = requests.post(
169+
url=token_request_url, data=data, headers=headers, auth=IgnoreNetrcAuth()
170+
)
153171
return response.json()
154172

155173
def __send_refresh_token_request(self, hostname, refresh_token):

0 commit comments

Comments
 (0)