Skip to content

Task Phase1 01

Notification

  • phase: phase1
  • id: task-phase1-01
  • module: User-Management
  • Depends on: None
  • Reference: 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.py
  • code:
    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 được python manage.py makemigrations mượ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_until
    
  • business_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 đếm failed_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, PermissionDenied
  • log_format: System Audit Log ghi dưới dạng chuỗi JSON vào bảng DB users_audit_log với format {"action": "LOGIN_FAILED", "ip": "192.168.1.1", "username": "admin"}.