Skip to content

Task 1-2: Review và Re-enable validateCode() trong signIn()

Phase: 1 - Security Priority: Medium Module: authentication Depends on: Không có Blocks: task-4-1 Reference: docs/BountyHunter-Backend/details/feature-authentication/SPEC.md

Background

validateCode() trong signIn() bị comment out trong AuthService. Chưa rõ lý do — nếu unintentional thì có security hole: bất kỳ code nào cũng pass.

Tasks

File: authentication/controllers/auth/AuthService.java

DI / Context note: validateCode() là một private method của chính AuthService — không cần inject từ bên ngoài. Nó nhận tham số UserModel user (được lấy từ DB trước đó trong signIn()) và String code (từ request body). Đảm bảo UserModel có các getter: getAuthCode()getExpireAuthCode() (kiểu Instant). Import cần thêm nếu chưa có: java.time.Instant.

  • [ ] Tìm và đọc logic validateCode():

    private void validateCode(UserModel user, String code) {
        // Check: code match + not expired
        if (!user.getAuthCode().equals(code)) throw new BadRequestException("INVALID_CODE");
        if (Instant.now().isAfter(user.getExpireAuthCode())) throw new BadRequestException("CODE_EXPIRED");
    }
    

  • [ ] Xác nhận lý do comment out (check git blame / PR history)

  • Nếu có lý do hợp lệ → thêm comment giải thích rõ ràng
  • Nếu unintentional → re-enable

  • [ ] Re-enable (nếu cần):

    // Trước:
    // this.validateCode(user, baseMap.get("code"));
    
    // Sau:
    this.validateCode(user, baseMap.get("code"));
    

  • [ ] Test trên staging với code đúng và code sai trước khi merge

Verification / Acceptance Criteria

  • [ ] Compile check: AuthService.java compile thành công — UserModelgetAuthCode()getExpireAuthCode(), import java.time.Instant tồn tại.
  • [ ] Valid code: POST /api/auth/sign-in với code đúng và còn hạn → login thành công, nhận UserAuthResponse.
  • [ ] Invalid code: POST /api/auth/sign-in với code sai → nhận lỗi INVALID_CODE (HTTP 400).
  • [ ] Expired code: POST /api/auth/sign-in với code đã hết hạn (expireAuthCode trong quá khứ) → nhận lỗi CODE_EXPIRED (HTTP 400).
  • [ ] Test reference: Scenario "expired auth code" trong task-4-3 pass sau khi thay đổi.

Files to Modify

  • authentication/src/main/java/com/figpop/authentication/controllers/auth/AuthService.java