python-tds
Advanced tools
| import struct | ||
| from pytds import tds_base | ||
| from .tds_base import _TdsLogin | ||
| def fedauth_packet(login: _TdsLogin, fedauth_required: bool) -> bytes: | ||
| if login.access_token is None: | ||
| raise ValueError("Cannot build fedauth packet without access_token") | ||
| fedauth_token = login.access_token.encode("UTF-16LE") | ||
| tokenlen = len(fedauth_token) | ||
| noncelen = len(login.nonce) if login.nonce else 0 | ||
| buffer = bytearray() | ||
| buffer.extend(struct.pack("B", tds_base.TDS_LOGIN_FEATURE_FEDAUTH)) | ||
| buffer.extend(struct.pack("<I", tokenlen + noncelen + 1 + 4)) | ||
| buffer.extend(struct.pack("B", (tds_base.TDS_FEDAUTH_OPTIONS_LIBRARY_SECURITYTOKEN << 1) | | ||
| (tds_base.TDS_FEDAUTH_OPTIONS_ECHO_YES if fedauth_required else tds_base.TDS_FEDAUTH_OPTIONS_ECHO_NO))) | ||
| buffer.extend(struct.pack("<I", tokenlen)) | ||
| buffer.extend(fedauth_token) | ||
| if login.nonce: | ||
| buffer.extend(login.nonce) | ||
| buffer.extend(struct.pack("B", 0xFF)) | ||
| return bytes(buffer) | ||
+1
-1
| Metadata-Version: 2.4 | ||
| Name: python-tds | ||
| Version: 1.16.1 | ||
| Version: 1.17.0 | ||
| Summary: Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation | ||
@@ -5,0 +5,0 @@ Home-page: https://github.com/denisenkom/pytds |
+43
-0
@@ -31,2 +31,3 @@ pytds | ||
| * Kerberos support on non-Windows platforms (requires kerberos package) | ||
| * Token-based authentication | ||
@@ -77,3 +78,45 @@ Installation | ||
| or with token-based authentication | ||
| .. code-block:: python | ||
| import os | ||
| import pytds | ||
| import msal | ||
| SQL_SERVER = 'your-server' | ||
| DATABASE = 'your-db' | ||
| CAFILE = '/etc/ssl/cert.pem' | ||
| def get_access_token(): | ||
| tenant_id = os.getenv("TENANT_ID") | ||
| client_id = os.getenv("CLIENT_ID") | ||
| client_secret = os.getenv("CLIENT_SECRET") | ||
| authority = f'https://login.microsoftonline.com/{tenant_id}' | ||
| scope = ['https://database.windows.net/.default'] | ||
| app = msal.ConfidentialClientApplication( | ||
| client_id=client_id, | ||
| client_credential=client_secret, | ||
| authority=authority | ||
| ) | ||
| result = app.acquire_token_for_client(scopes=scope) | ||
| if "access_token" not in result: | ||
| raise RuntimeError(f"Token acquisition failed: {result.get('error_description')}") | ||
| return result["access_token"] | ||
| # Use the token callable when connecting | ||
| with pytds.connect( | ||
| server=SQL_SERVER, | ||
| database=DATABASE, | ||
| access_token_callable=get_access_token, | ||
| cafile=CAFILE, | ||
| ) as conn: | ||
| with conn.cursor() as cur: | ||
| cur.execute('SELECT TOP 1 name FROM sys.databases') | ||
| print(cur.fetchone()) | ||
| To enable TLS you should also provide cafile parameter which should be a file name containing trusted CAs in PEM format. | ||
@@ -80,0 +123,0 @@ |
@@ -11,3 +11,3 @@ """DB-SIG compliant module for communicating with MS SQL servers""" | ||
| import warnings | ||
| from typing import Any | ||
| from typing import Any, Callable | ||
@@ -144,2 +144,3 @@ from pytds.tds_types import TzInfoFactoryType | ||
| isolation_level: int = 0, | ||
| access_token_callable: Callable[[], str] | None = None, | ||
| ): | ||
@@ -202,2 +203,4 @@ """ | ||
| Cannot be used together with auth parameter. | ||
| :keyword access_token_callable: Callable that returns a Federated Authentication Token | ||
| :type access_token_callable: Callable[[], str] | ||
| :returns: An instance of :class:`Connection` | ||
@@ -207,2 +210,6 @@ """ | ||
| raise ValueError("use_sso cannot be used with auth parameter defined") | ||
| if (user or password) and access_token_callable: | ||
| raise ValueError("user/password cannot be used with access_token_callable") | ||
| login = tds_base._TdsLogin() | ||
@@ -230,2 +237,4 @@ login.client_host_name = socket.gethostname()[:128] | ||
| login.enc_login_only = enc_login_only | ||
| login.access_token_callable = access_token_callable | ||
| if cafile: | ||
@@ -430,2 +439,6 @@ if not tls.OPENSSL_AVAILABLE: | ||
| ) | ||
| if login.access_token_callable is not None: | ||
| login.access_token = login.access_token_callable() | ||
| if not sock: | ||
@@ -432,0 +445,0 @@ logger.info("Opening socket to %s:%d", host, resolved_port) |
@@ -16,3 +16,3 @@ """ | ||
| from collections import deque | ||
| from typing import Protocol, Iterable, TypedDict, Tuple, Any | ||
| from typing import Callable, Protocol, Iterable, TypedDict, Tuple, Any | ||
@@ -56,2 +56,5 @@ import pytds | ||
| def IS_TDS74_PLUS(x: _TdsSession): | ||
| return x.tds_version >= TDS74 | ||
| # https://msdn.microsoft.com/en-us/library/dd304214.aspx | ||
@@ -258,2 +261,20 @@ class PacketType: | ||
| # as per https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-tds/773a62b6-ee89-4c02-9e5e-344882630aac | ||
| TDS_LOGIN_FEATURE_SESSIONRECOVERY = 0x01 | ||
| TDS_LOGIN_FEATURE_FEDAUTH = 0x02 | ||
| TDS_LOGIN_FEATURE_COLUMNENCRYPTION = 0x04 | ||
| TDS_LOGIN_FEATURE_GLOBALTRANSACTIONS = 0x05 | ||
| TDS_LOGIN_FEATURE_AZURESQLSUPPORT = 0x08 | ||
| TDS_LOGIN_FEATURE_DATACLASSIFICATION = 0x09 | ||
| TDS_LOGIN_FEATURE_UTF8_SUPPORT = 0x0A | ||
| TDS_LOGIN_FEATURE_AZURESQLDNSCACHING = 0x0B | ||
| TDS_FEDAUTH_OPTIONS_LIBRARY_LIVEID_COMPACTTOKEN = 0x00 | ||
| TDS_FEDAUTH_OPTIONS_LIBRARY_SECURITYTOKEN = 0x01 | ||
| TDS_FEDAUTH_OPTIONS_LIBRARY_ADAL = 0x02 | ||
| TDS_FEDAUTH_OPTIONS_ECHO_YES = 0x01 | ||
| TDS_FEDAUTH_OPTIONS_ECHO_NO = 0x00 | ||
| # | ||
@@ -974,2 +995,5 @@ # Sybase only types | ||
| self.server_enc_flag = 0 | ||
| self.access_token_callable: Callable[[], str] | None = None | ||
| self.access_token: str | None = None | ||
| self.nonce: bytes | None = None | ||
@@ -976,0 +1000,0 @@ |
@@ -79,2 +79,3 @@ """ | ||
| self.product_version = 0 | ||
| self.fedauth_required = False | ||
@@ -81,0 +82,0 @@ def __repr__(self) -> str: |
| Metadata-Version: 2.4 | ||
| Name: python-tds | ||
| Version: 1.16.1 | ||
| Version: 1.17.0 | ||
| Summary: Python DBAPI driver for MSSQL using pure Python TDS (Tabular Data Stream) protocol implementation | ||
@@ -5,0 +5,0 @@ Home-page: https://github.com/denisenkom/pytds |
@@ -15,2 +15,3 @@ LICENSE.txt | ||
| src/pytds/extensions.py | ||
| src/pytds/fedauth.py | ||
| src/pytds/instance_browser_client.py | ||
@@ -17,0 +18,0 @@ src/pytds/lcid.py |
@@ -16,3 +16,3 @@ pytest>=3.3.2 | ||
| # requiring older cryptography library to avoid this error | ||
| cryptography < 3.3 | ||
| cryptography>=3.3,<3.5 | ||
| sqlalchemy-pytds | ||
@@ -23,2 +23,2 @@ SQLAlchemy==2.0.34 | ||
| ruff==0.6.3 | ||
| setuptools==75.1.0 | ||
| setuptools==75.1.0 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Alert delta unavailable
Currently unable to show alert delta for PyPI packages.
360832
1.71%38
2.7%9215
0.81%