Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor #108

Merged
merged 4 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -2,7 +2,6 @@

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -62,13 +61,13 @@ public void rejectApplication(@PathVariable Long id) {

@GetMapping("/project/{id}")
@Operation(summary = "프로젝트에 신청한 모든 유저 조회")
public ResponseEntity<List<ApplicationResponse>> findProjectApplication(@PathVariable Long id) {
return ResponseEntity.ok(findProjectApplicationService.execute(id));
public List<ApplicationResponse> findProjectApplication(@PathVariable Long id) {
return findProjectApplicationService.execute(id);
}

@GetMapping("/{id}")
@Operation(summary = "신청 하나 조회")
public ResponseEntity<ApplicationResponse> findOneApplication(@PathVariable Long id) {
return ResponseEntity.ok(findApplicationService.execute(id));
public ApplicationResponse findOneApplication(@PathVariable Long id) {
return findApplicationService.execute(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.woongeya.zoing.global.feign;
package com.woongeya.zoing.domain.auth.infra.feign;

import com.woongeya.zoing.global.feign.dto.request.GoogleTokenRequest;
import com.woongeya.zoing.global.feign.dto.response.GoogleTokenResponse;
import com.woongeya.zoing.domain.auth.infra.feign.dto.request.GoogleTokenRequest;
import com.woongeya.zoing.domain.auth.infra.feign.dto.response.GoogleTokenResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.woongeya.zoing.global.feign;
package com.woongeya.zoing.domain.auth.infra.feign;

import com.woongeya.zoing.global.feign.dto.response.GoogleInfoResponse;
import com.woongeya.zoing.domain.auth.infra.feign.dto.response.GoogleInfoResponse;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.woongeya.zoing.domain.auth.infra.feign.dto.request;

import com.woongeya.zoing.global.config.credential.AuthCredentials;

public record GoogleTokenRequest (
String code,
String clientId,
String clientSecret,
String redirectUri,
String grantType
) {
public static GoogleTokenRequest from(String code, AuthCredentials authCredentials) {
return new GoogleTokenRequest(code, authCredentials.clientId(), authCredentials.clientSecret(),
authCredentials.redirectUri(), "authorization_code");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.woongeya.zoing.domain.auth.infra.feign.dto.response;

import static com.woongeya.zoing.domain.user.domain.autority.Authority.*;

import com.woongeya.zoing.domain.user.domain.User;

public record GoogleInfoResponse (
String name,
String email,
String picture
) {

public User toEntity() {
return User.builder()
.name(name)
.nickName(name)
.email(email)
.imgUrl(picture)
.authority(USER)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.woongeya.zoing.domain.auth.infra.feign.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;

public record GoogleTokenResponse(
@JsonProperty("access_token")
String accessToken
) {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.woongeya.zoing.domain.auth.intercepter;
package com.woongeya.zoing.domain.auth.interceptor;

import static org.springframework.http.HttpHeaders.*;

Expand All @@ -11,12 +11,14 @@
import com.woongeya.zoing.domain.auth.annotation.LoginRequired;
import com.woongeya.zoing.domain.auth.exception.TokenNotExistException;
import com.woongeya.zoing.domain.auth.exception.UserIsNotAdminException;
import com.woongeya.zoing.domain.auth.repository.AuthRepository;
import com.woongeya.zoing.domain.auth.service.implementation.AuthReader;
import com.woongeya.zoing.domain.auth.service.implementation.AuthUpdater;
import com.woongeya.zoing.domain.auth.util.BearerTokenExtractor;
import com.woongeya.zoing.domain.auth.util.JwtParser;
import com.woongeya.zoing.domain.user.UserFacade;
import com.woongeya.zoing.domain.user.domain.User;
import com.woongeya.zoing.domain.user.domain.autority.Authority;
import com.woongeya.zoing.domain.user.service.implementation.UserReader;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -27,32 +29,31 @@
public class AuthInterceptor implements HandlerInterceptor {

private final JwtParser jwtParser;
private final AuthRepository authRepository;
private final UserFacade userFacade;
private final AuthUpdater authUpdater;
private final AuthReader authReader;
private final UserReader userReader;

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (handler instanceof HandlerMethod hm) {
if (hm.hasMethodAnnotation(LoginOrNot.class)) {
String bearer = request.getHeader(AUTHORIZATION);

if (bearer == null) {
authRepository.updateCurrentUser(null);
} else {
if (!(bearer == null)) {
String jwt = BearerTokenExtractor.extract(bearer);
Long userId = jwtParser.getIdFromJwt(jwt);
User user = userFacade.getUserById(userId);
authRepository.updateCurrentUser(user);
User user = userReader.readUser(userId);
authUpdater.updateCurrentUser(user);
}
}

if (hm.hasMethodAnnotation(LoginRequired.class)) {
if (authRepository.getCurrentUser() == null) {
if (authReader.getCurrentUser() == null) {
throw new TokenNotExistException();
}
}
if (hm.hasMethodAnnotation(AdminOnly.class)) {
User currentUser = authRepository.getCurrentUser();
User currentUser = authReader.getCurrentUser();
shouldUserAdmin(currentUser);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import com.woongeya.zoing.domain.auth.presetation.dto.request.RefreshTokenRequest;
import com.woongeya.zoing.domain.auth.presetation.dto.response.TokenResponse;
import com.woongeya.zoing.domain.auth.service.OAuth2GoogleService;
import com.woongeya.zoing.domain.auth.service.RefreshTokenService;
import com.woongeya.zoing.domain.auth.service.CommandAuthService;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
Expand All @@ -21,18 +20,17 @@
@RequiredArgsConstructor
public class AuthController {

private final OAuth2GoogleService googleService;
private final RefreshTokenService refreshTokenService;
private final CommandAuthService commandAuthService;

@PostMapping("/google")
@Operation(summary = "구글 로그인")
public TokenResponse loginOfGoogle(@Validated @RequestParam(name = "code") String code) {
return TokenResponse.from(googleService.execute(code));
return TokenResponse.from(commandAuthService.login(code));
}

@PutMapping()
@Operation(summary = "토큰 재발급")
public TokenResponse refreshToken(@RequestBody RefreshTokenRequest refreshToken) {
return TokenResponse.from(refreshTokenService.execute(refreshToken.refreshToken()));
return TokenResponse.from(commandAuthService.refresh(refreshToken.refreshToken()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Repository
@RequestScope
public class AuthRepository {
private User currentUser;
private User currentUser = null;

public User getCurrentUser() {
if (currentUser == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.woongeya.zoing.domain.auth.service;

import java.util.Optional;

import org.springframework.stereotype.Service;

import com.woongeya.zoing.domain.auth.domain.Token;
import com.woongeya.zoing.domain.auth.infra.feign.GoogleAuthClient;
import com.woongeya.zoing.domain.auth.infra.feign.GoogleInfoClient;
import com.woongeya.zoing.domain.auth.infra.feign.dto.request.GoogleTokenRequest;
import com.woongeya.zoing.domain.auth.infra.feign.dto.response.GoogleInfoResponse;
import com.woongeya.zoing.domain.auth.infra.feign.dto.response.GoogleTokenResponse;
import com.woongeya.zoing.domain.auth.service.implementation.AuthReader;
import com.woongeya.zoing.domain.auth.service.implementation.TokenProvider;
import com.woongeya.zoing.domain.auth.util.BearerTokenExtractor;
import com.woongeya.zoing.domain.user.UserFacade;
import com.woongeya.zoing.domain.user.domain.User;
import com.woongeya.zoing.domain.user.domain.autority.Authority;
import com.woongeya.zoing.domain.user.domain.repository.UserRepository;
import com.woongeya.zoing.domain.user.exception.UserNotFoundException;
import com.woongeya.zoing.global.config.credential.AuthCredentials;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class CommandAuthService {

private final AuthReader authReader;
private final TokenProvider tokenProvider;
private final UserFacade userFacade;
private final GoogleAuthClient googleAuthClient;
private final GoogleInfoClient googleInfoClient;
private final AuthCredentials authCredentials;
private final UserRepository userRepository;

public Token login(String code) {
GoogleTokenResponse googleToken = googleAuthClient.getGoogleToken(
GoogleTokenRequest.from(code, authCredentials)
);
GoogleInfoResponse userInfo = googleInfoClient.getUserInfo(googleToken.accessToken());
User user = saveOrUpdate(userInfo);

return tokenProvider.createNewToken(user);
}

public Token refresh(String bearer) {
String refreshToken = BearerTokenExtractor.extract(bearer);
Long userId = authReader.getIdFromJwt(refreshToken);
String accessToken = tokenProvider.createAccessToken(userFacade.getUserById(userId));

return new Token(accessToken, refreshToken);
}

private User saveOrUpdate(GoogleInfoResponse userInfo) {
Optional<User> user = userRepository.findByEmail(userInfo.email());

if (user.isPresent()) {
return user.get().update(userInfo.toEntity());
}

return userRepository.save(userInfo.toEntity());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.woongeya.zoing.domain.auth.service.implementation;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.woongeya.zoing.domain.auth.repository.AuthRepository;
import com.woongeya.zoing.domain.auth.util.JwtParser;
import com.woongeya.zoing.domain.user.domain.User;

import lombok.RequiredArgsConstructor;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AuthReader {

private final AuthRepository authRepository;
private final JwtParser jwtParser;

public User getCurrentUser() {
return authRepository.getCurrentUser();
}

public User getNullableCurrentUser() {
return authRepository.getNullableCurrentUser();
}

public Long getIdFromJwt(String refresh) {
return jwtParser.getIdFromJwt(refresh);
}
}
Loading