Skip to content

Commit 091dab4

Browse files
authored
SNOW-1013595: Add support token_file_path in connection parameters (#1938)
1 parent 8cdca67 commit 091dab4

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

DESCRIPTION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Source code is also available at: https://github.com/snowflakedb/snowflake-conne
88

99
# Release Notes
1010

11+
- v3.11.0(TBD)
12+
13+
- Added support for `token_file_path` connection parameter to read an OAuth token from a file when connecting to Snowflake.
14+
1115
- v3.10.0(April 29,2024)
1216

1317
- Added support for structured types to fetch_pandas_all.

src/snowflake/connector/connection.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ class SnowflakeConnection:
359359
This parameter is only effective when the result format is JSON.
360360
server_session_keep_alive: When true, the connector does not destroy the session on the Snowflake server side
361361
before the connector shuts down. Default value is false.
362+
token_file_path: The file path of the token file. If both token and token_file_path are provided, the token in token_file_path will be used.
362363
"""
363364

364365
OCSP_ENV_LOCK = Lock()
@@ -1182,6 +1183,12 @@ def __config(self, **kwargs):
11821183
]:
11831184
self._authenticator = auth_tmp
11841185

1186+
# read OAuth token from
1187+
token_file_path = kwargs.get("token_file_path")
1188+
if token_file_path:
1189+
with open(token_file_path) as f:
1190+
self._token = f.read()
1191+
11851192
if not (self._master_token and self._session_token):
11861193
if not self.user and self._authenticator != OAUTH_AUTHENTICATOR:
11871194
# OAuth Authentication does not require a username

test/integ/test_connection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,3 +1350,16 @@ def test_connection_atexit_close(conn_cnx):
13501350
with conn_cnx() as conn:
13511351
conn._close_at_exit()
13521352
assert conn.is_closed()
1353+
1354+
1355+
@pytest.mark.skipolddriver
1356+
def test_token_file_path(tmp_path, db_parameters):
1357+
fake_token = "some token"
1358+
token_file_path = tmp_path / "token"
1359+
with open(token_file_path, "w") as f:
1360+
f.write(fake_token)
1361+
1362+
conn = snowflake.connector.connect(**db_parameters, token=fake_token)
1363+
assert conn._token == fake_token
1364+
conn = snowflake.connector.connect(**db_parameters, token_file_path=token_file_path)
1365+
assert conn._token == fake_token

0 commit comments

Comments
 (0)