Compare commits

...

2 Commits

Author SHA1 Message Date
Robert Brennan
8a0620b21e Update openhands/server/listen.py 2024-11-08 15:25:24 -05:00
openhands
3356753f79 Add cookie-based GitHub authentication caching
- Add cookie in /authenticate endpoint with 1-hour expiration
- Check for cookie in attach_session middleware before calling GitHub API
- Support cookie auth in WebSocket endpoint
- Maintain backward compatibility with X-GitHub-Token header
2024-11-08 20:23:07 +00:00

View File

@@ -204,12 +204,24 @@ 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'},
)
# First check for auth cookie
github_token = request.cookies.get('github_auth')
# If no cookie, fall back to header
if not github_token:
github_token = request.headers.get('X-GitHub-Token')
# If no header token either, return error
if not github_token:
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={'error': 'Not authenticated'},
)
# If using header token, verify with GitHub
if not await authenticate_github_user(github_token):
return JSONResponse(
status_code=status.HTTP_401_UNAUTHORIZED,
content={'error': 'Not authenticated'},
)
if not request.headers.get('Authorization'):
logger.warning('Missing Authorization header')
@@ -865,9 +877,17 @@ async def authenticate(request: Request):
)
response = JSONResponse(
status_code=status.HTTP_200_OK, content={'message': 'User authenticated'}
status_code=status.HTTP_200_OK, content={'message': 'User authenticated'})
# Set secure cookie that expires in 1 hour
response.set_cookie(
key="github_auth",
value=token,
max_age=3600, # 1 hour in seconds
httponly=True,
secure=True,
samesite="strict"
)
return response