Skip to content

feat(auth): close auth-flow error-code gaps (G1-G6) + 9 catalog entries - #10

Merged
voidvec merged 2 commits into
masterfrom
feat/auth-flow-error-code-gaps
Jul 7, 2026
Merged

voidvec merged 2 commits into
masterfrom
feat/auth-flow-error-code-gaps

Conversation

@voidvec

@voidvec voidvec commented Jul 7, 2026

Copy link
Copy Markdown
Owner

概述

修复 error-code-message-standardization 发版后遗留的认证流错误信息缺口:注册 / MFA / WebAuthn / 设备授权 / 密码重置 / 邮箱验证 / 限流等控制器把多种不同失败原因折叠进同一个笼统 Error_Code(多为 VALIDATION_INVALID_INPUT),导致用户看不出具体失败原因(如注册时看不出是用户名冲突还是邮箱冲突)。

实现依据 .kiro/specs/auth-flow-error-code-gaps/(requirements / design / tasks)。

改动内容

新增 9 条 Error_Catalog 条目(Req 8)

code numeric HTTP
VALIDATION_USERNAME_TAKEN 3006 409
VALIDATION_EMAIL_TAKEN 3007 409
VALIDATION_CREDENTIAL_ALREADY_REGISTERED 3008 409
VALIDATION_RESET_TOKEN_INVALID 3009 400
VALIDATION_VERIFICATION_TOKEN_INVALID 3010 400
VALIDATION_DEVICE_CODE_INVALID 3011 400
VALIDATION_RATE_LIMITED 3012 429
AUTH_MFA_CODE_INVALID 4004 401
AUTH_MFA_NOT_CONFIGURED 4005 401

后端 gap 路由(G1–G5)

  • G1 注册:AuthService::registerUser 回调从自由文本改为结构化 errorCode;按 DB 约束名(users_username_key / idx_users_email_unique)路由到精确冲突码(用户名冲突优先),未识别约束兜底 VALIDATION_INVALID_INPUT;基础设施失败改用 INTERNAL_ERROR。SessionController 直接转发 errorCode,不再硬编码回退。
  • G2 MFA:MfaController::verifySetup 未配置 → AUTH_MFA_NOT_CONFIGURED,TOTP 错误 → AUTH_MFA_CODE_INVALID。
  • G3 WebAuthn:registerFinish 检测 credential_id 唯一约束冲突 → VALIDATION_CREDENTIAL_ALREADY_REGISTERED,否则保留 DB_QUERY_ERROR。
  • G4 设备授权:approveDevice 的 affectedRows()==0 → VALIDATION_DEVICE_CODE_INVALID。
  • G5 token:密码重置 / 邮箱验证 token 失效 → 专用码,保留防枚举行为(不区分未找到/已过期/已用)。

G6 Hodor 限流 Envelope 化

main.cc 在确认 Hodor 加载后调用 setRejectResponseFactory,用 ErrorResponder::buildResponse + VALIDATION_RATE_LIMITED 取代纯文本拒绝体;插件未加载时跳过,不阻断启动(Req 6.4–6.7)。

关键缺陷修复:ErrorResponder::toDrogonStatus 原 switch 不支持 HTTP 429(落入 default → 500),新增 case 429: k429TooManyRequests,这是 VALIDATION_RATE_LIMITED 能返回正确 429 的前提。

前端同步(Req 9)

OAuth2Frontend / OAuth2Admin 的 zh-CN.ts 各追加相同的 9 条词条(跨应用逐字一致)。

G7 账户锁定(刻意保留,未改动)

AuthService::validateUser 锁定分支继续返回与密码错误相同的 AUTH_INVALID_CREDENTIALS,用于防止用户名枚举。

计划执行中发现并修复的必要同步

  • docs/backend/api-reference.md §5.1:追加 9 行表格 + 更新 override 说明段
  • ErrorCatalogPropertyTest::httpStatusOverrideFor():加入 4 个新 override 码
  • ErrorCatalogRegressionTest:总数断言 16 → 25

验证

测试套件 结果
后端 ./manage.ps1 full-test -debug(8 步全流程) ✅ 8/8 步通过
后端单元测试 ✅ 243 cases / 56872 assertions
后端 OAuth2 + Admin API(真实服务器) ✅ 全过
OAuth2Frontend npm run test:unit(vitest) ✅ 4 files / 15 tests
OAuth2Frontend npx playwright test(E2E) ✅ 98 passed
OAuth2Admin npm run test:unit(vitest) ✅ 1 file / 2 tests
OAuth2Admin npx playwright test(E2E) ✅ 163 passed / 6 skipped

0 失败。G6 Hodor 接线 + ErrorResponder 429 支持 + 各控制器路由在真实运行时环境与两个前端真实浏览器环境下均无回归。

注意

  • G7(账户锁定防枚举)按设计保留不改,仅通过既有测试固化不变量。
  • 本 PR 含一个独立 commit (e24907c) 更新 CLAUDE.md / README.md 的构建/测试命令说明(标注 Debug/Release、补充前端测试入口),与错误码特性无功能关联,但属同一开发周期产物。

voidvec added 2 commits July 7, 2026 13:57
修复 error-code-message-standardization 发版后遗留的认证流错误信息缺口:
注册/MFA/WebAuthn/设备授权/密码重置/邮箱验证/限流等控制器把多种不同失败
原因折叠进同一个笼统 Error_Code,导致用户看不出具体失败原因。

新增 9 条 Error_Catalog 条目(Req 8):
  VALIDATION_USERNAME_TAKEN(3006,409)/EMAIL_TAKEN(3007,409)/
  CREDENTIAL_ALREADY_REGISTERED(3008,409)/RESET_TOKEN_INVALID(3009)/
  VERIFICATION_TOKEN_INVALID(3010)/DEVICE_CODE_INVALID(3011)/
  RATE_LIMITED(3012,429)/AUTH_MFA_CODE_INVALID(4004)/MFA_NOT_CONFIGURED(4005)

后端 gap 路由(G1-G5):
- AuthService::registerUser 回调从自由文本改为结构化 errorCode;按 DB
  约束名(users_username_key/idx_users_email_unique)路由到精确冲突码,
  用户名冲突优先;基础设施失败改用 INTERNAL_ERROR。SessionController
  直接转发 errorCode,不再硬编码 VALIDATION_INVALID_INPUT (G1)
- MfaController::verifySetup 未配置→AUTH_MFA_NOT_CONFIGURED,
  TOTP 错误→AUTH_MFA_CODE_INVALID (G2)
- WebAuthnController::registerFinish 检测 credential_id 唯一约束冲突→
  VALIDATION_CREDENTIAL_ALREADY_REGISTERED (G3)
- DeviceAuthController::approveDevice→VALIDATION_DEVICE_CODE_INVALID (G4)
- PasswordReset/EmailVerification token 失效→专用码,保留防枚举行为 (G5)

G6 Hodor 限流 Envelope 化:
- main.cc 在确认 Hodor 加载后调用 setRejectResponseFactory,用
  ErrorResponder::buildResponse + VALIDATION_RATE_LIMITED 取代纯文本
  拒绝体;插件未加载时跳过,不阻断启动 (G6)
- ErrorResponder::toDrogonStatus 新增 429 case(原 switch 不支持 429,
  会落入 default→500,是本特性 429 码生效的前提)

前端同步(Req 9):OAuth2Frontend/OAuth2Admin zh-CN.ts 各追加相同 9 条

计划遗漏的必要同步(执行中发现并修复):
- docs/backend/api-reference.md §5.1 表格追加 9 行 + 更新 override 说明
  (被 ErrorCatalogDocTest 断言一一对应)
- ErrorCatalogPropertyTest httpStatusOverrideFor() 加入 4 个新 override 码
- ErrorCatalogRegressionTest 总数断言 16→25

验证:后端 243 test cases / 56872 assertions 全过;
前端 messageCatalog + crossAppConsistency property 测试 5/5 全过。
G7(账户锁定防枚举)为刻意保留的设计,未改动。
- CLAUDE.md: 标注 run-backend/test-backend/full-test 的 Release/Debug 用法;
  补充两个前端项目 (OAuth2Frontend/OAuth2Admin) 的 npx playwright test (E2E)
  与 npm run test:unit (单元) 命令入口
- README.md: 后端启动路径默认改为 Release 构建产物
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@voidvec
voidvec merged commit 4b64b16 into master Jul 7, 2026
3 checks passed
voidvec added a commit that referenced this pull request Jul 29, 2026
)

The authorization_code, refresh_token, and client_credentials paths left
OAuth2AccessToken::issuedAt at its 0 default (only device_code and GitHub
already set it). RFC 7662 introspection silently omits `iat` when issuedAt
is 0, so introspected tokens had no issue timestamp and admin token listings
sorted by issued_at were wrong. Set issuedAt = now at all three remaining
issuance points.

Test: TokenIssuedAtIntrospectionTest -- issue a client_credentials token,
introspect it (Bearer passes the OAuth2AuthFilter gate; client creds in the
body), assert `iat` is present, non-zero, and a recent timestamp.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@voidvec
voidvec deleted the feat/auth-flow-error-code-gaps branch July 31, 2026 06:17
voidvec added a commit that referenced this pull request Aug 30, 2026
…loor, bootstrap self-heal, review cleanups

Address PR #141 review (1 must-fix, 8 should-fix, quick considers):

- #1 consent_csrf declared as a required query parameter in openapi.yaml
  and the C++ endpoint doc registration; apps/server/docs/api/openapi.json
  regenerated from a live server run; Python/Go clients regenerated.
- #2 nonce one-shot is now atomic: the find/verify/erase sequence is
  serialized by a process-wide mutex (consent traffic is tiny; in-process
  session storage).
- #3 identity AuthService no longer captures this in the async validateUser
  callback — the legacy-hash policy flag is value-captured
  (db-operations rule 4).
- #4/#12 AdminBootstrapper reworked as ensure-* steps: an existing admin
  user row is reused and role/mapping completed (partial failures self-heal
  on restart); the random password is printed only after the insert
  succeeded; a mapping failure no longer reports created=true.
- #5 POST /api/admin/users enforces auth.min_password_length
  (VALIDATION_PASSWORD_TOO_SHORT) — closing the admin bypass.
- #6 consent integration tests reworked: missing-nonce now drives the real
  authorize-minted slot (400), deny-to-unregistered-uri (400, no Location)
  and deny-to-registered-uri (302 access_denied) branches are covered.
- #7/#8 deployment.md: auto-bootstrap wording fixed, bogus ADMIN_SALT
  removed (PBKDF2 keeps salt empty); website i18n error table mirrored
  (3014/4007).
- #9 main.cc bootstrap probes with backoff (5 attempts) and LOG_WARN on
  failure instead of a single silent 2s probe.
- #13 unique-violation detection layered (SqlError 23505 -> constraint
  name -> english/zh localized text), mirroring the
  PostgresSocialAccountRepository pattern.
- #11 gate-4 comment corrected; #15 CHANGELOG duplicate Unreleased merged;
  #17 AGENTS.md consent.csp entry removed.

Considered-but-not-taken: #10 multi-slot nonces (tracked follow-up), #14
admin UI hardcode (server now enforces; hint stays), #18 MFA-pending
session through consent (pre-existing authorize behavior, tracked).
voidvec added a commit that referenced this pull request Aug 30, 2026
…loor, bootstrap self-heal, review cleanups

Address PR #141 review (1 must-fix, 8 should-fix, quick considers):

- #1 consent_csrf declared as a required query parameter in openapi.yaml
  and the C++ endpoint doc registration; apps/server/docs/api/openapi.json
  regenerated from a live server run; Python/Go clients regenerated.
- #2 nonce one-shot is now atomic: the find/verify/erase sequence is
  serialized by a process-wide mutex (consent traffic is tiny; in-process
  session storage).
- #3 identity AuthService no longer captures this in the async validateUser
  callback — the legacy-hash policy flag is value-captured
  (db-operations rule 4).
- #4/#12 AdminBootstrapper reworked as ensure-* steps: an existing admin
  user row is reused and role/mapping completed (partial failures self-heal
  on restart); the random password is printed only after the insert
  succeeded; a mapping failure no longer reports created=true.
- #5 POST /api/admin/users enforces auth.min_password_length
  (VALIDATION_PASSWORD_TOO_SHORT) — closing the admin bypass.
- #6 consent integration tests reworked: missing-nonce now drives the real
  authorize-minted slot (400), deny-to-unregistered-uri (400, no Location)
  and deny-to-registered-uri (302 access_denied) branches are covered.
- #7/#8 deployment.md: auto-bootstrap wording fixed, bogus ADMIN_SALT
  removed (PBKDF2 keeps salt empty); website i18n error table mirrored
  (3014/4007).
- #9 main.cc bootstrap probes with backoff (5 attempts) and LOG_WARN on
  failure instead of a single silent 2s probe.
- #13 unique-violation detection layered (SqlError 23505 -> constraint
  name -> english/zh localized text), mirroring the
  PostgresSocialAccountRepository pattern.
- #11 gate-4 comment corrected; #15 CHANGELOG duplicate Unreleased merged;
  #17 AGENTS.md consent.csp entry removed.

Considered-but-not-taken: #10 multi-slot nonces (tracked follow-up), #14
admin UI hardcode (server now enforces; hint stays), #18 MFA-pending
session through consent (pre-existing authorize behavior, tracked).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants