feat(auth): implement JWT authentication with secure password hashing

This commit is contained in:
DS | Schubert 2026-07-02 15:43:00 +02:00
parent 6656a8782c
commit 56df3228a4
7 changed files with 146 additions and 74 deletions

View file

@ -0,0 +1,29 @@
from datetime import UTC, datetime, timedelta
from jose import jwt
from pwdlib import PasswordHash
password_hash = PasswordHash.recommended()
SECRET_KEY = "CHANGE_ME"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 60
def hash_password(password: str) -> str:
return password_hash.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
return password_hash.verify(plain_password, hashed_password)
def create_access_token(subject: str) -> str:
expire = datetime.now(UTC) + timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
payload = {
"sub": subject,
"exp": expire,
}
return jwt.encode(payload, SECRET_KEY, algorithm=ALGORITHM)