2023-10-17 19:33:31 +01:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import secrets
|
|
|
|
from functools import wraps
|
|
|
|
from hashlib import sha256
|
|
|
|
from pathlib import Path, PurePath
|
|
|
|
from time import time
|
|
|
|
|
|
|
|
import msgspec
|
|
|
|
|
|
|
|
|
|
|
|
class Config(msgspec.Struct):
|
2023-10-19 00:06:14 +01:00
|
|
|
path: Path
|
|
|
|
listen: str
|
2023-10-17 19:33:31 +01:00
|
|
|
secret: str = secrets.token_hex(12)
|
|
|
|
public: bool = False
|
2023-11-08 23:00:07 +00:00
|
|
|
name: str = ""
|
2023-10-17 19:33:31 +01:00
|
|
|
users: dict[str, User] = {}
|
|
|
|
links: dict[str, Link] = {}
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
class User(msgspec.Struct, omit_defaults=True):
|
|
|
|
privileged: bool = False
|
|
|
|
hash: str = ""
|
2023-11-08 20:38:40 +00:00
|
|
|
lastSeen: int = 0 # noqa: N815
|
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
|
|
|
|
class Link(msgspec.Struct, omit_defaults=True):
|
|
|
|
location: str
|
|
|
|
creator: str = ""
|
|
|
|
expires: int = 0
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
config = None
|
|
|
|
conffile = Path.home() / ".local/share/cista/db.toml"
|
2023-10-17 19:33:31 +01:00
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
def derived_secret(*params, len=8) -> bytes:
|
|
|
|
"""Used to derive secret keys from the main secret"""
|
|
|
|
# Each part is made the same length by hashing first
|
|
|
|
combined = b"".join(
|
2023-10-26 15:18:59 +01:00
|
|
|
sha256(p if isinstance(p, bytes) else f"{p}".encode()).digest()
|
2023-10-17 19:33:31 +01:00
|
|
|
for p in [config.secret, *params]
|
|
|
|
)
|
|
|
|
# Output a bytes of the desired length
|
|
|
|
return sha256(combined).digest()[:len]
|
|
|
|
|
|
|
|
|
|
|
|
def enc_hook(obj):
|
|
|
|
if isinstance(obj, PurePath):
|
|
|
|
return obj.as_posix()
|
|
|
|
raise TypeError
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
def dec_hook(typ, obj):
|
|
|
|
if typ is Path:
|
|
|
|
return Path(obj)
|
|
|
|
raise TypeError
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
def config_update(modify):
|
|
|
|
global config
|
2023-10-19 00:06:14 +01:00
|
|
|
if not conffile.exists():
|
|
|
|
conffile.parent.mkdir(parents=True, exist_ok=True)
|
2023-10-17 19:33:31 +01:00
|
|
|
tmpname = conffile.with_suffix(".tmp")
|
|
|
|
try:
|
|
|
|
f = tmpname.open("xb")
|
|
|
|
except FileExistsError:
|
|
|
|
if tmpname.stat().st_mtime < time() - 1:
|
|
|
|
tmpname.unlink()
|
|
|
|
return "collision"
|
|
|
|
try:
|
|
|
|
# Load, modify and save with atomic replace
|
|
|
|
try:
|
|
|
|
old = conffile.read_bytes()
|
|
|
|
c = msgspec.toml.decode(old, type=Config, dec_hook=dec_hook)
|
|
|
|
except FileNotFoundError:
|
2023-10-19 00:06:14 +01:00
|
|
|
# No existing config file, make sure we have a folder...
|
|
|
|
confdir = conffile.parent
|
|
|
|
confdir.mkdir(parents=True, exist_ok=True)
|
|
|
|
confdir.chmod(0o700)
|
2023-10-17 19:33:31 +01:00
|
|
|
old = b""
|
2023-10-19 00:06:14 +01:00
|
|
|
c = None
|
2023-10-17 19:33:31 +01:00
|
|
|
c = modify(c)
|
|
|
|
new = msgspec.toml.encode(c, enc_hook=enc_hook)
|
|
|
|
if old == new:
|
|
|
|
f.close()
|
|
|
|
tmpname.unlink()
|
|
|
|
config = c
|
|
|
|
return "read"
|
|
|
|
f.write(new)
|
|
|
|
f.close()
|
|
|
|
tmpname.rename(conffile) # Atomic replace
|
|
|
|
except:
|
|
|
|
f.close()
|
|
|
|
tmpname.unlink()
|
|
|
|
raise
|
|
|
|
config = c
|
|
|
|
return "modified" if old else "created"
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
def modifies_config(modify):
|
|
|
|
"""Decorator for functions that modify the config file"""
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
@wraps(modify)
|
|
|
|
def wrapper(*args, **kwargs):
|
2023-10-26 15:18:59 +01:00
|
|
|
def m(c):
|
|
|
|
return modify(c, *args, **kwargs)
|
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
# Retry modification in case of write collision
|
|
|
|
while (c := config_update(m)) == "collision":
|
|
|
|
time.sleep(0.01)
|
|
|
|
return c
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-17 19:33:31 +01:00
|
|
|
return wrapper
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
def load_config():
|
|
|
|
global config
|
|
|
|
config = msgspec.toml.decode(conffile.read_bytes(), type=Config, dec_hook=dec_hook)
|
2023-10-17 19:33:31 +01:00
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 00:06:14 +01:00
|
|
|
@modifies_config
|
2023-10-19 02:06:21 +01:00
|
|
|
def update_config(conf: Config, changes: dict) -> Config:
|
2023-10-19 00:06:14 +01:00
|
|
|
"""Create/update the config with new values, respecting changes done by others."""
|
|
|
|
# Encode into dict, update values with new, convert to Config
|
2023-10-19 02:06:21 +01:00
|
|
|
settings = {} if conf is None else msgspec.to_builtins(conf, enc_hook=enc_hook)
|
2023-10-19 00:06:14 +01:00
|
|
|
settings.update(changes)
|
|
|
|
return msgspec.convert(settings, Config, dec_hook=dec_hook)
|
2023-10-19 02:06:21 +01:00
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 02:06:21 +01:00
|
|
|
@modifies_config
|
|
|
|
def update_user(conf: Config, name: str, changes: dict) -> Config:
|
|
|
|
"""Create/update a user with new values, respecting changes done by others."""
|
|
|
|
# Encode into dict, update values with new, convert to Config
|
|
|
|
try:
|
|
|
|
u = conf.users[name].__copy__()
|
|
|
|
except KeyError:
|
|
|
|
u = User()
|
|
|
|
if "password" in changes:
|
|
|
|
from . import auth
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 02:06:21 +01:00
|
|
|
auth.set_password(u, changes["password"])
|
|
|
|
del changes["password"]
|
|
|
|
udict = msgspec.to_builtins(u, enc_hook=enc_hook)
|
|
|
|
udict.update(changes)
|
|
|
|
settings = msgspec.to_builtins(conf, enc_hook=enc_hook)
|
|
|
|
settings["users"][name] = msgspec.convert(udict, User, dec_hook=dec_hook)
|
|
|
|
return msgspec.convert(settings, Config, dec_hook=dec_hook)
|
|
|
|
|
2023-10-26 15:18:59 +01:00
|
|
|
|
2023-10-19 02:06:21 +01:00
|
|
|
@modifies_config
|
|
|
|
def del_user(conf: Config, name: str) -> Config:
|
|
|
|
"""Delete named user account."""
|
|
|
|
ret = conf.__copy__()
|
|
|
|
ret.users.pop(name)
|
|
|
|
return ret
|