Skip to content

Commit

Permalink
Merge pull request #15 from DoTheZ-Team/GLUE-100-add-kakao-login-rest
Browse files Browse the repository at this point in the history
GLUE-100 Feat: 이전 버전 카카오 로그인 api 복구
  • Loading branch information
sungsil0624 authored May 14, 2024
2 parents 293d970 + 6628048 commit 7868fd9
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 3 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ dependencies {

// swagger
implementation group: 'org.springdoc', name: 'springdoc-openapi-starter-webmvc-ui', version: '2.2.0'

// webclient
implementation 'org.springframework.boot:spring-boot-starter-webflux'
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.justdo.plug.auth.domain.member.dto.request.MemberInfoRequest;
import com.justdo.plug.auth.domain.member.dto.response.MemberInfoResponse;
import com.justdo.plug.auth.domain.member.service.MemberService;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.JwtTokenResponse;
import com.justdo.plug.auth.global.response.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand All @@ -23,6 +24,12 @@ public class AuthController {

private final MemberService memberService;

@GetMapping("/login")
@Operation(summary = "카카오 로그인", description = "전달받은 인가코드를 통해 카카오 로그인을 수행한다.")
public ApiResponse<JwtTokenResponse> kakaoLogin(@RequestParam String code) {
return ApiResponse.onSuccess(memberService.processKakaoLogin(code));
}

@GetMapping
@Operation(summary = "로그인 한 유저 정보 조회", description = "현재 로그인한 유저의 정보를 조회한다.")
public MemberInfoResponse getMyInfo(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,73 @@
import com.justdo.plug.auth.domain.member.repository.MemberRepository;
import com.justdo.plug.auth.global.exception.ApiException;
import com.justdo.plug.auth.global.jwt.JwtTokenProvider;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.KakaoTokenJsonData;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.KakaoUserInfoJsonData;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.JwtTokenResponse;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.KakaoAccount;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.KakaoProfile;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response.KakaoTokenResponse;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response.KakaoUserInfoResponse;
import com.justdo.plug.auth.global.response.code.status.ErrorStatus;
import java.util.List;
import com.justdo.plug.auth.global.utils.redis.RedisUtils;
import io.lettuce.core.RedisConnectionException;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.justdo.plug.auth.global.jwt.JwtTokenProvider.REFRESH_TOKEN_EXPIRATION_TIME;

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

private final MemberRepository memberRepository;
private final JwtTokenProvider jwtTokenProvider;
private final RedisUtils redisUtils;
private final KakaoTokenJsonData kakaoTokenJsonData;
private final KakaoUserInfoJsonData kakaoUserInfoJsonData;


// 카카오 로그인 처리
@Transactional
public JwtTokenResponse processKakaoLogin(String code) {
KakaoTokenResponse tokenResponse = kakaoTokenJsonData.getToken(code);
KakaoUserInfoResponse userInfo = kakaoUserInfoJsonData.getUserInfo(tokenResponse.getAccess_token());
registerMemberIfNew(userInfo);

String accessToken = jwtTokenProvider.generateAccessToken(userInfo.getId());
String refreshToken = jwtTokenProvider.generateRefreshToken();

try {
redisUtils.setData(userInfo.getId().toString(),refreshToken,REFRESH_TOKEN_EXPIRATION_TIME);
} catch (RedisConnectionException | DataAccessException e) {
throw new ApiException(ErrorStatus._REDIS_OPERATION_ERROR);
}

return JwtTokenResponse.createJwtTokenResponse(accessToken, refreshToken);
}

private void registerMemberIfNew(KakaoUserInfoResponse userInfo) {
if (!memberRepository.existsByProviderId(userInfo.getId())) {
Member newMember = createNewMember(userInfo);
memberRepository.save(newMember);
}
}

private Member createNewMember(KakaoUserInfoResponse userInfo) {
KakaoAccount kakaoAccount = userInfo.getKakao_account();
KakaoProfile kakaoProfile = kakaoAccount.getProfile();
return Member.builder()
.providerId(userInfo.getId())
.email(kakaoAccount.getEmail())
.nickname(kakaoProfile.getNickname())
.profile_url(kakaoProfile.getProfile_image_url())
.build();
}


// 멤버 정보 조회
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class JwtTokenProvider {

private final RedisUtils redisUtils;

public static final long ACCESS_TOKEN_EXPIRATION_TIME = 15 * 60 * 1000; // 15분
public static final long REFRESH_TOKEN_EXPIRATION_TIME = 7 * 24 * 60 * 60 * 1000; // 7일
public static final long ACCESS_TOKEN_EXPIRATION_TIME = 365L * 24 * 60 * 60 * 1000; // 1년
public static final long REFRESH_TOKEN_EXPIRATION_TIME = 365L * 24 * 60 * 60 * 1000; // 1년

private SecretKey getSecretKey() {
String keyBase64Encoded = Base64.getEncoder().encodeToString(secretKey.getBytes());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion;

import com.justdo.plug.auth.global.exception.ApiException;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response.KakaoTokenResponse;
import com.justdo.plug.auth.global.response.code.status.ErrorStatus;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;

@Component
@RequiredArgsConstructor
@Slf4j
public class KakaoTokenJsonData {
private final WebClient webClient = WebClient.create();

@Value("${kakao.token_uri}")
private String tokenUri;

@Value("${kakao.redirect_uri}")
private String redirectUri;

@Value("${kakao.grant_type}")
private String grantType;

@Value("${kakao.client_id}")
private String clientId;

public KakaoTokenResponse getToken(String code) {
String uri = tokenUri + "?grant_type=" + grantType + "&client_id="
+ clientId + "&redirect_uri=" + redirectUri + "&code=" + code;

log.info("uri = {}", uri);

Flux<KakaoTokenResponse> response = webClient.post()
.uri(uri)
.contentType(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToFlux(KakaoTokenResponse.class);

return response.onErrorMap(WebClientResponseException.class,
e -> new ApiException(ErrorStatus._KAKAO_TOKEN_ERROR)).blockFirst();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion;

import com.justdo.plug.auth.global.exception.ApiException;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response.KakaoUserInfoResponse;
import com.justdo.plug.auth.global.response.code.status.ErrorStatus;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClientResponseException;
import reactor.core.publisher.Flux;

@RequiredArgsConstructor
@Component
public class KakaoUserInfoJsonData {
private final WebClient webClient = WebClient.create();

@Value("${kakao.user_info_uri}")
private String userInfoUri;

public KakaoUserInfoResponse getUserInfo(String token) {

Flux<KakaoUserInfoResponse> response = webClient.get()
.uri(userInfoUri)
.header("Authorization", "Bearer " + token)
.retrieve()
.bodyToFlux(KakaoUserInfoResponse.class);

return response.onErrorMap(WebClientResponseException.class, ex -> {
return new ApiException(ErrorStatus._KAKAO_USER_INFO_ERROR);})
.blockFirst();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto;

import lombok.Builder;
import lombok.Getter;

@Getter
public class JwtTokenResponse {

private final String accessToken;
private final String refreshToken;

@Builder
public JwtTokenResponse(String accessToken, String refreshToken) {
this.accessToken = accessToken;
this.refreshToken = refreshToken;
}

public static JwtTokenResponse createJwtTokenResponse(String accessToken, String refreshToken) {
return JwtTokenResponse.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto;

import lombok.Getter;

@Getter
public class KakaoAccount {
private boolean profile_nickname_needs_agreement;
private boolean profile_image_needs_agreement;
private KakaoProfile profile;
private boolean has_email;
private boolean email_needs_agreement;
private boolean is_email_valid;
private boolean is_email_verified;
private String email;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto;

import lombok.Getter;

@Getter
public class KakaoProfile {
private String nickname;
private String thumbnail_image_url;
private String profile_image_url;
private boolean is_default_image;
private boolean is_default_nickname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto;

import lombok.Getter;

@Getter
public class KakaoProperties {
private String nickname;
private String profile_image;
private String thumbnail_image;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response;

import lombok.Getter;

@Getter
public class KakaoTokenResponse {
private String access_token;
private String token_type;
private String refresh_token;
private Integer expires_in;
private String scope;
private Integer refresh_token_expires_in;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.response;


import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.KakaoAccount;
import com.justdo.plug.auth.global.jwt.kakao.preVersion.dto.KakaoProperties;
import lombok.Getter;
@Getter
public class KakaoUserInfoResponse {
private Long id;
private String connectedAt;
private KakaoProperties properties;
private KakaoAccount kakao_account;
}

0 comments on commit 7868fd9

Please sign in to comment.