29 lines
No EOL
707 B
Python
29 lines
No EOL
707 B
Python
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) |