Skip to content

Commit

Permalink
Merge pull request #29 from WE-ARE-RACCOONS/RAC-110
Browse files Browse the repository at this point in the history
RAC-110 refactor : 카카오 로그인 code로 변경
  • Loading branch information
ywj9811 authored Nov 7, 2023
2 parents c133349 + 5f70d9f commit bce1469
Show file tree
Hide file tree
Showing 32 changed files with 406 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.postgraduate.domain.auth.application.dto.req;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class KakaoLoginRequest {
@Getter
public class KakaoCodeRequest {
@NotNull
private String accessToken;
private String code;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.postgraduate.domain.auth.application.dto.res;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class KakaoTokenInfoResponse {
private String access_token;
private String token_type;
private String refresh_token;
private String id_token;
private int expires_in;
private String cope;
private int refresh_token_expires_in;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.postgraduate.domain.auth.application.usecase.kakao;

import com.postgraduate.domain.auth.application.dto.res.KakaoAccessTokenResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoTokenInfoResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoUserInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;

@RequiredArgsConstructor
Expand All @@ -13,13 +17,38 @@ public class KakaoAccessTokenUseCase {

@Value("${app-id.kakao}")
private String APP_ID;
@Value("${kakao.redirect-uri}")
private String REDRECT_URI;
@Value("${kakao.authorization-grant-type}")
private String AUTHORIZATION_GRANT_TYPE;
private final WebClient webClient;

private static final String KAKAO_TOKEN_URI = "https://kauth.kakao.com/oauth/token";
private static final String USER_INFO_URI = "https://kapi.kakao.com/v2/user/me";
private static final String VALIDATE_TOKEN_URI = "https://kapi.kakao.com/v1/user/access_token_info";

public KakaoUserInfoResponse getKakaoToken(String code) {
MultiValueMap<String, String> requestBody = getRequestBody(code);
KakaoTokenInfoResponse tokenInfoResponse = webClient.post()
.uri(KAKAO_TOKEN_URI)
.headers(h -> h.setContentType(MediaType.APPLICATION_FORM_URLENCODED))
.bodyValue(requestBody)
.retrieve()
.bodyToMono(KakaoTokenInfoResponse.class)
.block();
return getUserInfo(tokenInfoResponse.getAccess_token());
}

public KakaoUserInfoResponse getUserInfo(String accessToken) {
private MultiValueMap<String, String> getRequestBody(String code) {
MultiValueMap<String, String> requestBody = new LinkedMultiValueMap<>();
requestBody.add("grant_type", AUTHORIZATION_GRANT_TYPE);
requestBody.add("client_id", APP_ID);
requestBody.add("redirect_uri", REDRECT_URI);
requestBody.add("code", code);
return requestBody;
}

private KakaoUserInfoResponse getUserInfo(String accessToken) {
verifyAccessToken(accessToken);

return webClient.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.postgraduate.domain.auth.application.usecase.kakao;

import com.postgraduate.domain.auth.application.dto.req.KakaoCodeRequest;
import com.postgraduate.domain.auth.application.dto.res.AuthUserResponse;
import com.postgraduate.domain.auth.application.dto.res.KakaoUserInfoResponse;
import com.postgraduate.domain.auth.application.dto.req.SignUpRequest;
Expand All @@ -22,8 +23,8 @@ public class KakaoSignInUseCase {
private final UserGetService userGetService;

@Transactional
public AuthUserResponse getUser(String token) {
KakaoUserInfoResponse userInfo = kakaoTokenUseCase.getUserInfo(token);
public AuthUserResponse getUser(KakaoCodeRequest codeRequest) {
KakaoUserInfoResponse userInfo = kakaoTokenUseCase.getKakaoToken(codeRequest.getCode());
Long socialId = userInfo.getId();
Optional<User> user = userGetService.bySocialId(socialId);
return AuthMapper.mapToAuthUser(user, socialId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.postgraduate.domain.auth.exception;

import com.postgraduate.global.exception.ApplicationException;

public class AuthException extends ApplicationException {
protected AuthException(String message, String errorCode) {
super(message, errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.postgraduate.domain.auth.exception;

import static com.postgraduate.domain.auth.presentation.contant.AuthResponseCode.AUTH_NONE;
import static com.postgraduate.domain.auth.presentation.contant.AuthResponseMessage.PERMISSION_DENIED_MESSAGE;


public class PermissionDeniedException extends AuthException {

public PermissionDeniedException() {
super(PERMISSION_DENIED_MESSAGE.getMessage(), AUTH_NONE.getCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.postgraduate.domain.auth.presentation;

import com.postgraduate.domain.auth.application.dto.req.KakaoLoginRequest;
import com.postgraduate.domain.auth.application.dto.req.KakaoCodeRequest;
import com.postgraduate.domain.auth.application.dto.req.SignUpRequest;
import com.postgraduate.domain.auth.application.dto.res.AuthUserResponse;
import com.postgraduate.domain.auth.application.dto.res.JwtTokenResponse;
Expand Down Expand Up @@ -32,11 +32,10 @@ public class AuthController {

@PostMapping("/login")
@Operation(summary = "카카오 로그인", description = "회원인 경우 JWT를, 회원이 아닌 경우 socialId를 반환합니다(회원가입은 진행하지 않습니다).")
public ResponseDto<?> authLogin(@RequestBody KakaoLoginRequest request) {
AuthUserResponse authUser = kakaoSignInUseCase.getUser(request.getAccessToken());
public ResponseDto<?> authLogin(@RequestBody KakaoCodeRequest tokenRequest) {
AuthUserResponse authUser = kakaoSignInUseCase.getUser(tokenRequest);
if (authUser.getUser().isEmpty())
return ResponseDto.create(AUTH_NONE.getCode(), NOT_REGISTERED_USER_MESSAGE.getMessage(), authUser);

JwtTokenResponse jwtToken = jwtUseCase.signIn(authUser.getUser().get());
return ResponseDto.create(AUTH_ALREADY.getCode(), SUCCESS_AUTH_MESSAGE.getMessage(), jwtToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
public enum AuthResponseMessage {
SUCCESS_AUTH_MESSAGE("사용자 인증에 성공하였습니다."),
NOT_REGISTERED_USER_MESSAGE("가입하지 않은 유저입니다."),
SUCCESS_REGENERATE_TOKEN_MESSAGE("토큰 재발급에 성공하였습니다.");
SUCCESS_REGENERATE_TOKEN_MESSAGE("토큰 재발급에 성공하였습니다."),

PERMISSION_DENIED_MESSAGE("권한이 없습니다.");
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.postgraduate.domain.mentoring.application.dto.req;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class MentoringApplyRequest {
@NotNull
private Long seniorId;
@NotNull
private String topic;
@NotNull
private String question;
@NotNull
private String date;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.postgraduate.domain.mentoring.application.dto.req;

import com.postgraduate.domain.mentoring.domain.entity.constant.Status;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class MentoringStatusRequest {
@NotNull
private Status status;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.postgraduate.domain.mentoring.application.dto;
package com.postgraduate.domain.mentoring.application.dto.res;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.postgraduate.domain.mentoring.application.dto;
package com.postgraduate.domain.mentoring.application.dto.res;

import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringInfo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.postgraduate.domain.mentoring.application.mapper;

import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringDetailResponse;
import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringInfo;
import com.postgraduate.domain.mentoring.application.dto.req.MentoringApplyRequest;
import com.postgraduate.domain.mentoring.application.dto.res.AppliedMentoringDetailResponse;
import com.postgraduate.domain.mentoring.domain.entity.Mentoring;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;

import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -51,4 +53,14 @@ public static AppliedMentoringDetailResponse mapToAppliedDetailInfo(Mentoring me
.dates(dates)
.build();
}

public static Mentoring mapToMentoring(User user, Senior senior, MentoringApplyRequest request) {
return Mentoring.builder()
.user(user)
.senior(senior)
.topic(request.getTopic())
.question(request.getQuestion())
.date(request.getDate())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.postgraduate.domain.mentoring.application.usecase;

import com.postgraduate.domain.auth.exception.PermissionDeniedException;
import com.postgraduate.domain.mentoring.domain.entity.Mentoring;
import com.postgraduate.domain.mentoring.domain.service.MentoringGetService;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.user.domain.entity.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class CheckIsMyMentoringUseCase {
private final MentoringGetService mentoringGetService;
public Mentoring checkByRole(User user, Long mentoringId) {
Mentoring mentoring = mentoringGetService.byMentoringId(mentoringId);
if (mentoring.getUser() != user) {
throw new PermissionDeniedException();
}
return mentoring;
}

public Mentoring checkByRole(Senior senior, Long mentoringId) {
Mentoring mentoring = mentoringGetService.byMentoringId(mentoringId);
if (mentoring.getSenior() != senior) {
throw new PermissionDeniedException();
}
return mentoring;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.postgraduate.domain.mentoring.application.usecase;

import com.postgraduate.domain.mentoring.application.dto.req.MentoringApplyRequest;
import com.postgraduate.domain.mentoring.application.mapper.MentoringMapper;
import com.postgraduate.domain.mentoring.domain.entity.Mentoring;
import com.postgraduate.domain.mentoring.domain.service.MentoringSaveService;
import com.postgraduate.domain.senior.domain.entity.Senior;
import com.postgraduate.domain.senior.domain.service.SeniorGetService;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.auth.AuthDetails;
import com.postgraduate.global.config.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class MentoringApplyUseCase {
private final SecurityUtils securityUtils;
private final MentoringSaveService mentoringSaveService;
private final SeniorGetService seniorGetService;

public void applyMentoring(AuthDetails authDetails, MentoringApplyRequest request) {
User user = securityUtils.getLoggedInUser(authDetails);
Senior senior = seniorGetService.bySeniorId(request.getSeniorId());
Mentoring mentoring = MentoringMapper.mapToMentoring(user, senior, request);
mentoringSaveService.save(mentoring);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.postgraduate.domain.mentoring.application.usecase;

import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringDetailResponse;
import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringResponse;
import com.postgraduate.domain.mentoring.application.dto.res.AppliedMentoringDetailResponse;
import com.postgraduate.domain.mentoring.application.dto.res.AppliedMentoringResponse;
import com.postgraduate.domain.mentoring.application.dto.AppliedMentoringInfo;
import com.postgraduate.domain.mentoring.application.mapper.MentoringMapper;
import com.postgraduate.domain.mentoring.domain.entity.Mentoring;
Expand All @@ -22,11 +22,11 @@
@RequiredArgsConstructor
public class MentoringInfoUseCase {
private final MentoringGetService mentoringGetService;

private final CheckIsMyMentoringUseCase checkIsMyMentoringUseCase;
private final SecurityUtils securityUtils;
/**
* securityUtils 이후 수정
*/
private final SecurityUtils securityUtils;

public AppliedMentoringResponse getMentorings(Status status, AuthDetails authDetails) {
User user = securityUtils.getLoggedInUser(authDetails);
Expand Down Expand Up @@ -55,9 +55,9 @@ private AppliedMentoringResponse getCategories(Status status, List<Mentoring> me
}
}

public AppliedMentoringDetailResponse getMentoringDetail(Long mentoringId) {
Mentoring mentoring = mentoringGetService.mentoringDetail(mentoringId);
AppliedMentoringDetailResponse appliedMentoringDetailResponse = MentoringMapper.mapToAppliedDetailInfo(mentoring);
return appliedMentoringDetailResponse;
public AppliedMentoringDetailResponse getMentoringDetail(AuthDetails authDetails, Long mentoringId) {
User user = securityUtils.getLoggedInUser(authDetails);
Mentoring mentoring = checkIsMyMentoringUseCase.checkByRole(user, mentoringId);
return MentoringMapper.mapToAppliedDetailInfo(mentoring);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.postgraduate.domain.mentoring.application.usecase;

import com.postgraduate.domain.mentoring.application.dto.req.MentoringStatusRequest;
import com.postgraduate.domain.mentoring.domain.entity.Mentoring;
import com.postgraduate.domain.mentoring.domain.service.MentoringUpdateService;
import com.postgraduate.domain.user.domain.entity.User;
import com.postgraduate.global.auth.AuthDetails;
import com.postgraduate.global.config.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class MentoringManageUseCase {
private final SecurityUtils securityUtils;
private final MentoringUpdateService mentoringUpdateService;
private final CheckIsMyMentoringUseCase checkIsMyMentoringUseCase;

public void updateStatus(AuthDetails authDetails, Long mentoringId, MentoringStatusRequest request) {
User user = securityUtils.getLoggedInUser(authDetails);
Mentoring mentoring = checkIsMyMentoringUseCase.checkByRole(user, mentoringId);
mentoringUpdateService.updateStatus(mentoring, request.getStatus());
}
}
Loading

0 comments on commit bce1469

Please sign in to comment.