@@ -15,6 +15,7 @@ class AuthType(Enum):
15
15
AZURE_OAUTH = "azure-oauth"
16
16
# TODO: Token federation should be a feature that works with different auth types,
17
17
# not an auth type itself. This will be refactored in a future change.
18
+ # We will add a use_token_federation flag that can be used with any auth type.
18
19
TOKEN_FEDERATION = "token-federation"
19
20
# other supported types (access_token) can be inferred
20
21
# we can add more types as needed later
@@ -49,10 +50,28 @@ def __init__(
49
50
50
51
51
52
def get_auth_provider (cfg : ClientContext ):
52
- # TODO: In a future refactoring, token federation should be a feature that wraps
53
- # any auth provider, not a separate auth type. The code below treats it as an auth type
54
- # for backward compatibility, but this approach will be revised.
55
-
53
+ """
54
+ Get an appropriate auth provider based on the provided configuration.
55
+
56
+ Token Federation Support:
57
+ -----------------------
58
+ Currently, token federation is implemented as a separate auth type, but the goal is to
59
+ refactor it as a feature that can work with any auth type. The current implementation
60
+ is maintained for backward compatibility while the refactoring is planned.
61
+
62
+ Future refactoring will introduce a `use_token_federation` flag that can be combined
63
+ with any auth type to enable token federation.
64
+
65
+ Args:
66
+ cfg: The client context containing configuration parameters
67
+
68
+ Returns:
69
+ An appropriate AuthProvider instance
70
+
71
+ Raises:
72
+ RuntimeError: If no valid authentication settings are provided
73
+ """
74
+ # If credentials_provider is explicitly provided
56
75
if cfg .credentials_provider :
57
76
# If token federation is enabled and credentials provider is provided,
58
77
# wrap the credentials provider with DatabricksTokenFederationProvider
@@ -73,13 +92,15 @@ def get_auth_provider(cfg: ClientContext):
73
92
74
93
# If we don't have a credentials provider but have token federation auth type with access token
75
94
if cfg .auth_type == AuthType .TOKEN_FEDERATION .value and cfg .access_token :
76
- # If only access_token is provided with token federation, use create_token_federation_provider
95
+ # Create a simple credentials provider and wrap it with token federation provider
77
96
from databricks .sql .auth .token_federation import (
78
- create_token_federation_provider ,
97
+ DatabricksTokenFederationProvider ,
98
+ SimpleCredentialsProvider ,
79
99
)
80
100
81
- federation_provider = create_token_federation_provider (
82
- cfg .access_token , cfg .hostname , cfg .identity_federation_client_id
101
+ simple_provider = SimpleCredentialsProvider (cfg .access_token )
102
+ federation_provider = DatabricksTokenFederationProvider (
103
+ simple_provider , cfg .hostname , cfg .identity_federation_client_id
83
104
)
84
105
return ExternalAuthProvider (federation_provider )
85
106
@@ -140,6 +161,27 @@ def get_client_id_and_redirect_port(use_azure_auth: bool):
140
161
141
162
142
163
def get_python_sql_connector_auth_provider (hostname : str , ** kwargs ):
164
+ """
165
+ Get an auth provider for the Python SQL connector.
166
+
167
+ This function is the main entry point for authentication in the SQL connector.
168
+ It processes the parameters and creates an appropriate auth provider.
169
+
170
+ TODO: Future refactoring needed:
171
+ 1. Add a use_token_federation flag that can be combined with any auth type
172
+ 2. Remove TOKEN_FEDERATION as an auth_type while maintaining backward compatibility
173
+ 3. Create a token federation wrapper that can wrap any existing auth provider
174
+
175
+ Args:
176
+ hostname: The Databricks server hostname
177
+ **kwargs: Additional configuration parameters
178
+
179
+ Returns:
180
+ An appropriate AuthProvider instance
181
+
182
+ Raises:
183
+ ValueError: If username/password authentication is attempted (no longer supported)
184
+ """
143
185
auth_type = kwargs .get ("auth_type" )
144
186
(client_id , redirect_port_range ) = get_client_id_and_redirect_port (
145
187
auth_type == AuthType .AZURE_OAUTH .value
@@ -150,10 +192,6 @@ def get_python_sql_connector_auth_provider(hostname: str, **kwargs):
150
192
"Please use OAuth or access token instead."
151
193
)
152
194
153
- # TODO: Future refactoring needed:
154
- # - Add a use_token_federation flag that can be combined with any auth type
155
- # - Remove TOKEN_FEDERATION as an auth_type and properly handle the underlying auth type
156
- # - Maintain backward compatibility during transition
157
195
cfg = ClientContext (
158
196
hostname = normalize_host_name (hostname ),
159
197
auth_type = auth_type ,
0 commit comments