|
@@ -394,6 +394,20 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
def _err(self, msg, code=400):
|
|
def _err(self, msg, code=400):
|
|
|
self._json({"ok": False, "error": msg}, code)
|
|
self._json({"ok": False, "error": msg}, code)
|
|
|
|
|
|
|
|
|
|
+ def _is_https(self):
|
|
|
|
|
+ """判断这次请求是不是 HTTPS(可能被 nginx 反代,所以看 X-Forwarded-Proto)"""
|
|
|
|
|
+ if (self.headers.get("X-Forwarded-Proto") or "").lower() == "https":
|
|
|
|
|
+ return True
|
|
|
|
|
+ try:
|
|
|
|
|
+ import ssl
|
|
|
|
|
+ return isinstance(self.connection, ssl.SSLSocket)
|
|
|
|
|
+ except Exception:
|
|
|
|
|
+ return False
|
|
|
|
|
+
|
|
|
|
|
+ def _cookie(self, value, max_age):
|
|
|
|
|
+ return "sid=%s; Path=/; Max-Age=%d; HttpOnly; SameSite=Lax%s" % (
|
|
|
|
|
+ value, max_age, "; Secure" if self._is_https() else "")
|
|
|
|
|
+
|
|
|
def _cookies(self):
|
|
def _cookies(self):
|
|
|
out = {}
|
|
out = {}
|
|
|
for part in (self.headers.get("Cookie") or "").split(";"):
|
|
for part in (self.headers.get("Cookie") or "").split(";"):
|
|
@@ -468,8 +482,7 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
"role": u["role"],
|
|
"role": u["role"],
|
|
|
"display_name": u["display_name"]}).encode()
|
|
"display_name": u["display_name"]}).encode()
|
|
|
return self._send(200, payload, "application/json; charset=utf-8", {
|
|
return self._send(200, payload, "application/json; charset=utf-8", {
|
|
|
- "Set-Cookie": "sid=%s; Path=/; Max-Age=%d; HttpOnly; SameSite=Lax"
|
|
|
|
|
- % (token, SESSION_DAYS * 86400)})
|
|
|
|
|
|
|
+ "Set-Cookie": self._cookie(token, SESSION_DAYS * 86400)})
|
|
|
|
|
|
|
|
def _user_create(self):
|
|
def _user_create(self):
|
|
|
try:
|
|
try:
|
|
@@ -714,7 +727,7 @@ class Handler(BaseHTTPRequestHandler):
|
|
|
db.delete_session(self.sid)
|
|
db.delete_session(self.sid)
|
|
|
return self._send(200, json.dumps({"ok": True}).encode(),
|
|
return self._send(200, json.dumps({"ok": True}).encode(),
|
|
|
"application/json; charset=utf-8",
|
|
"application/json; charset=utf-8",
|
|
|
- {"Set-Cookie": "sid=; Path=/; Max-Age=0; HttpOnly; SameSite=Lax"})
|
|
|
|
|
|
|
+ {"Set-Cookie": self._cookie("", 0)})
|
|
|
if u.path == "/api/users":
|
|
if u.path == "/api/users":
|
|
|
return self._user_create()
|
|
return self._user_create()
|
|
|
if u.path == "/api/import":
|
|
if u.path == "/api/import":
|