Skip to content

Commit 010b58e

Browse files
committed
nit
1 parent b79af03 commit 010b58e

File tree

2 files changed

+54
-54
lines changed

2 files changed

+54
-54
lines changed

src/databricks/sql/auth/auth_utils.py

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -62,56 +62,3 @@ def is_same_host(url1: str, url2: str) -> bool:
6262
except Exception as e:
6363
logger.debug("Failed to parse URLs: %s", e)
6464
return False
65-
66-
67-
class Token:
68-
"""
69-
Represents an OAuth token with expiration management.
70-
"""
71-
72-
def __init__(self, access_token: str, token_type: str = "Bearer"):
73-
"""
74-
Initialize a token.
75-
76-
Args:
77-
access_token: The access token string
78-
token_type: The token type (default: Bearer)
79-
"""
80-
self.access_token = access_token
81-
self.token_type = token_type
82-
self.expiry_time = self._calculate_expiry()
83-
84-
def _calculate_expiry(self) -> datetime:
85-
"""
86-
Calculate the token expiry time from JWT claims.
87-
88-
Returns:
89-
The token expiry datetime
90-
"""
91-
decoded = decode_token(self.access_token)
92-
if decoded and "exp" in decoded:
93-
# Use JWT exp claim with 1 minute buffer
94-
return datetime.fromtimestamp(decoded["exp"]) - timedelta(minutes=1)
95-
# Default to 1 hour if no expiry info
96-
return datetime.now() + timedelta(hours=1)
97-
98-
def is_expired(self) -> bool:
99-
"""
100-
Check if the token is expired.
101-
102-
Returns:
103-
True if token is expired, False otherwise
104-
"""
105-
return datetime.now() >= self.expiry_time
106-
107-
def to_dict(self) -> Dict[str, str]:
108-
"""
109-
Convert token to dictionary format.
110-
111-
Returns:
112-
Dictionary with access_token and token_type
113-
"""
114-
return {
115-
"access_token": self.access_token,
116-
"token_type": self.token_type,
117-
}

src/databricks/sql/auth/token_federation.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import logging
22
import json
3+
from datetime import datetime, timedelta
34
from typing import Optional, Dict, Tuple
45
from urllib.parse import urlencode
56

67
from databricks.sql.auth.authenticators import AuthProvider
78
from databricks.sql.auth.auth_utils import (
8-
Token,
99
parse_hostname,
1010
decode_token,
1111
is_same_host,
@@ -15,6 +15,59 @@
1515
logger = logging.getLogger(__name__)
1616

1717

18+
class Token:
19+
"""
20+
Represents an OAuth token with expiration management.
21+
"""
22+
23+
def __init__(self, access_token: str, token_type: str = "Bearer"):
24+
"""
25+
Initialize a token.
26+
27+
Args:
28+
access_token: The access token string
29+
token_type: The token type (default: Bearer)
30+
"""
31+
self.access_token = access_token
32+
self.token_type = token_type
33+
self.expiry_time = self._calculate_expiry()
34+
35+
def _calculate_expiry(self) -> datetime:
36+
"""
37+
Calculate the token expiry time from JWT claims.
38+
39+
Returns:
40+
The token expiry datetime
41+
"""
42+
decoded = decode_token(self.access_token)
43+
if decoded and "exp" in decoded:
44+
# Use JWT exp claim with 1 minute buffer
45+
return datetime.fromtimestamp(decoded["exp"]) - timedelta(minutes=1)
46+
# Default to 1 hour if no expiry info
47+
return datetime.now() + timedelta(hours=1)
48+
49+
def is_expired(self) -> bool:
50+
"""
51+
Check if the token is expired.
52+
53+
Returns:
54+
True if token is expired, False otherwise
55+
"""
56+
return datetime.now() >= self.expiry_time
57+
58+
def to_dict(self) -> Dict[str, str]:
59+
"""
60+
Convert token to dictionary format.
61+
62+
Returns:
63+
Dictionary with access_token and token_type
64+
"""
65+
return {
66+
"access_token": self.access_token,
67+
"token_type": self.token_type,
68+
}
69+
70+
1871
class TokenFederationProvider(AuthProvider):
1972
"""
2073
Implementation of Token Federation for Databricks SQL Python driver.

0 commit comments

Comments
 (0)