File size: 1,318 Bytes
24b8a5d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | """
auth.py — password hashing (bcrypt) and session tokens (JWT).
"""
import os
import time
import bcrypt
import jwt
JWT_SECRET = os.environ.get("JWT_SECRET", "change-me-in-space-secrets")
JWT_ALGO = "HS256"
JWT_EXPIRY_SECONDS = 60 * 60 * 24 * 30 # 30 days
def hash_password(plain_password: str) -> str:
return bcrypt.hashpw(plain_password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
def verify_password(plain_password: str, password_hash: str) -> bool:
return bcrypt.checkpw(plain_password.encode("utf-8"), password_hash.encode("utf-8"))
def issue_token(user_id: int, username: str) -> str:
payload = {
"sub": user_id,
"username": username,
"iat": int(time.time()),
"exp": int(time.time()) + JWT_EXPIRY_SECONDS,
}
return jwt.encode(payload, JWT_SECRET, algorithm=JWT_ALGO)
def verify_token(token: str):
try:
payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGO])
return payload
except jwt.ExpiredSignatureError:
return None
except jwt.InvalidTokenError:
return None
def is_valid_username(username: str) -> bool:
if not username or not (3 <= len(username) <= 20):
return False
return username.isalnum() or "_" in username and all(ch.isalnum() or ch == "_" for ch in username)
|