mirror of
https://github.com/dograh-hq/dograh.git
synced 2026-06-07 07:55:16 +02:00
29 lines
830 B
Python
29 lines
830 B
Python
|
|
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"])
|