added encryption

This commit is contained in:
2026-03-09 10:54:07 +05:30
parent 6e184dc590
commit 6720e28d08
27 changed files with 2093 additions and 709 deletions

View File

@@ -1,4 +1,4 @@
from pydantic import BaseModel, Field # type: ignore
from pydantic import BaseModel, Field # type: ignore
from datetime import datetime
from typing import Optional, List
from enum import Enum
@@ -85,35 +85,43 @@ class MoodEnum(str, Enum):
class EncryptionMetadata(BaseModel):
"""Optional encryption metadata for entries"""
encrypted: bool = False
iv: Optional[str] = None # Initialization vector as base64 string
algorithm: Optional[str] = None # e.g., "AES-256-GCM"
"""Encryption metadata for entries - zero-knowledge privacy"""
encrypted: bool = True
ciphertext: str # Base64-encoded encrypted content
nonce: str # Base64-encoded nonce used for encryption
algorithm: str = "XSalsa20-Poly1305" # crypto_secretbox algorithm
class Config:
json_schema_extra = {
"example": {
"encrypted": False,
"iv": None,
"algorithm": None
"encrypted": True,
"ciphertext": "base64_encoded_ciphertext...",
"nonce": "base64_encoded_nonce...",
"algorithm": "XSalsa20-Poly1305"
}
}
class JournalEntryCreate(BaseModel):
title: str
content: str
title: Optional[str] = None # Optional if encrypted
content: Optional[str] = None # Optional if encrypted
mood: Optional[MoodEnum] = None
tags: Optional[List[str]] = None
isPublic: Optional[bool] = False
entryDate: Optional[datetime] = None # Logical journal date; defaults to today
# Logical journal date; defaults to today
entryDate: Optional[datetime] = None
# Encryption metadata - present if entry is encrypted
encryption: Optional[EncryptionMetadata] = None
class Config:
json_schema_extra = {
"example": {
"title": "Today's Gratitude",
"content": "I'm grateful for...",
"encryption": {
"encrypted": True,
"ciphertext": "base64_ciphertext...",
"nonce": "base64_nonce...",
"algorithm": "XSalsa20-Poly1305"
},
"mood": "grateful",
"tags": ["work", "family"],
"isPublic": False,
@@ -142,15 +150,15 @@ class JournalEntryUpdate(BaseModel):
class JournalEntry(BaseModel):
id: str = Field(alias="_id")
userId: str # ObjectId as string
title: str
content: str
title: Optional[str] = None # None if encrypted
content: Optional[str] = None # None if encrypted
mood: Optional[MoodEnum] = None
tags: Optional[List[str]] = []
isPublic: bool = False
entryDate: datetime # Logical journal date
createdAt: datetime
updatedAt: datetime
encryption: EncryptionMetadata = Field(default_factory=lambda: EncryptionMetadata())
encryption: Optional[EncryptionMetadata] = None # Present if encrypted
class Config:
from_attributes = True
@@ -159,19 +167,18 @@ class JournalEntry(BaseModel):
"example": {
"_id": "507f1f77bcf86cd799439011",
"userId": "507f1f77bcf86cd799439012",
"title": "Today's Gratitude",
"content": "I'm grateful for...",
"encryption": {
"encrypted": True,
"ciphertext": "base64_ciphertext...",
"nonce": "base64_nonce...",
"algorithm": "XSalsa20-Poly1305"
},
"mood": "grateful",
"tags": ["work", "family"],
"isPublic": False,
"entryDate": "2026-03-05T00:00:00Z",
"createdAt": "2026-03-05T12:00:00Z",
"updatedAt": "2026-03-05T12:00:00Z",
"encryption": {
"encrypted": False,
"iv": None,
"algorithm": None
}
"updatedAt": "2026-03-05T12:00:00Z"
}
}