Spaces:
Running
on
T4
Running
on
T4
fix authen header
Browse files- custom_auth.py +19 -7
- main.py +11 -0
custom_auth.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import Depends, HTTPException, status, Header, Query
|
2 |
from typing import Optional
|
3 |
from database import get_users
|
4 |
from models import User, UserInDB
|
@@ -6,7 +6,8 @@ from token_store import token_store
|
|
6 |
|
7 |
|
8 |
async def get_token(
|
9 |
-
|
|
|
10 |
token: Optional[str] = Query(
|
11 |
None, description="Access token (alternative to Authorization header)"
|
12 |
),
|
@@ -15,17 +16,28 @@ async def get_token(
|
|
15 |
Extract token from Authorization header or query parameter
|
16 |
Supports both methods for better compatibility with various clients
|
17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
# First try to get token from Authorization header
|
19 |
-
if authorization:
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
22 |
else:
|
23 |
# If it doesn't have Bearer prefix, use as is
|
24 |
-
return
|
25 |
|
26 |
# Then try to get token from query parameter
|
27 |
if token:
|
28 |
-
return token
|
29 |
|
30 |
# If no token is provided, raise an error
|
31 |
raise HTTPException(
|
|
|
1 |
+
from fastapi import Depends, HTTPException, status, Header, Query, Request
|
2 |
from typing import Optional
|
3 |
from database import get_users
|
4 |
from models import User, UserInDB
|
|
|
6 |
|
7 |
|
8 |
async def get_token(
|
9 |
+
request: Request,
|
10 |
+
authorization: Optional[str] = Header(None, convert_underscores=False),
|
11 |
token: Optional[str] = Query(
|
12 |
None, description="Access token (alternative to Authorization header)"
|
13 |
),
|
|
|
16 |
Extract token from Authorization header or query parameter
|
17 |
Supports both methods for better compatibility with various clients
|
18 |
"""
|
19 |
+
# Debug headers
|
20 |
+
headers = dict(request.headers)
|
21 |
+
print(f"All headers: {headers}")
|
22 |
+
print(f"Authorization header from param: {authorization}")
|
23 |
+
auth_header = headers.get("authorization") or headers.get("Authorization")
|
24 |
+
print(f"Authorization header from request: {auth_header}")
|
25 |
+
|
26 |
# First try to get token from Authorization header
|
27 |
+
if authorization or auth_header:
|
28 |
+
# Use the authorization from parameter or from request headers
|
29 |
+
auth = authorization or auth_header
|
30 |
+
|
31 |
+
# Handle "Bearer" prefix if present
|
32 |
+
if auth.startswith("Bearer "):
|
33 |
+
return auth.replace("Bearer ", "").strip()
|
34 |
else:
|
35 |
# If it doesn't have Bearer prefix, use as is
|
36 |
+
return auth.strip()
|
37 |
|
38 |
# Then try to get token from query parameter
|
39 |
if token:
|
40 |
+
return token.strip()
|
41 |
|
42 |
# If no token is provided, raise an error
|
43 |
raise HTTPException(
|
main.py
CHANGED
@@ -4,6 +4,7 @@ from fastapi import FastAPI
|
|
4 |
import uvicorn
|
5 |
import traceback
|
6 |
from contextlib import asynccontextmanager
|
|
|
7 |
|
8 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
9 |
sys.path.append(current_dir)
|
@@ -51,6 +52,16 @@ app = FastAPI(
|
|
51 |
# Removed root_path since HF Spaces already handles it
|
52 |
)
|
53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
# Include Routers
|
55 |
app.include_router(health.router, tags=["Health"])
|
56 |
app.include_router(auth.router, tags=["Authentication"])
|
|
|
4 |
import uvicorn
|
5 |
import traceback
|
6 |
from contextlib import asynccontextmanager
|
7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
8 |
|
9 |
current_dir = os.path.dirname(os.path.abspath(__file__))
|
10 |
sys.path.append(current_dir)
|
|
|
52 |
# Removed root_path since HF Spaces already handles it
|
53 |
)
|
54 |
|
55 |
+
# Configure CORS
|
56 |
+
app.add_middleware(
|
57 |
+
CORSMiddleware,
|
58 |
+
allow_origins=["*"], # Allows all origins
|
59 |
+
allow_credentials=True,
|
60 |
+
allow_methods=["*"], # Allows all methods
|
61 |
+
allow_headers=["*"], # Allows all headers
|
62 |
+
expose_headers=["*"], # Expose all headers
|
63 |
+
)
|
64 |
+
|
65 |
# Include Routers
|
66 |
app.include_router(health.router, tags=["Health"])
|
67 |
app.include_router(auth.router, tags=["Authentication"])
|