Fix sync TUI freeze by completing auth before starting dashboard

This commit is contained in:
Bendt
2025-12-19 15:50:23 -05:00
parent 560bc1d3bd
commit aaabd83fc7
2 changed files with 82 additions and 0 deletions

View File

@@ -25,6 +25,49 @@ def ensure_directory_exists(path):
os.makedirs(path)
def has_valid_cached_token(scopes=None):
"""
Check if we have a valid cached token (without triggering auth flow).
Args:
scopes: List of scopes to check. If None, uses default scopes.
Returns:
bool: True if a valid cached token exists, False otherwise.
"""
if scopes is None:
scopes = ["https://graph.microsoft.com/Mail.Read"]
client_id = os.getenv("AZURE_CLIENT_ID")
tenant_id = os.getenv("AZURE_TENANT_ID")
if not client_id or not tenant_id:
return False
cache = msal.SerializableTokenCache()
cache_file = "token_cache.bin"
if not os.path.exists(cache_file):
return False
try:
cache.deserialize(open(cache_file, "r").read())
authority = f"https://login.microsoftonline.com/{tenant_id}"
app = msal.PublicClientApplication(
client_id, authority=authority, token_cache=cache
)
accounts = app.get_accounts()
if not accounts:
return False
# Try silent auth - this will return None if token is expired
token_response = app.acquire_token_silent(scopes, account=accounts[0])
return token_response is not None and "access_token" in token_response
except Exception:
return False
def get_access_token(scopes):
"""
Authenticate with Microsoft Graph API and obtain an access token.