Download auth.py from CabirPoya/poyaadmin: direct link, hf CLI and curl.
- Browser
- Download file 1.52 kB
-
https://huggingface.co/spaces/CabirPoya/poyaadmin/resolve/main/auth.py
- Command line
-
hf download hf://spaces/CabirPoya/poyaadmin/auth.py
-
curl -L -o auth.py https://huggingface.co/spaces/CabirPoya/poyaadmin/resolve/main/auth.py
1.52 kB
| # auth.py | |
| import os | |
| from datetime import datetime, timedelta | |
| from typing import Optional | |
| from passlib.context import CryptContext | |
| from jose import JWTError, jwt | |
| from dotenv import load_dotenv | |
| # بارگذاری متغیرهای .env | |
| load_dotenv() | |
| # تعریف رمزنگاری | |
| pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") | |
| # متغیرهای امنیتی | |
| SECRET_KEY = os.getenv("JWT_SECRET") or "your-default-secret-key" | |
| ALGORITHM = "HS256" | |
| ACCESS_TOKEN_EXPIRE_MINUTES = 30 | |
| # تابع هشکردن رمز | |
| def get_password_hash(password: str) -> str: | |
| return pwd_context.hash(password) | |
| # بررسی رمز هنگام لاگین | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| return pwd_context.verify(plain_password, hashed_password) | |
| # ساخت توکن دسترسی (Access Token) | |
| def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str: | |
| to_encode = data.copy() | |
| expire = datetime.utcnow() + (expires_delta or timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)) | |
| to_encode.update({"exp": expire}) | |
| encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM) | |
| return encoded_jwt | |
| # بررسی و رمزگشایی توکن JWT (میتوانید این را در dependency استفاده کنید) | |
| def decode_access_token(token: str) -> Optional[dict]: | |
| try: | |
| payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM]) | |
| return payload | |
| except JWTError: | |
| return None | |