Skip to content

Commit d9d7264

Browse files
authored
fix: Modify document authentication method (#4006)
1 parent b3cc8e2 commit d9d7264

File tree

1 file changed

+97
-29
lines changed

1 file changed

+97
-29
lines changed

apps/common/middleware/doc_headers_middleware.py

Lines changed: 97 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,102 @@
99
from django.http import HttpResponse
1010
from django.utils.deprecation import MiddlewareMixin
1111

12+
from common.auth import handles, TokenDetails
13+
1214
content = """
13-
<!doctype html>
15+
<!DOCTYPE html>
1416
<html lang="en">
1517
<head>
1618
<meta charset="UTF-8" />
1719
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
1820
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
1921
<title>Document</title>
22+
</head>
23+
<style>
24+
/* 弹框内容样式 */
25+
.modal-content {
26+
background-color: #fefefe;
27+
margin: 15% auto; /* 15% 从顶部和自动水平居中 */
28+
padding: 20px;
29+
border: 1px solid #888;
30+
width: 80%; /* 宽度 */
31+
}
32+
</style>
33+
<body>
34+
<div class="modal-content">
35+
<input type="text" id="auth-input" />
36+
<button id="auth">认证</button>
37+
<button id="goLogin">去登录</button>
38+
</div>
2039
<script>
21-
window.onload = () => {
22-
var xhr = new XMLHttpRequest()
23-
xhr.open('GET', '/api/user', true)
24-
25-
xhr.setRequestHeader('Content-Type', 'application/json')
26-
const token = localStorage.getItem('token')
27-
const pathname = window.location.pathname
28-
if (token) {
29-
xhr.setRequestHeader('Authorization', token)
30-
xhr.onreadystatechange = function () {
31-
if (xhr.readyState === 4) {
32-
if (xhr.status === 200) {
33-
window.location.href = pathname
34-
}
35-
if (xhr.status === 401) {
36-
window.location.href = '/ui/login'
37-
}
40+
const setCookie = (name, value, days) => {
41+
var expires = "";
42+
if (days) {
43+
var date = new Date();
44+
date.setTime(date.getTime() + days * 2);
45+
expires = "; expires=" + date.toUTCString();
46+
}
47+
document.cookie = name + "=" + (value || "") + expires + "; path=/";
48+
};
49+
const authToken = (token) => {
50+
return new Promise((resolve, reject) => {
51+
try {
52+
var xhr = new XMLHttpRequest();
53+
xhr.open("GET", "/api/user", true);
54+
xhr.setRequestHeader("Content-Type", "application/json");
55+
const pathname = window.location.pathname;
56+
if (token) {
57+
xhr.setRequestHeader("Authorization", token);
58+
xhr.onreadystatechange = function () {
59+
if (xhr.readyState === 4) {
60+
if (xhr.status === 200) {
61+
resolve(true);
62+
} else {
63+
reject(true);
64+
}
65+
}
66+
};
67+
68+
xhr.send();
3869
}
70+
} catch (e) {
71+
reject(false);
3972
}
73+
});
74+
};
75+
window.onload = () => {
76+
const token = localStorage.getItem("token");
77+
authToken(token)
78+
.then(() => {
79+
setCookie("Authorization", token);
80+
window.location.href = window.location.pathname;
81+
})
82+
.catch((e) => {});
83+
};
84+
// 获取元素
85+
const auth = document.getElementById("auth");
86+
const goLogin = document.getElementById("goLogin");
4087
41-
xhr.send()
42-
} else {
43-
window.location.href = '/ui/login'
44-
}
45-
}
88+
// 打开弹框函数
89+
auth.onclick = ()=> {
90+
const authInput = document.getElementById("auth-input");
91+
const token = authInput.value
92+
authToken(token)
93+
.then(() => {
94+
setCookie("Authorization", token);
95+
window.location.href = window.location.pathname;
96+
})
97+
.catch((e) => {
98+
alert("令牌错误");
99+
});
100+
};
101+
102+
// 去系统的登录页面
103+
goLogin.onclick = ()=> {
104+
window.location.href = "/ui/login";
105+
};
46106
</script>
47-
</head>
48-
<body></body>
107+
</body>
49108
</html>
50109
51110
"""
@@ -54,9 +113,18 @@
54113
class DocHeadersMiddleware(MiddlewareMixin):
55114
def process_response(self, request, response):
56115
if request.path.startswith('/doc/') or request.path.startswith('/doc/chat/'):
57-
HTTP_REFERER = request.META.get('HTTP_REFERER')
58-
if HTTP_REFERER is None:
116+
auth = request.COOKIES.get('Authorization')
117+
if auth is None:
59118
return HttpResponse(content)
60-
if HTTP_REFERER == request._current_scheme_host + request.path:
61-
return response
119+
else:
120+
try:
121+
token = auth
122+
token_details = TokenDetails(token)
123+
for handle in handles:
124+
if handle.support(request, token, token_details.get_token_details):
125+
handle.handle(request, token, token_details.get_token_details)
126+
return response
127+
return HttpResponse(content)
128+
except Exception as e:
129+
return HttpResponse(content)
62130
return response

0 commit comments

Comments
 (0)