From 32e1ab5755fab4b91beb7100f313f2e86147f6e9 Mon Sep 17 00:00:00 2001 From: You Jung <80906691+JangYouJung@users.noreply.github.com> Date: Wed, 24 Jul 2024 23:56:27 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Refactor:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=ED=95=A8=EC=88=98=EB=AA=85=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../meltingpot/server/auth/oauth/kakao/KakaoService.java | 2 +- .../java/meltingpot/server/auth/service/OAuthService.java | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java index 08e9822..2357183 100644 --- a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java +++ b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java @@ -34,7 +34,7 @@ public String getKakaoLogin(String redirect_uri) { // 프론트 구현부 + "&response_type=code"; } - public KaKaoTokenDto getKakaoInfo(String code) throws Exception { + public KaKaoTokenDto getKakaoToken(String code) throws Exception { if (code == null) throw new Exception("Failed get authorization code"); String accessToken = ""; diff --git a/src/main/java/meltingpot/server/auth/service/OAuthService.java b/src/main/java/meltingpot/server/auth/service/OAuthService.java index 59a39a4..d287cf4 100644 --- a/src/main/java/meltingpot/server/auth/service/OAuthService.java +++ b/src/main/java/meltingpot/server/auth/service/OAuthService.java @@ -2,7 +2,6 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import meltingpot.server.auth.controller.dto.AccountResponseDto; import meltingpot.server.auth.controller.dto.OAuthSignInRequestDto; import meltingpot.server.auth.controller.dto.OAuthSignupRequestDto; import meltingpot.server.auth.controller.dto.ProfileImageRequestDto; @@ -20,16 +19,12 @@ import meltingpot.server.domain.repository.RefreshTokenRepository; import meltingpot.server.exception.AuthException; import meltingpot.server.exception.IllegalArgumentException; -import meltingpot.server.exception.ResourceNotFoundException; import meltingpot.server.util.ResponseCode; import meltingpot.server.util.TokenDto; -import meltingpot.server.util.r2.FileService; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; -import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -133,7 +128,7 @@ public OAuthSignInResponseDto SNSLogin(OAuthSignInRequestDto request) throws Exc if(request.type() == OAuthType.KAKAO) { // 카카오 토큰 가져오기 - KaKaoTokenDto tokenDto = kakaoService.getKakaoInfo(request.code()); + KaKaoTokenDto tokenDto = kakaoService.getKakaoToken(request.code()); // 카카오 유저 정보 가져오기 KakaoDto kakaoDto = kakaoService.getUserInfoWithToken(tokenDto.accessToken()); From 05be8db1ac4cab3e0917dea10e51347fa04c7f1f Mon Sep 17 00:00:00 2001 From: You Jung <80906691+JangYouJung@users.noreply.github.com> Date: Sun, 28 Jul 2024 21:40:21 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Refactor:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20SDK=20=EB=B0=A9=EC=8B=9D=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/dto/OAuthSignInRequestDto.java | 2 +- .../server/auth/oauth/kakao/KakaoDto.java | 2 -- .../server/auth/oauth/kakao/KakaoService.java | 35 ++++++++++++++++--- .../server/auth/service/OAuthService.java | 12 +++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/main/java/meltingpot/server/auth/controller/dto/OAuthSignInRequestDto.java b/src/main/java/meltingpot/server/auth/controller/dto/OAuthSignInRequestDto.java index c5cd0d6..0139897 100644 --- a/src/main/java/meltingpot/server/auth/controller/dto/OAuthSignInRequestDto.java +++ b/src/main/java/meltingpot/server/auth/controller/dto/OAuthSignInRequestDto.java @@ -4,7 +4,7 @@ public record OAuthSignInRequestDto( OAuthType type, - String code, + String token, String push_token ) { } diff --git a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoDto.java b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoDto.java index 5c0780c..eb279b4 100644 --- a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoDto.java +++ b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoDto.java @@ -5,8 +5,6 @@ @Builder @Data public class KakaoDto { - - private long id; private String email; private String nickname; diff --git a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java index 2357183..970dfb8 100644 --- a/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java +++ b/src/main/java/meltingpot/server/auth/oauth/kakao/KakaoService.java @@ -1,5 +1,7 @@ package meltingpot.server.auth.oauth.kakao; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.springframework.beans.factory.annotation.Value; @@ -11,6 +13,7 @@ import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; +import java.util.Base64; @Service public class KakaoService { @@ -26,16 +29,17 @@ public class KakaoService { private final static String KAKAO_AUTH_URI = "https://kauth.kakao.com"; private final static String KAKAO_API_URI = "https://kapi.kakao.com"; + private final ObjectMapper objectMapper = new ObjectMapper(); public String getKakaoLogin(String redirect_uri) { // 프론트 구현부 return KAKAO_AUTH_URI + "/oauth/authorize" + "?client_id=" + KAKAO_CLIENT_ID + "&redirect_uri=" + redirect_uri - + "&response_type=code"; + + "&response_type=token"; } public KaKaoTokenDto getKakaoToken(String code) throws Exception { - if (code == null) throw new Exception("Failed get authorization code"); + if (code == null) throw new Exception("Failed get authorization token"); String accessToken = ""; String refreshToken = ""; @@ -48,7 +52,7 @@ public KaKaoTokenDto getKakaoToken(String code) throws Exception { params.add("grant_type" , "authorization_code"); params.add("client_id" , KAKAO_CLIENT_ID); params.add("client_secret", KAKAO_CLIENT_SECRET); - params.add("code" , code); + params.add("token" , code); params.add("redirect_uri" , redirect_uri); RestTemplate restTemplate = new RestTemplate(); @@ -103,7 +107,30 @@ public KakaoDto getUserInfoWithToken(String accessToken) throws Exception { String nickname = String.valueOf(profile.get("nickname")); return KakaoDto.builder() - .id(id) + .email(email) + .nickname(nickname).build(); + } + + public KakaoDto getUserInfoFromIdToken(String idToken) throws Exception { + // 온점 분리 + String[] parts = idToken.split("\\."); + if (parts.length != 3) { + throw new IllegalArgumentException("Invalid IdToken"); + } + + // Payload 디코딩 + String payload = parts[1]; + String decodedPayload = new String(Base64.getDecoder().decode(payload)); + + // JSON 파싱 + JsonNode jsonNode = objectMapper.readTree(decodedPayload); + + // email과 nickname 추출 + String email = jsonNode.path("email").asText(null); + String nickname = jsonNode.path("nickname").asText(null); + + // 정보 담아 보내기 + return KakaoDto.builder() .email(email) .nickname(nickname).build(); } diff --git a/src/main/java/meltingpot/server/auth/service/OAuthService.java b/src/main/java/meltingpot/server/auth/service/OAuthService.java index d287cf4..ab97ca8 100644 --- a/src/main/java/meltingpot/server/auth/service/OAuthService.java +++ b/src/main/java/meltingpot/server/auth/service/OAuthService.java @@ -6,7 +6,6 @@ import meltingpot.server.auth.controller.dto.OAuthSignupRequestDto; import meltingpot.server.auth.controller.dto.ProfileImageRequestDto; import meltingpot.server.auth.oauth.OAuthUserDetails; -import meltingpot.server.auth.oauth.kakao.KaKaoTokenDto; import meltingpot.server.auth.oauth.kakao.KakaoDto; import meltingpot.server.auth.oauth.kakao.KakaoService; import meltingpot.server.auth.service.dto.OAuthSignInResponseDto; @@ -104,6 +103,7 @@ public OAuthSignInResponseDto oauthSignup(OAuthSignupRequestDto signupRequest) { .build()).toList() ); + account.setLanguages(signupRequest.languages().stream().map( (language) -> AccountLanguage.builder() .account(account) @@ -127,11 +127,17 @@ public OAuthSignInResponseDto oauthSignup(OAuthSignupRequestDto signupRequest) { public OAuthSignInResponseDto SNSLogin(OAuthSignInRequestDto request) throws Exception { if(request.type() == OAuthType.KAKAO) { + + /* * * * RestAPI 버전 * * * / // 카카오 토큰 가져오기 - KaKaoTokenDto tokenDto = kakaoService.getKakaoToken(request.code()); + KaKaoTokenDto tokenDto = kakaoService.getKakaoToken(request.token()); // 카카오 유저 정보 가져오기 - KakaoDto kakaoDto = kakaoService.getUserInfoWithToken(tokenDto.accessToken()); + KakaoDto kakaoDto = kakaoService.getUserInfoWithToken(request.token()); + * * * * * * * * * * * * */ + + // SDK 버전: 아이디토큰으로 유저 정보 파싱해오기 + KakaoDto kakaoDto = kakaoService.getUserInfoFromIdToken(request.token()); // 이미 가입한 회원인지 확인 Optional account = accountRepository.findByUsernameAndIsQuitIsFalse(kakaoDto.getEmail());