Skip to content

Commit

Permalink
feat kookmin-sw#2 - 네이버, 카카오 로그인을 모두 지원하도록 Response 작성
Browse files Browse the repository at this point in the history
- OAuth2Response
oauth2의 카카오와 네이버 response를 추상화하기 위해 OAuth2Response 추상 클래스 구현
팩토리 메서드 of에 분기 처리 로직 구현
id, email, nickname을 가져올 수 있도록 추상 메서드 구현

- KakaoOAuth2Response
record -> class 변경 (상속 기능을 사용하기 위함)
  • Loading branch information
leejh7 committed Mar 10, 2024
1 parent 450209b commit 32e44e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package org.capstone.maru.repository;

import java.util.Optional;
import org.capstone.maru.domain.MemberAccount;
import org.capstone.maru.dto.SocialType;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MemberAccountRepository extends JpaRepository<MemberAccount, String> {

Optional<MemberAccount> findBySocialTypeAndSocialId(SocialType socialType, String id);
}
38 changes: 29 additions & 9 deletions src/main/java/org/capstone/maru/security/KakaoOAuth2Response.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package org.capstone.maru.dto.security;
package org.capstone.maru.security;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import lombok.Getter;

/**
* https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api#req-user-info 카카오로부터 사용자 정보를 불러올
* 때 카카오에서 응답 메시지 형식을 맞춘 것이다. 자세한 내용은 위 링크를 참조
*/
@SuppressWarnings("unchecked")
public record KakaoOAuth2Response(
Long id,
LocalDateTime connectedAt,
Map<String, Object> properties,
KakaoAccount kakaoAccount
) {
@Getter
public class KakaoOAuth2Response extends OAuth2Response {

private final Long id;
private final LocalDateTime connectedAt;
private final Map<String, Object> properties;
private final KakaoAccount kakaoAccount;

public record KakaoAccount(
Boolean profileNicknameNeedsAgreement,
Expand Down Expand Up @@ -51,6 +53,14 @@ public String nickname() {
}
}

private KakaoOAuth2Response(Long id, LocalDateTime connectedAt, Map<String, Object> properties,
KakaoAccount kakaoAccount) {
this.id = id;
this.connectedAt = connectedAt;
this.properties = properties;
this.kakaoAccount = kakaoAccount;
}

public static KakaoOAuth2Response from(Map<String, Object> attributes) {
return new KakaoOAuth2Response(
Long.valueOf(String.valueOf(attributes.get("id"))),
Expand All @@ -63,11 +73,21 @@ public static KakaoOAuth2Response from(Map<String, Object> attributes) {
);
}

// -- OAuth2Response abstract method 구현 -- //


@Override
public String id() {
return String.valueOf(this.id);
}

@Override
public String email() {
return this.kakaoAccount().email();
return this.getKakaoAccount().email();
}

@Override
public String nickname() {
return this.kakaoAccount().nickname();
return this.getKakaoAccount().nickname();
}
}
24 changes: 13 additions & 11 deletions src/main/java/org/capstone/maru/security/OAuth2Response.java
Original file line number Diff line number Diff line change
@@ -1,42 +1,44 @@
package org.capstone.maru.security;

import java.util.Map;
import lombok.Builder;
import lombok.Getter;
import org.capstone.maru.domain.MemberAccount;
import org.capstone.maru.security.constant.SocialType;
import org.springframework.security.oauth2.core.OAuth2AuthenticationException;

@Getter
public abstract class OAuthAttributes {
public abstract class OAuth2Response {

/**
* SocialType에 맞는 메소드 호출하여 OAuthAttributes 객체 반환 파라미터 : userNameAttributeName -> OAuth2 로그인 시
* 키(PK)가 되는 값 / attributes : OAuth 서비스의 유저 정보들 소셜별 of 메소드(ofGoogle, ofKaKao, ofNaver)들은 각각 소셜
* 로그인 API에서 제공하는 회원의 식별값(id), attributes, nameAttributeKey를 저장 후 build
*/
public static OAuthAttributes of(
public static OAuth2Response of(
SocialType socialType,
Map<String, Object> attributes
) {
) throws OAuth2AuthenticationException {
switch (socialType) {
case KAKAO -> {
ofKakao(attributes);
return ofKakao(attributes);
}
case NAVER -> {
ofNaver(attributes);
return ofNaver(attributes);
}
default -> throw new IllegalStateException("Unexpected value: " + socialType);
default ->
throw new OAuth2AuthenticationException("Unexpected socialType: " + socialType);
}
}

private static OAuthAttributes ofKakao(Map<String, Object> attributes) {
return null;
private static OAuth2Response ofKakao(Map<String, Object> attributes) {
return KakaoOAuth2Response.from(attributes);
}

public static OAuthAttributes ofNaver(Map<String, Object> attributes) {
public static OAuth2Response ofNaver(Map<String, Object> attributes) {
return null;
}

public abstract String id();

public abstract String email();

public abstract String nickname();
Expand Down

0 comments on commit 32e44e6

Please sign in to comment.