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

Style/#51 패키지 구조 변경 #52

Merged
merged 4 commits into from
Oct 25, 2023
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
@@ -1,51 +1,22 @@
package coffeemeet.server.auth.controller;

import coffeemeet.server.auth.dto.SignupRequest;
import coffeemeet.server.auth.domain.AuthTokens;
import coffeemeet.server.auth.service.AuthService;
import coffeemeet.server.auth.utils.AuthTokens;
import coffeemeet.server.common.annotation.Login;
import coffeemeet.server.user.domain.OAuthProvider;
import coffeemeet.server.user.dto.AuthInfo;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/oauth2/auth")
@RequestMapping("/auth")
public class AuthController {

private final AuthService authService;

@GetMapping("/{oAuthProvider}")
public ResponseEntity<Void> redirectAuthCodeRequestUrl(@PathVariable OAuthProvider oAuthProvider,
HttpServletResponse response) throws IOException {
String redirectUrl = authService.getAuthCodeRequestUrl(oAuthProvider);
response.sendRedirect(redirectUrl);
return new ResponseEntity<>(HttpStatus.FOUND);
}

@PostMapping("/sign-up")
public ResponseEntity<AuthTokens> signup(@Valid @RequestBody SignupRequest request) {
return ResponseEntity.ok(authService.signup(request));
}

@GetMapping("/login/{oAuthProvider}")
public ResponseEntity<AuthTokens> login(@PathVariable OAuthProvider oAuthProvider,
@RequestParam String authCode) {
return ResponseEntity.ok(authService.login(oAuthProvider, authCode));
}

@PostMapping("/renew-token")
public ResponseEntity<AuthTokens> renew(@Login AuthInfo authInfo) {
return ResponseEntity.ok(authService.renew(authInfo.userId(), authInfo.refreshToken()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.utils;
package coffeemeet.server.auth.domain;

public record AuthTokens(
String accessToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package coffeemeet.server.auth.utils;
package coffeemeet.server.auth.domain;

import coffeemeet.server.auth.domain.RefreshToken;
import coffeemeet.server.auth.infrastructure.RefreshTokenRepository;
import coffeemeet.server.auth.repository.RefreshTokenRepository;
import java.util.Date;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.utils;
package coffeemeet.server.auth.domain;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.infrastructure;
package coffeemeet.server.auth.repository;

import coffeemeet.server.auth.domain.RefreshToken;
import org.springframework.data.repository.CrudRepository;
Expand Down
76 changes: 4 additions & 72 deletions src/main/java/coffeemeet/server/auth/service/AuthService.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
package coffeemeet.server.auth.service;

import coffeemeet.server.auth.domain.authcode.AuthCodeRequestUrlProviderComposite;
import coffeemeet.server.auth.domain.client.OAuthMemberClientComposite;
import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.auth.dto.SignupRequest;
import coffeemeet.server.auth.infrastructure.RefreshTokenRepository;
import coffeemeet.server.auth.utils.AuthTokens;
import coffeemeet.server.auth.utils.AuthTokensGenerator;
import coffeemeet.server.auth.utils.JwtTokenProvider;
import coffeemeet.server.interest.domain.Interest;
import coffeemeet.server.interest.domain.Keyword;
import coffeemeet.server.interest.repository.InterestRepository;
import coffeemeet.server.user.domain.OAuthInfo;
import coffeemeet.server.user.domain.OAuthProvider;
import coffeemeet.server.user.domain.Profile;
import coffeemeet.server.user.domain.User;
import coffeemeet.server.user.repository.UserRepository;
import coffeemeet.server.auth.domain.AuthTokens;
import coffeemeet.server.auth.domain.AuthTokensGenerator;
import coffeemeet.server.auth.domain.JwtTokenProvider;
import coffeemeet.server.auth.repository.RefreshTokenRepository;
import coffeemeet.server.user.service.UserService;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -28,51 +14,12 @@
public class AuthService {

private static final String EXPIRED_REFRESH_TOKEN_MESSAGE = "리프레시 토큰이 만료되었습니다. 다시 로그인해 주세요.";
private static final String USER_NOT_REGISTERED_MESSAGE = "해당 아이디(%s)와 로그인 타입(%s)의 유저는 회원가입되지 않았습니다.";
private static final String DEFAULT_IMAGE_URL = "기본 이미지 URL";

private final AuthCodeRequestUrlProviderComposite authCodeRequestUrlProviderComposite;
private final OAuthMemberClientComposite oauthMemberClientComposite;
private final UserService userService;
private final UserRepository userRepository;
private final InterestRepository interestRepository;
private final AuthTokensGenerator authTokensGenerator;
private final JwtTokenProvider jwtTokenProvider;
private final RefreshTokenRepository refreshTokenRepository;

public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
return authCodeRequestUrlProviderComposite.provide(oAuthProvider);
}

@Transactional
public AuthTokens signup(SignupRequest request) {
OAuthInfoResponse response = oauthMemberClientComposite.fetch(request.oAuthProvider(),
request.authCode());

userService.checkDuplicatedUser(response);
userService.checkDuplicatedNickname(request.nickname());
String profileImage = checkProfileImage(response.profileImage());

User user = new User(new OAuthInfo(response.oAuthProvider(), response.oAuthProviderId()),
Profile.builder().name(response.name()).nickname(request.nickname()).email(response.email())
.profileImageUrl(profileImage).birth(response.birth()).build());

User newUser = userRepository.save(user);
saveInterests(request, newUser);

return authTokensGenerator.generate(newUser.getId());
}

public AuthTokens login(OAuthProvider oAuthProvider, String authCode) {
OAuthInfoResponse response = oauthMemberClientComposite.fetch(oAuthProvider, authCode);
User foundUser = userRepository.getUserByOauthInfoOauthProviderAndOauthInfoOauthProviderId(
response.oAuthProvider(), response.oAuthProviderId()).orElseThrow(
() -> new IllegalArgumentException(
String.format(USER_NOT_REGISTERED_MESSAGE, response.oAuthProviderId(),
response.oAuthProvider())));
return authTokensGenerator.generate(foundUser.getId());
}

public AuthTokens renew(Long userId, String refreshToken) {
if (jwtTokenProvider.isExpiredRefreshToken(refreshToken)) {
throw new IllegalArgumentException(EXPIRED_REFRESH_TOKEN_MESSAGE);
Expand All @@ -91,19 +38,4 @@ public void delete(Long userId) {
refreshTokenRepository.deleteById(userId);
}

private String checkProfileImage(String profileImage) {
if (profileImage == null) {
profileImage = DEFAULT_IMAGE_URL;
}
return profileImage;
}

private void saveInterests(SignupRequest request, User newUser) {
List<Interest> interests = new ArrayList<>();
for (Keyword value : request.keywords()) {
interests.add(new Interest(value, newUser));
}
interestRepository.saveAll(interests);
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package coffeemeet.server.common;

import coffeemeet.server.auth.domain.JwtTokenProvider;
import coffeemeet.server.auth.domain.RefreshToken;
import coffeemeet.server.auth.infrastructure.RefreshTokenRepository;
import coffeemeet.server.auth.utils.JwtTokenProvider;
import coffeemeet.server.auth.repository.RefreshTokenRepository;
import coffeemeet.server.common.annotation.Login;
import coffeemeet.server.user.dto.AuthInfo;
import jakarta.servlet.http.HttpServletRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package coffeemeet.server.common.config;

import coffeemeet.server.auth.infrastructure.RefreshTokenRepository;
import coffeemeet.server.auth.utils.JwtTokenProvider;
import coffeemeet.server.auth.utils.converter.OAuthProviderConverter;
import coffeemeet.server.auth.domain.JwtTokenProvider;
import coffeemeet.server.auth.repository.RefreshTokenRepository;
import coffeemeet.server.common.UserArgumentResolver;
import coffeemeet.server.oauth.utils.converter.OAuthProviderConverter;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.domain.authcode;
package coffeemeet.server.oauth.authcode;

import coffeemeet.server.user.domain.OAuthProvider;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.domain.authcode;
package coffeemeet.server.oauth.authcode;

import coffeemeet.server.user.domain.OAuthProvider;
import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coffeemeet.server.auth.domain.client;
package coffeemeet.server.oauth.client;

import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.user.domain.OAuthProvider;

public interface OAuthMemberClient {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coffeemeet.server.auth.domain.client;
package coffeemeet.server.oauth.client;

import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.user.domain.OAuthProvider;
import java.util.Map;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package coffeemeet.server.oauth.controller;

import coffeemeet.server.oauth.service.OAuthService;
import coffeemeet.server.user.domain.OAuthProvider;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/oauth2")
public class OAuthController {

private final OAuthService oAuthService;

@GetMapping("/{oAuthProvider}")
public ResponseEntity<Void> redirectAuthCodeRequestUrl(@PathVariable OAuthProvider oAuthProvider,
HttpServletResponse response) throws IOException {
String redirectUrl = oAuthService.getAuthCodeRequestUrl(oAuthProvider);
response.sendRedirect(redirectUrl);
return new ResponseEntity<>(HttpStatus.FOUND);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.dto;
package coffeemeet.server.oauth.dto;

import coffeemeet.server.user.domain.Birth;
import coffeemeet.server.user.domain.Email;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.authcode;
package coffeemeet.server.oauth.infrastructure.kakao.authcode;

import coffeemeet.server.auth.domain.authcode.AuthCodeRequestUrlProvider;
import coffeemeet.server.auth.infrastructure.oauth.kakao.config.KakaoProperties;
import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProvider;
import coffeemeet.server.oauth.infrastructure.kakao.config.KakaoProperties;
import coffeemeet.server.user.domain.OAuthProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.client;
package coffeemeet.server.oauth.infrastructure.kakao.client;

import coffeemeet.server.auth.infrastructure.oauth.kakao.config.KakaoProperties;
import coffeemeet.server.auth.infrastructure.oauth.kakao.dto.KakaoMemberResponse;
import coffeemeet.server.auth.infrastructure.oauth.kakao.dto.KakaoTokens;
import coffeemeet.server.oauth.infrastructure.kakao.config.KakaoProperties;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoMemberResponse;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoTokens;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.client;
package coffeemeet.server.oauth.infrastructure.kakao.client;

import coffeemeet.server.auth.domain.client.OAuthMemberClient;
import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.auth.infrastructure.oauth.kakao.dto.KakaoMemberResponse;
import coffeemeet.server.auth.infrastructure.oauth.kakao.dto.KakaoTokens;
import coffeemeet.server.oauth.client.OAuthMemberClient;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoMemberResponse;
import coffeemeet.server.oauth.infrastructure.kakao.dto.KakaoTokens;
import coffeemeet.server.user.domain.OAuthProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.config;
package coffeemeet.server.oauth.infrastructure.kakao.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.config;
package coffeemeet.server.oauth.infrastructure.kakao.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.dto;
package coffeemeet.server.oauth.infrastructure.kakao.dto;

import coffeemeet.server.auth.dto.OAuthInfoResponse;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.user.domain.Birth;
import coffeemeet.server.user.domain.Email;
import coffeemeet.server.user.domain.OAuthProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.infrastructure.oauth.kakao.dto;
package coffeemeet.server.oauth.infrastructure.kakao.dto;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/coffeemeet/server/oauth/service/OAuthService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package coffeemeet.server.oauth.service;

import coffeemeet.server.oauth.authcode.AuthCodeRequestUrlProviderComposite;
import coffeemeet.server.oauth.client.OAuthMemberClientComposite;
import coffeemeet.server.oauth.dto.OAuthInfoResponse;
import coffeemeet.server.user.domain.OAuthProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class OAuthService {

private final AuthCodeRequestUrlProviderComposite authCodeRequestUrlProviderComposite;
private final OAuthMemberClientComposite oauthMemberClientComposite;

public String getAuthCodeRequestUrl(OAuthProvider oAuthProvider) {
return authCodeRequestUrlProviderComposite.provide(oAuthProvider);
}

public OAuthInfoResponse getOAuthInfo(OAuthProvider oAuthProvider, String authCode) {
return oauthMemberClientComposite.fetch(oAuthProvider,
authCode);
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package coffeemeet.server.auth.utils.converter;
package coffeemeet.server.oauth.utils.converter;

import coffeemeet.server.user.domain.OAuthProvider;
import jakarta.validation.constraints.NotNull;
Expand Down
Loading