Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
0a37639
fix: 로그인 할 때 refresh_token을 항상 가져오도록 수정
kwon204 Feb 6, 2025
6aa22c4
refactor: google oauth 관련 기능 추출
kwon204 Feb 6, 2025
8b8af08
refactor: getAccessToken -> getGoogleTokens 로 수정
kwon204 Feb 6, 2025
b2bdf54
refactor: RestTemplate -> RestClient로 변경
kwon204 Feb 7, 2025
8e8afd4
feat: 구글 access token 재발급 기능 추가
kwon204 Feb 7, 2025
db490fc
feat: OAuth 요청 관련 예외 처리 추가
kwon204 Feb 7, 2025
1b05e2a
feat: 재발급한 access token 업데이트 기능 추가
kwon204 Feb 7, 2025
bfb14d3
refactor: 반복 사용 코드 메소드 추출
kwon204 Feb 7, 2025
4f84a63
refactor: User에서 인증 관련 기능을 Auth로 빼서 관리
kwon204 Feb 7, 2025
b9536a3
feat: 재발급한 구글 액세스 토큰을 db에 저장하는 기능 추가
kwon204 Feb 7, 2025
1b79eb0
refactor: 반복 사용되는 코드 메소드 추출
kwon204 Feb 7, 2025
bb2fa52
test: 사용하지 않는 UserServiceTest 삭제
kwon204 Feb 7, 2025
57ae903
refactor: reissueAccessToken은 토큰 재발급만 하도록 변경
kwon204 Feb 7, 2025
7e4ddff
fix: 구글 OAuth code와 사용자 정보 유효성 검사 코드 추가
kwon204 Feb 7, 2025
a0802fe
fix: Unauthorize -> Bad Request로 수정
kwon204 Feb 7, 2025
9cf4c37
fix: 로그인한 사용자에게 토큰 업데이트 하지 않는 버그 수정
kwon204 Feb 7, 2025
a3bb8c4
feat: readOnly 추가
kwon204 Feb 7, 2025
4c3f9d6
refactor: 좀 더 명확한 메소드 이름으로 수정
kwon204 Feb 7, 2025
1993600
fix: 사용하지 않는 status 삭제
kwon204 Feb 8, 2025
0874c90
fix: , 추가
kwon204 Feb 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package endolphin.backend.domain.auth;

import endolphin.backend.domain.auth.dto.OAuthResponse;
import endolphin.backend.domain.auth.dto.UrlResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Auth", description = "인증 관리 API")
@RestController
@RequiredArgsConstructor
public class AuthController {

private final AuthService authService;

@Operation(summary = "구글 로그인 URL", description = "구글 로그인 URL을 반환합니다.")
@GetMapping("/api/v1/google")
public ResponseEntity<UrlResponse> loginUrl() {
UrlResponse response = authService.getGoogleLoginUrl();
return ResponseEntity.ok(response);
}

@Operation(summary = "구글 로그인 콜백", description = "사용자가 Google 계정으로 로그인하여 JWT 토큰을 발급받습니다.")
@GetMapping("/oauth2/callback")
public ResponseEntity<OAuthResponse> oauthCallback(@RequestParam("code") String code) {
OAuthResponse response = authService.oauth2Callback(code);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package endolphin.backend.domain.auth;

import endolphin.backend.domain.user.UserService;
import endolphin.backend.global.error.exception.ErrorCode;
import endolphin.backend.global.error.exception.OAuthException;
import endolphin.backend.global.google.dto.GoogleTokens;
import endolphin.backend.global.google.dto.GoogleUserInfo;
import endolphin.backend.domain.auth.dto.OAuthResponse;
import endolphin.backend.domain.auth.dto.UrlResponse;
import endolphin.backend.domain.user.entity.User;
import endolphin.backend.global.config.GoogleOAuthProperties;
import endolphin.backend.global.google.GoogleOAuthService;
import endolphin.backend.global.security.JwtProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AuthService {

private final GoogleOAuthProperties googleOAuthProperties;
private final GoogleOAuthService googleOAuthService;
private final UserService userService;
private final JwtProvider jwtProvider;

public UrlResponse getGoogleLoginUrl() {
return new UrlResponse(String.format(
"%s?client_id=%s&redirect_uri=%s&response_type=code&scope=%s&access_type=offline&prompt=consent",
googleOAuthProperties.authUrl(), googleOAuthProperties.clientId(),
googleOAuthProperties.redirectUri(), googleOAuthProperties.scope()));
}

@Transactional
public OAuthResponse oauth2Callback(String code) {
if (code == null || code.isBlank()) {
throw new OAuthException(ErrorCode.INVALID_OAUTH_CODE);
}
GoogleTokens tokenResponse = googleOAuthService.getGoogleTokens(code);
GoogleUserInfo userInfo = googleOAuthService.getUserInfo(tokenResponse.accessToken());
validateUserInfo(userInfo);
User user = userService.upsertUser(userInfo, tokenResponse);
String accessToken = jwtProvider.createToken(user.getId(), user.getEmail());
return new OAuthResponse(accessToken);
}

private void validateUserInfo(GoogleUserInfo userInfo) {
if (userInfo == null) {
throw new OAuthException(ErrorCode.INVALID_OAUTH_USER_INFO);
}
if (userInfo.email() == null || userInfo.email().isBlank()) {
throw new OAuthException(ErrorCode.INVALID_OAUTH_USER_INFO);
}
if (userInfo.picture() == null || userInfo.picture().isBlank()) {
throw new OAuthException(ErrorCode.INVALID_OAUTH_USER_INFO);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package endolphin.backend.domain.user.dto;
package endolphin.backend.domain.auth.dto;

public record OAuthResponse(String accessToken) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package endolphin.backend.domain.user.dto;
package endolphin.backend.domain.auth.dto;

public record UrlResponse(String url) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package endolphin.backend.domain.user;

import endolphin.backend.domain.user.dto.OAuthResponse;
import endolphin.backend.domain.user.dto.UrlResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "User", description = "사용자 관리 API")
Expand All @@ -17,17 +11,4 @@ public class UserController {

private final UserService userService;

@Operation(summary = "구글 로그인 URL", description = "구글 로그인 URL을 반환합니다.")
@GetMapping("/api/v1/google")
public ResponseEntity<UrlResponse> loginUrl() {
UrlResponse response = userService.getGoogleLoginUrl();
return ResponseEntity.ok(response);
}

@Operation(summary = "구글 로그인 콜백", description = "사용자가 Google 계정으로 로그인하여 JWT 토큰을 발급받습니다.")
@GetMapping("/oauth2/callback")
public ResponseEntity<OAuthResponse> oauthCallback(@RequestParam("code") String code) {
OAuthResponse response = userService.oauth2Callback(code);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -1,112 +1,48 @@
package endolphin.backend.domain.user;

import endolphin.backend.domain.user.dto.GoogleUserInfo;
import endolphin.backend.domain.user.dto.GoogleTokens;
import endolphin.backend.domain.user.dto.OAuthResponse;
import endolphin.backend.domain.user.dto.UrlResponse;
import endolphin.backend.global.google.dto.GoogleUserInfo;
import endolphin.backend.global.google.dto.GoogleTokens;
import endolphin.backend.domain.user.entity.User;
import endolphin.backend.global.config.GoogleOAuthProperties;
import endolphin.backend.global.error.exception.ApiException;
import endolphin.backend.global.error.exception.ErrorCode;
import endolphin.backend.global.security.JwtProvider;
import endolphin.backend.global.security.UserContext;
import endolphin.backend.global.security.UserInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

@Service
@RequiredArgsConstructor
@Transactional
@Slf4j
public class UserService {

private final GoogleOAuthProperties googleOAuthProperties;
private final UserRepository userRepository;
private final JwtProvider jwtProvider;
private final RestTemplate restTemplate;

public UrlResponse getGoogleLoginUrl() {
return new UrlResponse(String.format(
"%s?client_id=%s&redirect_uri=%s&response_type=code&scope=%s&access_type=offline",
googleOAuthProperties.authUrl(), googleOAuthProperties.clientId(),
googleOAuthProperties.redirectUri(), googleOAuthProperties.scope()));
}

public OAuthResponse oauth2Callback(String code) {
GoogleTokens tokenResponse = getAccessToken(code);
GoogleUserInfo userInfo = getUserInfo(tokenResponse.accessToken());
User user = createUser(userInfo, tokenResponse);
String accessToken = jwtProvider.createToken(user.getId(), user.getEmail());
return new OAuthResponse(accessToken);
}

@Transactional(readOnly = true)
public User getCurrentUser() {
UserInfo userInfo = UserContext.get();
return userRepository.findById(userInfo.userId())
.orElseThrow(() -> new ApiException(ErrorCode.USER_NOT_FOUND));
}

private User createUser(GoogleUserInfo userInfo, GoogleTokens tokenResponse) {
public void updateAccessToken(User user, String accessToken) {
user.setAccessToken(accessToken);
userRepository.save(user);
}

public User upsertUser(GoogleUserInfo userInfo, GoogleTokens tokenResponse) {
User user = userRepository.findByEmail(userInfo.email())
.orElseGet(() -> {
return User.builder()
.email(userInfo.email())
.name(userInfo.name())
.picture(userInfo.picture())
.accessToken(tokenResponse.accessToken())
.refreshToken(tokenResponse.refreshToken())
.build();
});
user.setAccessToken(tokenResponse.accessToken());
user.setRefreshToken(tokenResponse.refreshToken());
userRepository.save(user);
return user;
}

private GoogleTokens getAccessToken(String code) {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

HttpEntity<MultiValueMap<String, String>> request = getHttpEntity(
code, headers);
ResponseEntity<GoogleTokens> response = restTemplate.postForEntity(
googleOAuthProperties.tokenUrl(), request,
GoogleTokens.class);

return response.getBody();
}

private HttpEntity<MultiValueMap<String, String>> getHttpEntity(String code,
HttpHeaders headers) {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("client_id", googleOAuthProperties.clientId());
params.add("client_secret", googleOAuthProperties.clientSecret());
params.add("redirect_uri", googleOAuthProperties.redirectUri());
params.add("code", code);
params.add("grant_type", "authorization_code");
params.add("access_type", "offline");

HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers);
return request;
}

private GoogleUserInfo getUserInfo(String accessToken) {
HttpHeaders headers = new HttpHeaders();
headers.setBearerAuth(accessToken);

HttpEntity<String> entity = new HttpEntity<>(headers);

ResponseEntity<GoogleUserInfo> result = restTemplate.exchange(googleOAuthProperties.userInfoUrl(),
HttpMethod.GET, entity, GoogleUserInfo.class);

return result.getBody();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ public class User extends BaseTimeEntity {
private String email;
@Column(nullable = false)
private String picture;
@Setter
@Column(name = "access_token")
private String accessToken;
@Setter
@Column(name = "refresh_token")
private String refreshToken;

Expand All @@ -42,4 +44,5 @@ public User(String name, String email, String picture, String accessToken,
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,16 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.client.RestClient;

@Configuration
public class RestTemplateConfig {
public class RestClientConfig {

@Bean
public RestTemplate restTemplate() {
RestTemplate restTemplate = new RestTemplate();
public RestClient restClient() {
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(5000);
requestFactory.setReadTimeout(5000);

restTemplate.setRequestFactory(requestFactory);

return restTemplate;
return RestClient.builder().requestFactory(requestFactory).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import endolphin.backend.global.error.exception.ApiException;
import endolphin.backend.global.error.exception.ErrorCode;
import endolphin.backend.global.error.exception.OAuthException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -20,6 +21,14 @@ public ResponseEntity<ErrorResponse> handleApiException(ApiException e) {
return ResponseEntity.status(e.getErrorCode().getHttpStatus()).body(response);
}

@ExceptionHandler(OAuthException.class)
public ResponseEntity<ErrorResponse> handleOAuthException(OAuthException e) {
log.error("[OAuth exception] Error code: {}, Message: {}",
e.getErrorCode(), e.getMessage(), e);
ErrorResponse response = ErrorResponse.of(e.getErrorCode());
return ResponseEntity.status(e.getErrorCode().getHttpStatus()).body(response);
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleUnexpectedException(Exception e) {
log.error("[Unexpected exception] ", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ public enum ErrorCode {
DISCUSSION_NOT_FOUND(HttpStatus.NOT_FOUND, "D001", "Discussion not found"),

//SharedEvent
SHARED_EVENT_NOT_FOUND(HttpStatus.NOT_FOUND, "S001", "Shared Event not found")
SHARED_EVENT_NOT_FOUND(HttpStatus.NOT_FOUND, "S001", "Shared Event not found"),

// OAuth
OAUTH_UNAUTHORIZED_ERROR(HttpStatus.UNAUTHORIZED, "O001", "OAuth Unauthorized Error"),
OAUTH_BAD_REQUEST_ERROR(HttpStatus.BAD_REQUEST, "O002", "OAuth Bad Request Error"),
OAUTH_FORBIDDEN_ERROR(HttpStatus.FORBIDDEN, "O003", "OAuth Forbidden Error"),
INVALID_OAUTH_CODE(HttpStatus.UNAUTHORIZED, "O004", "Invalid OAuth Code"),
INVALID_OAUTH_USER_INFO(HttpStatus.UNAUTHORIZED, "O005", "Invalid OAuth User Info"),
;
private final HttpStatus httpStatus;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package endolphin.backend.global.error.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

@Getter
public class OAuthException extends RuntimeException {

private final ErrorCode errorCode;

public OAuthException(HttpStatus status, String message) {
super(message);
switch (status) {
case UNAUTHORIZED -> errorCode = ErrorCode.OAUTH_UNAUTHORIZED_ERROR;
case FORBIDDEN -> errorCode = ErrorCode.OAUTH_FORBIDDEN_ERROR;
case BAD_REQUEST -> errorCode = ErrorCode.OAUTH_BAD_REQUEST_ERROR;
default -> errorCode = ErrorCode.INTERNAL_ERROR;
}
}

public OAuthException(ErrorCode errorCode) {
super(errorCode.getMessage());
this.errorCode = errorCode;
}
}
Loading