Skip to content

Commit dd5abf8

Browse files
committed
fix(manito): 핀 정규화 로직 의존성 추가
1 parent 2a967f9 commit dd5abf8

File tree

3 files changed

+41
-16
lines changed

3 files changed

+41
-16
lines changed

src/main/java/inha/gdgoc/domain/manito/service/ManitoAdminService.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class ManitoAdminService {
3232
private final ManitoSessionRepository sessionRepository;
3333
private final ManitoAssignmentRepository assignmentRepository;
3434
private final PasswordEncoder passwordEncoder;
35+
private final ManitoPinPolicy manitoPinPolicy; // ✅ PIN 정책 주입
3536

3637
/**
3738
* 간단 CSV escape (콤마/따옴표/줄바꿈 포함 시 따옴표 감싸기)
@@ -136,15 +137,9 @@ public void importParticipantsCsv(String sessionCode, MultipartFile file) {
136137

137138
String studentId = cleanCsvField(cols[studentIdx]);
138139
String name = cleanCsvField(cols[nameIdx]);
139-
String pinPlain = cleanCsvField(cols[pinIdx]);
140-
pinPlain = pinPlain.replaceAll("\\D", ""); // 숫자만 추출
140+
String pinRaw = cleanCsvField(cols[pinIdx]);
141141

142-
if (pinPlain.length() > 4) {
143-
pinPlain = pinPlain.substring(0, 4); // 혹시 4자리 넘으면 앞 4자리
144-
}
145-
146-
// zero padding to 4 digits
147-
pinPlain = String.format("%04d", Integer.parseInt(pinPlain));
142+
String pinPlain = manitoPinPolicy.normalize(pinRaw);
148143

149144
name = name.replace("`", "").trim();
150145

@@ -153,13 +148,6 @@ public void importParticipantsCsv(String sessionCode, MultipartFile file) {
153148
continue;
154149
}
155150

156-
// 여기서 PIN 길이 정책은 네가 선택
157-
// - 그대로 쓰기
158-
// - 숫자만 남기고 4자리 zero padding 하기 등
159-
// ex) 숫자만 추출:
160-
// pinPlain = pinPlain.replaceAll("\\D", "");
161-
// if (pinPlain.length() < 4) { ... }
162-
163151
String pinHash = passwordEncoder.encode(pinPlain);
164152

165153
var existingOpt = assignmentRepository.findBySessionAndStudentId(session, studentId);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package inha.gdgoc.domain.manito.service;
2+
3+
import inha.gdgoc.global.exception.BusinessException;
4+
import inha.gdgoc.global.exception.GlobalErrorCode;
5+
import org.springframework.stereotype.Component;
6+
7+
@Component
8+
public class ManitoPinPolicy {
9+
public String normalize(String rawPin) {
10+
if (rawPin == null) {
11+
throw new BusinessException(GlobalErrorCode.BAD_REQUEST, "PIN 값이 비어 있습니다.");
12+
}
13+
14+
// 숫자만 추출
15+
String digits = rawPin.replaceAll("\\D", "");
16+
17+
if (digits.isEmpty()) {
18+
throw new BusinessException(GlobalErrorCode.BAD_REQUEST, "PIN 값에는 적어도 1자리 이상의 숫자가 있어야 합니다.");
19+
}
20+
21+
// 4자리 zero-padding
22+
try {
23+
int asInt = Integer.parseInt(digits);
24+
return String.format("%04d", asInt);
25+
} catch (NumberFormatException e) {
26+
throw new BusinessException(GlobalErrorCode.BAD_REQUEST, "PIN 형식이 올바르지 않습니다.");
27+
}
28+
}
29+
}

src/main/java/inha/gdgoc/domain/manito/service/ManitoUserService.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public class ManitoUserService {
1717
private final ManitoSessionRepository sessionRepository;
1818
private final ManitoAssignmentRepository assignmentRepository;
1919
private final PasswordEncoder passwordEncoder;
20+
private final ManitoPinPolicy manitoPinPolicy; // ✅ PIN 정책 주입
2021

2122
/**
2223
* pin 검증 후 암호문 반환
@@ -29,7 +30,14 @@ public String verifyAndGetCipher(String sessionCode, String studentId, String pi
2930
ManitoAssignment assignment = assignmentRepository.findBySessionAndStudentId(session, studentId)
3031
.orElseThrow(() -> new BusinessException(GlobalErrorCode.RESOURCE_NOT_FOUND, "해당 학번은 세션에 참여하지 않았습니다."));
3132

32-
if (!passwordEncoder.matches(pinPlain, assignment.getPinHash())) {
33+
// ✅ Admin 쪽과 동일한 규칙으로 PIN 정규화
34+
String normalizedPin = manitoPinPolicy.normalize(pinPlain);
35+
36+
if (normalizedPin.isEmpty()) {
37+
throw new BusinessException(GlobalErrorCode.BAD_REQUEST, "PIN 형식이 올바르지 않습니다.");
38+
}
39+
40+
if (!passwordEncoder.matches(normalizedPin, assignment.getPinHash())) {
3341
throw new BusinessException(GlobalErrorCode.FORBIDDEN_USER, "PIN이 일치하지 않습니다.");
3442
}
3543

0 commit comments

Comments
 (0)