2022-06-22 18:11:22 +00:00
|
|
|
import os
|
2022-07-11 07:34:06 +00:00
|
|
|
import secrets
|
2022-06-22 18:11:22 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import bcrypt
|
2022-07-11 07:34:06 +00:00
|
|
|
import itsdangerous
|
2022-06-22 18:11:22 +00:00
|
|
|
import pydantic
|
|
|
|
import tomli
|
|
|
|
from fastapi import Form
|
|
|
|
from fastapi import HTTPException
|
|
|
|
from fastapi import Request
|
2022-07-11 07:34:06 +00:00
|
|
|
from itsdangerous import URLSafeTimedSerializer
|
|
|
|
from loguru import logger
|
2022-06-22 18:11:22 +00:00
|
|
|
|
2022-06-27 18:55:44 +00:00
|
|
|
from app.utils.emoji import _load_emojis
|
|
|
|
|
2022-06-22 18:11:22 +00:00
|
|
|
ROOT_DIR = Path().parent.resolve()
|
|
|
|
|
2022-07-07 18:37:16 +00:00
|
|
|
_CONFIG_FILE = os.getenv("MICROBLOGPUB_CONFIG_FILE", "profile.toml")
|
2022-06-22 18:11:22 +00:00
|
|
|
|
2022-07-18 18:44:55 +00:00
|
|
|
VERSION_COMMIT = "dev"
|
|
|
|
|
|
|
|
try:
|
|
|
|
from app._version import VERSION_COMMIT # type: ignore
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
2022-07-04 19:45:23 +00:00
|
|
|
|
|
|
|
VERSION = f"2.0.0+{VERSION_COMMIT}"
|
2022-06-22 18:11:22 +00:00
|
|
|
USER_AGENT = f"microblogpub/{VERSION}"
|
|
|
|
AP_CONTENT_TYPE = "application/activity+json"
|
|
|
|
|
|
|
|
|
2022-08-04 05:31:18 +00:00
|
|
|
class _PrivacyReplace(pydantic.BaseModel):
|
|
|
|
domain: str
|
|
|
|
replace_by: str
|
|
|
|
|
|
|
|
|
2022-08-10 06:58:18 +00:00
|
|
|
class _ProfileMetadata(pydantic.BaseModel):
|
|
|
|
key: str
|
|
|
|
value: str
|
|
|
|
|
|
|
|
|
2022-06-22 18:11:22 +00:00
|
|
|
class Config(pydantic.BaseModel):
|
|
|
|
domain: str
|
|
|
|
username: str
|
|
|
|
admin_password: bytes
|
|
|
|
name: str
|
|
|
|
summary: str
|
|
|
|
https: bool
|
|
|
|
icon_url: str
|
|
|
|
secret: str
|
|
|
|
debug: bool = False
|
2022-07-30 06:46:29 +00:00
|
|
|
trusted_hosts: list[str] = ["127.0.0.1"]
|
2022-08-02 18:14:40 +00:00
|
|
|
manually_approves_followers: bool = False
|
2022-08-04 05:31:18 +00:00
|
|
|
privacy_replace: list[_PrivacyReplace] | None = None
|
2022-08-10 06:58:18 +00:00
|
|
|
metadata: list[_ProfileMetadata] | None = None
|
2022-08-04 17:10:57 +00:00
|
|
|
code_highlighting_theme = "friendly_grayscale"
|
2022-06-22 18:11:22 +00:00
|
|
|
|
|
|
|
# Config items to make tests easier
|
2022-06-29 18:43:17 +00:00
|
|
|
sqlalchemy_database: str | None = None
|
2022-06-22 18:11:22 +00:00
|
|
|
key_path: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
def load_config() -> Config:
|
|
|
|
try:
|
|
|
|
return Config.parse_obj(
|
|
|
|
tomli.loads((ROOT_DIR / "data" / _CONFIG_FILE).read_text())
|
|
|
|
)
|
|
|
|
except FileNotFoundError:
|
2022-06-22 18:48:48 +00:00
|
|
|
raise ValueError(
|
|
|
|
f"Please run the configuration wizard, {_CONFIG_FILE} is missing"
|
|
|
|
)
|
2022-06-22 18:11:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_activitypub_requested(req: Request) -> bool:
|
|
|
|
accept_value = req.headers.get("accept")
|
|
|
|
if not accept_value:
|
|
|
|
return False
|
|
|
|
for val in {
|
|
|
|
"application/ld+json",
|
|
|
|
"application/activity+json",
|
|
|
|
}:
|
|
|
|
if accept_value.startswith(val):
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def verify_password(pwd: str) -> bool:
|
|
|
|
return bcrypt.checkpw(pwd.encode(), CONFIG.admin_password)
|
|
|
|
|
|
|
|
|
|
|
|
CONFIG = load_config()
|
|
|
|
DOMAIN = CONFIG.domain
|
|
|
|
_SCHEME = "https" if CONFIG.https else "http"
|
|
|
|
ID = f"{_SCHEME}://{DOMAIN}"
|
|
|
|
USERNAME = CONFIG.username
|
2022-08-02 18:14:40 +00:00
|
|
|
MANUALLY_APPROVES_FOLLOWERS = CONFIG.manually_approves_followers
|
2022-08-04 05:31:18 +00:00
|
|
|
PRIVACY_REPLACE = None
|
|
|
|
if CONFIG.privacy_replace:
|
|
|
|
PRIVACY_REPLACE = {pr.domain: pr.replace_by for pr in CONFIG.privacy_replace}
|
2022-06-22 18:11:22 +00:00
|
|
|
BASE_URL = ID
|
|
|
|
DEBUG = CONFIG.debug
|
2022-06-29 18:43:17 +00:00
|
|
|
DB_PATH = CONFIG.sqlalchemy_database or ROOT_DIR / "data" / "microblogpub.db"
|
|
|
|
SQLALCHEMY_DATABASE_URL = f"sqlite:///{DB_PATH}"
|
2022-06-22 18:11:22 +00:00
|
|
|
KEY_PATH = (
|
|
|
|
(ROOT_DIR / CONFIG.key_path) if CONFIG.key_path else ROOT_DIR / "data" / "key.pem"
|
|
|
|
)
|
2022-06-27 18:55:44 +00:00
|
|
|
EMOJIS = "😺 😸 😹 😻 😼 😽 🙀 😿 😾"
|
|
|
|
# Emoji template for the FE
|
|
|
|
EMOJI_TPL = '<img src="/static/twemoji/{filename}.svg" alt="{raw}" class="emoji">'
|
|
|
|
|
|
|
|
_load_emojis(ROOT_DIR, BASE_URL)
|
2022-06-22 18:11:22 +00:00
|
|
|
|
2022-08-04 17:10:57 +00:00
|
|
|
CODE_HIGHLIGHTING_THEME = CONFIG.code_highlighting_theme
|
2022-07-12 17:50:11 +00:00
|
|
|
|
2022-06-22 18:11:22 +00:00
|
|
|
|
2022-07-11 07:42:39 +00:00
|
|
|
session_serializer = URLSafeTimedSerializer(
|
|
|
|
CONFIG.secret,
|
|
|
|
salt=f"{ID}.session",
|
|
|
|
)
|
2022-07-11 07:34:06 +00:00
|
|
|
csrf_serializer = URLSafeTimedSerializer(
|
2022-07-11 07:42:39 +00:00
|
|
|
CONFIG.secret,
|
|
|
|
salt=f"{ID}.csrf",
|
2022-06-22 18:11:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_csrf_token() -> str:
|
2022-07-11 07:34:06 +00:00
|
|
|
return csrf_serializer.dumps(secrets.token_hex(16)) # type: ignore
|
2022-06-22 18:11:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def verify_csrf_token(csrf_token: str = Form()) -> None:
|
2022-07-11 07:34:06 +00:00
|
|
|
try:
|
2022-07-11 07:42:39 +00:00
|
|
|
csrf_serializer.loads(csrf_token, max_age=1800)
|
2022-07-11 07:34:06 +00:00
|
|
|
except (itsdangerous.BadData, itsdangerous.SignatureExpired):
|
|
|
|
logger.exception("Failed to verify CSRF token")
|
2022-06-22 18:11:22 +00:00
|
|
|
raise HTTPException(status_code=403, detail="CSRF error")
|
|
|
|
return None
|