Spaces:
Sleeping
Sleeping
Update core/security.py
Browse files- core/security.py +45 -68
core/security.py
CHANGED
|
@@ -1,80 +1,57 @@
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
-
from
|
| 3 |
-
from fastapi.security import OAuth2PasswordBearer
|
| 4 |
from jose import jwt, JWTError
|
| 5 |
-
from
|
| 6 |
-
from
|
|
|
|
|
|
|
| 7 |
import logging
|
| 8 |
-
from core.config import settings
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
scheme_name="JWT"
|
| 15 |
-
)
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
exp: int
|
| 20 |
-
iat: int
|
| 21 |
-
role: Optional[str] = None
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 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 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
raise credentials_exception
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 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 |
-
|
| 77 |
-
raise
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from datetime import datetime, timedelta
|
| 2 |
+
from passlib.context import CryptContext
|
|
|
|
| 3 |
from jose import jwt, JWTError
|
| 4 |
+
from fastapi import Depends, HTTPException, status
|
| 5 |
+
from fastapi.security import OAuth2PasswordBearer
|
| 6 |
+
from core.config import SECRET_KEY, ALGORITHM, ACCESS_TOKEN_EXPIRE_MINUTES
|
| 7 |
+
from db.mongo import users_collection
|
| 8 |
import logging
|
|
|
|
| 9 |
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
# OAuth2 setup
|
| 13 |
+
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
# Password hashing context
|
| 16 |
+
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
# Hash a plain password
|
| 19 |
+
def hash_password(password: str) -> str:
|
| 20 |
+
return pwd_context.hash(password)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
# Verify a plain password against the hash
|
| 23 |
+
def verify_password(plain: str, hashed: str) -> bool:
|
| 24 |
+
return pwd_context.verify(plain, hashed)
|
| 25 |
+
|
| 26 |
+
# Create a JWT access token
|
| 27 |
+
def create_access_token(data: dict, expires_delta: timedelta = None):
|
| 28 |
+
to_encode = data.copy()
|
| 29 |
+
expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES))
|
| 30 |
+
to_encode.update({"exp": expire})
|
| 31 |
+
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
|
|
|
| 32 |
|
| 33 |
+
# Get the current user from the JWT token
|
| 34 |
+
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 35 |
+
print("🔐 Token received:", token)
|
| 36 |
|
| 37 |
+
if not token:
|
| 38 |
+
print("❌ No token received")
|
| 39 |
+
raise HTTPException(status_code=401, detail="No token provided")
|
| 40 |
+
|
| 41 |
+
try:
|
| 42 |
+
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 43 |
+
print("🧠 Token payload:", payload)
|
| 44 |
+
|
| 45 |
+
email = payload.get("sub")
|
| 46 |
+
if not email:
|
| 47 |
+
raise HTTPException(status_code=401, detail="Invalid token: missing subject")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except JWTError as e:
|
| 49 |
+
print("❌ JWT decode error:", str(e))
|
| 50 |
+
raise HTTPException(status_code=401, detail="Could not validate token")
|
| 51 |
+
|
| 52 |
+
user = await users_collection.find_one({"email": email})
|
| 53 |
+
if not user:
|
| 54 |
+
raise HTTPException(status_code=404, detail="User not found")
|
| 55 |
+
|
| 56 |
+
print("✅ Authenticated user:", user["email"])
|
| 57 |
+
return user
|