From 364f77207869281053f33759875d68f1acd30a15 Mon Sep 17 00:00:00 2001 From: killian <63927363+KillianLucas@users.noreply.github.com> Date: Tue, 23 Jul 2024 21:16:10 -0700 Subject: [PATCH 1/2] Add authentication to server --- interpreter/core/async_core.py | 133 +++++++++++++++++++++++++-------- 1 file changed, 102 insertions(+), 31 deletions(-) diff --git a/interpreter/core/async_core.py b/interpreter/core/async_core.py index 8c6096598c..df56f3725b 100644 --- a/interpreter/core/async_core.py +++ b/interpreter/core/async_core.py @@ -17,8 +17,18 @@ try: import janus import uvicorn - from fastapi import APIRouter, FastAPI, File, Form, UploadFile, WebSocket - from fastapi.responses import PlainTextResponse, StreamingResponse + from fastapi import ( + APIRouter, + FastAPI, + File, + Form, + HTTPException, + Request, + UploadFile, + WebSocket, + ) + from fastapi.responses import JSONResponse, PlainTextResponse, StreamingResponse + from starlette.status import HTTP_403_FORBIDDEN except: # Server dependencies are not required by the main package. pass @@ -204,6 +214,24 @@ def accumulate(self, chunk): self.messages[-1]["content"] += chunk +def authenticate_function(key): + """ + This function checks if the provided key is valid for authentication. + + Returns True if the key is valid, False otherwise. + """ + # Fetch the API key from the environment variables. If it's not set, return True. + api_key = os.getenv("INTERPRETER_API_KEY", None) + + # If the API key is not set in the environment variables, return True. + # Otherwise, check if the provided key matches the fetched API key. + # Return True if they match, False otherwise. + if api_key is None: + return True + else: + return key == api_key + + def create_router(async_interpreter): router = APIRouter() @@ -226,6 +254,7 @@ async def home(): +