From 1e80bfc3d322e75abb542a5a677874dfcf112b45 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 13:28:56 +0900 Subject: [PATCH 01/15] feat: implement giftcard-request-dto --- .../giftcard/dto/GiftcardRequestDto.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardRequestDto.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardRequestDto.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardRequestDto.java new file mode 100644 index 00000000..07fa7e2d --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardRequestDto.java @@ -0,0 +1,22 @@ +package org.swmaestro.repl.gifthub.giftcard.dto; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +public class GiftcardRequestDto { + private String password; + + @Builder + public GiftcardRequestDto(String password) { + this.password = password; + } +} From 4e784568f4ef380a8099b518903e63ad698e5411 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 13:29:13 +0900 Subject: [PATCH 02/15] feat: implement giftcard-response-dto --- .../giftcard/dto/GiftcardResponseDto.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardResponseDto.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardResponseDto.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardResponseDto.java new file mode 100644 index 00000000..67db147a --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/dto/GiftcardResponseDto.java @@ -0,0 +1,31 @@ +package org.swmaestro.repl.gifthub.giftcard.dto; + +import java.time.LocalDate; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +public class GiftcardResponseDto { + private String sender; + private String message; + private String brandName; + private String productName; + private LocalDate expiresAt; + + @Builder + public GiftcardResponseDto(String sender, String message, String brandName, String productName, + LocalDate expiresAt) { + this.sender = sender; + this.message = message; + this.brandName = brandName; + this.productName = productName; + this.expiresAt = expiresAt; + } +} From c3868466491141285e20f150a4bdc5e40c406b43 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 13:32:07 +0900 Subject: [PATCH 03/15] feat: implement read-giftcard by password --- .../giftcard/service/GiftcardService.java | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) 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 11ee7327..7e025cc6 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 @@ -6,6 +6,7 @@ import org.springframework.stereotype.Service; import org.swmaestro.repl.gifthub.exception.BusinessException; +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.StatusEnum; @@ -37,8 +38,40 @@ public VoucherShareResponseDto create(Voucher voucher, String message) { .build(); } - public boolean isExist(Long id) { - return giftCardRepository.existsByVoucherId(id); + public Giftcard read(String id) { + if (!isExist(id)) { + throw new BusinessException("존재하지 않는 링크입니다.", StatusEnum.NOT_FOUND); + } + + return giftCardRepository.findById(id).get(); + } + + public GiftcardResponseDto read(String id, String password) { + Giftcard giftcard = read(id); + + if (giftcard.getExpiresAt().isBefore(LocalDateTime.now())) { + throw new BusinessException("만료된 링크입니다.", StatusEnum.BAD_REQUEST); + } + + if (!giftcard.getPassword().equals(password)) { + throw new BusinessException("비밀번호가 일치하지 않습니다.", StatusEnum.FORBIDDEN); + } + + return GiftcardResponseDto.builder() + .sender(giftcard.getVoucher().getUser().getNickname()) + .message(giftcard.getMessage()) + .brandName(giftcard.getVoucher().getBrand().getName()) + .productName(giftcard.getVoucher().getProduct().getName()) + .expiresAt(giftcard.getExpiresAt().toLocalDate()) + .build(); + } + + public boolean isExist(String id) { + return giftCardRepository.existsById(id); + } + + public boolean isExist(Long voucherId) { + return giftCardRepository.existsByVoucherId(voucherId); } public String generateUUID() { From 96d688e45f12bb36197b3af38b1ef3b54bce9184 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 13:32:34 +0900 Subject: [PATCH 04/15] feat: implement read-giftcard api --- .../contorller/GiftcardController.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java new file mode 100644 index 00000000..6a43f46c --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java @@ -0,0 +1,46 @@ +package org.swmaestro.repl.gifthub.giftcard.contorller; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardRequestDto; +import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardResponseDto; +import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService; +import org.swmaestro.repl.gifthub.util.Message; +import org.swmaestro.repl.gifthub.util.SuccessMessage; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; + +@RestController +@RequestMapping("/giftcards") +@RequiredArgsConstructor +@Tag(name = "GiftCard", description = "공유하기 관련 API") +public class GiftcardController { + private final GiftcardService giftcardService; + + @PostMapping("/{id}") + @Operation(summary = "공유 정보 요청 메서드", description = "클라이언트에서 요청한 공유 정보를 전달하기 위한 메서드입니다.") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "공유하기 정보 조회 성공"), + @ApiResponse(responseCode = "400", description = "만료된 공유하기 정보 접근"), + @ApiResponse(responseCode = "403", description = "일치하지 않는 비밀번호 입력"), + @ApiResponse(responseCode = "404", description = "존재하지 않는 공유하기 정보 접근") + }) + public ResponseEntity read(HttpServletRequest request, @PathVariable String id, + @RequestBody GiftcardRequestDto giftcardRequestDto) { + GiftcardResponseDto giftcardResponseDto = giftcardService.read(id, giftcardRequestDto.getPassword()); + return ResponseEntity.ok( + SuccessMessage.builder() + .path(request.getRequestURI()) + .data(giftcardResponseDto) + .build()); + } +} From 097875f3452c63dca636b21c6affbd1ea6624425 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:14:12 +0900 Subject: [PATCH 05/15] feat: implement basic-authentication-decoder --- .../gifthub/util/BasicAuthenticationDecoder.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/util/BasicAuthenticationDecoder.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/util/BasicAuthenticationDecoder.java b/src/main/java/org/swmaestro/repl/gifthub/util/BasicAuthenticationDecoder.java new file mode 100644 index 00000000..19b68c0f --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/util/BasicAuthenticationDecoder.java @@ -0,0 +1,12 @@ +package org.swmaestro.repl.gifthub.util; + +import java.util.Base64; + +import jakarta.servlet.http.HttpServletRequest; + +public class BasicAuthenticationDecoder { + public static String decode(HttpServletRequest request) { + String header = request.getHeader("Authorization"); + return new String(Base64.getDecoder().decode(header.replace("Basic ", ""))); + } +} From f43c1c6e474eb5813d595c91d51cf810604df423 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:14:32 +0900 Subject: [PATCH 06/15] feat: add giftcard url --- .../java/org/swmaestro/repl/gifthub/config/SecurityConfig.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/config/SecurityConfig.java b/src/main/java/org/swmaestro/repl/gifthub/config/SecurityConfig.java index 90fa0f48..0f7e7b83 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/config/SecurityConfig.java +++ b/src/main/java/org/swmaestro/repl/gifthub/config/SecurityConfig.java @@ -38,6 +38,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti "/auth/sign-up", "/auth/sign-in", "/auth/sign-in/**", + "/giftcards/**", "/swagger-resources/**", "/swagger-ui/**", "/v3/api-docs/**", From 9ccea76ba31badb36962a12cacd8c5bd38e5d71b Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:14:55 +0900 Subject: [PATCH 07/15] fix: change method --- .../giftcard/contorller/GiftcardController.java | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java index 6a43f46c..6fa0736d 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardController.java @@ -1,14 +1,13 @@ package org.swmaestro.repl.gifthub.giftcard.contorller; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardRequestDto; import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardResponseDto; import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService; +import org.swmaestro.repl.gifthub.util.BasicAuthenticationDecoder; import org.swmaestro.repl.gifthub.util.Message; import org.swmaestro.repl.gifthub.util.SuccessMessage; @@ -26,7 +25,7 @@ public class GiftcardController { private final GiftcardService giftcardService; - @PostMapping("/{id}") + @GetMapping("/{id}") @Operation(summary = "공유 정보 요청 메서드", description = "클라이언트에서 요청한 공유 정보를 전달하기 위한 메서드입니다.") @ApiResponses({ @ApiResponse(responseCode = "200", description = "공유하기 정보 조회 성공"), @@ -34,9 +33,8 @@ public class GiftcardController { @ApiResponse(responseCode = "403", description = "일치하지 않는 비밀번호 입력"), @ApiResponse(responseCode = "404", description = "존재하지 않는 공유하기 정보 접근") }) - public ResponseEntity read(HttpServletRequest request, @PathVariable String id, - @RequestBody GiftcardRequestDto giftcardRequestDto) { - GiftcardResponseDto giftcardResponseDto = giftcardService.read(id, giftcardRequestDto.getPassword()); + public ResponseEntity read(HttpServletRequest request, @PathVariable String id) { + GiftcardResponseDto giftcardResponseDto = giftcardService.read(id, BasicAuthenticationDecoder.decode(request)); return ResponseEntity.ok( SuccessMessage.builder() .path(request.getRequestURI()) From f97591620b3569d2d9f4de4b51bbef9a87150500 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:25:02 +0900 Subject: [PATCH 08/15] test: write giftcard-read test --- .../contorller/GiftcardControllerTest.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java diff --git a/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java b/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java new file mode 100644 index 00000000..74adaa52 --- /dev/null +++ b/src/test/java/org/swmaestro/repl/gifthub/giftcard/contorller/GiftcardControllerTest.java @@ -0,0 +1,67 @@ +package org.swmaestro.repl.gifthub.giftcard.contorller; + +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +import java.time.LocalDate; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; +import org.swmaestro.repl.gifthub.giftcard.dto.GiftcardResponseDto; +import org.swmaestro.repl.gifthub.giftcard.service.GiftcardService; + +import com.fasterxml.jackson.databind.ObjectMapper; + +@SpringBootTest +@AutoConfigureMockMvc +class GiftcardControllerTest { + @Autowired + private MockMvc mockMvc; + + @Autowired + private ObjectMapper objectMapper; + + @MockBean + private GiftcardService giftcardService; + + @Test + @WithMockUser(username = "test", roles = "USER") + void read() throws Exception { + // given + String giftcardId = "id"; + String apiPath = "/giftcards/" + giftcardId; + String encodedPassword = "MDAwMA=="; + String decodedPassword = "0000"; + + GiftcardResponseDto giftcardResponseDto = GiftcardResponseDto.builder() + .sender("보내는 사람") + .message("메시지") + .productName("상품명") + .brandName("브랜드명") + .expiresAt(LocalDate.now()) + .build(); + + // when + when(giftcardService.read(giftcardId, decodedPassword)).thenReturn(giftcardResponseDto); + + // then + mockMvc.perform(get(apiPath) + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "Basic " + encodedPassword) + .content(objectMapper.writeValueAsString("test"))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.path").value(apiPath)) + .andExpect(jsonPath("$.data.sender").value(giftcardResponseDto.getSender())) + .andExpect(jsonPath("$.data.message").value(giftcardResponseDto.getMessage())) + .andExpect(jsonPath("$.data.product_name").value(giftcardResponseDto.getProductName())) + .andExpect(jsonPath("$.data.brand_name").value(giftcardResponseDto.getBrandName())) + .andExpect(jsonPath("$.data.expires_at").value(giftcardResponseDto.getExpiresAt().toString())); + } +} \ No newline at end of file From a4faf22b8bf23a4c0d60c2251400d17dac71c60b Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:47:33 +0900 Subject: [PATCH 09/15] feat: implement giftcard-config --- .../gifthub/giftcard/config/GiftcardConfig.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java new file mode 100644 index 00000000..01fe0166 --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java @@ -0,0 +1,15 @@ +package org.swmaestro.repl.gifthub.giftcard.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import lombok.Getter; +import lombok.Setter; + +@Component +@ConfigurationProperties(prefix = "giftcard") +@Getter +@Setter +public class GiftcardConfig { + private int effectiveDay; +} From 716c32ed6997e77b1cb62f1f6d246289b3df4cdc Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 14:47:59 +0900 Subject: [PATCH 10/15] refactor: use effective-day to giftconfig --- .../repl/gifthub/giftcard/service/GiftcardService.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 7e025cc6..371498d8 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 @@ -6,6 +6,7 @@ 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; @@ -19,6 +20,7 @@ @RequiredArgsConstructor public class GiftcardService { private final GiftcardRepository giftCardRepository; + private final GiftcardConfig giftcardConfig; public VoucherShareResponseDto create(Voucher voucher, String message) { if (isExist(voucher.getId())) { @@ -29,7 +31,7 @@ public VoucherShareResponseDto create(Voucher voucher, String message) { .voucher(voucher) .password(generatePassword()) .message(message) - .expiresAt(LocalDateTime.now().plusDays(3)) + .expiresAt(LocalDateTime.now().plusDays(giftcardConfig.getEffectiveDay())) .build(); giftCardRepository.save(giftCard); From 80abd7d7d58e11e025cd58e3cbc1d5052f02dc3f Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 15:50:13 +0900 Subject: [PATCH 11/15] feat: implement byte-array-utils --- .../repl/gifthub/util/ByteArrayUtils.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/main/java/org/swmaestro/repl/gifthub/util/ByteArrayUtils.java diff --git a/src/main/java/org/swmaestro/repl/gifthub/util/ByteArrayUtils.java b/src/main/java/org/swmaestro/repl/gifthub/util/ByteArrayUtils.java new file mode 100644 index 00000000..3de67f11 --- /dev/null +++ b/src/main/java/org/swmaestro/repl/gifthub/util/ByteArrayUtils.java @@ -0,0 +1,23 @@ +package org.swmaestro.repl.gifthub.util; + +import java.nio.ByteBuffer; + +public class ByteArrayUtils { + public static String byteArrayToString(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + for (byte abyte : bytes) { + sb.append(abyte); + sb.append(" "); + } + return sb.toString(); + } + + public static byte[] stringToByteArray(String byteString) { + String[] split = byteString.split("\\s"); + ByteBuffer buffer = ByteBuffer.allocate(split.length); + for (String s : split) { + buffer.put((byte)Integer.parseInt(s)); + } + return buffer.array(); + } +} From 5b4228dbc1a4345fb9556e8e696996a202f93cb3 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 15:50:51 +0900 Subject: [PATCH 12/15] feat: add member-filed --- .../repl/gifthub/giftcard/config/GiftcardConfig.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java index 01fe0166..87a75d77 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/config/GiftcardConfig.java @@ -1,6 +1,8 @@ package org.swmaestro.repl.gifthub.giftcard.config; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.security.crypto.encrypt.AesBytesEncryptor; import org.springframework.stereotype.Component; import lombok.Getter; @@ -12,4 +14,11 @@ @Setter public class GiftcardConfig { private int effectiveDay; + private String secret; + private String salt; + + @Bean + public AesBytesEncryptor aesBytesEncryptor() { + return new AesBytesEncryptor(secret, salt); + } } From 3c8c2c300caced98b3ef3ee6a50c0f48ab039fc1 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 15:51:25 +0900 Subject: [PATCH 13/15] fix: modify password field length --- .../org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java b/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java index a480fa85..46f86576 100644 --- a/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java +++ b/src/main/java/org/swmaestro/repl/gifthub/giftcard/entity/Giftcard.java @@ -41,7 +41,7 @@ public class Giftcard { @Column(nullable = false) private LocalDateTime expiresAt; - @Column(length = 4) + @Column(length = 60, nullable = false) private String password; @Builder From e34da31e63376280aeaccfdfe3de069312ac93f6 Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 15:51:46 +0900 Subject: [PATCH 14/15] fix: add password encrypt & decrpyt logic --- .../giftcard/service/GiftcardService.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) 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); + } } From 892e52aae7dd378ef114040d38c2c0524fe5757c Mon Sep 17 00:00:00 2001 From: Jinwoo Lee Date: Thu, 19 Oct 2023 15:52:12 +0900 Subject: [PATCH 15/15] fix: remove sysout --- .../repl/gifthub/giftcard/service/GiftcardService.java | 2 -- 1 file changed, 2 deletions(-) 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 d6be6686..3af1cfff 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 @@ -40,8 +40,6 @@ public VoucherShareResponseDto create(Voucher voucher, String message) { .build(); giftCardRepository.save(giftCard); - System.out.println("Giftcard password: " + decryptPassword(giftCard.getPassword())); - return VoucherShareResponseDto.builder() .id(giftCard.getId()) .build();