OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
import json
import os
from typing import Any
import msal
from connectors.microsoft_oauth_utils import (
trusted_tenant_id_from_account,
trusted_tenant_id_from_token_result,
verify_ms_access_token,
)
from utils.logging_config import get_logger
logger = get_logger(__name__)
# Backward-compat alias used by get_access_token() call sites in this module.
_verify_access_token = verify_ms_access_token
def _tenant_id_for_result(result: dict | None, account: dict[str, Any] | None) -> str | None:
return trusted_tenant_id_from_token_result(result) or trusted_tenant_id_from_account(account)
class OneDriveOAuth:
"""Handles Microsoft Graph OAuth for OneDrive (personal Microsoft accounts by default)."""
# Reserved scopes that must NOT be sent on token or silent calls
RESERVED_SCOPES = {"openid", "profile", "offline_access"}
# For Microsoft Accounts (OneDrive consumer & work/school):
# - Use AUTH_SCOPES for interactive auth (consent + refresh token issuance)
# - Use RESOURCE_SCOPES for acquire_token_silent / refresh paths
# NOTE: Including Sites.Read.All for SharePoint/OneDrive for Business access.
# Sites.Read.All requires admin consent for work/school accounts.
AUTH_SCOPES = [
"User.Read",
"Files.Read",
"Files.Read.All", # Access all files user can access
"Files.Read.Selected",
"offline_access",
]
RESOURCE_SCOPES = ["User.Read", "Files.Read", "Files.Read.All", "Files.Read.Selected"]
SCOPES = AUTH_SCOPES # Backward-compat alias if something references .SCOPES
# OAuth prompt for interactive auth. Use "select_account" (not "consent") so a
# one-time tenant admin consent is reused instead of re-prompting every user/login.
AUTH_PROMPT = "select_account"
# Kept for reference; MSAL derives endpoints from `authority`
AUTH_ENDPOINT = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
TOKEN_ENDPOINT = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
def __init__(
self,
client_id: str,
client_secret: str,
token_file: str = "onedrive_token.json",
authority: str = "https://login.microsoftonline.com/common",
allow_json_refresh: bool = True,
):
"""
Initialize OneDriveOAuth.
Args:
client_id: Azure AD application (client) ID.
client_secret: Azure AD application client secret.
token_file: Path to persisted token cache file (MSAL cache format).
authority: Usually "https://login.microsoftonline.com/common" for MSA + org,
or tenant-specific for work/school.
allow_json_refresh: If True, permit one-time migration from legacy flat JSON
{"access_token","refresh_token",...}. Otherwise refuse it.
"""
self.client_id = client_id
self.client_secret = client_secret
self.token_file = token_file
self.authority = authority
self.allow_json_refresh = allow_json_refresh
self.token_cache = msal.SerializableTokenCache()
self._current_account: dict[str, Any] | None = None
# Initialize MSAL Confidential Client
self.app = msal.ConfidentialClientApplication(
client_id=self.client_id,
client_credential=self.client_secret,
authority=self.authority,
token_cache=self.token_cache,
)
async def load_credentials(self) -> bool:
"""Load existing credentials from token file (async)."""
try:
from utils.encryption import read_encrypted_file
logger.debug(f"OneDrive OAuth loading credentials from: {self.token_file}")
cache_data, needs_upgrade = await read_encrypted_file(self.token_file)
if cache_data and cache_data.strip():
# 1) Try legacy flat JSON first
try:
json_data = json.loads(cache_data)
if isinstance(json_data, dict) and "refresh_token" in json_data:
if self.allow_json_refresh:
logger.debug(
"Found legacy JSON refresh_token and allow_json_refresh=True; attempting migration refresh"
)
return await self._refresh_from_json_token(json_data)
else:
logger.warning(
"Token file contains a legacy JSON refresh_token, but allow_json_refresh=False. "
"Delete the file and re-auth."
)
return False
except json.JSONDecodeError:
logger.debug("Token file is not flat JSON; attempting MSAL cache format")
# 2) Try MSAL cache format
logger.debug("Attempting MSAL cache deserialization")
self.token_cache.deserialize(cache_data)
# Get accounts from loaded cache
accounts = self.app.get_accounts()
logger.debug(f"Found {len(accounts)} accounts in MSAL cache")
if accounts:
account = accounts[0]
self._current_account = account
logger.debug(f"Set current account: {account.get('username', 'no username')}")
if needs_upgrade:
await self.save_cache()
# Use RESOURCE_SCOPES (no reserved scopes) for silent acquisition
result = self.app.acquire_token_silent(
self.RESOURCE_SCOPES, account=self._current_account
)
logger.debug(
f"Silent token acquisition result keys: {list(result.keys()) if result else 'None'}"
)
if result and "access_token" in result:
logger.debug("Silent token acquisition successful")
if getattr(self.token_cache, "has_state_changed", False):
await self.save_cache()
return True
else:
error_msg = (result or {}).get("error") or "No result"
logger.warning(f"Silent token acquisition failed: {error_msg}")
else:
logger.debug(f"Token file {self.token_file} is empty or does not exist")
return False
except Exception:
logger.exception("[CONNECTOR] OneDrive credential load failed")
return False
async def _refresh_from_json_token(self, token_data: dict) -> bool:
"""
Use refresh token from a legacy JSON file to get new tokens (one-time migration path).
Prefer using an MSAL cache file and acquire_token_silent(); this path is only for migrating older files.
"""
try:
refresh_token = token_data.get("refresh_token")
if not refresh_token:
logger.error("No refresh_token found in JSON file - cannot refresh")
logger.error("You must re-authenticate interactively to obtain a valid token")
return False
# Use only RESOURCE_SCOPES when refreshing (no reserved scopes)
refresh_scopes = [s for s in self.RESOURCE_SCOPES if s not in self.RESERVED_SCOPES]
logger.debug(f"Using refresh token; refresh scopes = {refresh_scopes}")
result = self.app.acquire_token_by_refresh_token(
refresh_token=refresh_token,
scopes=refresh_scopes,
)
if result and "access_token" in result:
logger.debug("Successfully refreshed token via legacy JSON path")
await self.save_cache()
accounts = self.app.get_accounts()
logger.debug(f"After refresh, found {len(accounts)} accounts")
if accounts:
account = accounts[0]
self._current_account = account
logger.debug(
f"Set current account after refresh: {account.get('username', 'no username')}"
)
return True
# Error handling
err = (
(result or {}).get("error_description")
or (result or {}).get("error")
or "Unknown error"
)
logger.error(f"Refresh token failed: {err}")
if any(
code in err for code in ("AADSTS70000", "invalid_grant", "interaction_required")
):
logger.warning(
"Refresh denied due to unauthorized/expired scopes or invalid grant. "
"Delete the token file and perform interactive sign-in with correct scopes."
)
return False
except Exception:
logger.exception("[CONNECTOR] OneDrive JSON token refresh failed")
return False
async def save_cache(self):
"""Persist the token cache to file."""
try:
# Ensure parent directory exists
parent = os.path.dirname(os.path.abspath(self.token_file))
if parent and not os.path.exists(parent):
os.makedirs(parent, exist_ok=True)
cache_data = self.token_cache.serialize()
if cache_data:
from utils.encryption import write_encrypted_file
await write_encrypted_file(self.token_file, cache_data)
logger.debug(f"Token cache saved to {self.token_file}")
except Exception as e:
logger.error(f"Failed to save token cache: {e}")
def create_authorization_url(self, redirect_uri: str, state: str | None = None) -> str:
"""Create authorization URL for OAuth flow."""
# Store redirect URI for later use in callback
self._redirect_uri = redirect_uri
kwargs: dict[str, Any] = {
# Interactive auth includes offline_access
"scopes": self.AUTH_SCOPES,
"redirect_uri": redirect_uri,
"prompt": "select_account", # Let user pick account without forcing consent
}
if state:
kwargs["state"] = state # Optional CSRF protection
auth_url = self.app.get_authorization_request_url(**kwargs)
logger.debug(f"Generated auth URL: {auth_url}")
logger.debug(f"Auth scopes: {self.AUTH_SCOPES}")
return auth_url
async def handle_authorization_callback(
self, authorization_code: str, redirect_uri: str
) -> bool:
"""Handle OAuth callback and exchange code for tokens."""
try:
result = self.app.acquire_token_by_authorization_code(
authorization_code,
scopes=self.AUTH_SCOPES, # same as authorize step
redirect_uri=redirect_uri,
)
if result and "access_token" in result:
accounts = self.app.get_accounts()
if accounts:
self._current_account = accounts[0]
await self.save_cache()
logger.info("OneDrive OAuth authorization successful")
return True
error_msg = (
(result or {}).get("error_description")
or (result or {}).get("error")
or "Unknown error"
)
logger.error(f"OneDrive OAuth authorization failed: {error_msg}")
return False
except Exception as e:
logger.error(f"Exception during OneDrive OAuth authorization: {e}")
return False
async def is_authenticated(self) -> bool:
"""Check if we have valid credentials."""
logger.info(f"OneDrive is_authenticated: Starting check, token_file={self.token_file}")
try:
# First try to load credentials if we haven't already
if not self._current_account:
logger.info("OneDrive is_authenticated: No current account, loading credentials")
load_result = await self.load_credentials()
logger.info(f"OneDrive is_authenticated: load_credentials returned {load_result}")
logger.info(
f"OneDrive is_authenticated: current_account={self._current_account is not None}"
)
# Try to get a token (MSAL will refresh if needed)
if self._current_account:
logger.info(
f"OneDrive is_authenticated: Trying acquire_token_silent with account {self._current_account.get('username', 'unknown')}"
)
logger.info(f"OneDrive is_authenticated: RESOURCE_SCOPES={self.RESOURCE_SCOPES}")
result = self.app.acquire_token_silent(
self.RESOURCE_SCOPES, account=self._current_account
)
if result and "access_token" in result:
logger.info(
"OneDrive is_authenticated: Successfully acquired token with account"
)
if getattr(self.token_cache, "has_state_changed", False):
await self.save_cache()
return True
else:
error_msg = (
(result or {}).get("error")
or (result or {}).get("error_description")
or "No result returned"
)
logger.warning(
f"OneDrive is_authenticated: Token acquisition failed for current account: {error_msg}"
)
logger.warning(f"OneDrive is_authenticated: Full result: {result}")
# Fallback: try without specific account
logger.info(
"OneDrive is_authenticated: Fallback - trying acquire_token_silent without account"
)
result = self.app.acquire_token_silent(self.RESOURCE_SCOPES, account=None)
if result and "access_token" in result:
accounts = self.app.get_accounts()
logger.info(
f"OneDrive is_authenticated: Fallback succeeded, found {len(accounts)} accounts"
)
if accounts:
self._current_account = accounts[0]
if getattr(self.token_cache, "has_state_changed", False):
await self.save_cache()
return True
logger.warning(f"OneDrive is_authenticated: Fallback also failed, result: {result}")
return False
except Exception:
logger.exception("[CONNECTOR] OneDrive is_authenticated failed")
return False
def get_access_token(self) -> str:
"""Get an access token for Microsoft Graph."""
logger.info(
f"OneDrive get_access_token: Starting, current_account={self._current_account is not None}"
)
try:
# Try with current account first
if self._current_account:
logger.info(
f"OneDrive get_access_token: Trying with account {self._current_account.get('username', 'unknown')}"
)
result = self.app.acquire_token_silent(
self.RESOURCE_SCOPES, account=self._current_account
)
if result and "access_token" in result:
access_token = result["access_token"]
_verify_access_token(
access_token,
tenant_id=_tenant_id_for_result(result, self._current_account),
) # raises JWTVerificationError on tenant policy failure
logger.info("OneDrive get_access_token: Success with current account")
return access_token
else:
logger.warning(
f"OneDrive get_access_token: Failed with account, result: {result}"
)
# Fallback: try without specific account
logger.info("OneDrive get_access_token: Fallback - trying without account")
result = self.app.acquire_token_silent(self.RESOURCE_SCOPES, account=None)
if result and "access_token" in result:
access_token = result["access_token"]
accounts = self.app.get_accounts()
account = accounts[0] if accounts else None
_verify_access_token(
access_token,
tenant_id=_tenant_id_for_result(result, account),
) # raises JWTVerificationError on tenant policy failure
logger.info("OneDrive get_access_token: Fallback success")
return access_token
# If we get here, authentication has failed
error_msg = (
(result or {}).get("error_description")
or (result or {}).get("error")
or "No valid authentication"
)
logger.error(f"OneDrive get_access_token: All attempts failed, error: {error_msg}")
raise ValueError(f"Failed to acquire access token: {error_msg}")
except Exception:
logger.exception("[CONNECTOR] OneDrive get_access_token failed")
raise
async def revoke_credentials(self):
"""Clear token cache and remove token file."""
try:
# Clear in-memory state
self._current_account = None
self.token_cache = msal.SerializableTokenCache()
# Recreate MSAL app with fresh cache
self.app = msal.ConfidentialClientApplication(
client_id=self.client_id,
client_credential=self.client_secret,
authority=self.authority,
token_cache=self.token_cache,
)
# Remove token file
if os.path.exists(self.token_file):
os.remove(self.token_file)
logger.info(f"Removed OneDrive token file: {self.token_file}")
except Exception as e:
logger.error(f"Failed to revoke OneDrive credentials: {e}")
def get_service(self) -> str:
"""Return an access token (Graph client is just the bearer)."""
return self.get_access_token()