auth-system.canvas 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. {
  2. "nodes": [
  3. {
  4. "id": "title",
  5. "type": "text",
  6. "text": "# 认证鉴权系统设计\n\n**方案**: JWT + D1\n**状态**: 📋 设计中\n\n特点:\n- 无状态 JWT 认证\n- 基于角色的访问控制 (RBAC)\n- 细粒度资源权限\n- 边缘计算友好",
  7. "x": -100,
  8. "y": -600,
  9. "width": 380,
  10. "height": 160,
  11. "color": "6"
  12. },
  13. {
  14. "id": "auth_flow",
  15. "type": "text",
  16. "text": "## 认证流程\n\n```\n1. 登录请求\n POST /api/auth/login\n { username, password }\n ↓\n2. 验证密码\n bcrypt.compare()\n ↓\n3. 生成 JWT\n { userId, role, exp }\n ↓\n4. 返回 Token\n { accessToken, refreshToken }\n ↓\n5. 客户端存储\n Authorization: Bearer <token>\n```",
  17. "x": -500,
  18. "y": -380,
  19. "width": 300,
  20. "height": 340,
  21. "color": "2"
  22. },
  23. {
  24. "id": "jwt_structure",
  25. "type": "text",
  26. "text": "## JWT 结构\n\n**Header**\n```json\n{ \"alg\": \"HS256\", \"typ\": \"JWT\" }\n```\n\n**Payload**\n```json\n{\n \"sub\": \"user-id\",\n \"username\": \"admin\",\n \"role\": \"admin\",\n \"iat\": 1704067200,\n \"exp\": 1704153600\n}\n```\n\n**有效期**\n- accessToken: 24h\n- refreshToken: 7d",
  27. "x": -130,
  28. "y": -380,
  29. "width": 300,
  30. "height": 320,
  31. "color": "4"
  32. },
  33. {
  34. "id": "rbac",
  35. "type": "text",
  36. "text": "## RBAC 角色权限\n\n| 角色 | 权限 |\n|------|------|\n| admin | 全部操作 |\n| operator | 摄像头管理、直播控制 |\n| viewer | 仅查看 |\n\n**资源权限**\n```\nuser_permissions 表\n- view: 查看摄像头\n- control: 控制直播\n- manage: 管理配置\n```",
  37. "x": 240,
  38. "y": -380,
  39. "width": 280,
  40. "height": 280,
  41. "color": "3"
  42. },
  43. {
  44. "id": "middleware",
  45. "type": "text",
  46. "text": "## 中间件设计\n\n**authMiddleware**\n- 解析 Bearer Token\n- 验证 JWT 签名\n- 检查过期时间\n- 注入 c.set('user', payload)\n\n**requireRole(roles)**\n- 检查用户角色\n- 403 Forbidden\n\n**requirePermission(resource, action)**\n- 查询 user_permissions\n- 检查资源权限",
  47. "x": -500,
  48. "y": 20,
  49. "width": 300,
  50. "height": 260,
  51. "color": "5"
  52. },
  53. {
  54. "id": "api_endpoints",
  55. "type": "text",
  56. "text": "## API 端点\n\n**认证**\n- POST /api/auth/login\n- POST /api/auth/register\n- POST /api/auth/refresh\n- POST /api/auth/logout\n- GET /api/auth/me\n\n**用户管理 (admin)**\n- GET /api/users\n- GET /api/users/:id\n- POST /api/users\n- PUT /api/users/:id\n- DELETE /api/users/:id\n\n**权限管理 (admin)**\n- GET /api/users/:id/permissions\n- POST /api/users/:id/permissions\n- DELETE /api/permissions/:id",
  57. "x": -130,
  58. "y": 0,
  59. "width": 300,
  60. "height": 340,
  61. "color": "2"
  62. },
  63. {
  64. "id": "file_structure",
  65. "type": "text",
  66. "text": "## 文件结构\n\n```\nsrc/\n├── middleware/\n│ └── auth.ts\n├── services/\n│ ├── auth.ts\n│ └── user.ts\n├── routes/\n│ ├── auth.ts\n│ └── user.ts\n├── utils/\n│ ├── jwt.ts\n│ └── password.ts\n└── types/\n └── index.ts\n```",
  67. "x": 240,
  68. "y": -40,
  69. "width": 280,
  70. "height": 280,
  71. "color": "4"
  72. },
  73. {
  74. "id": "security",
  75. "type": "text",
  76. "text": "## 安全措施\n\n- 密码 bcrypt 哈希 (cost=12)\n- JWT 密钥环境变量\n- HTTPS 强制\n- Rate Limiting\n- 登录失败锁定\n- Audit Log 记录\n- CORS 白名单",
  77. "x": -500,
  78. "y": 340,
  79. "width": 300,
  80. "height": 180,
  81. "color": "1"
  82. },
  83. {
  84. "id": "env_config",
  85. "type": "text",
  86. "text": "## 环境配置\n\n**.dev.vars**\n```\nJWT_SECRET=your-secret-key\nJWT_EXPIRES_IN=86400\nREFRESH_EXPIRES_IN=604800\n```\n\n**wrangler secret**\n```bash\nwrangler secret put JWT_SECRET\n```",
  87. "x": -130,
  88. "y": 400,
  89. "width": 300,
  90. "height": 180,
  91. "color": "4"
  92. },
  93. {
  94. "id": "protected_routes",
  95. "type": "text",
  96. "text": "## 路由保护\n\n**公开路由**\n- /api/auth/login\n- /api/auth/register\n- / (健康检查)\n\n**需要认证**\n- /api/stream/*\n- /api/users/*\n- /api/auth/me\n\n**需要 admin**\n- POST/PUT/DELETE /api/users\n- /api/permissions/*",
  97. "x": 240,
  98. "y": 300,
  99. "width": 280,
  100. "height": 240,
  101. "color": "3"
  102. }
  103. ],
  104. "edges": [
  105. {
  106. "id": "edge_flow_jwt",
  107. "fromNode": "auth_flow",
  108. "fromSide": "right",
  109. "toNode": "jwt_structure",
  110. "toSide": "left",
  111. "label": "生成"
  112. },
  113. {
  114. "id": "edge_jwt_rbac",
  115. "fromNode": "jwt_structure",
  116. "fromSide": "right",
  117. "toNode": "rbac",
  118. "toSide": "left",
  119. "label": "携带 role"
  120. },
  121. {
  122. "id": "edge_mw_api",
  123. "fromNode": "middleware",
  124. "fromSide": "right",
  125. "toNode": "api_endpoints",
  126. "toSide": "left",
  127. "label": "保护"
  128. },
  129. {
  130. "id": "edge_api_files",
  131. "fromNode": "api_endpoints",
  132. "fromSide": "right",
  133. "toNode": "file_structure",
  134. "toSide": "left",
  135. "label": "实现"
  136. },
  137. {
  138. "id": "edge_mw_security",
  139. "fromNode": "middleware",
  140. "fromSide": "bottom",
  141. "toNode": "security",
  142. "toSide": "top",
  143. "label": "安全策略"
  144. }
  145. ]
  146. }