π OpenHands: AI-Driven Development
Add signed cookie-based GitHub authentication caching (#4853)
Robert Brennane1383afbc3a3
diff --git a/frontend/src/services/api.ts b/frontend/src/services/api.tsindex ab6006b6ab..33ecc0e637 100644--- a/frontend/src/services/api.ts+++ b/frontend/src/services/api.ts@@ -63,6 +63,16 @@ export async function request(} catch (e) {onFail(`Error fetching ${url}`);}+ if (response?.status === 401) {+ await request(+ "/api/authenticate",+ {+ method: "POST",+ },+ true,+ );+ return request(url, options, disableToast, returnResponse, maxRetries - 1);+ }if (response?.status && response?.status >= 400) {onFail(`${response.status} error while fetching ${url}: ${response?.statusText}`,diff --git a/openhands/server/listen.py b/openhands/server/listen.pyindex c679be2ded..3e6be135db 100644--- a/openhands/server/listen.py+++ b/openhands/server/listen.py@@ -2,10 +2,12 @@ import asyncioimport osimport reimport tempfile+import timeimport uuidimport warningsfrom contextlib import asynccontextmanager+import jwtimport requestsfrom pathspec import PathSpecfrom pathspec.patterns import GitWildMatchPattern@@ -15,6 +17,7 @@ from openhands.server.data_models.feedback import FeedbackDataModel, store_feedbfrom openhands.server.github import (GITHUB_CLIENT_ID,GITHUB_CLIENT_SECRET,+ UserVerifier,authenticate_github_user,)from openhands.storage import get_file_store@@ -60,7 +63,7 @@ from openhands.events.serialization import event_to_dictfrom openhands.events.stream import AsyncEventStreamWrapperfrom openhands.llm import bedrockfrom openhands.runtime.base import Runtime-from openhands.server.auth import get_sid_from_token, sign_token+from openhands.server.auth.auth import get_sid_from_token, sign_tokenfrom openhands.server.middleware import LocalhostCORSMiddleware, NoCacheMiddlewarefrom openhands.server.session import SessionManager@@ -204,12 +207,22 @@ async def attach_session(request: Request, call_next):response = await call_next(request)return response- github_token = request.headers.get('X-GitHub-Token')- if not await authenticate_github_user(github_token):- return JSONResponse(- status_code=status.HTTP_401_UNAUTHORIZED,- content={'error': 'Not authenticated'},- )+ user_verifier = UserVerifier()+ if user_verifier.is_active():+ signed_token = request.cookies.get('github_auth')+ if not signed_token:+ return JSONResponse(+ status_code=status.HTTP_401_UNAUTHORIZED,+ content={'error': 'Not authenticated'},+ )+ try:+ jwt.decode(signed_token, config.jwt_secret, algorithms=['HS256'])+ except Exception as e:+ logger.warning(f'Invalid token: {e}')+ return JSONResponse(+ status_code=status.HTTP_401_UNAUTHORIZED,+ content={'error': 'Invalid token'},+ )if not request.headers.get('Authorization'):logger.warning('Missing Authorization header')@@ -864,10 +877,26 @@ async def authenticate(request: Request):content={'error': 'Not authorized via GitHub waitlist'},)+ # Create a signed JWT token with 1-hour expiration+ cookie_data = {+ 'github_token': token,+ 'exp': int(time.time()) + 3600, # 1 hour expiration+ }+ signed_token = sign_token(cookie_data, config.jwt_secret)+response = JSONResponse(status_code=status.HTTP_200_OK, content={'message': 'User authenticated'})+ # Set secure cookie with signed token+ response.set_cookie(+ key='github_auth',+ value=signed_token,+ max_age=3600, # 1 hour in seconds+ httponly=True,+ secure=True,+ samesite='strict',+ )return response