Spaces:
Sleeping
Sleeping
Update core/security.py
Browse files- core/security.py +8 -7
core/security.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
from passlib.context import CryptContext
|
| 3 |
from jose import jwt, JWTError
|
|
@@ -36,13 +37,14 @@ def create_access_token(data: dict, expires_delta: timedelta = None):
|
|
| 36 |
logger.debug(f"Created JWT for {data.get('sub')}, expires at {expire}")
|
| 37 |
return encoded_jwt
|
| 38 |
|
| 39 |
-
#
|
| 40 |
async def get_current_user(request: Request, token: str = Depends(oauth2_scheme)):
|
| 41 |
-
|
| 42 |
-
logger.debug(f"
|
|
|
|
| 43 |
|
| 44 |
if not token:
|
| 45 |
-
logger.error("No token provided
|
| 46 |
raise HTTPException(
|
| 47 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 48 |
detail="No token provided",
|
|
@@ -62,7 +64,6 @@ async def get_current_user(request: Request, token: str = Depends(oauth2_scheme)
|
|
| 62 |
headers={"WWW-Authenticate": "Bearer"}
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Check token expiration explicitly
|
| 66 |
exp = payload.get("exp")
|
| 67 |
if exp and datetime.utcnow().timestamp() > exp:
|
| 68 |
logger.error(f"Token expired for {email}")
|
|
@@ -73,7 +74,7 @@ async def get_current_user(request: Request, token: str = Depends(oauth2_scheme)
|
|
| 73 |
)
|
| 74 |
|
| 75 |
except JWTError as e:
|
| 76 |
-
logger.error(f"JWT decode error: {str(e)}")
|
| 77 |
raise HTTPException(
|
| 78 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 79 |
detail=f"Could not validate token: {str(e)}",
|
|
@@ -88,5 +89,5 @@ async def get_current_user(request: Request, token: str = Depends(oauth2_scheme)
|
|
| 88 |
detail="User not found"
|
| 89 |
)
|
| 90 |
|
| 91 |
-
logger.info(f"Authenticated user: {user['email']}")
|
| 92 |
return user
|
|
|
|
| 1 |
+
# core/security.py
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
from passlib.context import CryptContext
|
| 4 |
from jose import jwt, JWTError
|
|
|
|
| 37 |
logger.debug(f"Created JWT for {data.get('sub')}, expires at {expire}")
|
| 38 |
return encoded_jwt
|
| 39 |
|
| 40 |
+
# Get the current user from the JWT token
|
| 41 |
async def get_current_user(request: Request, token: str = Depends(oauth2_scheme)):
|
| 42 |
+
auth_header = request.headers.get("Authorization", "No Authorization header")
|
| 43 |
+
logger.debug(f"Raw Authorization header: {auth_header}")
|
| 44 |
+
logger.debug(f"Processed token: {token[:10]}... if present")
|
| 45 |
|
| 46 |
if not token:
|
| 47 |
+
logger.error(f"No token provided. Full headers: {dict(request.headers)}")
|
| 48 |
raise HTTPException(
|
| 49 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 50 |
detail="No token provided",
|
|
|
|
| 64 |
headers={"WWW-Authenticate": "Bearer"}
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
exp = payload.get("exp")
|
| 68 |
if exp and datetime.utcnow().timestamp() > exp:
|
| 69 |
logger.error(f"Token expired for {email}")
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
except JWTError as e:
|
| 77 |
+
logger.error(f"JWT decode error: {str(e)}. Token: {token[:10]}...")
|
| 78 |
raise HTTPException(
|
| 79 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 80 |
detail=f"Could not validate token: {str(e)}",
|
|
|
|
| 89 |
detail="User not found"
|
| 90 |
)
|
| 91 |
|
| 92 |
+
logger.info(f"Authenticated user: {user['email']}, role: {user.get('role')}")
|
| 93 |
return user
|