diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java index 371498d8..d6be6686 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/service/GiftcardService.java @@ -1,15 +1,18 @@ package org.swmaestro.repl.gifthub.giftcard.service; +import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; import java.util.Random; import java.util.UUID; +import org.springframework.security.crypto.encrypt.AesBytesEncryptor; import org.springframework.stereotype.Service; import org.swmaestro.repl.gifthub.exception.BusinessException; import org.swmaestro.repl.gifthub.giftcard.config.GiftcardConfig; import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardResponseDto; import org.swmaestro.repl.gifthub.giftcard.entity.Giftcard; import org.swmaestro.repl.gifthub.giftcard.repository.GiftcardRepository; +import org.swmaestro.repl.gifthub.util.ByteArrayUtils; import org.swmaestro.repl.gifthub.util.StatusEnum; import org.swmaestro.repl.gifthub.vouchers.dto.VoucherShareResponseDto; import org.swmaestro.repl.gifthub.vouchers.entity.Voucher; @@ -21,20 +24,24 @@ public class GiftcardService { private final GiftcardRepository giftCardRepository; private final GiftcardConfig giftcardConfig; + private final AesBytesEncryptor aesBytesEncryptor; public VoucherShareResponseDto create(Voucher voucher, String message) { if (isExist(voucher.getId())) { throw new BusinessException("이미 공유된 기프티콘입니다.", StatusEnum.BAD_REQUEST); } + Giftcard giftCard = Giftcard.builder() .id(generateUUID()) .voucher(voucher) - .password(generatePassword()) + .password(encryptPassword(generatePassword())) .message(message) .expiresAt(LocalDateTime.now().plusDays(giftcardConfig.getEffectiveDay())) .build(); giftCardRepository.save(giftCard); + System.out.println("Giftcard password: " + decryptPassword(giftCard.getPassword())); + return VoucherShareResponseDto.builder() .id(giftCard.getId()) .build(); @@ -55,7 +62,7 @@ public GiftcardResponseDto read(String id, String password) { throw new BusinessException("만료된 링크입니다.", StatusEnum.BAD_REQUEST); } - if (!giftcard.getPassword().equals(password)) { + if (!decryptPassword(giftcard.getPassword()).equals(password)) { throw new BusinessException("비밀번호가 일치하지 않습니다.", StatusEnum.FORBIDDEN); } @@ -84,4 +91,16 @@ public String generatePassword() { int random = new Random().nextInt(10000); return String.format("%04d", random); } + + private String decryptPassword(String password) { + byte[] bytes = ByteArrayUtils.stringToByteArray(password); + byte[] decrypt = aesBytesEncryptor.decrypt(bytes); + return new String(decrypt, StandardCharsets.UTF_8); + } + + private String encryptPassword(String password) { + byte[] bytes = password.getBytes(StandardCharsets.UTF_8); + byte[] encrypt = aesBytesEncryptor.encrypt(bytes); + return ByteArrayUtils.byteArrayToString(encrypt); + } }