Browse Source

add jwt black list

ming 3 years ago
parent
commit
2b8811f069
3 changed files with 25 additions and 9 deletions
  1. BIN
      api/__pycache__/main.cpython-39.pyc
  2. 2 2
      api/mailer.py
  3. 23 7
      api/main.py

BIN
api/__pycache__/main.cpython-39.pyc


+ 2 - 2
api/mailer.py

@@ -7,8 +7,8 @@ from email.mime.multipart import MIMEMultipart
 from email.message import EmailMessage
 import codecs
 
-gmail_user = 'ming@choozmo.com'
-gmail_password='lzpxinencaawsjus'
+gmail_user = 'verify@choozmo.com'
+gmail_password='ckmspyijofyavuwg'
 
 sent_from = gmail_user
 #to = ['jared@choozmo.com','nina.huang@choozmo.com','ana@choozmo.com','ming@choozmo.com','mike@choozmo.com','andy@choozmo.com','hana@choozmo.com','stacy@choozmo.com','wen@choozmo.com','yukyo@choozmo.com','fxp87257@gmail.com','noodlesloves@gmail.com']

+ 23 - 7
api/main.py

@@ -156,9 +156,12 @@ async def make_video_slide(request: Request, response: Response, Authorize: Auth
     current_user = Authorize.get_jwt_subject()
     return templates.TemplateResponse("make_video_slide.html", {"request": request, "response": response})
 
-@app.post('/user_profile', response_class=HTMLResponse)
+@app.post('/user_profile')
 async def user_profile(token: str = Depends(oauth2_scheme)):
     db_check()
+    if 'ok'!=verify_jwt_token(token):
+        return {'msg':{'eng':'Please login first','zh':'請先登入帳號'}}
+    
     user_id = get_user_id(token)
     user_obj = first(db.query('SELECT * FROM users where id ="'+str(user_id)+'"'))
 
@@ -290,6 +293,14 @@ def logout(request: Request, Authorize: AuthJWT = Depends()):
     Authorize.unset_jwt_cookies()
     return {"msg": "ok"}
 
+@app.post('/logout_jwt')
+def logout(token: str = Depends(oauth2_scheme)):
+    db_check()
+    time_stamp = datetime.fromtimestamp(time.time())
+    time_stamp = time_stamp.strftime("%Y-%m-%d %H:%M:%S")
+    db['jwt_black_list'].insert({'token':token,'datetime':time_stamp})
+    return {"msg": "ok"}
+
 @app.post('/get_role')
 async def get_role(token: str = Depends(oauth2_scheme)):
      
@@ -964,14 +975,19 @@ class text_in(BaseModel):
 async def make_voice(in_text:text_in):
     x = threading.Thread(target=call_voice, args=(in_text.text,))
     x.start()
-    
+
+def verify_jwt_token(token):
+    for row in db.query('SELECT * FROM jwt_black_list'):
+        print(row)
+    print(first(db.query('SELECT COUNT(*) FROM jwt_black_list where token="'+token+'"'))['COUNT(*)'])
+    if first(db.query('SELECT COUNT(*) FROM jwt_black_list where token="'+token+'"'))['COUNT(*)'] == 0:
+        return  'ok'
+    else:
+        return 'please login again'
 def db_check():
     global db
-    try:
-        db.tables
-    except:
-        print("database down")
-        db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')
+    db.close()
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/AI_anchor?charset=utf8mb4')