-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
156 lines (132 loc) · 4.97 KB
/
main.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
from sanic import *
from sanic.response import text
from sanic_jinja2 import SanicJinja2
from jinja2 import FileSystemLoader
from datetime import datetime
import json, os
from functools import wraps
from api.jwt import verifyToken, createToken, extractToken
from api.account import *
app = Sanic(__name__)
app.static("static", os.path.abspath("static"))
# Create jinja object
jinja = SanicJinja2(
app, pkg_name="main", loader=FileSystemLoader(searchpath="template")
)
# Load configs
config = json.load(open("data/config.json", "r"))
app.config.URL = config["URL"]
app.config.KEY = config["KEY"] # Used for encrypting the jwt token
app.config.COOKIE = "razdor_dat"
# Create Decorators
# This one checks if they are trying to access the app without authorising
def authorized():
def decorator(f):
@wraps(f)
async def decorated_function(request, *args, **kwargs):
# run some method that checks the request
# for the client's authorization status
is_authorized = verifyToken(
app.config.KEY, request.cookies.get(app.config.COOKIE)
)
print(is_authorized)
if is_authorized:
# the user is authorized.
# run the handler method and return the response
response = await f(request, *args, **kwargs)
return response
else:
# the user is not authorized.
return redirect("/login")
return decorated_function
return decorator
# this one checks if they are authorised and are trying to login/register
def IsAuthed():
def decorator(f):
@wraps(f)
async def decorated_function(request, *args, **kwargs):
is_authorized = verifyToken(
app.config.KEY, request.cookies.get(app.config.COOKIE)
)
# Check if user is already authed and trying to login again
if is_authorized:
return redirect("/")
else:
response = await f(request, *args, **kwargs)
return response
return decorated_function
return decorator
@app.route("/")
@authorized()
async def index(request):
Ecookie = request.cookies.get(app.config.COOKIE)
cookie = extractToken(app.config.KEY, Ecookie)
return jinja.render("index.html", request, userdata=cookie["user"])
@app.route("/login", methods=["POST", "GET"])
@IsAuthed()
async def login(request):
if request.method == "POST":
username = request.form.get("username").split("#")
password = request.form.get("password")
try:
verify = GenAuthUsername(username[0], username[1], password, app.config.URL)
except IndexError:
return jinja.render(
"login.html",
request,
error="You have seemed to enter your username incorrectly.",
)
if verify["op"] == "Created.":
payload = {
"user": {
"id": verify["id"],
"username": username[0],
"discrim": username[1],
"password": password,
"authkey": verify["authentication"],
}
}
cookie = createToken(app.config.KEY, payload)
response = redirect("/")
response.add_cookie(
app.config.COOKIE, cookie, max_age=604800, httponly=False # One week
)
return response
elif verify["op"] == "void":
return jinja.render(
"login.html", request, error="Incorrect username or password."
)
return jinja.render("login.html", request, error=" ")
@app.route("/signup", methods=["POST", "GET"])
@IsAuthed()
async def signup(request):
if request.method == "POST":
username = request.form.get("username")
password = request.form.get("password")
try:
account = CreateAcc(username, password, app.config.URL)
except:
return text("Something went wrong")
if account["op"] == "Created.":
discrim = GetName(account["id"], app.config.URL)["discrim"]
authkey = GenAuthUsername(username, discrim, password, app.config.URL)
payload = {
"user": {
"id": account["id"],
"username": username,
"discrim": discrim,
"password": password,
"authkey": authkey["authentication"],
}
}
cookie = createToken(app.config.KEY, payload)
response = redirect("/")
response.add_cookie(
app.config.COOKIE, cookie, max_age=604800, httponly=False # One week
)
return response
else:
return text("Something went wrong :(")
return jinja.render("signup.html", request)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8888, debug=True)