Spaces:
Running
Running
Upload auth.py
Browse files- src/apps/auth.py +13 -5
src/apps/auth.py
CHANGED
|
@@ -25,17 +25,25 @@ ACCESS_TOKEN_EXPIRE_MINUTES = int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES", 30))
|
|
| 25 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 26 |
|
| 27 |
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 28 |
-
"""Verifies a plain text password against its hashed version."""
|
| 29 |
try:
|
|
|
|
|
|
|
| 30 |
if isinstance(hashed_password, str):
|
| 31 |
hashed_password = hashed_password.encode('utf-8')
|
| 32 |
-
return
|
| 33 |
-
except Exception:
|
|
|
|
| 34 |
return False
|
| 35 |
|
| 36 |
def get_password_hash(password: str) -> str:
|
| 37 |
-
"""Creates a secure hash for a password."""
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- Token Management ---
|
| 41 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|
|
|
|
| 25 |
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
| 26 |
|
| 27 |
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
| 28 |
+
"""Verifies a plain text password against its hashed version using bcrypt."""
|
| 29 |
try:
|
| 30 |
+
if isinstance(plain_password, str):
|
| 31 |
+
plain_password = plain_password.encode('utf-8')
|
| 32 |
if isinstance(hashed_password, str):
|
| 33 |
hashed_password = hashed_password.encode('utf-8')
|
| 34 |
+
return bcrypt.checkpw(plain_password, hashed_password)
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Bcrypt verification error: {e}")
|
| 37 |
return False
|
| 38 |
|
| 39 |
def get_password_hash(password: str) -> str:
|
| 40 |
+
"""Creates a secure bcrypt hash for a password."""
|
| 41 |
+
if isinstance(password, str):
|
| 42 |
+
password = password.encode('utf-8')
|
| 43 |
+
# Generate salt and hash
|
| 44 |
+
salt = bcrypt.gensalt()
|
| 45 |
+
hashed = bcrypt.hashpw(password, salt)
|
| 46 |
+
return hashed.decode('utf-8')
|
| 47 |
|
| 48 |
# --- Token Management ---
|
| 49 |
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
|