Skip to content

Commit

Permalink
[feat] #22 회원가입 시 비밀번호 검증 정규식 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ziiyouth committed Jan 5, 2024
1 parent bd18b04 commit ced34ea
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import org.gachon.checkmate.global.config.mail.MailProvider;
import org.gachon.checkmate.global.error.exception.ConflictException;
import org.gachon.checkmate.global.error.exception.EntityNotFoundException;
import org.gachon.checkmate.global.error.exception.InvalidValueException;
import org.gachon.checkmate.global.error.exception.UnauthorizedException;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.regex.Pattern;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -32,6 +34,7 @@ public class MemberService {
private final PasswordEncoder passwordEncoder;
private final UserRepository userRepository;
private final RefreshTokenRepository refreshTokenRepository;
private static final String PASSWORD_REGEX = "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[@$!%*?&])[A-Za-z\\d@$!%*?&]{8,20}$";

public EmailResponseDto sendMail(EmailPostRequestDto emailPostRequestDto) {
checkDuplicateEmail(emailPostRequestDto.email());
Expand All @@ -40,6 +43,7 @@ public EmailResponseDto sendMail(EmailPostRequestDto emailPostRequestDto) {
}

public MemberSignUpResponseDto signUp(MemberSignUpRequestDto memberSignUpRequestDto) {
validatePassword(memberSignUpRequestDto.password());
Long newMemberId = createMember(memberSignUpRequestDto);
String accessToken = issueNewAccessToken(newMemberId);
String refreshToken = issueNewRefreshToken(newMemberId);
Expand Down Expand Up @@ -71,14 +75,20 @@ public MypageResponseDto getMypage(Long userId) {
);
}

private void validatePassword(String password) {
if (!Pattern.matches(PASSWORD_REGEX, password)) {
throw new InvalidValueException(INVALID_PASSWORD);
}
}

private User findByIdOrThrow(Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
}

private void validatePassword(String enteredPassword, String storedPassword) {
if (!authenticatePassword(enteredPassword, storedPassword)) {
throw new UnauthorizedException(INVALID_PASSWORD);
throw new UnauthorizedException(NOT_MATCH_PASSWORD);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum ErrorCode {
BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."),
INVALID_ENUM_CODE(HttpStatus.BAD_REQUEST, "잘못된 Enum class code 입니다."),
INVALID_PAGING_SIZE(HttpStatus.BAD_REQUEST, "잘못된 Paging 크기입니다."),
INVALID_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호는 8~20자 대소문자 영문, 숫자, 특수문자의 조합이어야 합니다."),

/**
* 401 Unauthorized
Expand All @@ -26,7 +27,7 @@ public enum ErrorCode {
INVALID_REFRESH_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 값이 올바르지 않습니다."),
EXPIRED_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다. 다시 로그인해 주세요."),
NOT_MATCH_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "일치하지 않는 리프레시 토큰입니다."),
INVALID_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."),
NOT_MATCH_PASSWORD(HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지 않습니다."),

/**
* 403 Forbidden
Expand Down

0 comments on commit ced34ea

Please sign in to comment.