Skip to content

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

Python

refactor: Simplify Google Drive OAuth scopes management and add test for missing optional group scopes (#1744)

Rico Furtadocff41704b6a6
diff --git a/src/connectors/google_drive/oauth.py b/src/connectors/google_drive/oauth.py
index 672feba04..1d1e88bb2 100644
--- a/src/connectors/google_drive/oauth.py
+++ b/src/connectors/google_drive/oauth.py
@@ -18,12 +18,16 @@ _REFRESH_TIMEOUT_SECONDS = 30
class GoogleDriveOAuth:
"""Handles Google Drive OAuth authentication flow"""
+ REQUIRED_SCOPES = [
+ "https://www.googleapis.com/auth/drive.readonly",
+ "https://www.googleapis.com/auth/drive.metadata.readonly",
+ ]
+
SCOPES = [
"openid",
"email",
"profile",
- "https://www.googleapis.com/auth/drive.readonly",
- "https://www.googleapis.com/auth/drive.metadata.readonly",
+ *REQUIRED_SCOPES,
"https://www.googleapis.com/auth/cloud-identity.groups.readonly",
"https://www.googleapis.com/auth/admin.directory.group.readonly",
]
@@ -50,7 +54,7 @@ class GoogleDriveOAuth:
def _missing_required_scopes(self, scopes: list[str] | None) -> list[str]:
current_scopes = set(scopes or [])
- return [scope for scope in self.SCOPES if scope not in current_scopes]
+ return [scope for scope in self.REQUIRED_SCOPES if scope not in current_scopes]
def _remove_token_file(self) -> None:
if os.path.exists(self.token_file):
diff --git a/tests/unit/test_oauth_encryption.py b/tests/unit/test_oauth_encryption.py
index ba44fe48d..1dd1c62d8 100644
--- a/tests/unit/test_oauth_encryption.py
+++ b/tests/unit/test_oauth_encryption.py
@@ -66,6 +66,33 @@ async def test_google_drive_auth_missing_scopes_forces_reauth(tmp_path, monkeypa
assert not g_token_path.exists()
+async def test_google_drive_auth_allows_missing_optional_group_scopes(tmp_path, monkeypatch):
+ import utils.encryption
+
+ utils.encryption._cached_master_secret = None
+ monkeypatch.setenv(
+ "OPENRAG_ENCRYPTION_KEY",
+ "dGVzdC1rZXktMzItYnl0ZXMtZm9yLXVuaXQtdGVzdGluZy1vbmx5",
+ )
+
+ g_token_path = tmp_path / "fake_gdrive_token.json"
+ g_plain = {
+ "token": "google-token",
+ "refresh_token": "refresh",
+ "scopes": GoogleDriveOAuth.REQUIRED_SCOPES,
+ "expiry": "2028-03-02T15:46:11.368083",
+ }
+ with open(g_token_path, "w") as f:
+ json.dump(g_plain, f)
+
+ g_oauth = GoogleDriveOAuth(client_id="abc", client_secret="def", token_file=str(g_token_path))
+
+ assert await g_oauth.load_credentials() is not None
+ assert g_oauth.creds is not None
+ assert g_token_path.exists()
+
+
@pytest.mark.asyncio
async def test_msal_auth_upgrade(tmp_path, monkeypatch):
"""Test that MSAL credentials are encrypted on load if not already."""