Skip to content

Commit 2e4cb39

Browse files
committed
separate socket uri and redirect uri
1 parent 411c973 commit 2e4cb39

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/snowflake/connector/auth/_http_server.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ class AuthHttpServer:
6969
def __init__(
7070
self,
7171
uri: str,
72+
redirect_uri: str,
7273
buf_size: int = 16384,
7374
) -> None:
7475
parsed_uri = urllib.parse.urlparse(uri)
76+
parsed_redirect = urllib.parse.urlparse(redirect_uri)
7577
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
7678
self.buf_size = buf_size
7779
if os.getenv("SNOWFLAKE_AUTH_SOCKET_REUSE_PORT", "False").lower() == "true":
@@ -123,6 +125,24 @@ def __init__(
123125
query=parsed_uri.query,
124126
fragment=parsed_uri.fragment,
125127
)
128+
if parsed_redirect.hostname in ("localhost", "127.0.0.1"):
129+
logger.debug(
130+
f"Redirect URI hostname is {parsed_redirect.hostname}, redirect port {parsed_redirect.port} will be changed to the server port {port}."
131+
)
132+
self._redirect_uri = urllib.parse.ParseResult(
133+
scheme=parsed_redirect.scheme,
134+
netloc=parsed_redirect.hostname + ":" + str(port),
135+
path=parsed_redirect.path,
136+
params=parsed_redirect.params,
137+
query=parsed_redirect.query,
138+
fragment=parsed_redirect.fragment,
139+
)
140+
else:
141+
self._redirect_uri = parsed_redirect
142+
143+
@property
144+
def redirect_uri(self) -> str:
145+
return self._redirect_uri.geturl()
126146

127147
@property
128148
def url(self) -> str:

src/snowflake/connector/auth/oauth_code.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import hashlib
99
import json
1010
import logging
11+
import os
1112
import secrets
1213
import socket
1314
import time
@@ -117,7 +118,12 @@ def _request_tokens(
117118
) -> (str | None, str | None):
118119
"""Web Browser based Authentication."""
119120
logger.debug("authenticating with OAuth authorization code flow")
120-
with AuthHttpServer(self._redirect_uri) as callback_server:
121+
with AuthHttpServer(
122+
uri=os.environ.get("SNOWFLAKE_OAUTH_SOCKET_ADDRESS", "http://localhost")
123+
+ ":"
124+
+ os.environ.get("SNOWFLAKE_OAUTH_SOCKET_PORT", "0"),
125+
redirect_uri=self._redirect_uri,
126+
) as callback_server:
121127
code = self._do_authorization_request(callback_server, conn)
122128
return self._do_token_request(code, callback_server, conn)
123129

@@ -260,7 +266,7 @@ def _do_authorization_request(
260266
connection: SnowflakeConnection,
261267
) -> str | None:
262268
authorization_request = self._construct_authorization_request(
263-
callback_server.url
269+
callback_server.redirect_uri
264270
)
265271
logger.debug("step 1: going to open authorization URL")
266272
print(
@@ -314,7 +320,7 @@ def _do_token_request(
314320
fields = {
315321
"grant_type": "authorization_code",
316322
"code": code,
317-
"redirect_uri": callback_server.url,
323+
"redirect_uri": callback_server.redirect_uri,
318324
}
319325
if self._enable_single_use_refresh_tokens:
320326
fields["enable_single_use_refresh_tokens"] = "true"

0 commit comments

Comments
 (0)