2025-07-12 03:41:50 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: maxkb
|
|
|
|
|
|
@Author:虎
|
|
|
|
|
|
@file: static_headers_middleware.py
|
|
|
|
|
|
@date:2024/3/13 18:26
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
2025-07-15 05:49:33 +00:00
|
|
|
|
|
2025-07-12 03:41:50 +00:00
|
|
|
|
from django.http import HttpResponse
|
|
|
|
|
|
from django.utils.deprecation import MiddlewareMixin
|
|
|
|
|
|
|
2025-07-15 05:49:33 +00:00
|
|
|
|
from common.auth import TokenDetails, handles
|
|
|
|
|
|
from maxkb.const import CONFIG
|
|
|
|
|
|
|
2025-07-12 03:41:50 +00:00
|
|
|
|
content = """
|
|
|
|
|
|
<!doctype html>
|
|
|
|
|
|
<html lang="en">
|
|
|
|
|
|
<head>
|
|
|
|
|
|
<meta charset="UTF-8" />
|
|
|
|
|
|
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
|
|
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
|
|
|
|
|
<title>Document</title>
|
|
|
|
|
|
<script>
|
2025-07-15 05:49:33 +00:00
|
|
|
|
function setCookie(name, value, days) {
|
|
|
|
|
|
var expires = "";
|
|
|
|
|
|
if (days) {
|
|
|
|
|
|
var date = new Date();
|
|
|
|
|
|
date.setTime(date.getTime() + (days*2));
|
|
|
|
|
|
expires = "; expires=" + date.toUTCString();
|
|
|
|
|
|
}
|
|
|
|
|
|
document.cookie = name + "=" + (value || "") + expires + "; path=/";
|
|
|
|
|
|
}
|
2025-07-12 03:41:50 +00:00
|
|
|
|
window.onload = () => {
|
|
|
|
|
|
var xhr = new XMLHttpRequest()
|
2025-07-15 05:49:33 +00:00
|
|
|
|
xhr.open('GET', '/api/user/profile', true)
|
2025-07-12 03:41:50 +00:00
|
|
|
|
|
|
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/json')
|
|
|
|
|
|
const token = localStorage.getItem('token')
|
|
|
|
|
|
const pathname = window.location.pathname
|
|
|
|
|
|
if (token) {
|
2025-07-15 05:49:33 +00:00
|
|
|
|
xhr.setRequestHeader('Authorization', 'Bearer '+token)
|
2025-07-12 03:41:50 +00:00
|
|
|
|
xhr.onreadystatechange = function () {
|
|
|
|
|
|
if (xhr.readyState === 4) {
|
|
|
|
|
|
if (xhr.status === 200) {
|
2025-07-15 05:49:33 +00:00
|
|
|
|
setCookie("Authorization",'Bearer '+token)
|
2025-07-12 03:41:50 +00:00
|
|
|
|
window.location.href = pathname
|
|
|
|
|
|
}
|
|
|
|
|
|
if (xhr.status === 401) {
|
|
|
|
|
|
window.location.href = '/admin/login'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
xhr.send()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
window.location.href = '/admin/login'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
</head>
|
|
|
|
|
|
<body></body>
|
|
|
|
|
|
</html>
|
|
|
|
|
|
|
2025-07-15 05:49:33 +00:00
|
|
|
|
""".replace("/api/user/profile", CONFIG.get_admin_path() + '/api/user/profile').replace('/admin/login',
|
|
|
|
|
|
CONFIG.get_admin_path() + '/login')
|
2025-07-12 03:41:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class DocHeadersMiddleware(MiddlewareMixin):
|
|
|
|
|
|
def process_response(self, request, response):
|
2025-07-15 05:49:33 +00:00
|
|
|
|
if request.path.startswith('/doc/') or request.path.startswith('/doc_chat/'):
|
|
|
|
|
|
auth = request.COOKIES.get('Authorization')
|
|
|
|
|
|
if auth is None:
|
2025-07-12 03:41:50 +00:00
|
|
|
|
return HttpResponse(content)
|
2025-07-15 05:49:33 +00:00
|
|
|
|
else:
|
|
|
|
|
|
if not auth.startswith("Bearer "):
|
|
|
|
|
|
return HttpResponse(content)
|
|
|
|
|
|
try:
|
|
|
|
|
|
token = auth[7:]
|
|
|
|
|
|
token_details = TokenDetails(token)
|
|
|
|
|
|
for handle in handles:
|
|
|
|
|
|
if handle.support(request, token, token_details.get_token_details):
|
|
|
|
|
|
handle.handle(request, token, token_details.get_token_details)
|
|
|
|
|
|
return response
|
|
|
|
|
|
return HttpResponse(content)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
return HttpResponse(content)
|
2025-07-12 03:41:50 +00:00
|
|
|
|
return response
|