-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.py
More file actions
63 lines (52 loc) · 1.63 KB
/
init.py
File metadata and controls
63 lines (52 loc) · 1.63 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
import random
import json
import os
import getpass
import hashlib
import sys
try:
import jwt
except ModuleNotFoundError:
print(
"Error! Could not create JWT Tokens for you - run `pip3 install pyjwt` and try again"
)
sys.exit(1)
def generate_password():
alphabet = "qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM1234567890"
return "".join(random.sample(alphabet, k=40))
def get_postgrest_config(secret: str) -> str:
conf = (
f"""db-uri = "postgres://rest@db:5432/postgres"\n"""
f"""db-schema = "strichliste"\n"""
f"""db-anon-role = "web_anon"\n"""
f"""jwt-secret = "{secret}"\n"""
f"""max-rows = 200 \n"""
)
return conf
if __name__ == "__main__":
jwt_secret = generate_password()
with open("postgREST.conf", "w", os.O_CREAT) as f:
f.write(get_postgrest_config(jwt_secret))
# generate setup password
sha = hashlib.sha3_256()
sha.update(
getpass.getpass(
"Please Enter the Password used for setting up Tablets and backoffice access"
).encode("utf8")
)
password_sha3_256 = sha.hexdigest()
tokens = {
user: jwt.encode({"role": user}, jwt_secret, algorithm="HS256")
for user in ["order_user", "xxxx_user"]
}
with open("secrets.json", "w", os.O_CREAT) as f:
f.write(
json.dumps(
{
"jwt_secret": jwt_secret,
"password_sha3_256": password_sha3_256,
"tokens": tokens,
}
)
)
print("Congig generation successful! Secrets stored in 'secrets.json'.")