Spaces:
Sleeping
Sleeping
fix authen
Browse files- auth.py +1 -1
- main.py +1 -2
- routes/auth.py +17 -0
- routes/health.py +34 -1
auth.py
CHANGED
@@ -10,7 +10,7 @@ from typing import Annotated, Optional
|
|
10 |
from jwt.exceptions import InvalidTokenError
|
11 |
|
12 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
13 |
-
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="
|
14 |
|
15 |
# Authentication helper functions
|
16 |
def verify_password(plain_password, hashed_password):
|
|
|
10 |
from jwt.exceptions import InvalidTokenError
|
11 |
|
12 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
13 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token", auto_error=True)
|
14 |
|
15 |
# Authentication helper functions
|
16 |
def verify_password(plain_password, hashed_password):
|
main.py
CHANGED
@@ -48,8 +48,7 @@ app = FastAPI(
|
|
48 |
"description": "AI model endpoints for prediction and embedding",
|
49 |
},
|
50 |
],
|
51 |
-
#
|
52 |
-
root_path="/api",
|
53 |
)
|
54 |
|
55 |
# Include Routers
|
|
|
48 |
"description": "AI model endpoints for prediction and embedding",
|
49 |
},
|
50 |
],
|
51 |
+
# Removed root_path since HF Spaces already handles it
|
|
|
52 |
)
|
53 |
|
54 |
# Include Routers
|
routes/auth.py
CHANGED
@@ -37,3 +37,20 @@ async def register_user(user_data: UserCreate):
|
|
37 |
if not success:
|
38 |
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
|
39 |
return {"message": message}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
if not success:
|
38 |
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=message)
|
39 |
return {"message": message}
|
40 |
+
|
41 |
+
|
42 |
+
@router.get("/generate-test-token/{username}")
|
43 |
+
async def generate_test_token(username: str):
|
44 |
+
"""
|
45 |
+
Generate a test token for a user without requiring password
|
46 |
+
(For testing only, should be disabled in production)
|
47 |
+
"""
|
48 |
+
users = get_users()
|
49 |
+
if username not in users:
|
50 |
+
raise HTTPException(status_code=404, detail="User not found")
|
51 |
+
|
52 |
+
access_token_expires = timedelta(hours=ACCESS_TOKEN_EXPIRE_HOURS)
|
53 |
+
access_token = create_access_token(
|
54 |
+
data={"sub": username}, expires_delta=access_token_expires
|
55 |
+
)
|
56 |
+
return {"access_token": access_token, "token_type": "bearer", "username": username}
|
routes/health.py
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
-
from fastapi import APIRouter, Depends
|
2 |
from auth import get_current_user
|
3 |
from models import User
|
4 |
import time
|
|
|
|
|
5 |
|
6 |
router = APIRouter()
|
7 |
|
@@ -23,3 +25,34 @@ async def auth_check(current_user: User = Depends(get_current_user)):
|
|
23 |
"username": current_user.username,
|
24 |
"message": "Authentication successful",
|
25 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import APIRouter, Depends, Header, Request
|
2 |
from auth import get_current_user
|
3 |
from models import User
|
4 |
import time
|
5 |
+
import jwt
|
6 |
+
from config import SECRET_KEY, ALGORITHM
|
7 |
|
8 |
router = APIRouter()
|
9 |
|
|
|
25 |
"username": current_user.username,
|
26 |
"message": "Authentication successful",
|
27 |
}
|
28 |
+
|
29 |
+
|
30 |
+
@router.get("/debug-auth")
|
31 |
+
async def debug_auth(request: Request, authorization: str = Header(None)):
|
32 |
+
"""
|
33 |
+
Debug endpoint to manually inspect the authorization header and token
|
34 |
+
"""
|
35 |
+
headers = dict(request.headers)
|
36 |
+
auth_header = headers.get("authorization", "Not found")
|
37 |
+
|
38 |
+
token_info = {"valid": False, "error": None, "payload": None}
|
39 |
+
|
40 |
+
if authorization and authorization.startswith("Bearer "):
|
41 |
+
token = authorization.replace("Bearer ", "")
|
42 |
+
try:
|
43 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
44 |
+
token_info["valid"] = True
|
45 |
+
token_info["payload"] = payload
|
46 |
+
except Exception as e:
|
47 |
+
token_info["error"] = str(e)
|
48 |
+
|
49 |
+
return {
|
50 |
+
"headers": headers,
|
51 |
+
"auth_header": auth_header,
|
52 |
+
"token_info": token_info,
|
53 |
+
"host_info": {
|
54 |
+
"url": str(request.url),
|
55 |
+
"base_url": str(request.base_url),
|
56 |
+
"method": request.method,
|
57 |
+
},
|
58 |
+
}
|