Skip to content

Commit

Permalink
Merge pull request #86 from team-nabi/NO-JIRA--beommo--package-migration
Browse files Browse the repository at this point in the history
NO-JIRA--chore : package migration
  • Loading branch information
BeommoKoo-dev authored Dec 11, 2023
2 parents b054809 + 66a55a8 commit 68c6a70
Show file tree
Hide file tree
Showing 20 changed files with 207 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<String> requestAccessToken(String code) {
Map<String, Object> 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<String> accessToken = restTemplate.postForEntity(
GOOGLE_TOKEN_REQUEST_URL,
params,
String.class
);

return accessToken;
}

@Override
public GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException {
return objectMapper.readValue(accessToken.getBody(), GoogleOAuth2TokenDTO.class);
}

@Override
public ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken) {
HttpHeaders headers = new HttpHeaders();

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(headers);
headers.add("Authorization", "Bearer " + googleOAuthToken.accessToken());
ResponseEntity<String> exchange = restTemplate.exchange(
GOOGLE_USER_INFO_REQUEST_URL,
HttpMethod.GET,
request,
String.class
);

return exchange;
}

@Override
public GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfoResponse) throws JsonProcessingException {
GoogleUserInfoDTO googleUserInfoDTO = objectMapper.readValue(
userInfoResponse.getBody(),
GoogleUserInfoDTO.class
);

return googleUserInfoDTO;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> requestAccessToken(String code);

GoogleOAuth2TokenDTO getAccessToken(ResponseEntity<String> accessToken) throws JsonProcessingException;

ResponseEntity<String> requestUserInfo(GoogleOAuth2TokenDTO googleOAuthToken);

GoogleUserInfoDTO parseUserInfo(ResponseEntity<String> userInfo) throws JsonProcessingException;
}
Original file line number Diff line number Diff line change
@@ -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
) {
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<SingleResult<UserLoginResponseDTO>> 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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.prgrms.nabimarketbe.oauth2.kakao.dto;
package org.prgrms.nabimarketbe.domain.oauth2.kakao.dto;

import lombok.Getter;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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<MultiValueMap<String, String>> request = new HttpEntity<>(null, headers);

Expand All @@ -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<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.prgrms.nabimarketbe.domain.user;
package org.prgrms.nabimarketbe.domain.user.entity;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading

0 comments on commit 68c6a70

Please sign in to comment.