-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
82 lines (69 loc) · 2.94 KB
/
data.py
File metadata and controls
82 lines (69 loc) · 2.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# tried using SQL but i guess its ass cheeks so time to make my own with json (aka. the best data storage type)
from typing import Any
import datetime # i normally go with time but thats just not cool as this
import base64
import json # "in json we believe" - json cult /s
import os
VERSION = "API V2.3 STABLE (built 12:25 GMT+0 25/02/2026)"
BASEDIR = os.path.expanduser("~/app") # alwaysdata app folder
# BASEDIR = os.path.abspath(os.path.dirname(__file__)) # local testing dir
DOTENV_PATH = os.path.join(BASEDIR, ".env")
STORAGE = os.path.join(BASEDIR, "storage")
BASEMESSAGEDIR = os.path.join(STORAGE, "messages")
MESSAGECOUNTERDIR = os.path.join(BASEMESSAGEDIR, "messagecounter")
USERCOUNTERDIR = os.path.join(BASEMESSAGEDIR, "messagecounter")
USERDIR = os.path.join(STORAGE, "users")
DIHMDIR = os.path.join(STORAGE, "dihm")
AUTHCHALLENGEDIR = os.path.join(STORAGE, "challenge")
os.makedirs(BASEMESSAGEDIR, exist_ok=True)
os.makedirs(MESSAGECOUNTERDIR, exist_ok=True)
os.makedirs(STORAGE, exist_ok=True)
os.makedirs(USERDIR, exist_ok=True)
os.makedirs(AUTHCHALLENGEDIR, exist_ok=True)
def b64encodeUrlSafe(x: bytes) -> str:
return base64.urlsafe_b64encode(x).decode()
def b64decodeUrlSafe(s: str) -> bytes:
return base64.urlsafe_b64decode(s)
def str2byte(text: str) -> bytes:
byte = text.encode('utf-8')
return byte
def byte2str(bytetext: bytes) -> str:
text = bytetext.decode('utf-8')
return text
def byte2b64(bytetext: bytes) -> str:
return base64.b64encode(bytetext).decode()
def b642byte(b64text: str) -> bytes:
return base64.b64decode(b64text.encode())
def writejson(filepath: str, data: Any, indent: int = 4) -> None:
os.makedirs(os.path.dirname(filepath), exist_ok=True)
with open(filepath, "w", encoding="utf-8") as f:
json.dump(data, f, indent=indent)
def readjson(filepath: str) -> dict:
if not os.path.exists(filepath):
data = {}
writejson(filepath, data)
return data
with open(filepath, "r", encoding="utf-8") as f:
return json.load(f)
class UserExistsError(Exception):
pass
class UserClass:
def __init__(self, username: str, publickey_kyber: str, publickey_token: str, publickey_connection: str):
filepath = os.path.join(USERDIR, f"{username}-V1.json")
if os.path.exists(filepath):
raise UserExistsError(f"username {username} exists")
self.username = username
self.creation = datetime.datetime.now(datetime.timezone.utc)
self.publickey_kyber = publickey_kyber
self.publickey_token = publickey_token
self.publickey_connection = publickey_connection
def out(self):
return {
"ver": VERSION,
"type": "class User",
"username": self.username,
"publickey_kyber": self.publickey_kyber,
"publickey_token": self.publickey_token,
"publickey_connection": self.publickey_connection,
"creation": int(self.creation.timestamp())
}