Spaces:
Sleeping
Sleeping
Update src/utils/security.py
Browse files- src/utils/security.py +9 -7
src/utils/security.py
CHANGED
|
@@ -1,22 +1,25 @@
|
|
| 1 |
from passlib.context import CryptContext
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
-
from typing import Optional
|
| 4 |
import uuid
|
| 5 |
from jose import JWTError, jwt
|
| 6 |
from ..config import settings
|
| 7 |
|
| 8 |
# Password hashing context
|
| 9 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
def hash_password(password: str) -> str:
|
| 13 |
-
"""Hash a password using bcrypt."""
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 18 |
-
"""Verify a plain password against its hash."""
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
|
| 22 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
|
@@ -26,7 +29,6 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -
|
|
| 26 |
if expires_delta:
|
| 27 |
expire = datetime.utcnow() + expires_delta
|
| 28 |
else:
|
| 29 |
-
# Default to 7 days if no expiration is provided
|
| 30 |
expire = datetime.utcnow() + timedelta(days=settings.ACCESS_TOKEN_EXPIRE_DAYS)
|
| 31 |
|
| 32 |
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
|
|
@@ -54,4 +56,4 @@ def verify_user_id_from_token(token: str) -> Optional[uuid.UUID]:
|
|
| 54 |
return uuid.UUID(user_id_str)
|
| 55 |
except ValueError:
|
| 56 |
return None
|
| 57 |
-
return None
|
|
|
|
| 1 |
from passlib.context import CryptContext
|
| 2 |
from datetime import datetime, timedelta
|
| 3 |
+
from typing import Optional
|
| 4 |
import uuid
|
| 5 |
from jose import JWTError, jwt
|
| 6 |
from ..config import settings
|
| 7 |
|
| 8 |
# Password hashing context
|
| 9 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 10 |
+
BCRYPT_MAX_LENGTH = 72 # bcrypt limit
|
| 11 |
|
| 12 |
|
| 13 |
def hash_password(password: str) -> str:
|
| 14 |
+
"""Hash a password using bcrypt (truncate to 72 bytes)."""
|
| 15 |
+
truncated = password[:BCRYPT_MAX_LENGTH]
|
| 16 |
+
return pwd_context.hash(truncated)
|
| 17 |
|
| 18 |
|
| 19 |
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 20 |
+
"""Verify a plain password against its hash (truncate to 72 bytes)."""
|
| 21 |
+
truncated = plain_password[:BCRYPT_MAX_LENGTH]
|
| 22 |
+
return pwd_context.verify(truncated, hashed_password)
|
| 23 |
|
| 24 |
|
| 25 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
|
|
|
| 29 |
if expires_delta:
|
| 30 |
expire = datetime.utcnow() + expires_delta
|
| 31 |
else:
|
|
|
|
| 32 |
expire = datetime.utcnow() + timedelta(days=settings.ACCESS_TOKEN_EXPIRE_DAYS)
|
| 33 |
|
| 34 |
to_encode.update({"exp": expire, "iat": datetime.utcnow()})
|
|
|
|
| 56 |
return uuid.UUID(user_id_str)
|
| 57 |
except ValueError:
|
| 58 |
return None
|
| 59 |
+
return None
|