From f3a3dfea844ff74fe9fe6f7f5a7bf6674a985a95 Mon Sep 17 00:00:00 2001 From: jaehee329 Date: Thu, 16 Nov 2023 19:47:51 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20=ED=86=A0=ED=81=B0=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20=EB=A1=9C=EC=A7=81=EC=9D=84=20=EB=B3=84=EB=8F=84?= =?UTF-8?q?=EC=9D=98=20=ED=81=B4=EB=9E=98=EC=8A=A4=EB=A1=9C=20=EC=B6=94?= =?UTF-8?q?=EC=83=81=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/auth/util/AesTokenProvider.java | 53 ++++--------------- 1 file changed, 11 insertions(+), 42 deletions(-) diff --git a/backend/src/main/java/harustudy/backend/auth/util/AesTokenProvider.java b/backend/src/main/java/harustudy/backend/auth/util/AesTokenProvider.java index 8f407c03..2e7cd895 100644 --- a/backend/src/main/java/harustudy/backend/auth/util/AesTokenProvider.java +++ b/backend/src/main/java/harustudy/backend/auth/util/AesTokenProvider.java @@ -1,30 +1,28 @@ package harustudy.backend.auth.util; +import com.fasterxml.jackson.databind.ObjectMapper; import harustudy.backend.auth.exception.InvalidAccessTokenException; import java.nio.charset.StandardCharsets; import java.security.GeneralSecurityException; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.Base64; -import java.util.Date; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; +import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Component; +@RequiredArgsConstructor @Component public class AesTokenProvider { private static final String alg = "AES/CBC/PKCS5Padding"; private static final String iv = "0123456789abcdef"; // 16byte - private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("MMddHH:mm:ssyyyy"); + + private final ObjectMapper objectMapper; public String createAccessToken(Long subject, Long accessTokenExpireLength, String secretKey) { - Date now = new Date(); - Date expireAt = new Date(now.getTime() + accessTokenExpireLength); - String formatted = DATE_FORMAT.format(expireAt); - String text = subject + " " + formatted; - return encrypt(text, secretKey); + String token = AccessTokenUtils.issue(objectMapper, subject, accessTokenExpireLength); + return encrypt(token, secretKey); } private String encrypt(String text, String secretKey) { @@ -42,13 +40,11 @@ private String encrypt(String text, String secretKey) { } public Long parseSubject(String accessToken, String secretKey) { - String[] splitted = decrypt(accessToken, secretKey); - validateLength(splitted); - validateExpiration(splitted); - return parseSubject(splitted); + byte[] decrypted = decrypt(accessToken, secretKey); + return AccessTokenUtils.parseSubject(objectMapper, decrypted); } - private String[] decrypt(String accessToken, String secretKey) { + private byte[] decrypt(String accessToken, String secretKey) { try { Cipher cipher = Cipher.getInstance(alg); SecretKeySpec keySpec = new SecretKeySpec(secretKey.getBytes(), "AES"); @@ -56,36 +52,9 @@ private String[] decrypt(String accessToken, String secretKey) { cipher.init(Cipher.DECRYPT_MODE, keySpec, ivParamSpec); byte[] decodedBytes = Base64.getDecoder().decode(accessToken); - byte[] decrypted = cipher.doFinal(decodedBytes); - String string = new String(decrypted, StandardCharsets.UTF_8); - return string.split(" "); + return cipher.doFinal(decodedBytes); } catch (GeneralSecurityException | IllegalArgumentException e) { throw new InvalidAccessTokenException(); } } - - private void validateLength(String[] splitted) { - if (splitted.length != 2) { - throw new InvalidAccessTokenException(); - } - } - - private void validateExpiration(String[] splitted) { - Date expireAt = parseExpirationDate(splitted); - if (expireAt.before(new Date())) { - throw new InvalidAccessTokenException(); - } - } - - private Date parseExpirationDate(String[] splitted) { - try { - return DATE_FORMAT.parse(splitted[1]); - } catch (ParseException e) { - throw new InvalidAccessTokenException(); - } - } - - private Long parseSubject(String[] splitted) { - return Long.parseLong(splitted[0]); - } }