Task Phase1 01
Notification
phase: phase1id: task-phase1-01module: User-ManagementDepends on: NoneReference: SPEC.md, DESIGN.md, PLAN.md
Background
Tính năng User Management là nhóm bảo mật cốt lõi. Trong Phase 1, chúng ta cần thiết lập Custom User model, tích hợp thuật toán băm mật khẩu Argon2, thư viện SimpleJWT để xử lý Token, và xây dựng 3 API cơ bản: Login, Refresh Token, User Info. Đồng thời cài đặt system audit log ghi nhận lịch sử đăng nhập.
Code Snippets/Path file code cần sửa
path:./MES-Backend/users/models.pycode:from django.contrib.auth.models import AbstractBaseUser class CustomUser(AbstractBaseUser): username = models.CharField(max_length=150, unique=True) failed_login_attempts = models.IntegerField(default=0) locked_until = models.DateTimeField(null=True, blank=True) # ...business_logic: Định nghĩa schema CustomUser kế thừa AbstractBaseUser để tùy chỉnh trường dữ liệu bảo mật (quản lý block user).-
acceptance_criteria: Phải chạy đượcpython manage.py makemigrationsmượt mà và lưu vào PostgreSQL thành công. -
path:./MES-Backend/users/views.py code:class CustomTokenObtainPairView(TokenObtainPairView): # custom logic to log attempt and check locked_untilbusiness_logic: Xác thực credentials của user, kiểm tra tài khoản có đang bị khóa (locked_until) không. Nếu hợp lệ, sinh Access và Refresh token. Nếu sai, tăng biến đếmfailed_login_attempts.acceptance_criteria: Đăng nhập đúng trả về JWT hợp lệ. Quá 5 lần sai sẽ khóa tài khoản 15 phút.
Technical Acceptance Criteria
http_status: 200 OK (Thành công), 401 Unauthorized (Sai mật khẩu), 403 Forbidden (Tài khoản bị khóa).exception:AuthenticationFailed,PermissionDeniedlog_format: System Audit Log ghi dưới dạng chuỗi JSON vào bảng DBusers_audit_logvới format{"action": "LOGIN_FAILED", "ip": "192.168.1.1", "username": "admin"}.