1010import redis .asyncio as aioredis
1111from dotenv import load_dotenv
1212from fastapi import Depends , FastAPI , File , Form , HTTPException , Response , UploadFile , status
13- from fastapi .security import APIKeyHeader
13+ from fastapi .security import APIKeyHeader , HTTPBearer
1414from sqlalchemy import text
1515
1616from mxgo import user , validators
@@ -118,36 +118,9 @@ async def lifespan(_app: FastAPI):
118118 app .openapi_url = None
119119
120120api_auth_scheme = APIKeyHeader (name = "x-api-key" , auto_error = True )
121- suggestions_api_auth_scheme = APIKeyHeader ( name = "x-suggestions-api-key" , auto_error = False )
121+ bearer_auth_scheme = HTTPBearer ( )
122122
123123
124- async def validate_suggestions_api_key (api_key : str ) -> Response | None :
125- """
126- Validate the suggestions API key.
127-
128- Args:
129- api_key: The suggestions API key to validate
130-
131- Returns:
132- Response if validation fails, None if validation succeeds
133-
134- """
135- suggestions_api_key = os .getenv ("SUGGESTIONS_API_KEY" )
136- if not suggestions_api_key :
137- logger .error ("SUGGESTIONS_API_KEY environment variable not set" )
138- return Response (
139- content = json .dumps ({"message" : "Server configuration error" , "status" : "error" }),
140- status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
141- media_type = "application/json" ,
142- )
143-
144- if api_key != suggestions_api_key :
145- return Response (
146- content = json .dumps ({"message" : "Invalid suggestions API key" , "status" : "error" }),
147- status_code = status .HTTP_401_UNAUTHORIZED ,
148- media_type = "application/json" ,
149- )
150- return None
151124
152125
153126@app .get ("/health" )
@@ -827,15 +800,14 @@ async def process_email( # noqa: PLR0912, PLR0915
827800@app .post ("/suggestions" )
828801async def process_suggestions (
829802 requests : list [EmailSuggestionRequest ],
830- api_key : Annotated [str | None , Depends (suggestions_api_auth_scheme )] = None ,
831803 current_user : Annotated [AuthInfo , Depends (get_current_user )] = ...,
804+ _token : str = Depends (bearer_auth_scheme ),
832805) -> list [EmailSuggestionResponse ]:
833806 """
834807 Process a batch of email suggestion requests.
835808
836809 Args:
837810 requests: A list of email suggestion requests.
838- api_key: The API key for authentication.
839811 current_user: The authenticated user from JWT token.
840812
841813 Returns:
@@ -845,17 +817,6 @@ async def process_suggestions(
845817 # JWT Authentication is handled by dependency injection
846818 logger .info (f"JWT authentication successful for user { current_user .email } " )
847819
848- # Check if API key is provided
849- if api_key is None :
850- raise HTTPException (
851- status_code = status .HTTP_422_UNPROCESSABLE_ENTITY ,
852- detail = "Missing required header: x-suggestions-api-key" ,
853- )
854-
855- # Validate suggestions API key
856- if validation_response := await validate_suggestions_api_key (api_key ):
857- return validation_response
858-
859820 # Get the suggestions model once for all requests
860821 suggestions_model = get_suggestions_model ()
861822
@@ -902,7 +863,10 @@ async def process_suggestions(
902863
903864
904865@app .get ("/user" )
905- async def get_user_info (current_user : Annotated [AuthInfo , Depends (get_current_user )]) -> UserInfoResponse :
866+ async def get_user_info (
867+ current_user : Annotated [AuthInfo , Depends (get_current_user )] = ...,
868+ _token : str = Depends (bearer_auth_scheme ),
869+ ) -> UserInfoResponse :
906870 """
907871 Get user information including subscription, plan, and usage details.
908872
0 commit comments