-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
400 lines (333 loc) · 15.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
from ast import literal_eval
from fastapi import Depends, FastAPI, Request,Cookie , HTTPException, Response
from fastapi.responses import HTMLResponse,RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from typing import List, Union
from typing_extensions import Annotated
from fastapi_sessions.frontends.implementations import SessionCookie, CookieParameters
from fastapi_sessions.backends.implementations import InMemoryBackend
from fastapi_sessions.session_verifier import SessionVerifier
from uuid import UUID , uuid4
from Service import *
from Entity import *
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class SessionData(BaseModel):
username: str
access : str
cookie_params = CookieParameters()
# Uses UUID
cookie = SessionCookie(
cookie_name="cookie",
identifier="general_verifier",
auto_error=True,
secret_key="DONOTUSE",
cookie_params=cookie_params,
)
backend = InMemoryBackend[UUID, SessionData]()
class BasicVerifier(SessionVerifier[UUID, SessionData]):
def __init__(
self,
*,
identifier: str,
auto_error: bool,
backend: InMemoryBackend[UUID, SessionData],
auth_http_exception: HTTPException,
):
self._identifier = identifier
self._auto_error = auto_error
self._backend = backend
self._auth_http_exception = auth_http_exception
@property
def identifier(self):
return self._identifier
@property
def backend(self):
return self._backend
@property
def auto_error(self):
return self._auto_error
@property
def auth_http_exception(self):
return self._auth_http_exception
def verify_session(self, model: SessionData) -> bool:
"""If the session exists, it is valid"""
return True
verifier = BasicVerifier(
identifier="general_verifier",
auto_error=True,
backend=backend,
auth_http_exception=HTTPException(status_code=403, detail="invalid session"),
)
#initDataBase()
app = FastAPI()
app.mount("/static",StaticFiles(directory="static"),name="static")
templates = Jinja2Templates(directory="./static/template")
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request,exc):
return RedirectResponse("/404")
#async def validation_exception_handler(request, exc : RequestValidationError):
# return RedirectResponse("/login")
#app.add_exception_handler(RequestValidationError,validation_exception_handler)
@app.route("/404",methods=["GET","POST"])
async def _404(request : Request):
return templates.TemplateResponse(request=request,name="404.html")
@app.get("/",response_class=HTMLResponse)
async def hello(request : Request):
return templates.TemplateResponse(request=request,name="login.html")
@app.get("/register",response_class=HTMLResponse)
async def register(request : Request):
return templates.TemplateResponse(request=request,name="register.html")
@app.post("/register")
async def register(user : User):
euser = User_Entity(user)
if not UserService.getUser(euser):
UserService.registerUser(euser)
mail_host = "smtp.qq.com"
mail_user = "2698531708"
mail_pass = "reztbfnkwfqzddbh"
sender = "[email protected]"
message = MIMEText('通过链接 192.168.42.141:8000/activate/%s 激活邮箱'%(user.username),'plain','utf-8')
message['Subject'] = 'Nebula Space 校验码'
message['From'] = sender
message['To'] = user.email
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host,25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(
sender,[user.email],message.as_string())
smtpObj.quit()
return RedirectResponse("/register/success")
else:
return RedirectResponse("/register/failure")
@app.get("/activate/{username}",response_class=HTMLResponse)
async def activate(request : Request,username : str):
UserService.activateUserPrivilege(username)
print(username)
return templates.TemplateResponse(request=request,name="verify_prompt.html",context = {"signal":"pass","info":"激活成功"})
@app.get("/register/success",response_class=HTMLResponse)
async def register_success(request : Request):
return templates.TemplateResponse(request=request,name="register_prompt.html",context = {"signal":"pass" ,"info": "注册成功"})
@app.get("/register/failure",response_class=HTMLResponse)
async def register_failure(request : Request):
return templates.TemplateResponse(request=request,name="register_prompt.html",context = {"signal":"error","info":"用户名重复"})
@app.get("/login",response_class=HTMLResponse)
async def login(request : Request):
return templates.TemplateResponse(request=request,name="login.html",context = {"info": None})
@app.get("/changepassword",response_class=HTMLResponse)
async def changepassword(request : Request):
return templates.TemplateResponse(request=request,name="changepassword.html")
@app.post("/changepassword",dependencies=[Depends(cookie)])
async def changepassword(changePasswordValidation : ChangePasswordValidation,session_data: SessionData = Depends(verifier)):
username = session_data.username
password = UserService.fetchUserPassword(username)
if password == changePasswordValidation.password:
#print("enter")
#print(username,changePasswordValidation.password_new)
UserService.updateUserPassword(username,changePasswordValidation.password_new)
else:
return RedirectResponse("/changepassword/failure")
@app.route("/changepassword/failure",methods=["GET","POST"])
async def changepassword_failure(request:Request):
return templates.TemplateResponse(request=request,name="changepassword.html",context={"info":"原始密码错误"})
@app.get("/home",response_class=HTMLResponse,dependencies=[Depends(cookie)])
async def home(request : Request , session_data: SessionData = Depends(verifier)):
if session_data.access == "permmit":
return templates.TemplateResponse(request=request,name="home.html",context={ "username" : session_data.username, "p":["nav-link active","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white"]})
else:
return RedirectResponse("/login/failure",status_code=200)
@app.get("/home/person",response_class=HTMLResponse,dependencies=[Depends(cookie)])
async def home_person(request : Request, session_data: SessionData = Depends(verifier)):
user = UserService.getUserByName(session_data.username)
return templates.TemplateResponse(request=request,name="person.html",context=dict(zip(User_Entity.keys(),user)))
@app.post("/home/person",response_class=HTMLResponse,dependencies=[Depends(cookie)])
async def home_person(person : Person , request : Request, session_data : SessionData = Depends(verifier)):
if session_data.username == person.username:
username = person.username
user = UserService.getUserByName(username)
#print(user)
newuser = User(username = user[0],
password = user[1],
email = person.email,
privilege = user[3],
address = person.address,
phone = person.phone,
introduction = person.introduction)
#print(newuser)
euser = User_Entity(newuser)
UserService.updateUser(euser)
@app.post("/login")
async def login(user : Validation,response:Response):
euser = Validation_Entity(user)
if UserService.validateUser(euser):
session = uuid4()
data = SessionData(username=user.username,access="permmit")
await backend.create(session,data)
cookie.attach_to_response(response,session)
else:
return RedirectResponse("/login/failure")
@app.route("/login/failure",methods=["GET","POST"])
async def login_failure(request : Request):
return templates.TemplateResponse(request=request,name="login.html",context = {"info":"账号密码错误"})
@app.get("/forget",response_class=HTMLResponse)
async def forget(request : Request):
return templates.TemplateResponse(request=request,name="forget.html")
@app.get("/forget/failure",response_class=HTMLResponse)
async def forget_failure(request : Request):
return templates.TemplateResponse(request=request,name="verify_prompt.html",context = {"signal": "error","info": "校验码错误"})
@app.get("/forget/success",response_class=HTMLResponse)
async def forget_success(request : Request):
return templates.TemplateResponse(request=request,name="register_prompt.html",context = {"signal":"pass","info":"修改密码成功"})
@app.post("/forget")
async def forget(forgetValidation : ForgetValidation):
old_password = UserService.fetchUserPassword(forgetValidation.username)
if old_password == forgetValidation.password:
result = UserService.updateUserPassword(forgetValidation.username,forgetValidation.password_new)
return RedirectResponse("/forget/success")
else:
return RedirectResponse("/forget/failure")
@app.get("/sendverify",response_class=HTMLResponse)
async def sendVerify(request : Request, username : Optional[str]):
privilege = UserService.fetchUserPrivilege(username)
#print(privilege)
if privilege == "R":
return templates.TemplateResponse(request=request,name="verify_prompt.html",context = {"signal":"error","info":"您的用户名不存在"})
elif privilege is None:
return templates.TemplateResponse(request=request,name="verify_prompt.html",context = {"signal":"error","info":"您的邮箱还没有验证"})
else:
password = UserService.fetchUserPassword(username)
#print(password)
email = UserService.fetchUserEmail(username)
mail_host = "smtp.qq.com"
mail_user = "2698531708"
mail_pass = "reztbfnkwfqzddbh"
sender = "[email protected]"
message = MIMEText(password,'plain','utf-8')
message['Subject'] = 'Nebula Space 校验码'
message['From'] = sender
message['To'] = email
smtpObj = smtplib.SMTP()
smtpObj.connect(mail_host,25)
smtpObj.login(mail_user,mail_pass)
smtpObj.sendmail(
sender,[email],message.as_string())
smtpObj.quit()
return templates.TemplateResponse(request=request,name="verify_prompt.html",context= {"signal":"pass","info":"校验信息已经发送至您的邮箱"})
@app.post("/create_session/{username}/{password}")
async def create_session(username: str, password : str ,response: Response):
session = uuid4()
data = SessionData(username=username)
await backend.create(session, data)
cookie.attach_to_response(response, session)
@app.get("/whoami", dependencies=[Depends(cookie)])
async def whoami(session_data: SessionData = Depends(verifier)):
return session_data
@app.get("/logout")
async def del_session(response: Response, session_id: UUID = Depends(cookie)):
try:
await backend.delete(session_id)
cookie.delete_from_response(response)
finally:
return RedirectResponse("/login")
@app.get('/home/queryvertex',dependencies=[Depends(cookie)])
async def queryvertex(request: Request,session_data: SessionData = Depends(verifier)):
return templates.TemplateResponse(request=request,name="queryvertex.html",context={ "username" : session_data.username,"p":["nav-link text-white","nav-link active","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white"]})
example_body = '''{
"query_type":"tags",
"query_body":{
"body_name":"team",
"body_query":["NULL"]
}
}'''
import urllib3
http = urllib3.PoolManager()
class QueryBody(BaseModel):
body_name: str
body_query: List[str]
@app.post('/home/queryvertex',dependencies=[Depends(cookie)])
async def queryvertex(request:Request,querybody:QueryBody,session_data: SessionData = Depends(verifier)):
print(querybody)
vertexquery = '''{
"query_type":"tags",
"query_body":{
"body_name":"%s",
"body_query":%s
}
}'''%(querybody.body_name,querybody.body_query)
vertexquery = vertexquery.replace("'",'"')
#print(vertexquery)
r = http.request('POST',"http://127.0.0.1:10000",body=vertexquery.encode('utf-8'),headers={'Content-Type':'application/json'})
#return r.data.decode()
demo = '''
digraph {
node [shape=circle fontsize=16]
edge [length=100, color=gray, fontcolor=black]
%s
}
'''%("\n".join(["%s [label=\"%s\"]"%(k,i[0][2:-1]) for k,i in enumerate(literal_eval(r.data.decode()))]))
print(demo)
return demo,[i[1] for i in literal_eval(r.data.decode())]
@app.get('/home/queryedge',dependencies=[Depends(cookie)])
async def queryedge(request:Request,session_data: SessionData = Depends(verifier)):
return templates.TemplateResponse(request=request,name="queryedge.html",context={ "username" : session_data.username,"p":["nav-link text-white","nav-link text-white","nav-link active","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white"]})
@app.post('/home/queryedge',dependencies=[Depends(cookie)])
async def queryedge(request: Request,querybody:QueryBody,session_data: SessionData = Depends(verifier)):
print(querybody)
edgequery = '''{
"query_type":"edges",
"query_body":{
"body_name":"%s",
"body_query":%s
}
}'''%(querybody.body_name,querybody.body_query)
edgequery = edgequery.replace("'",'"')
#print(edgequery)
r = http.request('POST',"http://127.0.0.1:10000",body=edgequery.encode('utf-8'),headers={'Content-Type':'application/json'})
#print(r.data.decode())
outnodeid,innodeid,allnode,edges = literal_eval(r.data.decode())
tempnode = innodeid + outnodeid
(nodeid1,allnode1) = ([],[])
for i in range(len(tempnode)):
if tempnode[i] not in nodeid1:
nodeid1.append(tempnode[i])
allnode1.append(allnode[i])
edgeindex = []
for i in range(len(edges)):
edgeindex.append((nodeid1.index(outnodeid[i]),nodeid1.index(innodeid[i])))
demo = '''
digraph {
node [shape=circle fontsize=16]
edge [length=100, color=gray, fontcolor=black]
%s
%s
}
'''%("\n".join(["%s [label=%s]"%(k,i[2:-1]) for k,i in enumerate(nodeid1)]),"\n".join(["%s -> %s [label=\"%s\",id=%s]"%(edgeindex[k][0],edgeindex[k][1],querybody.body_name,k) for k,i in enumerate(edges)]))
print(demo)
return demo,allnode1,edges
@app.get('/home/playground',dependencies=[Depends(cookie)])
async def playground(request:Request,session_data: SessionData = Depends(verifier)):
return templates.TemplateResponse(request=request,name="playground.html",context={"username" : session_data.username,"p":["nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link active"]})
@app.get('/home/schema',dependencies=[Depends(cookie)])
async def schema(request:Request,session_data: SessionData = Depends(verifier)):
query='''{
"query_type":"schema",
"query_body":{
"body_name":"",
"body_query":[]
}
}
'''
r = http.request('POST',"http://127.0.0.1:10000",body=query.encode('utf-8'),headers={'Content-Type':'application/json'})
data = literal_eval(r.data.decode())
demo = '''
digraph {
node [shape=circle fontsize=16]
edge [length=300, color=gray, fontcolor=black]
%s
}'''%("\n\n".join(['%s -> %s [label=%s]'%(i[0],i[1],i[2]) for i in data]))
print(demo)
return templates.TemplateResponse(request=request,name="queryschema.html",context={"username" : session_data.username,"p":["nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link text-white","nav-link active","nav-link text-white"],"data" : demo})