Files
Lottery/tests/mock_auth_server.py
vincent dcc7339384 feat(BIZ-110): index.html 前端鉴权适配 - localStorage + login 页 + Bearer 注入
方案 A 落地(严维序 COO 09-01 14:46 GMT+8 确认):
- 新增登录浮层(#/login 概念,hash 路由由 bootstrap() 管控)
- localStorage.lotto_api_token 存/取/清 token
- api() 函数自动注入 'Authorization: Bearer <token>'
- 401 → 清 token + 跳登录页
- Header 右侧 '退出' 按钮
- bootstrap() 启动入口:无 token 跳登录,有 token 进主页

测试:tests/mock_auth_server.py + jsdom 21/21 全过。
闭环:前端适配完成,待严维序撤 AUTH_BYPASS_PATHS 白名单。

—— 徐聪 (costcodev) | 2026-09-02 16:35 GMT+8

Co-authored-by: multica-agent <github@multica.ai>
2026-09-02 16:45:43 +08:00

52 lines
2.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
BIZ-110 端到端 mock:模拟后端鉴权 + 前端 token 注入验证。
- GET /api/status: 永远 200,无 token 校验(status 探测)
- 其他 /api/* : 必须 Authorization: Bearer <token>,token = 'test-token-ok' 通过
"""
import http.server, json, sys
VALID_TOKEN = 'test-token-ok'
class H(http.server.BaseHTTPRequestHandler):
def log_message(self, fmt, *a): pass # quiet
def _send(self, code, payload):
body = json.dumps(payload).encode()
self.send_response(code)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(body)))
self.end_headers()
self.wfile.write(body)
def _check_auth(self):
h = self.headers.get('Authorization', '')
if not h.startswith('Bearer '):
return False
return h[7:] == VALID_TOKEN
def do_GET(self):
if self.path.startswith('/api/status'):
return self._send(200, {'success': True, 'data': {'ok': True}})
if self.path.startswith('/api/records'):
if not self._check_auth():
return self._send(401, {'success': False, 'error': '未授权访问'})
return self._send(200, {'success': True, 'data': {'records': [], 'total': 0, 'page': 1}})
return self._send(404, {'success': False, 'error': 'not found'})
def do_POST(self):
if self.path.startswith('/api/records') and 'fake-id' in self.path:
return self._send(404, {'success': False, 'error': 'not found'})
return self._send(404, {'success': False, 'error': 'not found'})
def do_DELETE(self):
if not self._check_auth():
return self._send(401, {'success': False, 'error': '未授权访问'})
return self._send(200, {'success': True, 'data': {}})
if __name__ == '__main__':
port = int(sys.argv[1]) if len(sys.argv) > 1 else 18085
srv = http.server.HTTPServer(('127.0.0.1', port), H)
print(f'mock listening on {port}')
srv.serve_forever()