Spaces:
Sleeping
Sleeping
Update core/security.py
Browse files- core/security.py +59 -68
core/security.py
CHANGED
|
@@ -1,89 +1,80 @@
|
|
| 1 |
-
# core/security.py
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
-
from
|
| 4 |
-
from jose import jwt, JWTError
|
| 5 |
-
from fastapi import Depends, HTTPException, status
|
| 6 |
from fastapi.security import OAuth2PasswordBearer
|
| 7 |
-
from
|
| 8 |
-
from
|
|
|
|
| 9 |
import logging
|
| 10 |
-
from
|
| 11 |
|
| 12 |
logger = logging.getLogger(__name__)
|
| 13 |
|
| 14 |
-
# OAuth2 setup
|
| 15 |
oauth2_scheme = OAuth2PasswordBearer(
|
| 16 |
-
tokenUrl="/auth/login",
|
| 17 |
scheme_name="JWT"
|
| 18 |
)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
def verify_password(plain: str, hashed: str) -> bool:
|
| 27 |
-
return pwd_context.verify(plain, hashed)
|
| 28 |
-
|
| 29 |
-
def create_access_token(data: dict, expires_delta: timedelta = None):
|
| 30 |
-
to_encode = data.copy()
|
| 31 |
-
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
| 32 |
-
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
|
| 33 |
-
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
| 34 |
-
logger.debug(f"Created JWT for {data.get('sub')}, expires at {expire}")
|
| 35 |
-
return encoded_jwt
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
raise HTTPException(
|
| 45 |
-
status_code=status.
|
| 46 |
-
detail="
|
| 47 |
-
headers={"WWW-Authenticate": "Bearer"}
|
| 48 |
)
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
logger.
|
| 57 |
-
raise HTTPException(
|
| 58 |
-
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 59 |
-
detail="Invalid token: missing subject",
|
| 60 |
-
headers={"WWW-Authenticate": "Bearer"}
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
raise HTTPException(
|
| 67 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 68 |
-
detail="Token
|
| 69 |
-
headers={"WWW-Authenticate": "Bearer"}
|
| 70 |
)
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
except JWTError as e:
|
| 73 |
-
logger.error(f"JWT
|
| 74 |
-
raise
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
user = await users_collection.find_one({"email": email})
|
| 81 |
-
if not user:
|
| 82 |
-
logger.error(f"User not found for email: {email}")
|
| 83 |
-
raise HTTPException(
|
| 84 |
-
status_code=status.HTTP_404_NOT_FOUND,
|
| 85 |
-
detail="User not found"
|
| 86 |
-
)
|
| 87 |
-
|
| 88 |
-
logger.info(f"Authenticated user: {user['email']}, role: {user.get('role')}")
|
| 89 |
-
return user
|
|
|
|
|
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
+
from fastapi import HTTPException, status, Depends, Request
|
|
|
|
|
|
|
| 3 |
from fastapi.security import OAuth2PasswordBearer
|
| 4 |
+
from jose import jwt, JWTError
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import Optional
|
| 7 |
import logging
|
| 8 |
+
from core.config import settings
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
|
|
|
| 12 |
oauth2_scheme = OAuth2PasswordBearer(
|
| 13 |
+
tokenUrl=f"{settings.API_V1_STR}/auth/login",
|
| 14 |
scheme_name="JWT"
|
| 15 |
)
|
| 16 |
|
| 17 |
+
class TokenPayload(BaseModel):
|
| 18 |
+
sub: str
|
| 19 |
+
exp: int
|
| 20 |
+
iat: int
|
| 21 |
+
role: Optional[str] = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def create_access_token(*, data: dict, expires_delta: timedelta = None) -> str:
|
| 24 |
+
try:
|
| 25 |
+
to_encode = data.copy()
|
| 26 |
+
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES))
|
| 27 |
+
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
|
| 28 |
+
encoded_jwt = jwt.encode(
|
| 29 |
+
to_encode,
|
| 30 |
+
settings.SECRET_KEY,
|
| 31 |
+
algorithm=settings.ALGORITHM
|
| 32 |
+
)
|
| 33 |
+
logger.info(f"Token created for {data.get('sub')}")
|
| 34 |
+
return encoded_jwt
|
| 35 |
+
except Exception as e:
|
| 36 |
+
logger.error(f"Token creation failed: {str(e)}")
|
| 37 |
raise HTTPException(
|
| 38 |
+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
| 39 |
+
detail="Token creation failed"
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
async def verify_token(token: str = Depends(oauth2_scheme)) -> TokenPayload:
|
| 43 |
+
credentials_exception = HTTPException(
|
| 44 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 45 |
+
detail="Could not validate credentials",
|
| 46 |
+
headers={"WWW-Authenticate": "Bearer"},
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
try:
|
| 50 |
+
if not token:
|
| 51 |
+
logger.error("Empty token provided")
|
| 52 |
+
raise credentials_exception
|
| 53 |
|
| 54 |
+
if token.startswith("Bearer "):
|
| 55 |
+
token = token[7:]
|
| 56 |
+
logger.debug("Removed 'Bearer ' prefix from token")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
payload = jwt.decode(
|
| 59 |
+
token,
|
| 60 |
+
settings.SECRET_KEY,
|
| 61 |
+
algorithms=[settings.ALGORITHM]
|
| 62 |
+
)
|
| 63 |
+
token_data = TokenPayload(**payload)
|
| 64 |
+
|
| 65 |
+
if datetime.fromtimestamp(token_data.exp) < datetime.utcnow():
|
| 66 |
+
logger.error("Token expired")
|
| 67 |
raise HTTPException(
|
| 68 |
status_code=status.HTTP_401_UNAUTHORIZED,
|
| 69 |
+
detail="Token expired"
|
|
|
|
| 70 |
)
|
| 71 |
+
|
| 72 |
+
logger.info(f"Token verified for {token_data.sub}")
|
| 73 |
+
return token_data
|
| 74 |
+
|
| 75 |
except JWTError as e:
|
| 76 |
+
logger.error(f"JWT validation failed: {str(e)}")
|
| 77 |
+
raise credentials_exception
|
| 78 |
+
except Exception as e:
|
| 79 |
+
logger.error(f"Unexpected token verification error: {str(e)}")
|
| 80 |
+
raise credentials_exception
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|