diff --git a/web/auth/README.md b/web/auth/README.md new file mode 100644 index 0000000..199c875 --- /dev/null +++ b/web/auth/README.md @@ -0,0 +1,63 @@ +# web/auth — iAOP 登录页与本地账号会话管理(issue #150 / PRD 8.2) + +登录认证是配置台/驾驶舱写操作的入口闸门。未登录用户不可访问写操作(PRD 8.2)。 + +## 组成 + +**前端(纯静态)** +- `login.html` / `auth.css` / `auth.js` — 深色主题登录页,对接 `/auth/login`、`/auth/me`; + `auth.js` 导出 `IAOP_AUTH.requireLoginElseRedirect()` 供其它页面做路由守卫。 + +**后端(core/auth,纯标准库)** +- `users.py` — `User` 模型 + `PBKDF2-HMAC-SHA256` 密码哈希(盐 16B / 迭代 200000, + OWASP 2023 量级)+ `UserStore`(内存,可换 PG 后端)。恒定时间校验防时序侧信道。 +- `session.py` — HMAC 签名会话 token(`..`),HttpOnly cookie `iaop_session`。 +- `postgres_users_schema.py` — PostgreSQL `users` 表 DDL(`BIGSERIAL id` / `username UNIQUE` / + `password_hash` / `role CHECK(readonly|engineer|admin)` / `active` / 时间戳),对齐 #30。 +- `auth_api.py` — 认证 HTTP 端点(`POST /auth/login` `POST /auth/logout` `GET /auth/me`)+ + `require_auth` / `can_write` 守卫(未登录 401、readonly 写 403,PRD 8.2)。 +- `tests/test_auth.py` — 单元测试。 + +## 跑测试 + +```bash +# 仓库根目录 +python -m pytest core/auth/tests/test_auth.py -v +# 或无 pytest: +python core/auth/tests/test_auth.py +``` + +## 冒烟(认证服务) + +```bash +python -m core.auth.auth_api +# → iAOP AuthAPI on http://127.0.0.1:8088(初始管理员 admin / change-me-now,生产必须改密) +``` + +```bash +curl -s -X POST http://127.0.0.1:8088/auth/login -H 'Content-Type: application/json' \ + -d '{"username":"admin","password":"change-me-now"}' -c /tmp/c.txt +curl -s http://127.0.0.1:8088/auth/me -b /tmp/c.txt +``` + +## 前端冒烟 + +```bash +cd web/auth && python -m http.server 8090 +# 浏览器开 http://localhost:8090/login.html(AUTH_BASE 指向 :8088 见 auth.js) +``` + +## 角色(对齐 core/template-console/rbac.py) + +| 角色 | 读 | 配置写 | 发布/回滚 | +|------|----|--------|----------| +| readonly | ✓ | ✗ | ✗ | +| engineer | ✓ | ✓ | ✗ | +| admin | ✓ | ✓ | ✓ | + +## 安全 + +- 永不存明文密码;存储 `pbkdf2_sha256$$$`。 +- `authenticate` 失败不区分"用户不存在/密码错",防用户名枚举。 +- token HMAC 恒定时间校验;cookie `HttpOnly; SameSite=Lax`。 +- 生产必须设置 `IAOP_AUTH_SECRET` 环境变量(多副本共享)并改初始管理员密码。