rohannsinghal commited on
Commit
c4e3eaf
·
1 Parent(s): ae9db2f

added auth

Browse files
Files changed (1) hide show
  1. app/main_api.py +27 -5
app/main_api.py CHANGED
@@ -11,7 +11,7 @@ import asyncio
11
  from collections import defaultdict
12
 
13
  # FastAPI and core dependencies
14
- from fastapi import FastAPI, Body, HTTPException, Request
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from pydantic import BaseModel
17
 
@@ -40,14 +40,36 @@ logger = logging.getLogger(__name__)
40
 
41
  app = FastAPI(title="Optimized Semantic RAG", version="2.1.0")
42
 
 
43
  app.add_middleware(
44
  CORSMiddleware,
45
  allow_origins=["*"],
46
  allow_credentials=True,
47
- allow_methods=["*"],
48
- allow_headers=["*"],
49
  )
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  # --- OPTIMIZED SEMANTIC DOCUMENT PARSER ---
52
 
53
  class DocumentChunk:
@@ -686,9 +708,9 @@ class Answer(BaseModel):
686
  class SubmissionResponse(BaseModel):
687
  answers: List[Answer]
688
 
689
- # --- MAIN ENDPOINT ---
690
 
691
- @app.post("/hackrx/run", response_model=SubmissionResponse)
692
  async def run_submission(request: Request, submission_request: SubmissionRequest = Body(...)):
693
  try:
694
  logger.info(f"🎯 Processing {len(submission_request.documents)} documents, {len(submission_request.questions)} questions")
 
11
  from collections import defaultdict
12
 
13
  # FastAPI and core dependencies
14
+ from fastapi import FastAPI, Body, HTTPException, Request, Depends, Header
15
  from fastapi.middleware.cors import CORSMiddleware
16
  from pydantic import BaseModel
17
 
 
40
 
41
  app = FastAPI(title="Optimized Semantic RAG", version="2.1.0")
42
 
43
+ # Updated CORS middleware for hackathon
44
  app.add_middleware(
45
  CORSMiddleware,
46
  allow_origins=["*"],
47
  allow_credentials=True,
48
+ allow_methods=["GET", "POST"],
49
+ allow_headers=["*", "Authorization", "Content-Type"],
50
  )
51
 
52
+ # --- AUTHENTICATION MIDDLEWARE ---
53
+
54
+ async def verify_bearer_token(authorization: str = Header(None)):
55
+ """Verify Bearer token authentication as required by hackathon"""
56
+ if not authorization:
57
+ raise HTTPException(status_code=401, detail="Authorization header required")
58
+
59
+ if not authorization.startswith("Bearer "):
60
+ raise HTTPException(status_code=401, detail="Invalid authorization format. Use 'Bearer <token>'")
61
+
62
+ token = authorization.replace("Bearer ", "")
63
+
64
+ # Basic token validation
65
+ if len(token) < 10:
66
+ raise HTTPException(status_code=401, detail="Invalid token format")
67
+
68
+ # For hackathon, we accept any properly formatted Bearer token
69
+ # In production, you would validate against a specific token or database
70
+ logger.info(f"✅ Authentication successful with token: {token[:10]}...")
71
+ return token
72
+
73
  # --- OPTIMIZED SEMANTIC DOCUMENT PARSER ---
74
 
75
  class DocumentChunk:
 
708
  class SubmissionResponse(BaseModel):
709
  answers: List[Answer]
710
 
711
+ # --- MAIN ENDPOINT WITH AUTHENTICATION ---
712
 
713
+ @app.post("/hackrx/run", response_model=SubmissionResponse, dependencies=[Depends(verify_bearer_token)])
714
  async def run_submission(request: Request, submission_request: SubmissionRequest = Body(...)):
715
  try:
716
  logger.info(f"🎯 Processing {len(submission_request.documents)} documents, {len(submission_request.questions)} questions")