37 lines
1.1 KiB
Python
37 lines
1.1 KiB
Python
"""Authentication service."""
|
|
import aiosqlite
|
|
from typing import Optional
|
|
from ..database import db_connection
|
|
from ..models import ClientKey
|
|
|
|
|
|
class AuthService:
|
|
"""Service for client key authentication."""
|
|
|
|
@staticmethod
|
|
async def validate_key(api_key: str) -> Optional[ClientKey]:
|
|
"""Validate a client API key.
|
|
|
|
Args:
|
|
api_key: The API key to validate
|
|
|
|
Returns:
|
|
ClientKey if valid and active, None otherwise
|
|
"""
|
|
async with db_connection() as db:
|
|
cursor = await db.execute(
|
|
"""SELECT id, key, name, is_active, created_at
|
|
FROM client_keys WHERE key = ?""",
|
|
(api_key,)
|
|
)
|
|
row = await cursor.fetchone()
|
|
if row and row["is_active"]:
|
|
return ClientKey(
|
|
id=row["id"],
|
|
key=row["key"],
|
|
name=row["name"],
|
|
is_active=row["is_active"],
|
|
created_at=row["created_at"]
|
|
)
|
|
return None
|