feat: add authentication for OSS (#167)

* feat: add authentication for OSS

Fixes #157 and #156

* fix: fix token generation

* fix: limit fastapi workers to 1
This commit is contained in:
Abhishek 2026-02-20 18:21:24 +05:30 committed by GitHub
parent 0791975864
commit 642cc34e8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
48 changed files with 994 additions and 303 deletions

28
api/utils/auth.py Normal file
View file

@ -0,0 +1,28 @@
from datetime import UTC, datetime, timedelta
import bcrypt
import jwt
from api.constants import OSS_JWT_EXPIRY_HOURS, OSS_JWT_SECRET
def hash_password(password: str) -> str:
return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
def verify_password(password: str, password_hash: str) -> bool:
return bcrypt.checkpw(password.encode("utf-8"), password_hash.encode("utf-8"))
def create_jwt_token(user_id: int, email: str) -> str:
payload = {
"sub": str(user_id),
"email": email,
"exp": datetime.now(UTC) + timedelta(hours=OSS_JWT_EXPIRY_HOURS),
"iat": datetime.now(UTC),
}
return jwt.encode(payload, OSS_JWT_SECRET, algorithm="HS256")
def decode_jwt_token(token: str) -> dict:
return jwt.decode(token, OSS_JWT_SECRET, algorithms=["HS256"])