Spaces:
Sleeping
Sleeping
Upload 14 files
Browse files- auth/__pycache__/authentication.cpython-311.pyc +0 -0
- auth/authentication.py +133 -0
- controller/CallbackController.py +156 -0
- controller/ChatController.py +99 -0
- controller/GroupOrderController.py +25 -0
- controller/RecommendController.py +25 -0
- controller/RedirectController.py +95 -0
- controller/__init__.py +0 -0
- controller/__pycache__/CallbackController.cpython-311.pyc +0 -0
- controller/__pycache__/ChatController.cpython-311.pyc +0 -0
- controller/__pycache__/GroupOrderController.cpython-311.pyc +0 -0
- controller/__pycache__/RecommendController.cpython-311.pyc +0 -0
- controller/__pycache__/RedirectController.cpython-311.pyc +0 -0
- controller/__pycache__/__init__.cpython-311.pyc +0 -0
auth/__pycache__/authentication.cpython-311.pyc
ADDED
|
Binary file (3.98 kB). View file
|
|
|
auth/authentication.py
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from typing import Dict
|
| 3 |
+
import jwt
|
| 4 |
+
import secrets
|
| 5 |
+
import logging
|
| 6 |
+
from fastapi import Depends, HTTPException
|
| 7 |
+
import base64
|
| 8 |
+
from datetime import datetime, timedelta
|
| 9 |
+
# from repository import UserRepository, UserLoginRepository
|
| 10 |
+
import string, random
|
| 11 |
+
|
| 12 |
+
# def check_token_is_valid(token):
|
| 13 |
+
# check = UserRepository.getEmailUserByAccessToken(token)
|
| 14 |
+
# if check is None:
|
| 15 |
+
# return False
|
| 16 |
+
# return True
|
| 17 |
+
|
| 18 |
+
def unique_string(byte: int = 8) -> str:
|
| 19 |
+
return secrets.token_urlsafe(byte)
|
| 20 |
+
JWT_SECRET = "404E635266556A586E3272357538782F413F4428472B4B6250645367566B5970"
|
| 21 |
+
JWT_ALGORITHM = "HS256"
|
| 22 |
+
SECRET_KEY="404E635266556A586E3272357538782F413F4428472B4B6250645367566B5970"
|
| 23 |
+
|
| 24 |
+
def token_response(token: str):
|
| 25 |
+
return {
|
| 26 |
+
"access_token": token
|
| 27 |
+
}
|
| 28 |
+
def str_encode(string: str) -> str:
|
| 29 |
+
return base64.b85encode(string.encode('ascii')).decode('ascii')
|
| 30 |
+
|
| 31 |
+
def get_token_payload(token: str, secret: str, algo: str):
|
| 32 |
+
try:
|
| 33 |
+
payload = jwt.decode(token, secret, algorithms=algo)
|
| 34 |
+
except Exception as jwt_exec:
|
| 35 |
+
logging.debug(f"JWT Error: {str(jwt_exec)}")
|
| 36 |
+
payload = None
|
| 37 |
+
return payload
|
| 38 |
+
|
| 39 |
+
from datetime import datetime
|
| 40 |
+
def generate_token(payload: dict, secret: str, algo: str, expiry: timedelta):
|
| 41 |
+
expire = datetime.now() + expiry
|
| 42 |
+
payload.update({"exp": expire})
|
| 43 |
+
return jwt.encode(payload, secret, algorithm=algo)
|
| 44 |
+
|
| 45 |
+
def str_decode(string: str) -> str:
|
| 46 |
+
return base64.b85decode(string.encode('ascii')).decode('ascii')
|
| 47 |
+
|
| 48 |
+
def generate_random_string(length=12):
|
| 49 |
+
characters = string.ascii_letters + string.digits
|
| 50 |
+
random_string = ''.join(random.choice(characters) for i in range(length))
|
| 51 |
+
return random_string
|
| 52 |
+
|
| 53 |
+
import pytz
|
| 54 |
+
from datetime import datetime
|
| 55 |
+
# def signJWT(user_email: str) -> Dict[str, str]:
|
| 56 |
+
# rt_expires = timedelta(days=3)
|
| 57 |
+
# refresh_key = unique_string(100)
|
| 58 |
+
# access_key = unique_string(50)
|
| 59 |
+
# at_expires = timedelta(minutes=180)
|
| 60 |
+
# at_payload = {
|
| 61 |
+
# "sub": str_encode(str(user_email)),
|
| 62 |
+
# 'a': access_key,
|
| 63 |
+
# }
|
| 64 |
+
# access_token = generate_token(at_payload, JWT_SECRET, JWT_ALGORITHM, at_expires)
|
| 65 |
+
# rt_payload = {"sub": str_encode(str(user_email)), "t": refresh_key, 'a': access_key}
|
| 66 |
+
# refresh_token = generate_token(rt_payload, SECRET_KEY,JWT_ALGORITHM, rt_expires)
|
| 67 |
+
# expires_in = at_expires.seconds
|
| 68 |
+
# vn_timezone = pytz.timezone('Asia/Ho_Chi_Minh')
|
| 69 |
+
# current_time = datetime.now().replace(tzinfo=pytz.utc).astimezone(vn_timezone) + timedelta(seconds=expires_in)
|
| 70 |
+
# formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S ')
|
| 71 |
+
# existing_user = UserRepository.getUserByEmail(user_email)
|
| 72 |
+
# if existing_user is None:
|
| 73 |
+
# UserRepository.addUser(user_email, access_token, refresh_token, formatted_time)
|
| 74 |
+
# else:
|
| 75 |
+
# UserRepository.updateUserLogin(user_email, access_token, refresh_token, formatted_time)
|
| 76 |
+
# user_record = UserRepository.getUserByEmail(user_email)
|
| 77 |
+
# session_id = ""
|
| 78 |
+
# if user_record:
|
| 79 |
+
# session_id = generate_random_string()
|
| 80 |
+
# existing_userlogin = UserLoginRepository.getUserLogin(user_email)
|
| 81 |
+
# if existing_userlogin is None:
|
| 82 |
+
# UserLoginRepository.addUserLogin(user_email,session_id=session_id)
|
| 83 |
+
# else:
|
| 84 |
+
# UserLoginRepository.updateUserLogin(user_email, session_id)
|
| 85 |
+
# return {
|
| 86 |
+
# "access_token": access_token,
|
| 87 |
+
# "refresh_token": refresh_token,
|
| 88 |
+
# "expires_in": at_expires.seconds,
|
| 89 |
+
# "session_id": session_id
|
| 90 |
+
# }
|
| 91 |
+
|
| 92 |
+
# def returnAccessToken(user_email: str, refresh_token: str) -> Dict[str, str]:
|
| 93 |
+
# access_key = unique_string(50)
|
| 94 |
+
# at_expires = timedelta(minutes=180)
|
| 95 |
+
# at_payload = {
|
| 96 |
+
# "sub": str_encode(str(user_email)),
|
| 97 |
+
# 'a': access_key,
|
| 98 |
+
# }
|
| 99 |
+
# access_token = generate_token(at_payload, JWT_SECRET, JWT_ALGORITHM, at_expires)
|
| 100 |
+
# user_record = UserRepository.getUserByEmail(user_email)
|
| 101 |
+
# session_id = ""
|
| 102 |
+
# if user_record:
|
| 103 |
+
# email1 = user_record.email
|
| 104 |
+
# if email1:
|
| 105 |
+
# session_id = generate_random_string()
|
| 106 |
+
# existing_userlogin = UserLoginRepository.getUserLogin(user_email)
|
| 107 |
+
# if existing_userlogin is None:
|
| 108 |
+
# UserLoginRepository.addUserLogin(user_email,session_id=session_id)
|
| 109 |
+
# else:
|
| 110 |
+
# UserLoginRepository.updateUserLogin(user_email,session_id)
|
| 111 |
+
# return {
|
| 112 |
+
# "access_token": access_token,
|
| 113 |
+
# "refresh_token": refresh_token,
|
| 114 |
+
# "expires_in": at_expires.seconds,
|
| 115 |
+
# "session_id": session_id
|
| 116 |
+
# }
|
| 117 |
+
|
| 118 |
+
def decodeJWT(token: str) -> dict:
|
| 119 |
+
try:
|
| 120 |
+
decoded_token = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGORITHM])
|
| 121 |
+
return decoded_token if decoded_token["exp"] >= time.time() else None
|
| 122 |
+
except:
|
| 123 |
+
return {}
|
| 124 |
+
|
| 125 |
+
# def get_refresh_token(refresh_token, email):
|
| 126 |
+
# token_payload = get_token_payload(refresh_token, SECRET_KEY, JWT_ALGORITHM)
|
| 127 |
+
# if not token_payload:
|
| 128 |
+
# raise HTTPException(status_code=400, detail="Invalid Request.")
|
| 129 |
+
# exp = token_payload.get('exp')
|
| 130 |
+
# if exp >= time.time() and token_payload:
|
| 131 |
+
# return returnAccessToken(email,refresh_token)
|
| 132 |
+
# elif not token_payload:
|
| 133 |
+
# return signJWT(email)
|
controller/CallbackController.py
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Request,HTTPException
|
| 2 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 3 |
+
from fastapi.requests import Request
|
| 4 |
+
import yaml
|
| 5 |
+
from typing import Dict
|
| 6 |
+
from fastapi.responses import RedirectResponse
|
| 7 |
+
from service import CallBackService
|
| 8 |
+
import json
|
| 9 |
+
from fastapi.responses import JSONResponse
|
| 10 |
+
with open("config.yaml", "r") as f:
|
| 11 |
+
config = yaml.safe_load(f)
|
| 12 |
+
base_backend_java = config["callback_urls"]
|
| 13 |
+
base_frontend = config["frontend"]
|
| 14 |
+
|
| 15 |
+
router = APIRouter()
|
| 16 |
+
|
| 17 |
+
class JWTBearer(HTTPBearer):
|
| 18 |
+
def __init__(self, auto_error: bool = True):
|
| 19 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 20 |
+
|
| 21 |
+
async def __call__(self, request: Request):
|
| 22 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 23 |
+
if credentials:
|
| 24 |
+
if credentials.scheme != "Bearer":
|
| 25 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 26 |
+
return credentials.credentials
|
| 27 |
+
else:
|
| 28 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 29 |
+
|
| 30 |
+
jwt_bearer = JWTBearer()
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@router.post("/android/zalo")
|
| 35 |
+
async def zalo_callback_post(request: Request):
|
| 36 |
+
try:
|
| 37 |
+
json_data = await request.json()
|
| 38 |
+
data_str =json_data.get("data")
|
| 39 |
+
if data_str:
|
| 40 |
+
data_dict = json.loads(data_str)
|
| 41 |
+
app_trans_id = data_dict.get("app_trans_id")
|
| 42 |
+
print(app_trans_id)
|
| 43 |
+
data = await CallBackService.zalo_callback(str(app_trans_id))
|
| 44 |
+
return data
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print("Lỗi khi gọi callback web zalo:", e)
|
| 47 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@router.post("/android/group/zalo")
|
| 51 |
+
async def zalo_callback_post(request: Request):
|
| 52 |
+
try:
|
| 53 |
+
json_data = await request.json()
|
| 54 |
+
data_str =json_data.get("data")
|
| 55 |
+
if data_str:
|
| 56 |
+
data_dict = json.loads(data_str)
|
| 57 |
+
app_trans_id = data_dict.get("app_trans_id")
|
| 58 |
+
print(app_trans_id)
|
| 59 |
+
data = await CallBackService.zalo_group_callback(str(app_trans_id))
|
| 60 |
+
return data
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("Lỗi khi gọi callback group ảndroid zalo:", e)
|
| 63 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
@router.get("/android/payOs")
|
| 67 |
+
async def return_url_handler(code: str = None, id: str = None, cancel: bool = None, status: str = None, orderCode: str = None):
|
| 68 |
+
try:
|
| 69 |
+
return await CallBackService.android_payos_callback(status,orderCode)
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print("Lỗi khi gọi callback android payos:", e)
|
| 72 |
+
# Trả về lỗi 500 - Internal Server Error
|
| 73 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
@router.get("/android/group/payOs")
|
| 77 |
+
async def return_url_handler1(code: str = None, id: str = None, cancel: bool = None, status: str = None, orderCode: str = None):
|
| 78 |
+
try:
|
| 79 |
+
return await CallBackService.android_group_payOs_callback(status,orderCode)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
print("Lỗi khi gọi callback group payos:", e)
|
| 82 |
+
# Trả về lỗi 500 - Internal Server Error
|
| 83 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
@router.get("/web/payOs")
|
| 87 |
+
async def return_url_handler(code: str = None, id: str = None, cancel: bool = None, status: str = None, orderCode: str = None):
|
| 88 |
+
try:
|
| 89 |
+
return await CallBackService.web_payos_callback(code, id, cancel, status, orderCode)
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print("Lỗi khi gọi callback web payos:", e)
|
| 92 |
+
# Trả về lỗi 500 - Internal Server Error
|
| 93 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
@router.post("/android/momo")
|
| 97 |
+
async def return_url_handler(request: Request):
|
| 98 |
+
json_data = await request.json()
|
| 99 |
+
print(json_data)
|
| 100 |
+
data_str =json_data.get("data")
|
| 101 |
+
order_id = json_data.get("orderId")
|
| 102 |
+
result_code = json_data.get("resultCode")
|
| 103 |
+
print(data_str)
|
| 104 |
+
|
| 105 |
+
try:
|
| 106 |
+
result = await CallBackService.momo_callback(order_id,result_code)
|
| 107 |
+
print(result)
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print("Lỗi khi gọi call back android momo:", e)
|
| 110 |
+
|
| 111 |
+
if 0 != int(result_code):
|
| 112 |
+
return RedirectResponse(url=f"{base_frontend['android_redirect_base']}?status=-49")
|
| 113 |
+
else :
|
| 114 |
+
return RedirectResponse(url=f"{base_frontend['android_redirect_base']}?status=1")
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@router.post("/android/group/momo")
|
| 118 |
+
async def return_url_handler(request: Request):
|
| 119 |
+
json_data = await request.json()
|
| 120 |
+
print(json_data)
|
| 121 |
+
data_str =json_data.get("data")
|
| 122 |
+
order_id = json_data.get("orderId")
|
| 123 |
+
result_code = json_data.get("resultCode")
|
| 124 |
+
print(data_str)
|
| 125 |
+
|
| 126 |
+
try:
|
| 127 |
+
result = await CallBackService.momo_group_callback(order_id,result_code)
|
| 128 |
+
print(result)
|
| 129 |
+
except Exception as e:
|
| 130 |
+
print("Lỗi khi gọi callback android group momo:", e)
|
| 131 |
+
if 0 != int(result_code):
|
| 132 |
+
return RedirectResponse(url=f"""{base_frontend["android_group_redirect_base"]}?status=-49""")
|
| 133 |
+
else :
|
| 134 |
+
return RedirectResponse(url=f"""{base_frontend["android_group_redirect_base"]}?status=1""")
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
@router.get("/android/vnpay_ipn")
|
| 138 |
+
async def vnpay_ipn(request: Request):
|
| 139 |
+
params: Dict[str, str] = dict(request.query_params)
|
| 140 |
+
try:
|
| 141 |
+
return await CallBackService.vnpay_callbackk(params)
|
| 142 |
+
except Exception as e:
|
| 143 |
+
print("Lỗi khi gọi callback vnpay:", e)
|
| 144 |
+
# Trả về lỗi 500 - Internal Server Error
|
| 145 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
@router.get("/android/group/vnpay_ipn")
|
| 149 |
+
async def vnpay_ipn(request: Request):
|
| 150 |
+
params: Dict[str, str] = dict(request.query_params)
|
| 151 |
+
try:
|
| 152 |
+
return await CallBackService.vnpay_group_callback(params)
|
| 153 |
+
except Exception as e:
|
| 154 |
+
print("Lỗi khi gọi callback group vnpay ipn:", e)
|
| 155 |
+
# Trả về lỗi 500 - Internal Server Error
|
| 156 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
controller/ChatController.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Form, Request,Depends,HTTPException
|
| 2 |
+
from service import ChatService
|
| 3 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
+
from request import RequestChat
|
| 5 |
+
from fastapi.requests import Request
|
| 6 |
+
from fastapi.responses import JSONResponse
|
| 7 |
+
router = APIRouter()
|
| 8 |
+
|
| 9 |
+
class JWTBearer(HTTPBearer):
|
| 10 |
+
def __init__(self, auto_error: bool = True):
|
| 11 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 12 |
+
|
| 13 |
+
async def __call__(self, request: Request):
|
| 14 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 15 |
+
if credentials:
|
| 16 |
+
if credentials.scheme != "Bearer":
|
| 17 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 18 |
+
return credentials.credentials
|
| 19 |
+
else:
|
| 20 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 21 |
+
|
| 22 |
+
jwt_bearer = JWTBearer()
|
| 23 |
+
import decode_token
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
@router.get("/chat/user_history")
|
| 27 |
+
async def get_user_chat_history(token: str = Depends(jwt_bearer)):
|
| 28 |
+
try:
|
| 29 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 30 |
+
return await ChatService.get_user_chat_history(user_id_token)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print("Lỗi khi gọi user history chat:", e)
|
| 33 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
@router.post("/new_chat/create/")
|
| 37 |
+
async def create_chat(token: str = Depends(jwt_bearer)):
|
| 38 |
+
try:
|
| 39 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 40 |
+
new_chat = await ChatService.create_new_chat_history(user_id_token)
|
| 41 |
+
return new_chat
|
| 42 |
+
except Exception as e:
|
| 43 |
+
print("Lỗi khi gọi create new chat:", e)
|
| 44 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@router.post("/question")
|
| 48 |
+
async def question(request: RequestChat.ChatWithServer, token: str = Depends(jwt_bearer)):
|
| 49 |
+
try:
|
| 50 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 51 |
+
user_role = decode_token.JwtService.extract_user_role(token)
|
| 52 |
+
new_chat = await ChatService.chat_with_user(request.user_input,user_id_token,request.language,user_role,token,request.chat_history_id)
|
| 53 |
+
return new_chat
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print("Lỗi khi chạy:", e)
|
| 56 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@router.put("/regenerate")
|
| 60 |
+
async def regenerate(request: RequestChat.Regenerate,token: str = Depends(jwt_bearer)):
|
| 61 |
+
try:
|
| 62 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 63 |
+
user_role = decode_token.JwtService.extract_user_role(token)
|
| 64 |
+
new_chat = await ChatService.regenerate(request.question_new ,user_id_token,request.languages ,user_role,token,request.chat_id)
|
| 65 |
+
return new_chat
|
| 66 |
+
except Exception as e:
|
| 67 |
+
print("Lỗi khi gọi regenerate:", e)
|
| 68 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 69 |
+
|
| 70 |
+
@router.put("/update")
|
| 71 |
+
async def update_chat_name(request: RequestChat.UpdateNameChat, token: str = Depends(jwt_bearer)):
|
| 72 |
+
try:
|
| 73 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 74 |
+
updated_chat = await ChatService.update_chat_name(request.chat_id, request.name_chat,user_id_token)
|
| 75 |
+
return updated_chat
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print("Lỗi khi gọi update:", e)
|
| 78 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
@router.delete("/delete")
|
| 82 |
+
async def delete_chat(request: RequestChat.DeleteChatRequest, token: str = Depends(jwt_bearer)):
|
| 83 |
+
try:
|
| 84 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 85 |
+
deleted_chat = await ChatService.soft_delete_chat(request.chat_id,user_id_token)
|
| 86 |
+
return deleted_chat
|
| 87 |
+
except Exception as e:
|
| 88 |
+
print("Lỗi khi gọi deleted:", e)
|
| 89 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@router.get("/list_detail_chat/{chat_id}")
|
| 93 |
+
async def get_chat_details(chat_id: str, token: str = Depends(jwt_bearer)):
|
| 94 |
+
try:
|
| 95 |
+
user_id_token = decode_token.JwtService.extract_user_id(token)
|
| 96 |
+
return await ChatService.get_chat_details(chat_id,user_id_token)
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print("Lỗi khi gọi list_detail_chat:", e)
|
| 99 |
+
return JSONResponse(content={"error": "Internal server error"}, status_code=500)
|
controller/GroupOrderController.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Form, Request,Depends,HTTPException
|
| 2 |
+
from service import ChatService
|
| 3 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
+
from request import RequestChat
|
| 5 |
+
from typing import Optional
|
| 6 |
+
from fastapi.requests import Request
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
class JWTBearer(HTTPBearer):
|
| 12 |
+
def __init__(self, auto_error: bool = True):
|
| 13 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 14 |
+
|
| 15 |
+
async def __call__(self, request: Request):
|
| 16 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 17 |
+
if credentials:
|
| 18 |
+
if credentials.scheme != "Bearer":
|
| 19 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 20 |
+
return credentials.credentials
|
| 21 |
+
else:
|
| 22 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 23 |
+
|
| 24 |
+
jwt_bearer = JWTBearer()
|
| 25 |
+
import decode_token
|
controller/RecommendController.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Form, Request,Depends,HTTPException
|
| 2 |
+
from service import ChatService
|
| 3 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
+
from request import RequestChat
|
| 5 |
+
from typing import Optional
|
| 6 |
+
from fastapi.requests import Request
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
class JWTBearer(HTTPBearer):
|
| 12 |
+
def __init__(self, auto_error: bool = True):
|
| 13 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 14 |
+
|
| 15 |
+
async def __call__(self, request: Request):
|
| 16 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 17 |
+
if credentials:
|
| 18 |
+
if credentials.scheme != "Bearer":
|
| 19 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 20 |
+
return credentials.credentials
|
| 21 |
+
else:
|
| 22 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 23 |
+
|
| 24 |
+
jwt_bearer = JWTBearer()
|
| 25 |
+
import decode_token
|
controller/RedirectController.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter, Form, Request,Depends,HTTPException
|
| 2 |
+
from service import ChatService
|
| 3 |
+
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
| 4 |
+
from request import RequestChat
|
| 5 |
+
from typing import Optional
|
| 6 |
+
from fastapi.requests import Request
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
router = APIRouter()
|
| 10 |
+
|
| 11 |
+
class JWTBearer(HTTPBearer):
|
| 12 |
+
def __init__(self, auto_error: bool = True):
|
| 13 |
+
super(JWTBearer, self).__init__(auto_error=auto_error)
|
| 14 |
+
|
| 15 |
+
async def __call__(self, request: Request):
|
| 16 |
+
credentials: HTTPAuthorizationCredentials = await super(JWTBearer, self).__call__(request)
|
| 17 |
+
if credentials:
|
| 18 |
+
if credentials.scheme != "Bearer":
|
| 19 |
+
raise HTTPException(status_code=401, detail="Invalid authentication scheme.")
|
| 20 |
+
return credentials.credentials
|
| 21 |
+
else:
|
| 22 |
+
raise HTTPException(status_code=401, detail="Invalid authorization code.")
|
| 23 |
+
|
| 24 |
+
jwt_bearer = JWTBearer()
|
| 25 |
+
import decode_token
|
| 26 |
+
from service import RedirectService
|
| 27 |
+
from typing import Optional
|
| 28 |
+
from fastapi import Request, Query
|
| 29 |
+
from fastapi.responses import JSONResponse
|
| 30 |
+
@router.get("/android/zalo")
|
| 31 |
+
async def zalo_callback_get(
|
| 32 |
+
amount: Optional[int] = Query(None),
|
| 33 |
+
appid: Optional[str] = Query(None),
|
| 34 |
+
apptransid: Optional[str] = Query(None),
|
| 35 |
+
bankcode: Optional[str] = Query(None),
|
| 36 |
+
checksum: Optional[str] = Query(None),
|
| 37 |
+
discountamount: Optional[int] = Query(None),
|
| 38 |
+
pmcid: Optional[int] = Query(None),
|
| 39 |
+
status: Optional[int] = Query(None)
|
| 40 |
+
):
|
| 41 |
+
return await RedirectService.zalo_redirect(status)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@router.get("/android/group/zalo")
|
| 45 |
+
async def zalo_callback_get(
|
| 46 |
+
amount: Optional[int] = Query(None),
|
| 47 |
+
appid: Optional[str] = Query(None),
|
| 48 |
+
apptransid: Optional[str] = Query(None),
|
| 49 |
+
bankcode: Optional[str] = Query(None),
|
| 50 |
+
checksum: Optional[str] = Query(None),
|
| 51 |
+
discountamount: Optional[int] = Query(None),
|
| 52 |
+
pmcid: Optional[int] = Query(None),
|
| 53 |
+
status: Optional[int] = Query(None)
|
| 54 |
+
):
|
| 55 |
+
return await RedirectService.zalo_group_redirect(status)
|
| 56 |
+
|
| 57 |
+
@router.get("/web/zalo")
|
| 58 |
+
async def zalo_web_callback_get(request: Request):
|
| 59 |
+
return await RedirectService.zalo_web_redirect(request.query_params)
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
@router.get("/android/momo")
|
| 63 |
+
async def return_url_handler_1(request: Request):
|
| 64 |
+
query_params = request.query_params
|
| 65 |
+
return await RedirectService.android_momo_redirect(query_params)
|
| 66 |
+
|
| 67 |
+
@router.get("/android/group/momo")
|
| 68 |
+
async def return_url_handler_group_momo(request: Request):
|
| 69 |
+
query_params = request.query_params
|
| 70 |
+
return await RedirectService.android_group_momo_redirect(query_params)
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
from urllib.parse import urlencode
|
| 74 |
+
@router.get("/web/momo")
|
| 75 |
+
async def return_url_handler_web(request: Request):
|
| 76 |
+
query_params = request.query_params
|
| 77 |
+
return await RedirectService.web_momo_redirect(query_params)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
from typing import Dict
|
| 81 |
+
@router.get("/android/vnpay")
|
| 82 |
+
async def vnpay_ipn_redirect(request: Request):
|
| 83 |
+
params: Dict[str, str] = dict(request.query_params)
|
| 84 |
+
return await RedirectService.android_vnpay_ipn_redirect(params)
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@router.get("/android/group/vnpay")
|
| 88 |
+
async def vnpay_ipn_redirect(request: Request):
|
| 89 |
+
params: Dict[str, str] = dict(request.query_params)
|
| 90 |
+
return await RedirectService.android_vnpay_group_ipn_redirect(params)
|
| 91 |
+
|
| 92 |
+
@router.get("/web/vnpay")
|
| 93 |
+
async def vnpay_ipn_redirect(request: Request):
|
| 94 |
+
query_params = request.query_params
|
| 95 |
+
return await RedirectService.web_vnpay_ipn_redirect(query_params)
|
controller/__init__.py
ADDED
|
File without changes
|
controller/__pycache__/CallbackController.cpython-311.pyc
ADDED
|
Binary file (11.1 kB). View file
|
|
|
controller/__pycache__/ChatController.cpython-311.pyc
ADDED
|
Binary file (8.65 kB). View file
|
|
|
controller/__pycache__/GroupOrderController.cpython-311.pyc
ADDED
|
Binary file (2.15 kB). View file
|
|
|
controller/__pycache__/RecommendController.cpython-311.pyc
ADDED
|
Binary file (2.08 kB). View file
|
|
|
controller/__pycache__/RedirectController.cpython-311.pyc
ADDED
|
Binary file (7 kB). View file
|
|
|
controller/__pycache__/__init__.cpython-311.pyc
ADDED
|
Binary file (167 Bytes). View file
|
|
|