ЛР 2: мессенджер на PostgreSQL и сессии в Redis
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
from .passwords import hash_password, password_matches
|
||||
from .tokens import create_token
|
||||
|
||||
__all__ = ['create_token', 'hash_password', 'password_matches']
|
||||
@@ -0,0 +1,15 @@
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerificationError, VerifyMismatchError
|
||||
|
||||
_hasher = PasswordHasher()
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return _hasher.hash(password)
|
||||
|
||||
|
||||
def password_matches(stored_hash: str, password: str) -> bool:
|
||||
try:
|
||||
return _hasher.verify(stored_hash, password)
|
||||
except (VerifyMismatchError, VerificationError):
|
||||
return False
|
||||
@@ -0,0 +1,11 @@
|
||||
import hashlib
|
||||
import secrets
|
||||
|
||||
|
||||
def create_token() -> str:
|
||||
return secrets.token_urlsafe(32)
|
||||
|
||||
|
||||
def session_key(token: str) -> str:
|
||||
digest = hashlib.sha256(token.encode('utf-8')).hexdigest()
|
||||
return f'session:{digest}'
|
||||
Reference in New Issue
Block a user