Spaces:
Running
Running
vlff李飞飞
commited on
Commit
·
522b6de
1
Parent(s):
dbadc8c
add token file
Browse files
qwen_server/assistant_server.py
CHANGED
@@ -25,6 +25,7 @@ mem = Memory(llm=llm, stream=False)
|
|
25 |
|
26 |
cache_file = os.path.join(server_config.path.cache_root, 'browse.jsonl')
|
27 |
cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')
|
|
|
28 |
|
29 |
PAGE_URL = {}
|
30 |
|
@@ -70,6 +71,18 @@ def set_page_url(access_token):
|
|
70 |
|
71 |
def initialize(request: gr.Request):
|
72 |
access_token = request.query_params["access_token"] or request.session["access_token"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
set_page_url(access_token)
|
74 |
return access_token
|
75 |
|
|
|
25 |
|
26 |
cache_file = os.path.join(server_config.path.cache_root, 'browse.jsonl')
|
27 |
cache_file_popup_url = os.path.join(server_config.path.cache_root, 'popup_url.jsonl')
|
28 |
+
access_token_file = os.path.join(server_config.path.cache_root, 'access_token.jsonl')
|
29 |
|
30 |
PAGE_URL = {}
|
31 |
|
|
|
71 |
|
72 |
def initialize(request: gr.Request):
|
73 |
access_token = request.query_params["access_token"] or request.session["access_token"]
|
74 |
+
is_valid = False
|
75 |
+
if access_token:
|
76 |
+
if os.getenv("ACCESS_TOKEN") == access_token:
|
77 |
+
is_valid = True
|
78 |
+
else:
|
79 |
+
for line in jsonlines.open(access_token_file):
|
80 |
+
if line['access_token'] == access_token:
|
81 |
+
is_valid = True
|
82 |
+
break
|
83 |
+
if not is_valid:
|
84 |
+
gr.Info("The token is not valid, Please reset!")
|
85 |
+
return
|
86 |
set_page_url(access_token)
|
87 |
return access_token
|
88 |
|
qwen_server/database_server.py
CHANGED
@@ -32,7 +32,7 @@ origins = [
|
|
32 |
'http://' + get_local_ip() + ':' +
|
33 |
str(server_config.server.workstation_port),
|
34 |
]
|
35 |
-
|
36 |
app.add_middleware(
|
37 |
CORSMiddleware,
|
38 |
# allow_origins=origins,
|
@@ -80,10 +80,19 @@ def change_checkbox_state(text, cache_file, access_token):
|
|
80 |
|
81 |
|
82 |
@app.middleware("http")
|
83 |
-
async def
|
84 |
# print(f"Request URL path: {request.url}")
|
85 |
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
|
86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
return Response(status_code=401, content="the token is not valid")
|
88 |
request.session.setdefault("access_token", access_token)
|
89 |
return await call_next(request)
|
@@ -94,8 +103,27 @@ async def healthz(request: Request):
|
|
94 |
return JSONResponse({"healthz": True})
|
95 |
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
@app.get('/cachedata/{file_name}')
|
98 |
-
async def cache_data(file_name: str):
|
|
|
|
|
|
|
99 |
cache_file = os.path.join(server_config.path.cache_root, file_name)
|
100 |
lines = []
|
101 |
for line in jsonlines.open(cache_file):
|
|
|
32 |
'http://' + get_local_ip() + ':' +
|
33 |
str(server_config.server.workstation_port),
|
34 |
]
|
35 |
+
access_token_file = os.path.join(server_config.path.cache_root, 'access_token.jsonl')
|
36 |
app.add_middleware(
|
37 |
CORSMiddleware,
|
38 |
# allow_origins=origins,
|
|
|
80 |
|
81 |
|
82 |
@app.middleware("http")
|
83 |
+
async def access_token_auth(request: Request, call_next):
|
84 |
# print(f"Request URL path: {request.url}")
|
85 |
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
|
86 |
+
is_valid = False
|
87 |
+
if access_token:
|
88 |
+
if os.getenv("ACCESS_TOKEN") == access_token:
|
89 |
+
is_valid = True
|
90 |
+
else:
|
91 |
+
for line in jsonlines.open(access_token_file):
|
92 |
+
if line['access_token'] == access_token:
|
93 |
+
is_valid = True
|
94 |
+
break
|
95 |
+
if not is_valid:
|
96 |
return Response(status_code=401, content="the token is not valid")
|
97 |
request.session.setdefault("access_token", access_token)
|
98 |
return await call_next(request)
|
|
|
103 |
return JSONResponse({"healthz": True})
|
104 |
|
105 |
|
106 |
+
@app.post('/token/add')
|
107 |
+
async def add_token(request: Request):
|
108 |
+
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
|
109 |
+
if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
110 |
+
return Response(status_code=401, content="the token is not valid")
|
111 |
+
data = await request.json()
|
112 |
+
lines = []
|
113 |
+
for line in jsonlines.open(access_token_file):
|
114 |
+
lines.append(line)
|
115 |
+
lines.append(data)
|
116 |
+
with jsonlines.open(access_token_file, mode='w') as writer:
|
117 |
+
for new_line in lines:
|
118 |
+
writer.write(new_line)
|
119 |
+
return JSONResponse({"success": True})
|
120 |
+
|
121 |
+
|
122 |
@app.get('/cachedata/{file_name}')
|
123 |
+
async def cache_data(request: Request, file_name: str):
|
124 |
+
access_token: str = request.headers.get("Authorization") or request.query_params.get("access_token") or request.session.get("access_token")
|
125 |
+
if not access_token or os.getenv("ACCESS_TOKEN") != access_token:
|
126 |
+
return Response(status_code=401, content="the token is not valid")
|
127 |
cache_file = os.path.join(server_config.path.cache_root, file_name)
|
128 |
lines = []
|
129 |
for line in jsonlines.open(cache_file):
|
workspace/browser_cache/access_token.jsonl
ADDED
File without changes
|