ЛР2: PostgreSQL, сессии (Basic + Bearer), Compose, миграции
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import delete, func, select
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from models import AuthSession
|
||||
|
||||
|
||||
def create_session(db: Session, token_hash: bytes, user_id: uuid.UUID,
|
||||
created_at: datetime, expires_at: datetime) -> AuthSession:
|
||||
auth_session = AuthSession(token_hash=token_hash, user_id=user_id, created_at=created_at, expires_at=expires_at)
|
||||
db.add(auth_session)
|
||||
db.commit()
|
||||
return auth_session
|
||||
|
||||
|
||||
def get_active_session(db: Session, token_hash: bytes) -> AuthSession | None:
|
||||
query = select(AuthSession).where(
|
||||
AuthSession.token_hash == token_hash,
|
||||
AuthSession.expires_at > func.now(),
|
||||
)
|
||||
return db.scalar(query)
|
||||
|
||||
|
||||
def delete_session(db: Session, token_hash: bytes) -> bool:
|
||||
result = db.execute(delete(AuthSession).where(AuthSession.token_hash == token_hash))
|
||||
db.commit()
|
||||
return result.rowcount > 0
|
||||
Reference in New Issue
Block a user