You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.3 KiB
40 lines
1.3 KiB
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.models.entities import AuthSession
|
|
|
|
|
|
class AuthSessionRepository:
|
|
def __init__(self, session: AsyncSession) -> None:
|
|
self.session = session
|
|
|
|
async def create(self, auth_session: AuthSession) -> AuthSession:
|
|
self.session.add(auth_session)
|
|
await self.session.commit()
|
|
await self.session.refresh(auth_session)
|
|
return auth_session
|
|
|
|
async def get_active_by_token_hash(self, token_hash: str, now: datetime) -> AuthSession | None:
|
|
result = await self.session.execute(
|
|
select(AuthSession).where(
|
|
AuthSession.token_hash == token_hash,
|
|
AuthSession.revoked_at.is_(None),
|
|
AuthSession.expires_at > now,
|
|
)
|
|
)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def get_by_token_hash(self, token_hash: str) -> AuthSession | None:
|
|
result = await self.session.execute(
|
|
select(AuthSession).where(AuthSession.token_hash == token_hash)
|
|
)
|
|
return result.scalar_one_or_none()
|
|
|
|
async def save(self, auth_session: AuthSession) -> AuthSession:
|
|
await self.session.commit()
|
|
await self.session.refresh(auth_session)
|
|
return auth_session
|
|
|