Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
23.2 KB
Raw Blame
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 SharePointOAuth:
    """Handles Microsoft Graph OAuth authentication flow following Google Drive pattern."""

    # Reserved scopes that must NOT be sent on token or silent calls
    RESERVED_SCOPES = {"openid", "profile", "offline_access"}

    # For SharePoint (work/school accounts):
    # - 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 site access and AllSites.Read for SharePoint API.
    # 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",
        "Sites.Read.All",  # Read SharePoint sites (for File Picker)
        "offline_access",
    ]
    RESOURCE_SCOPES = [
        "User.Read",
        "Files.Read",
        "Files.Read.All",
        "Files.Read.Selected",
        "Sites.Read.All",
    ]
    SCOPES = AUTH_SCOPES  # Backward compatibility alias

    # 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 = "sharepoint_token.json",
        authority: str = "https://login.microsoftonline.com/common",
        allow_json_refresh: bool = True,
    ):
        """
        Initialize SharePointOAuth.

        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"SharePoint 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:
                    self._current_account = accounts[0]
                    username = (
                        self._current_account.get("username", "no username")
                        if self._current_account
                        else "no username"
                    )
                    logger.debug(f"Set current account: {username}")

                    if needs_upgrade:
                        await self.save_cache()

                    # IMPORTANT: 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] SharePoint 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).

        Notes:
            - Prefer using an MSAL cache file and acquire_token_silent().
            - This path is only for migrating older refresh_token JSON 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:
                    self._current_account = accounts[0]
                    username = (
                        self._current_account.get("username", "no username")
                        if self._current_account
                        else "no username"
                    )
                    logger.debug(f"Set current account after refresh: {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] SharePoint 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] = {
            # IMPORTANT: 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:
            # For code exchange, we pass the same auth scopes as used in the authorize step
            result = self.app.acquire_token_by_authorization_code(
                authorization_code,
                scopes=self.AUTH_SCOPES,
                redirect_uri=redirect_uri,
            )

            if result and "access_token" in result:
                # Store the account for future use
                accounts = self.app.get_accounts()
                if accounts:
                    self._current_account = accounts[0]

                await self.save_cache()
                logger.info("SharePoint OAuth authorization successful")
                return True

            error_msg = (
                (result or {}).get("error_description")
                or (result or {}).get("error")
                or "Unknown error"
            )
            logger.error(f"SharePoint OAuth authorization failed: {error_msg}")
            return False

        except Exception as e:
            logger.error(f"Exception during SharePoint OAuth authorization: {e}")
            return False

    async def is_authenticated(self) -> bool:
        """Check if we have valid credentials (simplified like Google Drive)."""
        logger.info(f"SharePoint 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("SharePoint is_authenticated: No current account, loading credentials")
                load_result = await self.load_credentials()
                logger.info(f"SharePoint is_authenticated: load_credentials returned {load_result}")

            logger.info(
                f"SharePoint is_authenticated: current_account={self._current_account is not None}"
            )

            # If we have an account, try to get a token (MSAL will refresh if needed)
            if self._current_account:
                logger.info(
                    f"SharePoint is_authenticated: Trying acquire_token_silent with account {self._current_account.get('username', 'unknown')}"
                )
                logger.info(f"SharePoint is_authenticated: RESOURCE_SCOPES={self.RESOURCE_SCOPES}")
                # IMPORTANT: use RESOURCE_SCOPES here
                result = self.app.acquire_token_silent(
                    self.RESOURCE_SCOPES, account=self._current_account
                )
                if result and "access_token" in result:
                    logger.info(
                        "SharePoint 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"SharePoint is_authenticated: Token acquisition failed for current account: {error_msg}"
                    )
                    logger.warning(f"SharePoint is_authenticated: Full result: {result}")

            # Fallback: try without specific account
            logger.info(
                "SharePoint 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:
                # Update current account if this worked
                accounts = self.app.get_accounts()
                logger.info(
                    f"SharePoint 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"SharePoint is_authenticated: Fallback also failed, result: {result}")
            return False

        except Exception:
            logger.exception("[CONNECTOR] SharePoint is_authenticated failed")
            return False

    def get_access_token(self) -> str:
        """Get an access token for Microsoft Graph (simplified like Google Drive)."""
        logger.info(
            f"SharePoint 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"SharePoint 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("SharePoint get_access_token: Success with current account")
                    return access_token
                else:
                    logger.warning(
                        f"SharePoint get_access_token: Failed with account, result: {result}"
                    )

            # Fallback: try without specific account
            logger.info("SharePoint 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("SharePoint 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"SharePoint get_access_token: All attempts failed, error: {error_msg}")
            raise ValueError(f"Failed to acquire access token: {error_msg}")

        except Exception:
            logger.exception("[CONNECTOR] SharePoint get_access_token failed")
            raise

    def get_access_token_for_resource(self, resource_url: str) -> str:
        """
        Get an access token for a specific SharePoint resource.

        The SharePoint File Picker v8 requires a token with the SharePoint URL as the audience,
        not Microsoft Graph. This method acquires a token with SharePoint-specific scopes.

        Args:
            resource_url: The SharePoint site URL (e.g., https://contoso.sharepoint.com)

        Returns:
            Access token with the SharePoint resource as the audience
        """
        logger.info(f"SharePoint get_access_token_for_resource: resource_url={resource_url}")

        # Use /.default to request whatever permissions are configured for this resource
        # in the Azure AD app registration
        sharepoint_scopes = [f"{resource_url.rstrip('/')}/.default"]
        logger.info(f"SharePoint get_access_token_for_resource: scopes={sharepoint_scopes}")

        try:
            # Try with current account first
            if self._current_account:
                logger.info(
                    f"SharePoint get_access_token_for_resource: Trying with account {self._current_account.get('username', 'unknown')}"
                )
                result = self.app.acquire_token_silent(
                    sharepoint_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(
                        "SharePoint get_access_token_for_resource: Success with current account"
                    )
                    return access_token
                else:
                    error_msg = (
                        (result or {}).get("error_description")
                        or (result or {}).get("error")
                        or "Unknown error"
                    )
                    logger.warning(
                        f"SharePoint get_access_token_for_resource: Failed with account: {error_msg}"
                    )

            # Fallback: try without specific account
            logger.info(
                "SharePoint get_access_token_for_resource: Fallback - trying without account"
            )
            result = self.app.acquire_token_silent(sharepoint_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("SharePoint get_access_token_for_resource: Fallback success")
                return access_token

            # If silent acquisition fails, we may need to acquire interactively or via refresh
            # Try using the refresh token if available
            error_msg = (
                (result or {}).get("error_description")
                or (result or {}).get("error")
                or "No valid authentication"
            )
            logger.error(
                f"SharePoint get_access_token_for_resource: All attempts failed: {error_msg}"
            )

            # Provide helpful error message
            if "AADSTS65001" in str(error_msg) or "consent" in str(error_msg).lower():
                raise ValueError(
                    f"Admin consent required for SharePoint API access. "
                    f"Please grant consent in Azure AD for the SharePoint resource: {resource_url}"
                )

            raise ValueError(f"Failed to acquire SharePoint token: {error_msg}")

        except ValueError:
            raise
        except Exception as e:
            logger.exception("[CONNECTOR] SharePoint get_access_token_for_resource failed")
            raise ValueError(f"Failed to acquire SharePoint token: {str(e)}") from e

    async def revoke_credentials(self):
        """Clear token cache and remove token file (like Google Drive)."""
        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 SharePoint token file: {self.token_file}")

        except Exception as e:
            logger.error(f"Failed to revoke SharePoint credentials: {e}")

    def get_service(self) -> str:
        """Return an access token (Graph doesn't need a generated client like Google Drive)."""
        return self.get_access_token()