Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 46d7546

Browse files
author
berrysauce
committed
Added Docstrings and changed web design
1 parent a3bd391 commit 46d7546

File tree

11 files changed

+149
-17
lines changed

11 files changed

+149
-17
lines changed

main.py

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from deta import Deta
1212
from dotenv import load_dotenv
1313
import os
14-
from tools import hashing, dnsverify
14+
from tools import hashing, dnsverify, htmlgen
1515
from datetime import datetime, timedelta
1616
import sentry_sdk
1717
import requests
@@ -53,6 +53,10 @@
5353
# FUNCTION AND CLASS DECLARATION ###############################
5454

5555
def createtoken(data: dict, expires_delta: Optional[timedelta] = None):
56+
"""
57+
Creates JWT token
58+
"""
59+
5660
to_encode = data.copy()
5761
if expires_delta:
5862
expire = datetime.utcnow() + expires_delta
@@ -63,6 +67,10 @@ def createtoken(data: dict, expires_delta: Optional[timedelta] = None):
6367
return encoded_jwt
6468

6569
def get_current_user(access_token: Optional[str] = Cookie(None)):
70+
"""
71+
Gets current user by JWT token
72+
"""
73+
6674
if access_token is None:
6775
#raise HTTPException(status_code=401, detail="Unauthorized. Authentication failed.")
6876
return None
@@ -80,6 +88,10 @@ def get_current_user(access_token: Optional[str] = Cookie(None)):
8088
return site["key"]
8189

8290
def verifycaptcha(response: str):
91+
"""
92+
Verifies that hCaptcha result is valid
93+
"""
94+
8395
data = {"secret": str(os.getenv("CAPTCHA_SECRET")), "response": response}
8496
r = requests.post("https://hcaptcha.com/siteverify", data=data)
8597
if r.status_code == 200 and json.loads(r.text)["success"] == True:
@@ -114,8 +126,19 @@ def privacy(request: Request):
114126
return templates.TemplateResponse("privacy.html", {"request": request})
115127

116128
@app.get("/login")
117-
def login(request: Request):
118-
return templates.TemplateResponse("login.html", {"request": request})
129+
def login(request: Request, error: Optional[str] = None):
130+
if error == None:
131+
popup = ""
132+
elif error == "auth":
133+
popup = htmlgen.loginalert("Wrong domain/password.")
134+
elif error == "captcha":
135+
popup = htmlgen.loginalert("Captcha failed.")
136+
elif error == "locked":
137+
popup = htmlgen.loginalert("This account was locked. Contact support for further details.")
138+
else:
139+
popup = htmlgen.loginalert("Login error.")
140+
141+
return templates.TemplateResponse("login.html", {"request": request, "login_alert": popup})
119142

120143
@app.get("/signup")
121144
def signup(request: Request):
@@ -149,23 +172,27 @@ def dashboard(request: Request, key: str = Depends(get_current_user)):
149172
"""
150173
@app.post("/login/auth")
151174
def loginauth(response: Response, username: str = Form(...), password: str = Form(...), captcha: str = Form(None, alias="h-captcha-response")):
175+
"""
176+
Handles user authentication
177+
"""
178+
152179
domain = username
153180
if captcha is None:
154-
raise HTTPException(status_code=401, detail="Unauthorized. Captcha failed.")
181+
return RedirectResponse(url=f"/login?error=captcha", status_code=status.HTTP_303_SEE_OTHER)
155182
else:
156183
if verifycaptcha(captcha) is False:
157-
raise HTTPException(status_code=401, detail="Unauthorized. Captcha failed.")
184+
return RedirectResponse(url=f"/login?error=captcha", status_code=status.HTTP_303_SEE_OTHER)
158185

159186
try:
160187
site = sitesdb.fetch({"domain": domain}).items[0]
161188
except IndexError:
162-
raise HTTPException(status_code=401, detail="Unauthorized. Authentication failed.")
189+
return RedirectResponse(url=f"/login?error=auth", status_code=status.HTTP_303_SEE_OTHER)
163190

164191
if hashing.verifypw(site["password"], password) is False:
165-
raise HTTPException(status_code=401, detail="Unauthorized. Authentication failed.")
192+
return RedirectResponse(url=f"/login?error=auth", status_code=status.HTTP_303_SEE_OTHER)
166193

167194
if site["locked"] is True:
168-
raise HTTPException(status_code=401, detail="This account was locked. Contact support for further details.")
195+
return RedirectResponse(url=f"/login?error=locked", status_code=status.HTTP_303_SEE_OTHER)
169196

170197
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
171198
access_token = createtoken(
@@ -178,12 +205,20 @@ def loginauth(response: Response, username: str = Form(...), password: str = For
178205

179206
@app.get("/logout")
180207
def logout(response: Response, key: str = Depends(get_current_user)):
208+
"""
209+
Logs out user
210+
"""
211+
181212
response = RedirectResponse(url="/login", status_code=status.HTTP_303_SEE_OTHER)
182213
response.delete_cookie(key="access_token")
183214
return response
184215

185216
@app.post("/login/create")
186217
def create(username: str = Form(...), password: str = Form(...), captcha: str = Form(None, alias="h-captcha-response")):
218+
"""
219+
Creates a new user
220+
"""
221+
187222
if captcha is None:
188223
raise HTTPException(status_code=401, detail="Unauthorized. Captcha failed.")
189224
else:
@@ -226,6 +261,10 @@ def create(username: str = Form(...), password: str = Form(...), captcha: str =
226261

227262
@app.post("/login/reset")
228263
def loginreset(username: str = Form(...), captcha: str = Form(None, alias="h-captcha-response")):
264+
"""
265+
Handles password resets
266+
"""
267+
229268
domain = username
230269
try:
231270
site = sitesdb.fetch({"domain": domain}).items[0]
@@ -246,6 +285,10 @@ def loginreset(username: str = Form(...), captcha: str = Form(None, alias="h-cap
246285

247286
@app.get("/login/reset/check")
248287
def loginresetcheck(key: str):
288+
"""
289+
Checks if the correct TXT DNS record to reset the user's password is set
290+
"""
291+
249292
verification = dnsverify.verify(key)
250293
if verification is None:
251294
raise HTTPException(status_code=404, detail="Reset key not found")
@@ -257,6 +300,10 @@ def loginresetcheck(key: str):
257300

258301
@app.post("/login/reset/set")
259302
def loginset(key: str, password: str = Form(...)):
303+
"""
304+
Sets a new account password
305+
"""
306+
260307
verification = dnsverify.get(key)
261308
if verification is None:
262309
raise HTTPException(status_code=404, detail="Reset key not found")
@@ -276,6 +323,10 @@ def loginset(key: str, password: str = Form(...)):
276323

277324
@app.get("/forgot/2")
278325
def forgot2(request: Request, key: str):
326+
"""
327+
Displays fogot (step 2) page
328+
"""
329+
279330
verification = dnsverify.get(key)
280331
if verification is None:
281332
raise HTTPException(status_code=404, detail="Reset key not found")
@@ -288,6 +339,10 @@ def forgot2(request: Request, key: str):
288339

289340
@app.get("/forgot/3")
290341
def forgot3(request: Request, key: str):
342+
"""
343+
Displays fogot (step 3) page
344+
"""
345+
291346
verification = dnsverify.get(key)
292347
dt = datetime.now()
293348
if verification is None:
@@ -313,6 +368,10 @@ def forgot3(request: Request, key: str):
313368

314369
@app.get("/api/{key}/widget.js")
315370
def widgetjs(key: str):
371+
"""
372+
Creates and serves widget for site
373+
"""
374+
316375
# add site key to widget code
317376
site = sitesdb.get(key)
318377
if site is False or site is None:
@@ -329,6 +388,10 @@ def widgetjs(key: str):
329388

330389
@app.get("/api/{key}/report/{feedback}")
331390
def getfeedback(key: str, feedback: str, origin: Optional[str] = Header(None)):
391+
"""
392+
Receives and stores feedback reported from the widget
393+
"""
394+
332395
site = sitesdb.get(key)
333396
if not site:
334397
raise HTTPException(status_code=404, detail="Site key not found")
@@ -378,6 +441,10 @@ def getfeedback(key: str, feedback: str, origin: Optional[str] = Header(None)):
378441

379442
@app.post("/api/settings")
380443
def widgetsettings(key: str = Depends(get_current_user), text: str = Form(...)):
444+
"""
445+
Changes account settings
446+
"""
447+
381448
site = sitesdb.get(key)
382449
if site is False:
383450
raise HTTPException(status_code=404, detail="Site key not found")
@@ -389,6 +456,10 @@ def widgetsettings(key: str = Depends(get_current_user), text: str = Form(...)):
389456

390457
@app.get("/api/clear")
391458
def clearwidget(key: str = Depends(get_current_user)):
459+
"""
460+
Clears the account's feedback
461+
"""
462+
392463
site = sitesdb.get(key)
393464
if site is False:
394465
raise HTTPException(status_code=404, detail="Site key not found")
@@ -403,6 +474,10 @@ def clearwidget(key: str = Depends(get_current_user)):
403474

404475
@app.get("/api/delete")
405476
def deletewidget(key: str = Depends(get_current_user)):
477+
"""
478+
Deletes the account
479+
"""
480+
406481
site = sitesdb.get(key)
407482
if site is False:
408483
raise HTTPException(status_code=404, detail="Site key not found")

templates/dashboard.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
};
5050
};
5151
</script>
52-
</div></div></section><footer class="footer-basic" style="margin-top: -15px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
52+
</div></div></section><footer class="footer-basic" style="margin-top: -15px;padding-right: 20px;padding-left: 20px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
5353
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
5454
<circle cx="12" cy="12" r="4"></circle>
5555
</svg>&nbsp;Service Status</a></li><li class="list-inline-item"><a href="/terms">Terms</a></li><li class="list-inline-item"><a href="/privacy">Privacy</a></li></ul><p class="copyright">reacty by berrysauce © 2021</p></footer><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script><script src="assets/js/script.min.js"></script></body></html>

templates/forgot-step1.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<meta name="theme-color" content="#ffffff">
1111
<!-- Cloudflare Web Analytics --><script defer src='https://static.cloudflareinsights.com/beacon.min.js' data-cf-beacon='{"token": "c5d1704791d642f9a64b5086a05c691a"}'></script><!-- End Cloudflare Web Analytics -->
1212
<script src="https://cdnjs.cloudflare.com/ajax/libs/zxcvbn/4.2.0/zxcvbn.js"></script>
13-
</head><body style="font-family: Inter, sans-serif;"><section class="highlight-clean" style="min-height: 80vh;"><div class="container d-flex d-xl-flex justify-content-center justify-content-xl-center align-items-xl-center" style="margin-bottom: 60px;"><nav class="navbar navbar-light navbar-expand-md navigation-clean" style="min-width: 100%;max-width: 100%;"><div class="container"><a class="navbar-brand" href="/" style="margin-left: -20px;"><img src="assets/img/logo-black.png" style="width: 135px;margin-left: 0px;"></a><button data-bs-toggle="collapse" class="navbar-toggler" data-bs-target="#navcol-1"><span class="visually-hidden">Toggle navigation</span><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navcol-1"><ul class="navbar-nav ms-auto"><li class="nav-item"><a class="nav-link" href="/" style="color: rgb(0,108,11)!important;"><strong>Get started</strong></a></li><li class="nav-item"><a class="nav-link" href="https://docs.reacty.net" style="color: rgb(0,108,11)!important;" target="_blank"><strong>Help</strong></a></li><li class="nav-item"><a class="nav-link" href="/login" style="color: rgb(0,108,11)!important;"><strong>Log in</strong></a></li><li class="nav-item"><a class="nav-link" href="/signup" style="color: rgb(0,108,11)!important;border-radius: 10px;background: #a0f9a9;margin-left: 10px;margin-right: -10px;"><strong>Sign up</strong></a></li></ul></div></div></nav></div><div class="container" style="margin-top: 80px;margin-bottom: 100px;"><div class="intro"><h2 class="text-start" style="margin-bottom: 5px;color: rgb(0,0,0);">Reset your Password</h2><p class="text-start">Forgot your password? No problem! Just enter your domain below and you'll be guided through our password reset procedure.</p></div><div class="intro"><form action="/login/reset" method="post" enctype="multipart/form-data"><small class="form-text">Domain (with http:// or https://)</small><input class="form-control" type="url" style="margin-bottom: 10px;" name="username" placeholder="Domain" required=""><div class="h-captcha" data-sitekey="6bd5882a-dcaa-44a6-a43b-1588bc6e14be"></div><div class="row" style="margin-top: 25px;"><div class="col"><button class="btn btn-primary" type="submit" style="border-radius: 10px;background: rgb(160,249,169);color: rgb(0,108,11)!important;border-width: 0px;border-right-width: 0px;border-right-style: none;"><strong>Reset Password</strong></button></div></div></form></div></div></section><footer class="footer-basic" style="margin-top: -15px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
13+
</head><body style="font-family: Inter, sans-serif;"><section class="highlight-clean" style="min-height: 80vh;"><div class="container d-flex d-xl-flex justify-content-center justify-content-xl-center align-items-xl-center" style="margin-bottom: 60px;"><nav class="navbar navbar-light navbar-expand-md navigation-clean" style="min-width: 100%;max-width: 100%;"><div class="container"><a class="navbar-brand" href="/" style="margin-left: -20px;"><img src="assets/img/logo-black.png" style="width: 135px;margin-left: 0px;"></a><button data-bs-toggle="collapse" class="navbar-toggler" data-bs-target="#navcol-1"><span class="visually-hidden">Toggle navigation</span><span class="navbar-toggler-icon"></span></button><div class="collapse navbar-collapse" id="navcol-1"><ul class="navbar-nav ms-auto"><li class="nav-item"><a class="nav-link" href="/" style="color: rgb(0,108,11)!important;"><strong>Get started</strong></a></li><li class="nav-item"><a class="nav-link" href="https://docs.reacty.net" style="color: rgb(0,108,11)!important;" target="_blank"><strong>Help</strong></a></li><li class="nav-item"><a class="nav-link" href="/login" style="color: rgb(0,108,11)!important;"><strong>Log in</strong></a></li><li class="nav-item"><a class="nav-link" href="/signup" style="color: rgb(0,108,11)!important;border-radius: 10px;background: #a0f9a9;margin-left: 10px;margin-right: -10px;"><strong>Sign up</strong></a></li></ul></div></div></nav></div><div class="container" style="margin-top: 80px;margin-bottom: 100px;"><div class="intro"><h2 class="text-start" style="margin-bottom: 5px;color: rgb(0,0,0);">Reset your Password</h2><p class="text-start">Forgot your password? No problem! Just enter your domain below and you'll be guided through our password reset procedure.</p></div><div class="intro"><form action="/login/reset" method="post" enctype="multipart/form-data"><small class="form-text">Domain (with http:// or https://)</small><input class="form-control" type="url" style="margin-bottom: 10px;" name="username" placeholder="Domain" required=""><div class="h-captcha" data-sitekey="6bd5882a-dcaa-44a6-a43b-1588bc6e14be"></div><div class="row" style="margin-top: 25px;"><div class="col"><button class="btn btn-primary" type="submit" style="border-radius: 10px;background: rgb(160,249,169);color: rgb(0,108,11)!important;border-width: 0px;border-right-width: 0px;border-right-style: none;"><strong>Reset Password</strong></button></div></div></form></div></div></section><footer class="footer-basic" style="margin-top: -15px;padding-right: 20px;padding-left: 20px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
1414
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
1515
<circle cx="12" cy="12" r="4"></circle>
1616
</svg>&nbsp;Service Status</a></li><li class="list-inline-item"><a href="/terms">Terms</a></li><li class="list-inline-item"><a href="/privacy">Privacy</a></li></ul><p class="copyright">reacty by berrysauce © 2021</p></footer><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script><script src="assets/js/script.min.js"></script></body></html>

templates/forgot-step2.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
document.getElementById("failedAlert").style.display = "block";
5050
});
5151
}, 10000);
52-
</script></div></div></section><footer class="footer-basic" style="margin-top: -15px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
52+
</script></div></div></section><footer class="footer-basic" style="margin-top: -15px;padding-right: 20px;padding-left: 20px;"><ul class="list-inline" style="font-size: 14px;"><li class="list-inline-item"><a href="/">Home</a></li><li class="list-inline-item"><a href="https://docs.reacty.net" target="_blank">Help</a></li><li class="list-inline-item"><a href="https://github.com/berrysauce/reacty" target="_blank">GitHub</a></li><li class="list-inline-item"><a href="https://status.reacty.net" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round" class="icon icon-tabler icon-tabler-point" style="color: rgb(7,214,28);font-size: 20px;margin-bottom: 2px;">
5353
<path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
5454
<circle cx="12" cy="12" r="4"></circle>
5555
</svg>&nbsp;Service Status</a></li><li class="list-inline-item"><a href="/terms">Terms</a></li><li class="list-inline-item"><a href="/privacy">Privacy</a></li></ul><p class="copyright">reacty by berrysauce © 2021</p></footer><script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script><script src="assets/js/script.min.js"></script></body></html>

0 commit comments

Comments
 (0)