|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import time |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | +import httpx |
| 8 | +from uipath._cli._auth._portal_service import PortalService |
| 9 | +from uipath._cli._auth._url_utils import build_service_url, resolve_domain |
| 10 | +from uipath._cli._auth._utils import get_auth_data, update_auth_file |
| 11 | +from uipath._utils._auth import parse_access_token |
| 12 | +from uipath._utils._ssl_context import get_httpx_client_kwargs |
| 13 | +from uipath._utils.constants import ENV_UIPATH_ACCESS_TOKEN |
| 14 | +from uipath.platform import UiPath |
| 15 | +from uipath.platform.common import TokenData |
| 16 | +from uipath.platform.common._config import UiPathApiConfig |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | +REFRESH_MARGIN_SECONDS = 300 # Refresh 5 minutes before expiry |
| 21 | +FALLBACK_REFRESH_INTERVAL = 45 * 60 # 45 minutes when exp claim is unavailable |
| 22 | +MAX_RETRY_ATTEMPTS = 3 |
| 23 | +RETRY_BASE_DELAY = 5 # seconds |
| 24 | +RETRY_FALLBACK_INTERVAL = 60 # seconds to wait after all retries fail |
| 25 | + |
| 26 | + |
| 27 | +class AuthStrategy(Enum): |
| 28 | + OAUTH = "oauth" |
| 29 | + CLIENT_CREDENTIALS = "client_credentials" |
| 30 | + NONE = "none" |
| 31 | + |
| 32 | + |
| 33 | +class TokenRefresher: |
| 34 | + """Manages token refresh for long-lived MCP runtime connections.""" |
| 35 | + |
| 36 | + def __init__(self, uipath: UiPath): |
| 37 | + self._uipath = uipath |
| 38 | + self._refresh_task: asyncio.Task[None] | None = None |
| 39 | + self._cancel_event = asyncio.Event() |
| 40 | + |
| 41 | + self._client_id: str | None = os.environ.get("UIPATH_CLIENT_ID") |
| 42 | + self._client_secret: str | None = os.environ.get("UIPATH_CLIENT_SECRET") |
| 43 | + |
| 44 | + self._base_url: str = uipath._config.base_url |
| 45 | + self._domain: str = resolve_domain(self._base_url, environment=None) |
| 46 | + |
| 47 | + self._strategy = self._detect_strategy() |
| 48 | + self._token_url: str | None = self._resolve_token_url() |
| 49 | + |
| 50 | + if ( |
| 51 | + self._strategy == AuthStrategy.CLIENT_CREDENTIALS |
| 52 | + and self._token_url is None |
| 53 | + ): |
| 54 | + logger.error("Token refresh disabled: could not resolve token URL") |
| 55 | + self._strategy = AuthStrategy.NONE |
| 56 | + |
| 57 | + def _detect_strategy(self) -> AuthStrategy: |
| 58 | + """Detect which auth flow is available for token refresh.""" |
| 59 | + if self._client_id and self._client_secret: |
| 60 | + return AuthStrategy.CLIENT_CREDENTIALS |
| 61 | + |
| 62 | + try: |
| 63 | + auth_data = get_auth_data() |
| 64 | + if auth_data.refresh_token: |
| 65 | + return AuthStrategy.OAUTH |
| 66 | + except Exception as e: |
| 67 | + logger.debug(f"Could not read auth file for strategy detection: {e}") |
| 68 | + |
| 69 | + return AuthStrategy.NONE |
| 70 | + |
| 71 | + def _resolve_token_url(self) -> str | None: |
| 72 | + """Derive the identity token endpoint for client_credentials flow.""" |
| 73 | + if self._strategy != AuthStrategy.CLIENT_CREDENTIALS: |
| 74 | + return None |
| 75 | + |
| 76 | + try: |
| 77 | + return build_service_url(self._domain, "/identity_/connect/token") |
| 78 | + except Exception as e: |
| 79 | + logger.error( |
| 80 | + f"Could not resolve token URL from base_url '{self._base_url}': {e}" |
| 81 | + ) |
| 82 | + return None |
| 83 | + |
| 84 | + @property |
| 85 | + def strategy(self) -> AuthStrategy: |
| 86 | + return self._strategy |
| 87 | + |
| 88 | + def start(self) -> None: |
| 89 | + """Start the background refresh task.""" |
| 90 | + if self._strategy == AuthStrategy.NONE: |
| 91 | + logger.info("No token refresh strategy available; refresh disabled") |
| 92 | + return |
| 93 | + |
| 94 | + self._cancel_event.clear() |
| 95 | + self._refresh_task = asyncio.create_task(self._refresh_loop()) |
| 96 | + logger.info("Token refresh background task started") |
| 97 | + |
| 98 | + async def stop(self) -> None: |
| 99 | + """Stop the background refresh task.""" |
| 100 | + self._cancel_event.set() |
| 101 | + if self._refresh_task and not self._refresh_task.done(): |
| 102 | + self._refresh_task.cancel() |
| 103 | + try: |
| 104 | + await asyncio.wait_for(self._refresh_task, timeout=5.0) |
| 105 | + except (asyncio.CancelledError, asyncio.TimeoutError): |
| 106 | + pass |
| 107 | + self._refresh_task = None |
| 108 | + logger.info("Token refresh stopped") |
| 109 | + |
| 110 | + async def _wait_for_cancel(self, seconds: float) -> bool: |
| 111 | + """Sleep for `seconds`, returning True if cancellation was requested.""" |
| 112 | + try: |
| 113 | + await asyncio.wait_for(self._cancel_event.wait(), timeout=seconds) |
| 114 | + return True |
| 115 | + except asyncio.TimeoutError: |
| 116 | + return False |
| 117 | + |
| 118 | + async def _refresh_loop(self) -> None: |
| 119 | + """Background loop that refreshes the token before expiry.""" |
| 120 | + try: |
| 121 | + while not self._cancel_event.is_set(): |
| 122 | + wait_seconds = self._seconds_until_refresh() |
| 123 | + if wait_seconds > 0 and await self._wait_for_cancel(wait_seconds): |
| 124 | + break |
| 125 | + |
| 126 | + if not await self._try_refresh() and not self._cancel_event.is_set(): |
| 127 | + logger.error( |
| 128 | + "All token refresh attempts failed. " |
| 129 | + "The token may expire causing failures." |
| 130 | + ) |
| 131 | + # Avoid retry loop when the token is already expired |
| 132 | + if await self._wait_for_cancel(RETRY_FALLBACK_INTERVAL): |
| 133 | + break |
| 134 | + except asyncio.CancelledError: |
| 135 | + logger.info("Token refresh loop cancelled") |
| 136 | + raise |
| 137 | + |
| 138 | + async def _try_refresh(self) -> bool: |
| 139 | + """Attempt to refresh the token with retries. Returns True on success.""" |
| 140 | + for attempt in range(MAX_RETRY_ATTEMPTS): |
| 141 | + try: |
| 142 | + if self._strategy == AuthStrategy.OAUTH: |
| 143 | + token_data = await self._refresh_oauth() |
| 144 | + else: |
| 145 | + token_data = await self._refresh_client_credentials() |
| 146 | + |
| 147 | + self._propagate_token(token_data) |
| 148 | + logger.info("Token refreshed successfully.") |
| 149 | + return True |
| 150 | + |
| 151 | + except Exception as e: |
| 152 | + safe_msg = ( |
| 153 | + f"HTTP {e.response.status_code}" |
| 154 | + if isinstance(e, httpx.HTTPStatusError) |
| 155 | + else type(e).__name__ |
| 156 | + ) |
| 157 | + logger.error( |
| 158 | + f"Token refresh attempt {attempt + 1}/{MAX_RETRY_ATTEMPTS} " |
| 159 | + f"failed: {safe_msg}" |
| 160 | + ) |
| 161 | + if attempt < MAX_RETRY_ATTEMPTS - 1: |
| 162 | + logger.info(f"Retrying in {RETRY_BASE_DELAY}s...") |
| 163 | + if await self._wait_for_cancel(RETRY_BASE_DELAY): |
| 164 | + return False |
| 165 | + |
| 166 | + return False |
| 167 | + |
| 168 | + async def _refresh_oauth(self) -> TokenData: |
| 169 | + """Refresh using OAuth refresh_token grant.""" |
| 170 | + auth_data = get_auth_data() |
| 171 | + refresh_token = auth_data.refresh_token |
| 172 | + if not refresh_token: |
| 173 | + raise ValueError("No refresh_token found in .uipath/.auth.json") |
| 174 | + |
| 175 | + def _do_refresh() -> TokenData: |
| 176 | + with PortalService(domain=self._domain) as portal: |
| 177 | + return portal.refresh_access_token(refresh_token) |
| 178 | + |
| 179 | + # run in a thread to avoid blocking |
| 180 | + token_data = await asyncio.to_thread(_do_refresh) |
| 181 | + |
| 182 | + try: |
| 183 | + update_auth_file(token_data) |
| 184 | + except Exception as e: |
| 185 | + logger.warning(f"Failed to update .auth.json: {type(e).__name__}") |
| 186 | + |
| 187 | + return token_data |
| 188 | + |
| 189 | + async def _refresh_client_credentials(self) -> TokenData: |
| 190 | + """Refresh using client_credentials grant.""" |
| 191 | + assert self._token_url is not None, ( |
| 192 | + "token_url must be set for client_credentials strategy" |
| 193 | + ) |
| 194 | + |
| 195 | + data = { |
| 196 | + "grant_type": "client_credentials", |
| 197 | + "client_id": self._client_id, |
| 198 | + "client_secret": self._client_secret, |
| 199 | + "scope": os.environ.get("UIPATH_CLIENT_SCOPE", "OR.Execution"), |
| 200 | + } |
| 201 | + |
| 202 | + async with httpx.AsyncClient(**get_httpx_client_kwargs()) as client: |
| 203 | + response = await client.post( |
| 204 | + self._token_url, |
| 205 | + data=data, |
| 206 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 207 | + ) |
| 208 | + response.raise_for_status() |
| 209 | + return TokenData.model_validate(response.json()) |
| 210 | + |
| 211 | + def _propagate_token(self, token_data: TokenData) -> None: |
| 212 | + """Update all token consumers after a successful refresh.""" |
| 213 | + new_token = token_data.access_token |
| 214 | + |
| 215 | + self._uipath._config = UiPathApiConfig( |
| 216 | + base_url=self._uipath._config.base_url, |
| 217 | + secret=new_token, |
| 218 | + ) |
| 219 | + |
| 220 | + os.environ[ENV_UIPATH_ACCESS_TOKEN] = new_token |
| 221 | + |
| 222 | + def _seconds_until_refresh(self) -> float: |
| 223 | + """Calculate seconds to wait before next refresh attempt.""" |
| 224 | + try: |
| 225 | + claims = parse_access_token(self._uipath._config.secret) |
| 226 | + exp = claims.get("exp") |
| 227 | + if exp is not None: |
| 228 | + remaining = float(exp) - time.time() |
| 229 | + if remaining <= REFRESH_MARGIN_SECONDS: |
| 230 | + return 0 |
| 231 | + return remaining - REFRESH_MARGIN_SECONDS |
| 232 | + except Exception as e: |
| 233 | + logger.warning(f"Failed to parse token expiry: {e}") |
| 234 | + |
| 235 | + return FALLBACK_REFRESH_INTERVAL |
0 commit comments