Skip to content

Commit 8b5f9ff

Browse files
committed
fix: Access can only be granted after OpenAPI authentication
1 parent c2f52d0 commit 8b5f9ff

File tree

1 file changed

+81
-34
lines changed

1 file changed

+81
-34
lines changed

apps/common/middleware/doc_headers_middleware.py

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,99 @@
1414
from maxkb.const import CONFIG
1515

1616
content = """
17-
<!doctype html>
17+
<!DOCTYPE html>
1818
<html lang="en">
1919
<head>
2020
<meta charset="UTF-8" />
2121
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
2222
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
2323
<title>Document</title>
24-
<script>
25-
function setCookie(name, value, days) {
26-
var expires = "";
27-
if (days) {
28-
var date = new Date();
29-
date.setTime(date.getTime() + (days*2));
30-
expires = "; expires=" + date.toUTCString();
24+
</head>
25+
<style>
26+
/* 弹框内容样式 */
27+
.modal-content {
28+
background-color: #fefefe;
29+
margin: 15% auto; /* 15% 从顶部和自动水平居中 */
30+
padding: 20px;
31+
border: 1px solid #888;
32+
width: 80%; /* 宽度 */
3133
}
32-
document.cookie = name + "=" + (value || "") + expires + "; path=/";
33-
}
34-
window.onload = () => {
35-
var xhr = new XMLHttpRequest()
36-
xhr.open('GET', '/api/user/profile', true)
34+
</style>
35+
<body>
36+
<div class="modal-content">
37+
<input type="text" id="auth-input" />
38+
<button id="auth">认证</button>
39+
<button id="goLogin">去登录</button>
40+
</div>
41+
<script>
42+
const setCookie = (name, value, days) => {
43+
var expires = "";
44+
if (days) {
45+
var date = new Date();
46+
date.setTime(date.getTime() + days * 2);
47+
expires = "; expires=" + date.toUTCString();
48+
}
49+
document.cookie = name + "=" + (value || "") + expires + "; path=/";
50+
};
51+
const authToken = (token) => {
52+
return new Promise((resolve, reject) => {
53+
try {
54+
var xhr = new XMLHttpRequest();
55+
xhr.open("GET", "/api/user/profile", true);
56+
xhr.setRequestHeader("Content-Type", "application/json");
57+
const pathname = window.location.pathname;
58+
if (token) {
59+
xhr.setRequestHeader("Authorization", "Bearer " + token);
60+
xhr.onreadystatechange = function () {
61+
if (xhr.readyState === 4) {
62+
if (xhr.status === 200) {
63+
resolve(true);
64+
} else {
65+
reject(true);
66+
}
67+
}
68+
};
3769
38-
xhr.setRequestHeader('Content-Type', 'application/json')
39-
const token = localStorage.getItem('token')
40-
const pathname = window.location.pathname
41-
if (token) {
42-
xhr.setRequestHeader('Authorization', 'Bearer '+token)
43-
xhr.onreadystatechange = function () {
44-
if (xhr.readyState === 4) {
45-
if (xhr.status === 200) {
46-
setCookie("Authorization",'Bearer '+token)
47-
window.location.href = pathname
48-
}
49-
if (xhr.status === 401) {
50-
window.location.href = '/admin/login'
51-
}
70+
xhr.send();
5271
}
72+
} catch (e) {
73+
reject(false);
5374
}
75+
});
76+
};
77+
window.onload = () => {
78+
const token = localStorage.getItem("token");
79+
authToken(token)
80+
.then(() => {
81+
setCookie("Authorization", "Bearer " + token);
82+
window.location.href = window.location.pathname;
83+
})
84+
.catch((e) => {});
85+
};
86+
// 获取元素
87+
const auth = document.getElementById("auth");
88+
const goLogin = document.getElementById("goLogin");
5489
55-
xhr.send()
56-
} else {
57-
window.location.href = '/admin/login'
58-
}
59-
}
90+
// 打开弹框函数
91+
auth.onclick = ()=> {
92+
const authInput = document.getElementById("auth-input");
93+
const token = authInput.value
94+
authToken(token)
95+
.then(() => {
96+
setCookie("Authorization", "Bearer " + token);
97+
window.location.href = window.location.pathname;
98+
})
99+
.catch((e) => {
100+
alert("令牌错误");
101+
});
102+
};
103+
104+
// 去系统的登录页面
105+
goLogin.onclick = ()=> {
106+
window.location.href = "/admin/login";
107+
};
60108
</script>
61-
</head>
62-
<body></body>
109+
</body>
63110
</html>
64111
65112
""".replace("/api/user/profile", CONFIG.get_admin_path() + '/api/user/profile').replace('/admin/login',

0 commit comments

Comments
 (0)