forked from databricks/databricks-sql-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoidc_utils.py
More file actions
75 lines (60 loc) · 2.16 KB
/
Copy pathoidc_utils.py
File metadata and controls
75 lines (60 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import logging
import requests
from typing import Optional
from urllib.parse import urlparse
from databricks.sql.auth.endpoint import (
get_oauth_endpoints,
infer_cloud_from_host,
)
logger = logging.getLogger(__name__)
class OIDCDiscoveryUtil:
"""
Utility class for OIDC discovery operations.
This class handles discovery of OIDC endpoints through standard
discovery mechanisms, with fallback to default endpoints if needed.
"""
# Standard token endpoint path for Databricks workspaces
DEFAULT_TOKEN_PATH = "oidc/v1/token"
@staticmethod
def discover_token_endpoint(hostname: str) -> str:
"""
Get the token endpoint for the given Databricks hostname.
For Databricks workspaces, the token endpoint is always at host/oidc/v1/token.
Args:
hostname: The hostname to get token endpoint for
Returns:
str: The token endpoint URL
"""
# Format the hostname and return the standard endpoint
hostname = OIDCDiscoveryUtil.format_hostname(hostname)
token_endpoint = f"{hostname}{OIDCDiscoveryUtil.DEFAULT_TOKEN_PATH}"
logger.info(f"Using token endpoint: {token_endpoint}")
return token_endpoint
@staticmethod
def format_hostname(hostname: str) -> str:
"""
Format hostname to ensure it has proper https:// prefix and trailing slash.
Args:
hostname: The hostname to format
Returns:
str: The formatted hostname
"""
if not hostname.startswith("https://"):
hostname = f"https://{hostname}"
if not hostname.endswith("/"):
hostname = f"{hostname}/"
return hostname
def is_same_host(url1: str, url2: str) -> bool:
"""
Check if two URLs have the same host.
"""
try:
if not url1.startswith(("http://", "https://")):
url1 = f"https://{url1}"
if not url2.startswith(("http://", "https://")):
url2 = f"https://{url2}"
parsed1 = urlparse(url1)
parsed2 = urlparse(url2)
return parsed1.netloc.lower() == parsed2.netloc.lower()
except Exception:
return False