mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-04 16:13:23 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
154 lines
5.8 KiB
Python
154 lines
5.8 KiB
Python
import datetime
|
|
import logging
|
|
from typing import Optional, Dict, Any
|
|
|
|
from msal import ConfidentialClientApplication
|
|
|
|
from docsgpt.core.settings import settings
|
|
from docsgpt.parser.connectors._auth_utils import session_token_fingerprint
|
|
from docsgpt.parser.connectors.base import BaseConnectorAuth
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SharePointAuth(BaseConnectorAuth):
|
|
"""
|
|
Handles Microsoft OAuth 2.0 authentication for SharePoint/OneDrive.
|
|
|
|
Note: Files.Read scope allows access to files the user has granted access to,
|
|
similar to Google Drive's drive.file scope.
|
|
"""
|
|
|
|
SCOPES = [
|
|
"Files.Read",
|
|
"Sites.Read.All",
|
|
"User.Read",
|
|
]
|
|
|
|
def __init__(self):
|
|
self.client_id = settings.MICROSOFT_CLIENT_ID
|
|
self.client_secret = settings.MICROSOFT_CLIENT_SECRET
|
|
|
|
if not self.client_id:
|
|
raise ValueError(
|
|
"Microsoft OAuth credentials not configured. Please set MICROSOFT_CLIENT_ID in settings."
|
|
)
|
|
|
|
if not self.client_secret:
|
|
raise ValueError(
|
|
"Microsoft OAuth credentials not configured. Please set MICROSOFT_CLIENT_SECRET in settings."
|
|
)
|
|
|
|
self.redirect_uri = settings.CONNECTOR_REDIRECT_BASE_URI
|
|
self.tenant_id = settings.MICROSOFT_TENANT_ID
|
|
self.authority = getattr(settings, "MICROSOFT_AUTHORITY", f"https://login.microsoftonline.com/{self.tenant_id}")
|
|
|
|
self.auth_app = ConfidentialClientApplication(
|
|
client_id=self.client_id,
|
|
client_credential=self.client_secret,
|
|
authority=self.authority
|
|
)
|
|
|
|
def get_authorization_url(self, state: Optional[str] = None) -> str:
|
|
return self.auth_app.get_authorization_request_url(
|
|
scopes=self.SCOPES, state=state, redirect_uri=self.redirect_uri
|
|
)
|
|
|
|
def exchange_code_for_tokens(self, authorization_code: str) -> Dict[str, Any]:
|
|
result = self.auth_app.acquire_token_by_authorization_code(
|
|
code=authorization_code,
|
|
scopes=self.SCOPES,
|
|
redirect_uri=self.redirect_uri
|
|
)
|
|
|
|
if "error" in result:
|
|
logger.error("Token exchange failed: %s", result.get("error_description"))
|
|
raise ValueError(f"Error acquiring token: {result.get('error_description')}")
|
|
|
|
return self.map_token_response(result)
|
|
|
|
def refresh_access_token(self, refresh_token: str) -> Dict[str, Any]:
|
|
result = self.auth_app.acquire_token_by_refresh_token(refresh_token=refresh_token, scopes=self.SCOPES)
|
|
|
|
if "error" in result:
|
|
logger.error("Token refresh failed: %s", result.get("error_description"))
|
|
raise ValueError(f"Error refreshing token: {result.get('error_description')}")
|
|
|
|
return self.map_token_response(result)
|
|
|
|
def get_token_info_from_session(self, session_token: str) -> Dict[str, Any]:
|
|
try:
|
|
from docsgpt.storage.db.repositories.connector_sessions import (
|
|
ConnectorSessionsRepository,
|
|
)
|
|
from docsgpt.storage.db.session import db_readonly
|
|
|
|
with db_readonly() as conn:
|
|
session = ConnectorSessionsRepository(conn).get_by_session_token(
|
|
session_token
|
|
)
|
|
|
|
if not session:
|
|
raise ValueError(
|
|
f"Invalid session token ({session_token_fingerprint(session_token)})"
|
|
)
|
|
|
|
token_info = session.get("token_info")
|
|
if not token_info:
|
|
raise ValueError("Session missing token information")
|
|
|
|
required_fields = ["access_token", "refresh_token"]
|
|
missing_fields = [field for field in required_fields if field not in token_info or not token_info.get(field)]
|
|
if missing_fields:
|
|
raise ValueError(f"Missing required token fields: {missing_fields}")
|
|
|
|
if 'token_uri' not in token_info:
|
|
token_info['token_uri'] = f"https://login.microsoftonline.com/{settings.MICROSOFT_TENANT_ID}/oauth2/v2.0/token"
|
|
|
|
return token_info
|
|
|
|
except Exception as e:
|
|
logger.error("Failed to retrieve token from session: %s", e)
|
|
raise ValueError(f"Failed to retrieve SharePoint token information: {str(e)}")
|
|
|
|
def is_token_expired(self, token_info: Dict[str, Any]) -> bool:
|
|
if not token_info:
|
|
return True
|
|
|
|
expiry_timestamp = token_info.get("expiry")
|
|
|
|
if expiry_timestamp is None:
|
|
return True
|
|
|
|
current_timestamp = int(datetime.datetime.now().timestamp())
|
|
return (expiry_timestamp - current_timestamp) < 60
|
|
|
|
def sanitize_token_info(self, token_info: Dict[str, Any], **extra_fields) -> Dict[str, Any]:
|
|
return super().sanitize_token_info(
|
|
token_info,
|
|
allows_shared_content=token_info.get("allows_shared_content", False),
|
|
**extra_fields,
|
|
)
|
|
|
|
PERSONAL_ACCOUNT_TENANT_ID = "9188040d-6c67-4c5b-b112-36a304b66dad"
|
|
|
|
def _allows_shared_content(self, id_token_claims: Dict[str, Any]) -> bool:
|
|
"""Return True when the account is a work/school tenant that can access SharePoint shared content."""
|
|
tid = id_token_claims.get("tid", "")
|
|
return bool(tid) and tid != self.PERSONAL_ACCOUNT_TENANT_ID
|
|
|
|
def map_token_response(self, result) -> Dict[str, Any]:
|
|
claims = result.get("id_token_claims", {})
|
|
return {
|
|
"access_token": result.get("access_token"),
|
|
"refresh_token": result.get("refresh_token"),
|
|
"token_uri": claims.get("iss"),
|
|
"scopes": result.get("scope"),
|
|
"expiry": claims.get("exp"),
|
|
"allows_shared_content": self._allows_shared_content(claims),
|
|
"user_info": {
|
|
"name": claims.get("name"),
|
|
"email": claims.get("preferred_username"),
|
|
},
|
|
}
|