57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
import uuid
|
|
from datetime import datetime, timedelta
|
|
from sqlalchemy import Column, String, DateTime, Text, Boolean
|
|
from sqlalchemy.sql import func
|
|
|
|
from app.models.base import Base
|
|
from app.core.config import settings
|
|
|
|
|
|
class Secret(Base):
|
|
"""SQLAlchemy model for storing secrets."""
|
|
__tablename__ = "secrets"
|
|
|
|
# Primary key - unique identifier for the secret
|
|
id = Column(String(36), primary_key=True, index=True, default=lambda: str(uuid.uuid4()))
|
|
|
|
# The encrypted secret content
|
|
content = Column(Text, nullable=False)
|
|
|
|
# When the secret was created
|
|
created_at = Column(DateTime, default=func.now(), nullable=False)
|
|
|
|
# When the secret expires
|
|
expires_at = Column(DateTime, nullable=False)
|
|
|
|
# Whether the secret has been viewed
|
|
is_viewed = Column(Boolean, default=False, nullable=False)
|
|
|
|
@classmethod
|
|
def create_with_ttl(cls, content: str, ttl_hours: int = None):
|
|
"""
|
|
Create a new secret with TTL (time-to-live).
|
|
|
|
Args:
|
|
content: The secret content to store
|
|
ttl_hours: Time-to-live in hours (default: settings.DEFAULT_SECRET_TTL_HOURS)
|
|
|
|
Returns:
|
|
A new Secret instance
|
|
"""
|
|
if ttl_hours is None:
|
|
ttl_hours = settings.DEFAULT_SECRET_TTL_HOURS
|
|
|
|
# Ensure TTL doesn't exceed maximum
|
|
ttl_hours = min(ttl_hours, settings.MAX_SECRET_TTL_HOURS)
|
|
|
|
# Calculate expiration time
|
|
expires_at = datetime.utcnow() + timedelta(hours=ttl_hours)
|
|
|
|
return cls(
|
|
content=content,
|
|
expires_at=expires_at
|
|
)
|
|
|
|
def is_expired(self) -> bool:
|
|
"""Check if the secret is expired."""
|
|
return datetime.utcnow() > self.expires_at |