Skip to content

Commit

Permalink
Merge branch 'develop' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
hajungIm committed May 31, 2024
2 parents 42b2351 + ba0e61d commit 934d5a1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.umc5th.muffler.domain.member.dto.RefreshTokenRequest;
import com.umc5th.muffler.domain.member.dto.WithdrawRequest;
import com.umc5th.muffler.domain.member.service.MemberService;
import com.umc5th.muffler.entity.constant.SocialType;
import com.umc5th.muffler.global.response.Response;
import com.umc5th.muffler.global.security.jwt.TokenInfo;
import javax.validation.Valid;
Expand All @@ -17,7 +16,6 @@
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;
import org.springframework.web.servlet.view.RedirectView;

Expand All @@ -34,7 +32,7 @@ public Response<Void> connectTest() {
}

@PostMapping("/login")
public Response<LoginResponse> login(@RequestBody LoginRequest request) {
public Response<LoginResponse> login(@RequestBody @Valid LoginRequest request) {
return Response.success(memberService.login(request));
}

Expand All @@ -44,9 +42,8 @@ public Response<MemberInfo> join(@RequestBody MemberInfo request, Authentication
}

@PostMapping("/leave")
public Response<Void> withdraw(@RequestParam SocialType type, Authentication authentication,
@RequestBody @Valid WithdrawRequest request) {
memberService.withdraw(type, authentication.getName(), request);
public Response<Void> withdraw(@RequestBody @Valid WithdrawRequest request, Authentication authentication) {
memberService.withdraw(request, authentication.getName());
return Response.success();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.umc5th.muffler.domain.member.dto;

import com.umc5th.muffler.entity.constant.SocialType;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -9,6 +12,9 @@
@AllArgsConstructor
@NoArgsConstructor
public class LoginRequest {
@NotNull
@Enumerated(EnumType.STRING)
private SocialType socialType;
private String idToken;
@NotNull
private String token;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.umc5th.muffler.domain.member.dto;

import com.umc5th.muffler.entity.constant.SocialType;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.validation.constraints.NotNull;
import lombok.Getter;

@Getter
public class WithdrawRequest {
@NotNull
@Enumerated(EnumType.STRING)
private SocialType socialType;
@NotNull
private String reason;

// 애플 탈퇴 시 필요
private String authenticationCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static com.umc5th.muffler.global.response.code.ErrorCode.INTERNAL_SERVER_ERROR;

import com.umc5th.muffler.domain.member.dto.AppleIdToken;
import com.umc5th.muffler.domain.member.dto.LoginRequest;
import com.umc5th.muffler.domain.member.dto.AppleToken;
import com.umc5th.muffler.global.feign.AppleClient;
import com.umc5th.muffler.global.response.exception.MemberException;
import com.umc5th.muffler.global.security.jwt.JwtDecoder;
Expand All @@ -27,16 +27,28 @@ public class AppleService {
private final AppleProperties appleProperties;
private final DateTimeProvider dateTimeProvider;

public String login(LoginRequest request) {
String authentication = request.getIdToken();
String idToken = appleClient.getIdToken(
public String login(String authenticationCode) {
String idToken = getAppleToken(authenticationCode).getIdToken();
return JwtDecoder.decodePayload(idToken, AppleIdToken.class).getSub();
}

public void leave(String authenticationCode) {
String accessToken = getAppleToken(authenticationCode).getAccessToken();

appleClient.leave(
appleProperties.getClientId(),
generateClientSecret(),
appleProperties.getGrantType(),
authentication
).getIdToken();
accessToken
);
}

return JwtDecoder.decodePayload(idToken, AppleIdToken.class).getSub();
private AppleToken getAppleToken(String authenticationCode) {
return appleClient.getAuthToken(
appleProperties.getClientId(),
generateClientSecret(),
appleProperties.getGrantType(),
authenticationCode
);
}

private String generateClientSecret() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@RequiredArgsConstructor
public class KakaoService {

private final String ISS = "https://kauth.kakao.com";
private final String KAKAO_LEAVE_EXCEPTION = "NotRegisteredUserException";
private static final String ISS = "https://kauth.kakao.com";
private static final String KAKAO_LEAVE_EXCEPTION = "NotRegisteredUserException";
@Value("${jwt.aud}")
private String AUD;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.umc5th.muffler.entity.constant.SocialType.APPLE;
import static com.umc5th.muffler.entity.constant.SocialType.KAKAO;
import static com.umc5th.muffler.global.response.code.ErrorCode.BAD_REQUEST;
import static com.umc5th.muffler.global.response.code.ErrorCode.INVALID_TOKEN;
import static com.umc5th.muffler.global.response.code.ErrorCode.MEMBER_NOT_FOUND;
import static com.umc5th.muffler.global.response.code.ErrorCode.UNSUPPORTED_SOCIAL_TYPE;
Expand Down Expand Up @@ -37,7 +38,6 @@
@RequiredArgsConstructor
@Transactional
public class MemberService {

private final MemberRepository memberRepository;
private final WithdrawalReasonRepository withdrawalRepository;
private final BatchUpdateCategoryRepository batchUpdateCategoryRepository;
Expand Down Expand Up @@ -81,32 +81,22 @@ public TokenInfo refreshAccessToken(String refreshToken) {
return newToken;
}

public void withdraw(SocialType type, String memberId, WithdrawRequest request) {
public void withdraw(WithdrawRequest request, String memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberException(MEMBER_NOT_FOUND));

withdrawalRepository.save(WithdrawalConverter.toEntity(member, request));
socialWithdraw(type, memberId);
socialWithdraw(request, memberId);
memberRepository.deleteMemberAndRelatedEntities(member.getId());
}

private void socialWithdraw(SocialType type, String memberId) {
if (type == KAKAO) {
kakaoService.leave(memberId);
return;
}
if (type == APPLE) {
return;
}
throw new MemberException(UNSUPPORTED_SOCIAL_TYPE);
}

private String socialLogin(LoginRequest request) {
if (request.getSocialType() == APPLE) {
return appleService.login(request);
SocialType type = request.getSocialType();
if (type == APPLE) {
return appleService.login(request.getToken());
}
if (request.getSocialType() == KAKAO) {
return kakaoService.login(request.getIdToken());
if (type == KAKAO) {
return kakaoService.login(request.getToken());
}
throw new MemberException(UNSUPPORTED_SOCIAL_TYPE);
}
Expand All @@ -128,4 +118,20 @@ private Member registerUserIfNeed(String memberId, SocialType socialType) {

return member;
}

private void socialWithdraw(WithdrawRequest request, String memberId) {
SocialType type = request.getSocialType();
if (type == KAKAO) {
kakaoService.leave(memberId);
return;
}
if (type == APPLE) {
if (request.getAuthenticationCode() == null) {
throw new MemberException(BAD_REQUEST, "애플 AuthenticationCode가 없습니다.");
}
appleService.leave(request.getAuthenticationCode());
return;
}
throw new MemberException(UNSUPPORTED_SOCIAL_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
)
public interface AppleClient {
@PostMapping("/auth/token")
AppleToken getIdToken(
AppleToken getAuthToken(
@RequestParam("client_id") String clientId,
@RequestParam("client_secret") String clientSecret,
@RequestParam("grant_type") String grantType,
@RequestParam("code") String code
);

@PostMapping("/auth/revoke")
void leave(
@RequestParam("client_id") String clientId,
@RequestParam("client_secret") String clientSecret,
@RequestParam("token") String token
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
@RequiredArgsConstructor
public class JwtTokenFilter extends OncePerRequestFilter {

private final String TOKEN_ERROR_MESSAGE = "유효한 인증 토큰이 필요합니다.";
private static final String TOKEN_ERROR_MESSAGE = "유효한 인증 토큰이 필요합니다.";
private final JwtTokenUtils jwtTokenUtils;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package com.umc5th.muffler.global.security.jwt;

import static com.umc5th.muffler.global.response.code.ErrorCode.INVALID_TOKEN;

import com.umc5th.muffler.entity.constant.Role;
import com.umc5th.muffler.global.response.exception.CommonException;
import com.umc5th.muffler.global.util.DateTimeProvider;
import io.jsonwebtoken.*;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;

import java.security.Key;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -16,19 +28,11 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.security.Key;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.umc5th.muffler.global.response.code.ErrorCode.INVALID_TOKEN;

@Slf4j
@Component
public class JwtTokenUtils {
private final int ACCESS_TOKEN_DURATION = 1;
private final int REFRESH_TOKEN_DURATION = 30;
private static final int ACCESS_TOKEN_DURATION = 1;
private static final int REFRESH_TOKEN_DURATION = 30;

private final DateTimeProvider dateTimeProvider;
private final Key key;
Expand Down

0 comments on commit 934d5a1

Please sign in to comment.