Skip to content

Commit cb3bf69

Browse files
author
Jesse Whitehouse
committed
Oauth: ignore ~/.netrc file for requests to unauthenticated endpoints
https://requests.readthedocs.io/en/latest/user/authentication/#new-forms-of-authentication Closes #121 Affects databricks/dbt-databricks#337 Signed-off-by: Jesse Whitehouse <[email protected]>
1 parent 2eedcb2 commit cb3bf69

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/databricks/sql/auth/oauth.py

+15-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717

1818
logger = logging.getLogger(__name__)
1919

20+
class IgnoreNetrcAuth(requests.auth.AuthBase):
21+
"""This auth method is a no-op.
22+
23+
We use it to force requestslib to not use .netrc to write auth headers
24+
when making .post() requests to the oauth token endpoints, since these
25+
don't require authentication.
26+
27+
In cases where .netrc is outdated or corrupt, these requests will fail.
28+
29+
See issue #121
30+
"""
31+
def __call__(self, r):
32+
return r
2033

2134
class OAuthManager:
2235
OIDC_REDIRECTOR_PATH = "oidc"
@@ -38,7 +51,7 @@ def __get_redirect_url(redirect_port: int):
3851
def __fetch_well_known_config(idp_url: str):
3952
known_config_url = f"{idp_url}/.well-known/oauth-authorization-server"
4053
try:
41-
response = requests.get(url=known_config_url)
54+
response = requests.get(url=known_config_url, auth=IgnoreNetrcAuth())
4255
except RequestException as e:
4356
logger.error(
4457
f"Unable to fetch OAuth configuration from {idp_url}.\n"
@@ -150,7 +163,7 @@ def __send_token_request(token_request_url, data):
150163
"Accept": "application/json",
151164
"Content-Type": "application/x-www-form-urlencoded",
152165
}
153-
response = requests.post(url=token_request_url, data=data, headers=headers)
166+
response = requests.post(url=token_request_url, data=data, headers=headers, auth=IgnoreNetrcAuth())
154167
return response.json()
155168

156169
def __send_refresh_token_request(self, hostname, refresh_token):

0 commit comments

Comments
 (0)