|
| 1 | +# Implementation Plan: OAuth2 Authentication (Google + GitHub) |
| 2 | + |
| 3 | +## Overview |
| 4 | +Thêm xác thực xã hội (OAuth2) cho Google và GitHub để người dùng đăng nhập/đăng ký an toàn bằng tài khoản bên ngoài. |
| 5 | + |
| 6 | +## Tasks |
| 7 | +- [ ] Thêm dependencies: passport, passport-google-oauth20, passport-github2, express-session |
| 8 | +- [ ] Tạo routes: `/auth/google`, `/auth/google/callback`, `/auth/github`, `/auth/github/callback` |
| 9 | +- [ ] Cấu hình Passport strategies cho Google & GitHub |
| 10 | +- [ ] Lưu session / token (secure cookie / DB session) |
| 11 | +- [ ] UI: login button + callback handling |
| 12 | +- [ ] Viết unit & integration tests |
| 13 | +- [ ] Cập nhật docs / env vars (.env.example) |
| 14 | +- [ ] QA & rollout (canary → production) |
| 15 | + |
| 16 | +## Detailed Steps |
| 17 | +1. Install: |
| 18 | + - npm i passport passport-google-oauth20 passport-github2 express-session |
| 19 | +2. Env: |
| 20 | + - thêm GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, SESSION_SECRET |
| 21 | +3. Backend: |
| 22 | + - `src/auth/passport.ts`: cấu hình serializeUser/deserializeUser và strategies |
| 23 | + - `src/routes/auth.ts`: implement routes, redirect to provider, handle callbacks, create/find user in DB |
| 24 | + - Ví dụ route: `GET /auth/google` → `passport.authenticate('google', { scope: ['profile','email'] })` |
| 25 | +4. Session: |
| 26 | + - Sử dụng express-session, lưu sessions vào Redis/DB nếu production |
| 27 | +5. Frontend: |
| 28 | + - Thêm nút “Sign in with Google/GitHub”, xử lý redirect và hiển thị user info |
| 29 | +6. Tests: |
| 30 | + - Unit: strategy config, user upsert logic |
| 31 | + - Integration: simulate OAuth callback (stub), end-to-end login flow |
| 32 | +7. Security: |
| 33 | + - Kiểm tra callback origin, dùng HTTPS, set secure cookie flags, validate scopes |
| 34 | +8. Rollout: |
| 35 | + - Merge -> deploy on staging, smoke test login, enable for 5% canary users, monitor logs & errors, full rollout |
| 36 | + |
| 37 | +## Acceptance Criteria |
| 38 | +- Người dùng có thể đăng nhập bằng Google hoặc GitHub |
| 39 | +- Tokens/session lưu an toàn, không lộ secret |
| 40 | +- Tests coverage cho logic auth >= 80% |
| 41 | +- Docs (.env.example) cập nhật |
| 42 | + |
| 43 | +## Estimated time |
| 44 | +- Dev: 1.5–2 days |
| 45 | +- Tests & QA: 0.5–1 day |
| 46 | +- Rollout & monitoring: 0.5 day |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +# Security checklist — Tokens / Sessions |
| 51 | + |
| 52 | +Dưới đây là checklist chi tiết để đảm bảo tokens và session được lưu trữ an toàn và không lộ secret. |
| 53 | + |
| 54 | +- Secrets: lưu mọi secret ngoài mã nguồn (env vars, GitHub Secrets, Vault). Ghi rõ biến env cần set (GOOGLE_CLIENT_ID, SESSION_SECRET, ...). |
| 55 | +- Session store: dùng Redis/DB production-grade (không dùng in-memory). Kết nối Redis qua TLS, giới hạn truy cập mạng. |
| 56 | +- Cookie flags: HttpOnly, Secure, SameSite=Lax/Strict; set đúng domain/path và expiry. |
| 57 | +- Token lifecycle: access token ngắn hạn; dùng refresh token có rotation; hạn chế scope. |
| 58 | +- At-rest encryption: mã hóa trường token trong DB khi cần tuân thủ. |
| 59 | +- No secrets in logs: mask/strip tokens & secrets trước khi log; review log config. |
| 60 | +- Transport security: HTTPS everywhere, HSTS, TLS 1.2+; force redirect HTTP→HTTPS. |
| 61 | +- CSRF & clickjacking: CSRF token cho các POST/PUT; X-Frame-Options header. |
| 62 | +- Input validation: kiểm tra redirect_uri, origin, state parameter khi xử lý callback. |
| 63 | +- Rate limiting & brute-force: giới hạn attempts trên callback/login endpoints; block abusive IPs. |
| 64 | +- Rotation & revocation: có endpoint `/admin/revoke-session` và kế hoạch rotate SESSION_SECRET/keys. |
| 65 | +- Secrets lifecycle: periodic rotation, audit trail, and emergency rotation playbook. |
| 66 | +- CI static checks: git-secrets/truffleHog, secret-scan trong CI; fail build on secret leak. |
| 67 | +- Dependency security: Dependabot or equivalent; run SCA & vuln scans (Snyk/OSS). |
| 68 | +- Monitoring & alerts: instrument auth flows, track 5xx, token error rates, and alert on anomalies. |
| 69 | +- Tests: unit tests cho auth logic, integration tests simulating OAuth callbacks, end-to-end smoke tests on staging. |
| 70 | +- Canary & rollout: deploy to staging → canary (5% users) → full; monitor logs and error budgets. |
| 71 | +- Acceptance criteria (security): no secrets in repo, secure cookies, token rotation validated, tests pass, monitoring alerts baseline acceptable. |
| 72 | + |
| 73 | +--- |
| 74 | + |
| 75 | +*END* |
0 commit comments