Skip to content

feat(auth): トークン認証・HTTPS対応 #331#334

Merged
Kewton merged 5 commits intodevelopfrom
feature/331-worktree
Feb 21, 2026
Merged

feat(auth): トークン認証・HTTPS対応 #331#334
Kewton merged 5 commits intodevelopfrom
feature/331-worktree

Conversation

@Kewton
Copy link
Owner

@Kewton Kewton commented Feb 21, 2026

Summary

Issue #331 の実装:トークン認証(ログイン機能)と HTTPS 直接配信モードの追加。

  • 後方互換性維持: commandmate start(認証なし)は従来と同じ動作
  • トークン認証: commandmate start --auth でランダムトークンを生成・表示し、全 API ルート・WebSocket 接続を保護
  • HTTPS 対応: --cert/--key オプションで HTTPS サーバーとして起動
  • ブルートフォース対策: IP ベースのレート制限(5 回失敗 → 15 分ロックアウト)
  • セキュリティ: crypto.timingSafeEqual() によるタイミング攻撃対策、パスの完全一致マッチング

主な変更ファイル

新規作成

  • src/lib/auth.ts — コア認証モジュール(トークン生成・検証・レート制限・Cookie パース)
  • src/middleware.ts — Next.js 認証ミドルウェア(CM_AUTH_TOKEN_HASH 未設定時は即 NextResponse.next()
  • src/app/api/auth/login/route.ts — ログイン API
  • src/app/api/auth/logout/route.ts — ログアウト API
  • src/app/api/auth/status/route.ts — 認証状態確認 API
  • src/app/login/page.tsx — ログイン画面(i18n 対応・ロックアウト表示)
  • src/components/common/LogoutButton.tsx — ログアウトボタン(認証有効時のみ表示)
  • locales/en/auth.json / locales/ja/auth.json — 認証関連翻訳

主要変更

  • server.tshttps.createServer() 条件分岐・証明書バリデーション・gracefulShutdown 拡張
  • src/lib/ws-server.ts — WebSocket 接続認証追加・引数型を http.Server | https.Server に拡張
  • src/cli/commands/start.ts--auth/--auth-expire/--https/--cert/--key/--allow-http オプション追加
  • src/cli/utils/daemon.ts — 認証・HTTPS 環境変数を子プロセスに伝達
  • src/cli/types/index.tsStartOptions に認証フィールド追加
  • src/i18n.tsauth 名前空間追加
  • tsconfig.server.jsonsrc/lib/auth.ts を include に追加
  • docs/security-guide.md — mkcert 手順・クイックスタート追記

Test plan

  • npm run test:unit — 3724 tests pass(新規 89 テスト追加)
  • npm run test:integration — auth-middleware / ws-auth / i18n 統合テスト pass
  • npx tsc --noEmit — 型エラー 0 件
  • npm run lint — ESLint エラー 0 件
  • npm run build / build:server / build:cli — ビルド成功
  • 受入条件 37/37 件充足

Security

  • S001: crypto.timingSafeEqual() によるタイミング攻撃対策 ✅
  • S002: 認証除外パスは === 完全一致マッチング(startsWith 禁止)✅
  • C001: src/lib/auth.ts に Next.js 固有モジュール依存なし(CLI ビルド互換性)✅
  • Cookie: HttpOnly + SameSite=Strict + HTTPS 時 Secure
  • CSRF: SameSite=Strict により防止 ✅

Commits

  • feat(auth): implement token authentication and HTTPS support
  • refactor(auth): improve code quality of token auth and HTTPS modules
  • docs(auth): add token authentication and HTTPS setup documentation
  • chore(deps): update vitest to 4.0.16

🤖 Generated with Claude Code

Kewton and others added 5 commits February 21, 2026 14:40
- Add src/lib/auth.ts core module (generateToken, hashToken, verifyToken
  with timingSafeEqual, parseDuration, parseCookies, createRateLimiter)
- Add src/middleware.ts for Next.js request authentication
- Add auth API routes (login, logout, status)
- Add login page with i18n, rate limit display, and redirect logic
- Add WebSocket authentication via Cookie header in ws-server.ts
- Add HTTPS support in server.ts with certificate validation
- Add CLI options: --auth, --auth-expire, --cert, --key, --allow-http
- Add LogoutButton component in sidebar (desktop + mobile drawer)
- Add auth i18n namespace (en/ja)
- Update .env.example with auth/HTTPS documentation
- All 89 new tests pass (unit + integration)
- All 3724 existing unit tests pass

Resolves #331

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Apply DRY, readability, and maintainability improvements to Issue #331 code:

- Extract buildAuthCookieOptions() to centralize cookie security settings (login/logout DRY)
- Extract isHttpsEnabled() helper for CM_HTTPS_CERT environment check
- Extract DEFAULT_COOKIE_MAX_AGE_SECONDS constant (remove magic number 86400)
- Extract MS_PER_MINUTE/MS_PER_HOUR/MS_PER_DAY time constants (eliminate magic numbers)
- Replace switch-case in parseDuration() with data-driven unitMultipliers lookup
- Simplify rate limiter cleanup condition (remove redundant null check)
- Remove dead code (empty if-body) in daemon.ts auth env forwarding
- Add 'as const' to authEnvKeys array in daemon.ts for type narrowing
- Extract displayAuthToken() in start.ts (remove duplicated token display logic)
- Extract isExpectedWebSocketError() in ws-server.ts (remove duplicated error patterns)
- Add JSDoc to getClientIp(), displayAuthToken(), isExpectedWebSocketError()
- Add AuthCookieOptions interface with C001 constraint documentation

Security constraints maintained:
- S001: crypto.timingSafeEqual() for token verification
- S002: AUTH_EXCLUDED_PATHS exact match (===)
- C001: No Next.js module dependencies in auth.ts

Quality: tsc 0 errors, ESLint 0 errors, 3724/3724 tests pass

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add Quick Start section for token auth + HTTPS to security-guide.md
- Add mkcert certificate generation instructions for macOS and Linux
- Add Linux CA certificate distribution procedures
- Update Security Checklist with built-in auth options
- Update Migration from CM_AUTH_TOKEN section (--auth warning note)
- Add src/lib/auth.ts and src/middleware.ts to CLAUDE.md module list

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
- Add design policy: issue-331-token-auth-design-policy.md
- Add multi-stage design review reports (stage1-4)
- Add multi-stage issue review reports
- Add work plan: work-plan.md
- Add pm-auto-dev iteration-1 reports

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Kewton Kewton merged commit 26caf64 into develop Feb 21, 2026
5 checks passed
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.

1 participant