DBFahrerKaterchen3 commited on
Commit
e8b014c
·
verified ·
1 Parent(s): 13b777f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -2
app.py CHANGED
@@ -1,7 +1,7 @@
1
- from fastapi import FastAPI, Form
2
  from fastapi.responses import FileResponse
3
  from fastapi.staticfiles import StaticFiles
4
- import requests
5
 
6
  app = FastAPI()
7
 
@@ -23,4 +23,115 @@ def send(name: str = Form(...), message: str = Form(...)):
23
  requests.post(WEBHOOK, json={"content": f"# Neue Kontaktanfrage\n**Von** {name}:\n**Nachricht**:\n{message}"})
24
  return {"status": "ok"}
25
 
 
 
 
 
26
  app.mount("/static", StaticFiles(directory="static"), name="static")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Form, HTTPException
2
  from fastapi.responses import FileResponse
3
  from fastapi.staticfiles import StaticFiles
4
+ import requests, json, os, bcrypt, random, string
5
 
6
  app = FastAPI()
7
 
 
23
  requests.post(WEBHOOK, json={"content": f"# Neue Kontaktanfrage\n**Von** {name}:\n**Nachricht**:\n{message}"})
24
  return {"status": "ok"}
25
 
26
+
27
+ # -----------------------------
28
+ # STATIC FILES
29
+ # -----------------------------
30
  app.mount("/static", StaticFiles(directory="static"), name="static")
31
+
32
+
33
+ # -----------------------------
34
+ # LOGIN SYSTEM
35
+ # -----------------------------
36
+
37
+ DB_FILE = "users.json"
38
+
39
+ # Falls Datei nicht existiert → erstellen
40
+ if not os.path.exists(DB_FILE):
41
+ with open(DB_FILE, "w") as f:
42
+ json.dump({"users": {}, "roblox_codes": {}}, f)
43
+
44
+
45
+ def load_db():
46
+ with open(DB_FILE, "r") as f:
47
+ return json.load(f)
48
+
49
+
50
+ def save_db(data):
51
+ with open(DB_FILE, "w") as f:
52
+ json.dump(data, f, indent=4)
53
+
54
+
55
+ # -----------------------------
56
+ # EMAIL REGISTER
57
+ # -----------------------------
58
+ @app.post("/auth/email/register")
59
+ def register(user: dict):
60
+ db = load_db()
61
+ email = user["email"]
62
+ password = user["password"]
63
+
64
+ if email in db["users"]:
65
+ raise HTTPException(400, "Email already exists")
66
+
67
+ hashed = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
68
+ db["users"][email] = {"password": hashed}
69
+ save_db(db)
70
+
71
+ return {"success": True}
72
+
73
+
74
+ # -----------------------------
75
+ # EMAIL LOGIN
76
+ # -----------------------------
77
+ @app.post("/auth/email/login")
78
+ def login(user: dict):
79
+ db = load_db()
80
+ email = user["email"]
81
+ password = user["password"]
82
+
83
+ if email not in db["users"]:
84
+ raise HTTPException(400, "User not found")
85
+
86
+ hashed = db["users"][email]["password"].encode()
87
+
88
+ if not bcrypt.checkpw(password.encode(), hashed):
89
+ raise HTTPException(400, "Wrong password")
90
+
91
+ return {"success": True, "token": email}
92
+
93
+
94
+ # -----------------------------
95
+ # ROBLOX LOGIN START
96
+ # -----------------------------
97
+ @app.post("/auth/roblox/start")
98
+ def roblox_start(data: dict):
99
+ username = data["username"]
100
+
101
+ code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
102
+
103
+ db = load_db()
104
+ db["roblox_codes"][username] = code
105
+ save_db(db)
106
+
107
+ return {"code": code}
108
+
109
+
110
+ # -----------------------------
111
+ # ROBLOX VERIFY
112
+ # -----------------------------
113
+ @app.post("/auth/roblox/verify")
114
+ def roblox_verify(data: dict):
115
+ username = data["username"]
116
+
117
+ db = load_db()
118
+ if username not in db["roblox_codes"]:
119
+ raise HTTPException(400, "No verification started")
120
+
121
+ code = db["roblox_codes"][username]
122
+
123
+ # Roblox API: User suchen
124
+ r = requests.get(f"https://users.roblox.com/v1/users/search?keyword={username}")
125
+ results = r.json().get("data", [])
126
+ if not results:
127
+ raise HTTPException(400, "User not found")
128
+
129
+ user_id = results[0]["id"]
130
+
131
+ # Beschreibung abrufen
132
+ desc = requests.get(f"https://users.roblox.com/v1/users/{user_id}").json().get("description", "")
133
+
134
+ if code in desc:
135
+ return {"success": True, "token": username}
136
+
137
+ raise HTTPException(400, "Verification failed")