Skip to content

Kiến trúc

1. System Context (C4 L1)

Identity là trust root — mọi JWT trong nền tảng đều được service này ký. JWKS tại /jw-certs là endpoint công khai mà các sister service dùng để xác minh chữ ký.

2. Container View (C4 L2)

3. Component View (C4 L3) — Phân lớp nội bộ

LớpTrách nhiệm
Routes12 base path trong RestPaths
ControllersAuth gate (JWT / BASIC) + permission
ServicesBusiness logic (auth + RBAC + OTP + quản lý user)
RepositoriesTruy vấn Drizzle; lookup theo identifier
ComponentsMail / SMS / WebSocket
Token serviceJWKSIssuerTokenService từ @nx/core (ES256 signer)

4. Máy trạng thái

Identity có rất ít state-machine; phần lớn flow là chuỗi validation thay vì máy trạng thái persistent.

EntityTrạng tháiGhi chú
User.statusACTIVATED / DEACTIVATED / LOCKEDguard cho sign-in
UserIdentifier.verifiedfalsetruexác thực qua OTP/token
Role.typeSYSTEM / CUSTOM / UNKNOWNBản ghi SYSTEM bất biến

5. Kịch bản Runtime

5.1 Đăng nhập (BASIC scheme)

5.2 Đăng ký

5.3 Email OTP — Flow xác thực Email

5.4 Sister Service xác minh JWKS

6. Vấn đề xuyên suốt

Vấn đềCách service xử lý
Ký tokenES256 qua JWKSIssuerTokenService (từ @nx/core); private key từ environment / secure config
Xác minh tokenKHÔNG thực hiện ở đây — sister service pull JWKS qua HTTP và xác minh tại chỗ
Lưu trữ mật khẩuBun.password hashing (kiểu argon2, mặc định nội bộ)
Lưu trữ OTPRedis với TTL — mã đã hash + bộ đếm số lần thử + cooldown + hạn ngạch hàng ngày
i18nTemplates Mail/SMS có EN + VI; locale của user quyết định khi gửi
LoggingIGNIS structured (key: %s); request-id được truyền xuyên suốt
AuthZCasbin + cạnh PolicyDefinition; permissions cache trong Redis
Soft-deleteSoftDeletableRepositorydeletedAt; tính duy nhất của identifier giới hạn ở deletedAt IS NULL
IDsSnowflake qua IdGenerator, worker 1

7. Trang liên quan

Proprietary and Confidential. Unauthorized copying, distribution, or use of this software is strictly prohibited.