Skip to content

Commit

Permalink
fix: add password encrypt & decrpyt logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jinlee1703 committed Oct 19, 2023
1 parent 3c8c2c3 commit e34da31
Showing 1 changed file with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
}

0 comments on commit e34da31

Please sign in to comment.