From e448cb311fc404295e23813b4cf1c9a056d9ed84 Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Fri, 1 Dec 2023 17:29:30 +0900 Subject: [PATCH 1/2] =?UTF-8?q?NO-JIRA--chore=20:=20oAuth2=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20domain=EC=9C=BC=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../google/api/GoogleOAuth2Controller.java | 4 +- .../oauth2/google/domain/GoogleOAuth2.java | 117 ++++++++++++++++++ .../domain/oauth2/google/domain/OAuth2.java | 19 +++ .../google/dto/GoogleOAuth2TokenDTO.java | 20 +++ .../oauth2/google/dto/GoogleUserInfoDTO.java | 19 +++ .../google/service/GoogleOAuth2Service.java | 8 +- .../oauth2/kakao/api/OAuth2Controller.java | 14 +-- .../oauth2/kakao/dto/KakaoProfile.java | 2 +- .../oauth2/kakao/dto/RetKakaoOAuth.java | 2 +- .../service/CustomUserDetailsService.java | 3 +- .../oauth2/kakao/service/OAuth2Service.java | 20 +-- .../oauth2/google/domain/GoogleOAuth2.java | 117 ------------------ .../oauth2/google/domain/OAuth2.java | 19 --- .../oauth2/google/domain/Role.java | 13 -- .../google/dto/GoogleOAuth2TokenDTO.java | 20 --- .../oauth2/google/dto/GoogleUserInfoDTO.java | 19 --- 16 files changed, 203 insertions(+), 213 deletions(-) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/google/api/GoogleOAuth2Controller.java (92%) create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/GoogleOAuth2.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/OAuth2.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleOAuth2TokenDTO.java create mode 100644 src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleUserInfoDTO.java rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/google/service/GoogleOAuth2Service.java (83%) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/kakao/api/OAuth2Controller.java (89%) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/kakao/dto/KakaoProfile.java (81%) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/kakao/dto/RetKakaoOAuth.java (82%) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/kakao/service/CustomUserDetailsService.java (93%) rename src/main/java/org/prgrms/nabimarketbe/{ => domain}/oauth2/kakao/service/OAuth2Service.java (83%) delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java delete mode 100644 src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/api/GoogleOAuth2Controller.java similarity index 92% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/api/GoogleOAuth2Controller.java index 5d01b21b..acf836a8 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/api/GoogleOAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/api/GoogleOAuth2Controller.java @@ -1,15 +1,15 @@ -package org.prgrms.nabimarketbe.oauth2.google.api; +package org.prgrms.nabimarketbe.domain.oauth2.google.api; import java.io.IOException; import javax.servlet.http.HttpServletResponse; +import org.prgrms.nabimarketbe.domain.oauth2.google.service.GoogleOAuth2Service; import org.prgrms.nabimarketbe.domain.user.dto.request.SocialUserInfoDTO; import org.prgrms.nabimarketbe.domain.user.dto.response.UserLoginResponseDTO; import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.SingleResult; -import org.prgrms.nabimarketbe.oauth2.google.service.GoogleOAuth2Service; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/GoogleOAuth2.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/GoogleOAuth2.java new file mode 100644 index 00000000..703ea7cb --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/GoogleOAuth2.java @@ -0,0 +1,117 @@ +package org.prgrms.nabimarketbe.domain.oauth2.google.domain; + +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleOAuth2TokenDTO; +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleUserInfoDTO; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import lombok.RequiredArgsConstructor; + +@Component +@RequiredArgsConstructor +public class GoogleOAuth2 implements OAuth2 { + @Value("${spring.security.oauth2.client.registration.google.url}") + private String GOOGLE_SNS_LOGIN_URL; + + @Value("${spring.security.oauth2.client.registration.google.client-id}") + private String GOOGLE_SNS_CLIENT_ID; + + @Value("${spring.security.oauth2.client.registration.google.redirect-uri}") + private String GOOGLE_SNS_CALLBACK_URL; + + @Value("${spring.security.oauth2.client.registration.google.client-secret}") + private String GOOGLE_SNS_CLIENT_SECRET; + + @Value("${spring.security.oauth2.client.registration.google.scope}") + private String GOOGLE_DATA_ACCESS_SCOPE; + + @Value("${spring.security.oauth2.client.registration.google.token-url}") + private String GOOGLE_TOKEN_REQUEST_URL; + + @Value("${spring.security.oauth2.client.registration.google.user-info-url}") + private String GOOGLE_USER_INFO_REQUEST_URL; + + private final ObjectMapper objectMapper; + + private final RestTemplate restTemplate; + + @Override + public String getOAuth2RedirectUrl() { + Map params = new HashMap<>(); + + params.put("scope", GOOGLE_DATA_ACCESS_SCOPE); + params.put("response_type", "code"); + params.put("client_id", GOOGLE_SNS_CLIENT_ID); + params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); + + String parameterString = params.entrySet().stream() + .map(x -> x.getKey() + "=" + x.getValue()) + .collect(Collectors.joining("&")); + + String redirectURL = GOOGLE_SNS_LOGIN_URL + "?" + parameterString; + return redirectURL; + } + + @Override + public ResponseEntity requestAccessToken(String code) { + Map params = new HashMap<>(); + + params.put("code", code); + params.put("client_id", GOOGLE_SNS_CLIENT_ID); + params.put("client_secret", GOOGLE_SNS_CLIENT_SECRET); + params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); + params.put("grant_type", "authorization_code"); + + ResponseEntity accessToken = restTemplate.postForEntity( + GOOGLE_TOKEN_REQUEST_URL, + params, + String.class + ); + + return accessToken; + } + + @Override + public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException { + return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class); + } + + @Override + public ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) { + HttpHeaders headers = new HttpHeaders(); + + HttpEntity> request = new HttpEntity<>(headers); + headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken()); + ResponseEntity exchange = restTemplate.exchange( + GOOGLE_USER_INFO_REQUEST_URL, + HttpMethod.GET, + request, + String.class + ); + + return exchange; + } + + @Override + public GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfoResponse) throws JsonProcessingException { + GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue( + userInfoResponse.getBody(), + GoogleUserInfoDTO.class + ); + + return googleUserInfoDTO; + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/OAuth2.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/OAuth2.java new file mode 100644 index 00000000..cc1ff249 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/domain/OAuth2.java @@ -0,0 +1,19 @@ +package org.prgrms.nabimarketbe.domain.oauth2.google.domain; + +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleOAuth2TokenDTO; +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleUserInfoDTO; +import org.springframework.http.ResponseEntity; + +import com.fasterxml.jackson.core.JsonProcessingException; + +public interface OAuth2 { + String getOAuth2RedirectUrl(); + + ResponseEntity requestAccessToken(String code); + + GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException; + + ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken); + + GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfo) throws JsonProcessingException; +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleOAuth2TokenDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleOAuth2TokenDTO.java new file mode 100644 index 00000000..fd117d2d --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleOAuth2TokenDTO.java @@ -0,0 +1,20 @@ +package org.prgrms.nabimarketbe.domain.oauth2.google.dto; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public record GoogleOAuth2TokenDTO( + @JsonProperty("access_token") + String accessToken, + + @JsonProperty("expires_in") + int expiresIn, + + String scope, + + @JsonProperty("token_type") + String tokenType, + + @JsonProperty("id_token") + String idToken +) { +} diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleUserInfoDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleUserInfoDTO.java new file mode 100644 index 00000000..c7c4bfe4 --- /dev/null +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/dto/GoogleUserInfoDTO.java @@ -0,0 +1,19 @@ +package org.prgrms.nabimarketbe.domain.oauth2.google.dto; + +import org.prgrms.nabimarketbe.domain.user.entity.Role; +import org.prgrms.nabimarketbe.domain.user.entity.User; + +public record GoogleUserInfoDTO( + String id +) { + private static final String PROVIDER = "GOOGLE"; + + public User toEntity(String nickName) { + return User.builder() + .nickname(nickName) + .provider(PROVIDER) + .accountId(id) + .role(Role.USER.getKey()) + .build(); + } +} diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/service/GoogleOAuth2Service.java similarity index 83% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/service/GoogleOAuth2Service.java index 769c4e2f..d6605ce1 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/service/GoogleOAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/google/service/GoogleOAuth2Service.java @@ -1,9 +1,9 @@ -package org.prgrms.nabimarketbe.oauth2.google.service; +package org.prgrms.nabimarketbe.domain.oauth2.google.service; +import org.prgrms.nabimarketbe.domain.oauth2.google.domain.OAuth2; +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleOAuth2TokenDTO; import org.prgrms.nabimarketbe.domain.user.dto.request.SocialUserInfoDTO; -import org.prgrms.nabimarketbe.oauth2.google.domain.OAuth2; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; +import org.prgrms.nabimarketbe.domain.oauth2.google.dto.GoogleUserInfoDTO; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/api/OAuth2Controller.java similarity index 89% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/api/OAuth2Controller.java index bc469e8c..ced08fc9 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/api/OAuth2Controller.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/api/OAuth2Controller.java @@ -1,17 +1,17 @@ -package org.prgrms.nabimarketbe.oauth2.kakao.api; +package org.prgrms.nabimarketbe.domain.oauth2.kakao.api; import java.io.IOException; import javax.servlet.http.HttpServletResponse; +import org.prgrms.nabimarketbe.domain.oauth2.kakao.dto.KakaoProfile; +import org.prgrms.nabimarketbe.domain.oauth2.kakao.service.OAuth2Service; import org.prgrms.nabimarketbe.domain.user.dto.request.SocialUserInfoDTO; import org.prgrms.nabimarketbe.domain.user.dto.response.UserLoginResponseDTO; import org.prgrms.nabimarketbe.domain.user.service.SignService; import org.prgrms.nabimarketbe.global.util.ResponseFactory; import org.prgrms.nabimarketbe.global.util.model.CommonResult; import org.prgrms.nabimarketbe.global.util.model.SingleResult; -import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; -import org.prgrms.nabimarketbe.oauth2.kakao.service.OAuth2Service; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; @@ -37,22 +37,22 @@ public class OAuth2Controller { private final Environment env; - private final OAuth2Service OAuth2Service; + private final OAuth2Service oAuth2Service; private final SignService signService; @GetMapping("/login") public void socialLogin(HttpServletResponse response) throws IOException { - StringBuilder loginUri = OAuth2Service.createUri(); + StringBuilder loginUri = oAuth2Service.createUri(); response.sendRedirect(String.valueOf(loginUri)); } @GetMapping(value = "/redirect") public ResponseEntity> redirectKakao(@RequestParam String code) { - KakaoProfile profile = OAuth2Service.getResultProfile(code); + KakaoProfile profile = oAuth2Service.getResultProfile(code); if (profile == null) throw new RuntimeException("카카오에 해당 회원이 없습니다."); - + SocialUserInfoDTO socialUserInfoDTO = SocialUserInfoDTO.builder() .accountId(profile.getId()) .provider("KAKAO") diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/KakaoProfile.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/KakaoProfile.java similarity index 81% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/KakaoProfile.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/KakaoProfile.java index 8a7f1d94..01367c4a 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/KakaoProfile.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/KakaoProfile.java @@ -1,4 +1,4 @@ -package org.prgrms.nabimarketbe.oauth2.kakao.dto; +package org.prgrms.nabimarketbe.domain.oauth2.kakao.dto; import lombok.Getter; import lombok.ToString; diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/RetKakaoOAuth.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/RetKakaoOAuth.java similarity index 82% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/RetKakaoOAuth.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/RetKakaoOAuth.java index b30ee4aa..35b1c629 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/dto/RetKakaoOAuth.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/dto/RetKakaoOAuth.java @@ -1,4 +1,4 @@ -package org.prgrms.nabimarketbe.oauth2.kakao.dto; +package org.prgrms.nabimarketbe.domain.oauth2.kakao.dto; import lombok.Getter; diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/CustomUserDetailsService.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/CustomUserDetailsService.java similarity index 93% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/CustomUserDetailsService.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/CustomUserDetailsService.java index 9b6258ff..299df92f 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/CustomUserDetailsService.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/CustomUserDetailsService.java @@ -1,6 +1,7 @@ -package org.prgrms.nabimarketbe.oauth2.kakao.service; +package org.prgrms.nabimarketbe.domain.oauth2.kakao.service; import lombok.RequiredArgsConstructor; + import org.prgrms.nabimarketbe.domain.user.repository.UserRepository; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/OAuth2Service.java similarity index 83% rename from src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java rename to src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/OAuth2Service.java index c07b1a27..469042ef 100644 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/kakao/service/OAuth2Service.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/oauth2/kakao/service/OAuth2Service.java @@ -1,7 +1,7 @@ -package org.prgrms.nabimarketbe.oauth2.kakao.service; +package org.prgrms.nabimarketbe.domain.oauth2.kakao.service; -import org.prgrms.nabimarketbe.oauth2.kakao.dto.KakaoProfile; -import org.prgrms.nabimarketbe.oauth2.kakao.dto.RetKakaoOAuth; +import org.prgrms.nabimarketbe.domain.oauth2.kakao.dto.KakaoProfile; +import org.prgrms.nabimarketbe.domain.oauth2.kakao.dto.RetKakaoOAuth; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.env.Environment; import org.springframework.http.HttpEntity; @@ -37,10 +37,10 @@ public class OAuth2Service { public StringBuilder createUri() { StringBuilder loginUri = new StringBuilder() - .append(env.getProperty("social.kakao.url.login")) - .append("?response_type=code") - .append("&client_id=").append(kakaoClientId) - .append("&redirect_uri=").append(kakaoRedirectUri); + .append(env.getProperty("social.kakao.url.login")) + .append("?response_type=code") + .append("&client_id=").append(kakaoClientId) + .append("&redirect_uri=").append(kakaoRedirectUri); return loginUri; } @@ -52,7 +52,8 @@ public KakaoProfile getKakaoProfile(String kakaoAccessToken) { String requestUrl = env.getProperty("social.kakao.url.profile"); - if (requestUrl == null) throw new RuntimeException("CommunicationException"); + if (requestUrl == null) + throw new RuntimeException("CommunicationException"); HttpEntity> request = new HttpEntity<>(null, headers); @@ -79,7 +80,8 @@ public RetKakaoOAuth getKakaoTokenInfo(String code) { params.add("code", code); String requestUri = env.getProperty("social.kakao.url.token"); - if (requestUri == null) throw new RuntimeException("CommunicationException"); + if (requestUri == null) + throw new RuntimeException("CommunicationException"); HttpEntity> request = new HttpEntity<>(params, headers); diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java deleted file mode 100644 index 06cfae24..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/GoogleOAuth2.java +++ /dev/null @@ -1,117 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.domain; - -import java.util.HashMap; -import java.util.Map; -import java.util.stream.Collectors; - -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.util.MultiValueMap; -import org.springframework.web.client.RestTemplate; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; - -import lombok.RequiredArgsConstructor; - -@Component -@RequiredArgsConstructor -public class GoogleOAuth2 implements OAuth2 { - @Value("${spring.security.oauth2.client.registration.google.url}") - private String GOOGLE_SNS_LOGIN_URL; - - @Value("${spring.security.oauth2.client.registration.google.client-id}") - private String GOOGLE_SNS_CLIENT_ID; - - @Value("${spring.security.oauth2.client.registration.google.redirect-uri}") - private String GOOGLE_SNS_CALLBACK_URL; - - @Value("${spring.security.oauth2.client.registration.google.client-secret}") - private String GOOGLE_SNS_CLIENT_SECRET; - - @Value("${spring.security.oauth2.client.registration.google.scope}") - private String GOOGLE_DATA_ACCESS_SCOPE; - - @Value("${spring.security.oauth2.client.registration.google.token-url}") - private String GOOGLE_TOKEN_REQUEST_URL; - - @Value("${spring.security.oauth2.client.registration.google.user-info-url}") - private String GOOGLE_USER_INFO_REQUEST_URL; - - private final ObjectMapper objectMapper; - - private final RestTemplate restTemplate; - - @Override - public String getOAuth2RedirectUrl() { - Map params = new HashMap<>(); - - params.put("scope", GOOGLE_DATA_ACCESS_SCOPE); - params.put("response_type", "code"); - params.put("client_id", GOOGLE_SNS_CLIENT_ID); - params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); - - String parameterString = params.entrySet().stream() - .map(x -> x.getKey() + "=" + x.getValue()) - .collect(Collectors.joining("&")); - - String redirectURL = GOOGLE_SNS_LOGIN_URL + "?" + parameterString; - return redirectURL; - } - - @Override - public ResponseEntity requestAccessToken(String code) { - Map params = new HashMap<>(); - - params.put("code", code); - params.put("client_id", GOOGLE_SNS_CLIENT_ID); - params.put("client_secret", GOOGLE_SNS_CLIENT_SECRET); - params.put("redirect_uri", GOOGLE_SNS_CALLBACK_URL); - params.put("grant_type", "authorization_code"); - - ResponseEntity accessToken = restTemplate.postForEntity( - GOOGLE_TOKEN_REQUEST_URL, - params, - String.class - ); - - return accessToken; - } - - @Override - public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException { - return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class); - } - - @Override - public ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) { - HttpHeaders headers = new HttpHeaders(); - - HttpEntity> request = new HttpEntity<>(headers); - headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken()); - ResponseEntity exchange = restTemplate.exchange( - GOOGLE_USER_INFO_REQUEST_URL, - HttpMethod.GET, - request, - String.class - ); - - return exchange; - } - - @Override - public GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfoResponse) throws JsonProcessingException { - GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue( - userInfoResponse.getBody(), - GoogleUserInfoDTO.class - ); - - return googleUserInfoDTO; - } -} diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java deleted file mode 100644 index 8b25537c..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/OAuth2.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.domain; - -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleOAuth2TokenDTO; -import org.prgrms.nabimarketbe.oauth2.google.dto.GoogleUserInfoDTO; -import org.springframework.http.ResponseEntity; - -import com.fasterxml.jackson.core.JsonProcessingException; - -public interface OAuth2 { - String getOAuth2RedirectUrl(); - - ResponseEntity requestAccessToken(String code); - - GoogleOAuth2TokenDTO getAccessToken(ResponseEntity accessToken) throws JsonProcessingException; - - ResponseEntity requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken); - - GoogleUserInfoDTO parseUserInfo(ResponseEntity userInfo) throws JsonProcessingException; -} diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java deleted file mode 100644 index 901952f2..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/domain/Role.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.domain; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public enum Role { - GUEST("ROLE_GUEST"), - USER("ROLE_USER"); - - private final String key; -} diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java deleted file mode 100644 index 2d923a0b..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleOAuth2TokenDTO.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.dto; - -import com.fasterxml.jackson.annotation.JsonProperty; - -public record GoogleOAuth2TokenDTO( - @JsonProperty("access_token") - String accessToken, - - @JsonProperty("expires_in") - int expiresIn, - - String scope, - - @JsonProperty("token_type") - String tokenType, - - @JsonProperty("id_token") - String idToken -) { -} diff --git a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java b/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java deleted file mode 100644 index 479a08a3..00000000 --- a/src/main/java/org/prgrms/nabimarketbe/oauth2/google/dto/GoogleUserInfoDTO.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.prgrms.nabimarketbe.oauth2.google.dto; - -import org.prgrms.nabimarketbe.domain.user.Role; -import org.prgrms.nabimarketbe.domain.user.entity.User; - -public record GoogleUserInfoDTO( - String id -) { - private static final String PROVIDER = "GOOGLE"; - - public User toEntity(String nickName) { - return User.builder() - .nickname(nickName) - .provider(PROVIDER) - .accountId(id) - .role(Role.USER.getKey()) - .build(); - } -} From 66a55a804b34d3e9aee3dac6daa72cc703780fff Mon Sep 17 00:00:00 2001 From: BeommoKoo-dev <95630007+BeommoKoo-dev@users.noreply.github.com> Date: Fri, 1 Dec 2023 17:29:56 +0900 Subject: [PATCH 2/2] =?UTF-8?q?NO-JIRA--chore=20:=20Role=20Enum=20user=20e?= =?UTF-8?q?ntity=ED=8C=A8=ED=82=A4=EC=A7=80=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 --- .../nabimarketbe/domain/user/dto/request/SocialUserInfoDTO.java | 2 +- .../org/prgrms/nabimarketbe/domain/user/{ => entity}/Role.java | 2 +- .../nabimarketbe/global/config/SecurityConfiguration.java | 2 +- .../java/org/prgrms/nabimarketbe/setup/user/UserBuilder.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/main/java/org/prgrms/nabimarketbe/domain/user/{ => entity}/Role.java (79%) diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/SocialUserInfoDTO.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/SocialUserInfoDTO.java index a42371ff..e8bd65f0 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/SocialUserInfoDTO.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/dto/request/SocialUserInfoDTO.java @@ -2,7 +2,7 @@ import javax.validation.constraints.NotBlank; -import org.prgrms.nabimarketbe.domain.user.Role; +import org.prgrms.nabimarketbe.domain.user.entity.Role; import org.prgrms.nabimarketbe.domain.user.entity.User; import lombok.Builder; diff --git a/src/main/java/org/prgrms/nabimarketbe/domain/user/Role.java b/src/main/java/org/prgrms/nabimarketbe/domain/user/entity/Role.java similarity index 79% rename from src/main/java/org/prgrms/nabimarketbe/domain/user/Role.java rename to src/main/java/org/prgrms/nabimarketbe/domain/user/entity/Role.java index 21caaedc..5c6c9100 100644 --- a/src/main/java/org/prgrms/nabimarketbe/domain/user/Role.java +++ b/src/main/java/org/prgrms/nabimarketbe/domain/user/entity/Role.java @@ -1,4 +1,4 @@ -package org.prgrms.nabimarketbe.domain.user; +package org.prgrms.nabimarketbe.domain.user.entity; import lombok.Getter; import lombok.RequiredArgsConstructor; diff --git a/src/main/java/org/prgrms/nabimarketbe/global/config/SecurityConfiguration.java b/src/main/java/org/prgrms/nabimarketbe/global/config/SecurityConfiguration.java index 6047f89c..f1be59e2 100644 --- a/src/main/java/org/prgrms/nabimarketbe/global/config/SecurityConfiguration.java +++ b/src/main/java/org/prgrms/nabimarketbe/global/config/SecurityConfiguration.java @@ -1,6 +1,6 @@ package org.prgrms.nabimarketbe.global.config; -import org.prgrms.nabimarketbe.domain.user.Role; +import org.prgrms.nabimarketbe.domain.user.entity.Role; import org.prgrms.nabimarketbe.global.security.handler.CustomAccessDeniedHandler; import org.prgrms.nabimarketbe.global.security.handler.CustomAuthenticationEntryPoint; import org.prgrms.nabimarketbe.global.security.jwt.filter.JwtAuthenticationFilter; diff --git a/src/test/java/org/prgrms/nabimarketbe/setup/user/UserBuilder.java b/src/test/java/org/prgrms/nabimarketbe/setup/user/UserBuilder.java index b87c942a..43fa12b5 100644 --- a/src/test/java/org/prgrms/nabimarketbe/setup/user/UserBuilder.java +++ b/src/test/java/org/prgrms/nabimarketbe/setup/user/UserBuilder.java @@ -1,6 +1,6 @@ package org.prgrms.nabimarketbe.setup.user; -import org.prgrms.nabimarketbe.domain.user.Role; +import org.prgrms.nabimarketbe.domain.user.entity.Role; import org.prgrms.nabimarketbe.domain.user.entity.User; public class UserBuilder {