OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
Add IBM Watsonx Data (AMS) auth support
Edwin Josef4e0cf8b8a3a
diff --git a/.env.example b/.env.exampleindex 2cb3df3fc..4ad67a9f9 100644--- a/.env.example+++ b/.env.example@@ -94,6 +94,14 @@ OPENSEARCH_USERNAME=admin# Change this if you want to use a different index name or avoid conflictsOPENSEARCH_INDEX_NAME=documents+# IBM AMS Authentication (IBM Watsonx Data embedded mode)+# Set IBM_AUTH_ENABLED=true to authenticate via the ibm-lh-console-session cookie+# instead of Google OAuth. The raw IBM JWT is also passed directly to OpenSearch.+# When enabled, GOOGLE_OAUTH_CLIENT_ID/SECRET are not required for login.+IBM_AUTH_ENABLED=false+# URL to fetch IBM's JWT public key (used to validate ibm-lh-console-session tokens).+IBM_JWT_PUBLIC_KEY_URL=+# make here https://console.cloud.google.com/apis/credentialsGOOGLE_OAUTH_CLIENT_ID=GOOGLE_OAUTH_CLIENT_SECRET=diff --git a/frontend/app/login/page.tsx b/frontend/app/login/page.tsxindex b05e2b0e5..5b577e901 100644--- a/frontend/app/login/page.tsx+++ b/frontend/app/login/page.tsx@@ -9,18 +9,18 @@ import { Button } from "@/components/ui/button";import { useAuth } from "@/contexts/auth-context";function LoginPageContent() {- const { isLoading, isAuthenticated, isNoAuthMode, login } = useAuth();+ const { isLoading, isAuthenticated, isNoAuthMode, isIbmAuthMode, login } = useAuth();const router = useRouter();const searchParams = useSearchParams();const redirect = searchParams.get("redirect") || "/chat";- // Redirect if already authenticated or in no-auth mode+ // Redirect if already authenticated, in no-auth mode, or IBM auth modeuseEffect(() => {- if (!isLoading && (isAuthenticated || isNoAuthMode)) {+ if (!isLoading && (isAuthenticated || isNoAuthMode || isIbmAuthMode)) {router.push(redirect);}- }, [isLoading, isAuthenticated, isNoAuthMode, router, redirect]);+ }, [isLoading, isAuthenticated, isNoAuthMode, isIbmAuthMode, router, redirect]);if (isLoading) {return (@@ -33,7 +33,7 @@ function LoginPageContent() {);}- if (isAuthenticated || isNoAuthMode) {+ if (isAuthenticated || isNoAuthMode || isIbmAuthMode) {return null; // Will redirect in useEffect}diff --git a/frontend/contexts/auth-context.tsx b/frontend/contexts/auth-context.tsxindex 7eb0182b2..3b36667ad 100644--- a/frontend/contexts/auth-context.tsx+++ b/frontend/contexts/auth-context.tsx@@ -62,17 +62,22 @@ export function AuthProvider({ children }: AuthProviderProps) {const data = await response.json();- setIsIbmAuthMode(!!data.ibm_auth_mode);-- // Check if we're in no-auth mode- if (data.no_auth_mode) {+ // Check auth mode flags+ if (data.ibm_auth_mode) {+ setIsIbmAuthMode(true);+ setIsNoAuthMode(false);+ setUser(data.authenticated && data.user ? data.user : null);+ } else if (data.no_auth_mode) {setIsNoAuthMode(true);+ setIsIbmAuthMode(false);setUser(null);} else if (data.authenticated && data.user) {setIsNoAuthMode(false);+ setIsIbmAuthMode(false);setUser(data.user);} else {setIsNoAuthMode(false);+ setIsIbmAuthMode(false);setUser(null);}@@ -86,11 +91,15 @@ export function AuthProvider({ children }: AuthProviderProps) {}, []);const login = () => {- // Don't allow login in no-auth mode+ // Don't allow login in no-auth mode or IBM auth modeif (isNoAuthMode) {console.log("Login attempted in no-auth mode - ignored");return;}+ if (isIbmAuthMode) {+ console.log("Login attempted in IBM auth mode - ignored (auth managed by IBM Watsonx Data)");+ return;+ }// Use the correct auth callback URL, not connectors callbackconst redirectUri = `${window.location.origin}/auth/callback`;@@ -151,9 +160,9 @@ export function AuthProvider({ children }: AuthProviderProps) {};const logout = async () => {- // Don't allow logout in no-auth mode- if (isNoAuthMode) {- console.log("Logout attempted in no-auth mode - ignored");+ // Don't allow logout in no-auth mode or IBM auth mode+ if (isNoAuthMode || isIbmAuthMode) {+ console.log("Logout attempted in no-auth/IBM auth mode - ignored");return;}@@ -178,7 +187,7 @@ export function AuthProvider({ children }: AuthProviderProps) {const value: AuthContextType = {user,isLoading,- isAuthenticated: !!user,+ isAuthenticated: !!user || isIbmAuthMode,isNoAuthMode,isIbmAuthMode,login,diff --git a/src/api/auth.py b/src/api/auth.pyindex 7c5e97a75..be8612054 100644--- a/src/api/auth.py+++ b/src/api/auth.py@@ -109,7 +109,16 @@ async def auth_logout(user: User = Depends(get_current_user),):"""Logout user by clearing auth cookie"""+ from config.settings import IBM_AUTH_ENABLED+await TelemetryClient.send_event(Category.AUTHENTICATION, MessageId.ORB_AUTH_LOGOUT)++ if IBM_AUTH_ENABLED:+ # IBM manages its own session cookie; there is no local auth_token to clear.+ return JSONResponse(+ {"status": "no_local_session", "message": "Session is managed by IBM Watsonx Data"}+ )+response = JSONResponse({"status": "logged_out", "message": "Successfully logged out"})diff --git a/src/auth/__init__.py b/src/auth/__init__.pynew file mode 100644index 000000000..e69de29bbdiff --git a/src/auth/ibm_auth.py b/src/auth/ibm_auth.pynew file mode 100644index 000000000..65d8c06c2--- /dev/null+++ b/src/auth/ibm_auth.py@@ -0,0 +1,58 @@+"""IBM AMS JWT validation helper.++Validates JWTs from the `ibm-lh-console-session` cookie issued by IBM+Watsonx Data (lakehouse). The public key is fetched from IBM_JWT_PUBLIC_KEY_URL+at startup and cached in-process. On validation failure the key is re-fetched+once to handle key rotation.+"""+import httpx+import jwt+from cryptography.hazmat.primitives.serialization import load_pem_public_key++from utils.logging_config import get_logger++logger = get_logger(__name__)++# Module-level cache; populated by fetch_ibm_public_key() at startup.+_cached_public_key = None+++async def fetch_ibm_public_key(url: str):+ """Fetch IBM's JWT public key PEM from *url* and cache it.++ Returns the loaded public key object.+ """+ global _cached_public_key+ logger.info("Fetching IBM JWT public key", url=url)+ async with httpx.AsyncClient(timeout=10.0) as client:+ resp = await client.get(url)+ resp.raise_for_status()+ _cached_public_key = load_pem_public_key(resp.content)+ logger.info("IBM JWT public key cached successfully")+ return _cached_public_key+++def validate_ibm_jwt(token: str, public_key) -> dict | None:+ """Validate *token* with *public_key*.++ Returns the decoded claims dict on success, or ``None`` on any failure+ (expired, bad signature, missing claims, etc.).+ """+ if public_key is None:+ logger.warning("IBM JWT validation skipped — no public key loaded")+ return None+ try:+ return jwt.decode(+ token,+ public_key,+ algorithms=["RS256"],+ audience="AMS-UI",+ issuer="IBMLH",+ options={"verify_exp": True},+ )+ except jwt.ExpiredSignatureError:+ logger.warning("IBM JWT has expired")+ return None+ except jwt.InvalidTokenError as exc:+ logger.warning("IBM JWT validation failed", error=str(exc))+ return Nonediff --git a/src/config/settings.py b/src/config/settings.pyindex 3924382bd..09f7e9210 100644--- a/src/config/settings.py+++ b/src/config/settings.py@@ -48,6 +48,10 @@ LANGFLOW_KEY = os.getenv("LANGFLOW_KEY")SESSION_SECRET = os.getenv("SESSION_SECRET", "your-secret-key-change-in-production")GOOGLE_OAUTH_CLIENT_ID = os.getenv("GOOGLE_OAUTH_CLIENT_ID")GOOGLE_OAUTH_CLIENT_SECRET = os.getenv("GOOGLE_OAUTH_CLIENT_SECRET")++# IBM AMS authentication (Watsonx Data embedded mode)+IBM_AUTH_ENABLED = os.getenv("IBM_AUTH_ENABLED", "false").lower() in ("true", "1", "yes")+IBM_JWT_PUBLIC_KEY_URL = os.getenv("IBM_JWT_PUBLIC_KEY_URL", "")DOCLING_OCR_ENGINE = os.getenv("DOCLING_OCR_ENGINE")IBM_AUTH_ENABLED = os.getenv("IBM_AUTH_ENABLED", "false").lower() in ("true", "1", "yes")@@ -93,6 +97,8 @@ INGESTION_TIMEOUT = get_env_int("INGESTION_TIMEOUT", 3600)def is_no_auth_mode():"""Check if we're running in no-auth mode (OAuth credentials missing)"""+ if IBM_AUTH_ENABLED:+ return False # IBM cookie auth is a valid auth moderesult = not (GOOGLE_OAUTH_CLIENT_ID and GOOGLE_OAUTH_CLIENT_SECRET)return resultdiff --git a/src/dependencies.py b/src/dependencies.pyindex 37bcdbf30..e35c34e3f 100644--- a/src/dependencies.py+++ b/src/dependencies.py@@ -85,6 +85,60 @@ def get_flows_service(services: dict = Depends(get_services)):return services["flows_service"]+# ─────────────────────────────────────────────+# IBM AMS authentication helper+# ─────────────────────────────────────────────++def _get_ibm_user(request: Request, required: bool) -> Optional["User"]:+ """Extract and validate the IBM AMS JWT from ibm-lh-console-session cookie.++ If *required* is True, raises HTTP 401 on missing/invalid token.+ If *required* is False, returns None instead of raising.+ """+ import auth.ibm_auth as ibm_auth+ from config.settings import IBM_JWT_PUBLIC_KEY_URL++ ibm_token = request.cookies.get("ibm-lh-console-session")+ if not ibm_token:+ if required:+ raise HTTPException(status_code=401, detail="IBM session cookie missing")+ request.state.user = None+ return None++ claims = ibm_auth.validate_ibm_jwt(ibm_token, ibm_auth._cached_public_key)++ # On validation failure, try re-fetching the public key once (key rotation)+ if claims is None and IBM_JWT_PUBLIC_KEY_URL:+ try:+ import httpx+ from cryptography.hazmat.primitives.serialization import load_pem_public_key+ with httpx.Client(timeout=10.0) as client:+ resp = client.get(IBM_JWT_PUBLIC_KEY_URL)+ resp.raise_for_status()+ ibm_auth._cached_public_key = load_pem_public_key(resp.content)+ logger.info("IBM public key refreshed (key rotation detected)")+ claims = ibm_auth.validate_ibm_jwt(ibm_token, ibm_auth._cached_public_key)+ except Exception as exc:+ logger.warning("Failed to refresh IBM public key", error=str(exc))++ if claims is None:+ if required:+ raise HTTPException(status_code=401, detail="Invalid or expired IBM session")+ request.state.user = None+ return None++ user = User(+ user_id=claims.get("uid") or claims["sub"],+ email=claims.get("username", claims["sub"]),+ name=claims.get("display_name", claims.get("username", claims["sub"])),+ picture=None,+ provider="ibm_ams",+ jwt_token=ibm_token, # raw IBM JWT passed directly to OpenSearch+ )+ request.state.user = user+ return user++# ─────────────────────────────────────────────# Authentication dependencies# ─────────────────────────────────────────────@@ -99,9 +153,13 @@ def get_current_user(Sets request.state.user.Raises HTTP 401 if the user is not authenticated."""- from config.settings import is_no_auth_mode+ from config.settings import IBM_AUTH_ENABLED, is_no_auth_modefrom session_manager import AnonymousUser+ # IBM AMS cookie auth takes priority when enabled+ if IBM_AUTH_ENABLED:+ return _get_ibm_user(request, required=True)+if is_no_auth_mode():user = AnonymousUser()request.state.user = user@@ -135,9 +193,13 @@ def get_optional_user(Sets request.state.user (may be None).Never raises — returns None if unauthenticated."""- from config.settings import is_no_auth_mode+ from config.settings import IBM_AUTH_ENABLED, is_no_auth_modefrom session_manager import AnonymousUser+ # IBM AMS cookie auth takes priority when enabled+ if IBM_AUTH_ENABLED:+ return _get_ibm_user(request, required=False)+if is_no_auth_mode():user = AnonymousUser()request.state.user = userdiff --git a/src/main.py b/src/main.pyindex 2586d849d..c537c741a 100644--- a/src/main.py+++ b/src/main.py@@ -1275,6 +1275,16 @@ async def initialize_services():# Generate JWT keys if they don't existgenerate_jwt_keys()+ # Pre-fetch IBM JWT public key when IBM auth is enabled+ from config.settings import IBM_AUTH_ENABLED, IBM_JWT_PUBLIC_KEY_URL+ if IBM_AUTH_ENABLED:+ if not IBM_JWT_PUBLIC_KEY_URL:+ logger.error("IBM_AUTH_ENABLED=true but IBM_JWT_PUBLIC_KEY_URL is not set")+ raise RuntimeError("IBM_JWT_PUBLIC_KEY_URL must be set when IBM_AUTH_ENABLED=true")+ from auth.ibm_auth import fetch_ibm_public_key+ await fetch_ibm_public_key(IBM_JWT_PUBLIC_KEY_URL)+ logger.info("IBM auth mode enabled", public_key_url=IBM_JWT_PUBLIC_KEY_URL)+# Initialize clients (now async to generate Langflow API key)try:await clients.initialize()diff --git a/src/services/auth_service.py b/src/services/auth_service.pyindex ae34b0066..72361964f 100644--- a/src/services/auth_service.py+++ b/src/services/auth_service.py@@ -41,6 +41,14 @@ class AuthService:user_id: str = None,) -> dict:"""Initialize OAuth flow for authentication or data source connection"""+ from config.settings import IBM_AUTH_ENABLED++ # IBM auth mode — Google OAuth login is not used+ if IBM_AUTH_ENABLED and purpose == "app_auth":+ raise ValueError(+ "IBM AMS authentication is active. Login is handled via the IBM Watsonx Data session cookie."+ )+# Check if we're in no-auth modeif is_no_auth_mode():if purpose == "app_auth":@@ -495,6 +503,24 @@ class AuthService:"""Get current user information from request"""from config.settings import IBM_AUTH_ENABLED+ # IBM auth mode: user is set by get_optional_user from IBM cookie+ if IBM_AUTH_ENABLED:+ user = getattr(request.state, "user", None)+ if user and user.provider == "ibm_ams":+ return {+ "authenticated": True,+ "ibm_auth_mode": True,+ "user": {+ "user_id": user.user_id,+ "email": user.email,+ "name": user.name,+ "picture": user.picture,+ "provider": user.provider,+ "last_login": user.last_login.isoformat() if user.last_login else None,+ },+ }+ return {"authenticated": False, "ibm_auth_mode": True, "user": None}+# In no-auth mode, return a consistent responseif is_no_auth_mode():return {"authenticated": False, "user": None, "no_auth_mode": True}diff --git a/src/session_manager.py b/src/session_manager.pyindex 2de2d9653..aea73e0d0 100644--- a/src/session_manager.py+++ b/src/session_manager.py@@ -262,7 +262,11 @@ class SessionManager:def get_effective_jwt_token(self, user_id: str, jwt_token: str) -> str:"""Get the effective JWT token, creating anonymous JWT if needed in no-auth mode"""- from config.settings import is_no_auth_mode+ from config.settings import IBM_AUTH_ENABLED, is_no_auth_mode++ # IBM JWT is used as-is — never override with an anonymous OpenRAG JWT+ if IBM_AUTH_ENABLED and jwt_token:+ return jwt_tokenif jwt_token is not None:return jwt_token