|
9 | 9 | from django.http import HttpResponse |
10 | 10 | from django.utils.deprecation import MiddlewareMixin |
11 | 11 |
|
| 12 | +from common.auth import handles, TokenDetails |
| 13 | + |
12 | 14 | content = """ |
13 | | -<!doctype html> |
| 15 | +<!DOCTYPE html> |
14 | 16 | <html lang="en"> |
15 | 17 | <head> |
16 | 18 | <meta charset="UTF-8" /> |
17 | 19 | <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
18 | 20 | <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
19 | 21 | <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> |
20 | 39 | <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(); |
38 | 69 | } |
| 70 | + } catch (e) { |
| 71 | + reject(false); |
39 | 72 | } |
| 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"); |
40 | 87 |
|
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 | + }; |
46 | 106 | </script> |
47 | | - </head> |
48 | | - <body></body> |
| 107 | + </body> |
49 | 108 | </html> |
50 | 109 |
|
51 | 110 | """ |
|
54 | 113 | class DocHeadersMiddleware(MiddlewareMixin): |
55 | 114 | def process_response(self, request, response): |
56 | 115 | 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: |
59 | 118 | 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) |
62 | 130 | return response |
0 commit comments