|
| 1 | +from typing import Optional |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +import httpx |
| 5 | + |
| 6 | +from .._utils._console import ConsoleLogger |
| 7 | +from ._models import TokenData |
| 8 | +from ._utils import parse_access_token, update_env_file |
| 9 | + |
| 10 | +console = ConsoleLogger() |
| 11 | + |
| 12 | + |
| 13 | +class ClientCredentialsService: |
| 14 | + """Service for client credentials authentication flow.""" |
| 15 | + |
| 16 | + def __init__(self, domain: str): |
| 17 | + self.domain = domain |
| 18 | + |
| 19 | + def get_token_url(self) -> str: |
| 20 | + """Get the token URL for the specified domain.""" |
| 21 | + if self.domain == "alpha": |
| 22 | + return "https://alpha.uipath.com/identity_/connect/token" |
| 23 | + elif self.domain == "staging": |
| 24 | + return "https://staging.uipath.com/identity_/connect/token" |
| 25 | + else: # cloud (default) |
| 26 | + return "https://cloud.uipath.com/identity_/connect/token" |
| 27 | + |
| 28 | + def extract_domain_from_base_url(self, base_url: str) -> str: |
| 29 | + """Extract domain from base URL. |
| 30 | +
|
| 31 | + Args: |
| 32 | + base_url: The base URL to extract domain from |
| 33 | +
|
| 34 | + Returns: |
| 35 | + The domain (alpha, staging, or cloud) |
| 36 | + """ |
| 37 | + try: |
| 38 | + parsed = urlparse(base_url) |
| 39 | + hostname = parsed.hostname |
| 40 | + |
| 41 | + if hostname: |
| 42 | + if "alpha.uipath.com" in hostname: |
| 43 | + return "alpha" |
| 44 | + elif "staging.uipath.com" in hostname: |
| 45 | + return "staging" |
| 46 | + elif "cloud.uipath.com" in hostname: |
| 47 | + return "cloud" |
| 48 | + |
| 49 | + # Default to cloud if we can't determine |
| 50 | + return "cloud" |
| 51 | + except Exception: |
| 52 | + # Default to cloud if parsing fails |
| 53 | + return "cloud" |
| 54 | + |
| 55 | + def authenticate( |
| 56 | + self, client_id: str, client_secret: str, scope: str = "OR.Execution" |
| 57 | + ) -> Optional[TokenData]: |
| 58 | + """Authenticate using client credentials flow. |
| 59 | +
|
| 60 | + Args: |
| 61 | + client_id: The client ID for authentication |
| 62 | + client_secret: The client secret for authentication |
| 63 | + scope: The scope for the token (default: OR.Execution) |
| 64 | +
|
| 65 | + Returns: |
| 66 | + Token data if successful, None otherwise |
| 67 | + """ |
| 68 | + token_url = self.get_token_url() |
| 69 | + |
| 70 | + data = { |
| 71 | + "grant_type": "client_credentials", |
| 72 | + "client_id": client_id, |
| 73 | + "client_secret": client_secret, |
| 74 | + "scope": scope, |
| 75 | + } |
| 76 | + |
| 77 | + try: |
| 78 | + with httpx.Client(timeout=30.0) as client: |
| 79 | + response = client.post(token_url, data=data) |
| 80 | + |
| 81 | + if response.status_code == 200: |
| 82 | + token_data = response.json() |
| 83 | + # Convert to our TokenData format |
| 84 | + return { |
| 85 | + "access_token": token_data["access_token"], |
| 86 | + "token_type": token_data.get("token_type", "Bearer"), |
| 87 | + "expires_in": token_data.get("expires_in", 3600), |
| 88 | + "scope": token_data.get("scope", scope), |
| 89 | + # Client credentials flow doesn't provide these, but we need them for compatibility |
| 90 | + "refresh_token": "", |
| 91 | + "id_token": "", |
| 92 | + } |
| 93 | + elif response.status_code == 400: |
| 94 | + console.error("Invalid client credentials or request parameters.") |
| 95 | + return None |
| 96 | + elif response.status_code == 401: |
| 97 | + console.error("Unauthorized: Invalid client credentials.") |
| 98 | + return None |
| 99 | + else: |
| 100 | + console.error( |
| 101 | + f"Authentication failed: {response.status_code} - {response.text}" |
| 102 | + ) |
| 103 | + return None |
| 104 | + |
| 105 | + except httpx.RequestError as e: |
| 106 | + console.error(f"Network error during authentication: {e}") |
| 107 | + return None |
| 108 | + except Exception as e: |
| 109 | + console.error(f"Unexpected error during authentication: {e}") |
| 110 | + return None |
| 111 | + |
| 112 | + def setup_environment(self, token_data: TokenData, base_url: str): |
| 113 | + """Setup environment variables for client credentials authentication. |
| 114 | +
|
| 115 | + Args: |
| 116 | + token_data: The token data from authentication |
| 117 | + base_url: The base URL for the UiPath instance |
| 118 | + """ |
| 119 | + parsed_access_token = parse_access_token(token_data["access_token"]) |
| 120 | + |
| 121 | + env_vars = { |
| 122 | + "UIPATH_ACCESS_TOKEN": token_data["access_token"], |
| 123 | + "UIPATH_URL": base_url, |
| 124 | + "UIPATH_ORGANIZATION_ID": parsed_access_token.get("prt_id", ""), |
| 125 | + "UIPATH_TENANT_ID": "", |
| 126 | + } |
| 127 | + |
| 128 | + update_env_file(env_vars) |
0 commit comments