mirror of
https://github.com/furyhawk/agent_alpha.git
synced 2026-07-21 02:25:34 +00:00
- Introduced `auth_token_repo` for handling auth token operations in Redis. - Created `chat_repo` for managing chat sessions and messages in Redis. - Implemented `ChatService` and `AuthService` to encapsulate business logic for chat and authentication. - Updated routes to utilize new service and repository layers, removing direct database calls. - Enhanced user management with `UserService` for CRUD operations and user authentication. - Revised architecture documentation to reflect the new service-repository pattern.
42 lines
1.1 KiB
Python
42 lines
1.1 KiB
Python
"""Repository for auth token operations (Valkey).
|
|
|
|
Usage::
|
|
|
|
from backend.repositories.auth_token_repo import create_token, resolve_token
|
|
|
|
token = await create_token(valkey, "user-uuid")
|
|
user_id = await resolve_token(valkey, token)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
|
|
from redis.asyncio import Redis
|
|
|
|
_AUTH_TOKEN_KEY = "auth_token:{token}"
|
|
_AUTH_TOKEN_TTL = 86400 * 7 # 7 days
|
|
|
|
|
|
async def create_token(valkey: Redis, user_id: str) -> str:
|
|
"""Create an auth token for a user, stored in Valkey with TTL.
|
|
|
|
Returns the token string.
|
|
"""
|
|
token = uuid.uuid4().hex
|
|
key = _AUTH_TOKEN_KEY.format(token=token)
|
|
await valkey.setex(key, _AUTH_TOKEN_TTL, user_id)
|
|
return token
|
|
|
|
|
|
async def resolve_token(valkey: Redis, token: str) -> str | None:
|
|
"""Resolve an auth token to a user_id string, or ``None`` if invalid/expired."""
|
|
key = _AUTH_TOKEN_KEY.format(token=token)
|
|
return await valkey.get(key)
|
|
|
|
|
|
async def revoke_token(valkey: Redis, token: str) -> None:
|
|
"""Delete an auth token (logout)."""
|
|
key = _AUTH_TOKEN_KEY.format(token=token)
|
|
await valkey.delete(key)
|