Skip to content

Commit

Permalink
feat(AttendeeService): 비밀번호 암호화 추가로 인한 주요 Service 로직 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ikjo39 committed Sep 12, 2024
1 parent 6be03f6 commit f7cc204
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 25 deletions.
17 changes: 9 additions & 8 deletions backend/src/main/java/kr/momo/domain/attendee/Attendee.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;

@Table(name = "attendee")
@Entity
Expand Down Expand Up @@ -46,17 +47,17 @@ public class Attendee extends BaseEntity {
@Column(nullable = false, length = 10)
private Role role;

public Attendee(Meeting meeting, String name, String password, Role role) {
this(meeting, new AttendeeName(name), new AttendeePassword(password), role);
}

public Attendee(Meeting meeting, AttendeeName name, AttendeePassword password, Role role) {
this.meeting = meeting;
this.name = name;
this.password = password;
this.role = role;
}

public Attendee(Meeting meeting, String name, String password, Role role) {
this(meeting, new AttendeeName(name), new AttendeePassword(password), role);
}

public boolean isHost() {
return role.isHost();
}
Expand All @@ -65,15 +66,15 @@ public boolean isNotHost() {
return !isHost();
}

public void verifyPassword(AttendeePassword other) {
this.password.verifyPassword(other);
public void verifyPassword(AttendeePassword rawPassword, PasswordEncoder passwordEncoder) {
password.matchWithRawPassword(rawPassword, passwordEncoder);
}

public String name() {
return this.name.getName();
return name.getName();
}

public String password() {
return this.password.getPassword();
return password.getPassword();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import kr.momo.service.attendee.dto.AttendeeLoginResponse;
import kr.momo.service.auth.JwtManager;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -24,6 +25,7 @@ public class AttendeeService {
private final AttendeeRepository attendeeRepository;
private final MeetingRepository meetingRepository;
private final JwtManager jwtManager;
private final PasswordEncoder passwordEncoder;

@Transactional
public AttendeeLoginResponse login(String uuid, AttendeeLoginRequest request) {
Expand All @@ -39,7 +41,7 @@ public AttendeeLoginResponse login(String uuid, AttendeeLoginRequest request) {
}

private AttendeeLoginResponse verifyPassword(Attendee attendee, AttendeePassword password) {
attendee.verifyPassword(password);
attendee.verifyPassword(password, passwordEncoder);
return AttendeeLoginResponse.from(jwtManager.generate(attendee.getId()), attendee);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,55 @@

import kr.momo.exception.MomoException;
import kr.momo.exception.code.AttendeeErrorCode;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

class AttendeePasswordTest {

@DisplayName("참가자 비밀번호가 4글자를 초과하면 예외를 발생시킨다.")
private PasswordEncoder passwordEncoder;

@BeforeEach
void setup() {
passwordEncoder = Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
}

@DisplayName("참가자 비밀번호 객체가 정상 생성된다.")
@Test
void createAttendeePasswordObjectSuccessfully() {
assertThatNoException()
.isThrownBy(() -> new AttendeePassword("1234"));
}

@DisplayName("참가자 비밀번호가 숫자가 아니거나 4자를 초과하면 예외를 발생시킨다.")
@Test
void throwsExceptionIfAttendeePasswordIsTooLong() {
assertThatThrownBy(() -> new AttendeePassword("invalid_password_length_invalid_password_length"))
.isInstanceOf(MomoException.class)
.hasMessage(AttendeeErrorCode.INVALID_PASSWORD_FORMAT.message());
}

@DisplayName("참가자 비밀번호 객체가 정상 생성된다.")
@DisplayName("비밀번호와 동일한지 검증한다.")
@Test
void createAttendeePasswordObjectSuccessfully() {
void matchWithRawPassword() throws Exception {
String rawPassword = "1234";
AttendeePassword password = AttendeeEncryptedPasswordFixture.createAttendeePassword(rawPassword);
AttendeePassword other = new AttendeePassword(rawPassword);

assertThatNoException()
.isThrownBy(() -> new AttendeePassword("1234"));
.isThrownBy(() -> password.matchWithRawPassword(other, passwordEncoder));
}

@DisplayName("비밀번호가 서로 다르면 예외를 발생시킨다.")
@DisplayName("암호화된 비밀번호와 서로 다르면 예외를 발생시킨다.")
@Test
void throwsExceptionForMismatchedPasswords() {
AttendeePassword password = new AttendeePassword("1234");
void throwsExceptionForMismatchedPasswords() throws Exception {
String rawPassword = "1234";
AttendeePassword password = AttendeeEncryptedPasswordFixture.createAttendeePassword(rawPassword);
AttendeePassword other = new AttendeePassword("4321");

assertThatThrownBy(() -> password.verifyPassword(other))
assertThatThrownBy(() -> password.matchWithRawPassword(other, passwordEncoder))
.isInstanceOf(MomoException.class)
.hasMessage(AttendeeErrorCode.PASSWORD_MISMATCHED.message());
}
Expand Down
31 changes: 23 additions & 8 deletions backend/src/test/java/kr/momo/domain/attendee/AttendeeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,47 @@
import kr.momo.domain.meeting.Meeting;
import kr.momo.exception.MomoException;
import kr.momo.exception.code.AttendeeErrorCode;
import kr.momo.fixture.AttendeeFixture;
import kr.momo.fixture.MeetingFixture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

class AttendeeTest {

private PasswordEncoder passwordEncoder;

@BeforeEach
void setup() {
passwordEncoder = Argon2PasswordEncoder.defaultsForSpringSecurity_v5_8();
}

@DisplayName("참가자의 비밀번호가 일치하지 않으면 예외를 발생시킨다.")
@Test
void throwsExceptionIfPasswordDoesNotMatch() {
void throwsExceptionIfPasswordDoesNotMatch() throws Exception {
String rawPassword = "1234";
AttendeePassword attendeePassword = AttendeeEncryptedPasswordFixture.createAttendeePassword(rawPassword);
Meeting meeting = MeetingFixture.DINNER.create();
Attendee attendee = new Attendee(meeting, "jazz", "1111", Role.GUEST);
AttendeePassword other = new AttendeePassword("1234");
Attendee attendee = AttendeeFixture.HOST_JAZZ.create(meeting, attendeePassword);
AttendeePassword other = new AttendeePassword("9999");

assertThatThrownBy(() -> attendee.verifyPassword(other))
assertThatThrownBy(() -> attendee.verifyPassword(other, passwordEncoder))
.isInstanceOf(MomoException.class)
.hasMessage(AttendeeErrorCode.PASSWORD_MISMATCHED.message());
}

@DisplayName("참가자의 비밀번호가 일치하면 정상 기능한다.")
@Test
void doesNotThrowExceptionIfPasswordMatches() {
void doesNotThrowExceptionIfPasswordMatches() throws Exception {
String rawPassword = "1234";
AttendeePassword attendeePassword = AttendeeEncryptedPasswordFixture.createAttendeePassword(rawPassword);
Meeting meeting = MeetingFixture.DINNER.create();
Attendee attendee = new Attendee(meeting, "jazz", "1111", Role.GUEST);
AttendeePassword other = new AttendeePassword("1111");
Attendee attendee = AttendeeFixture.HOST_JAZZ.create(meeting, attendeePassword);
AttendeePassword other = new AttendeePassword(rawPassword);

assertThatNoException()
.isThrownBy(() -> attendee.verifyPassword(other));
.isThrownBy(() -> attendee.verifyPassword(other, passwordEncoder));
}
}

0 comments on commit f7cc204

Please sign in to comment.