diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/CustomOAuth2UserService.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/CustomOAuth2UserService.java index 61f802a6..24401eb6 100644 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/CustomOAuth2UserService.java +++ b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/CustomOAuth2UserService.java @@ -2,7 +2,6 @@ import com.inhabas.api.auth.domain.exception.InvalidUserInfoException; import com.inhabas.api.auth.domain.oauth2.member.domain.entity.Member; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccountService; import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID; import com.inhabas.api.auth.domain.oauth2.userAuthorityProvider.UserAuthorityProvider; import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo; @@ -25,7 +24,6 @@ @RequiredArgsConstructor public class CustomOAuth2UserService extends DefaultOAuth2UserService { - private final SocialAccountService socialAccountService; private final UserAuthorityProvider userAuthorityProvider; private final MemberService memberService; private final MemberRepository memberRepository; diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccount.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccount.java deleted file mode 100644 index 5e0e71fc..00000000 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccount.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialAccount; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.inhabas.api.auth.domain.oauth2.OAuth2Provider; -import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID; -import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.NoArgsConstructor; - -import javax.persistence.*; -import java.time.LocalDateTime; - -@Entity @Getter -@Table(name = "SOCIALACCOUNT", - uniqueConstraints = { @UniqueConstraint(name = "unique_socialaccount", columnNames = {"PROVIDER", "UID"})}) -@NoArgsConstructor(access = AccessLevel.PROTECTED) -public class SocialAccount { - - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Integer id; - - @Enumerated(value = EnumType.STRING) - private OAuth2Provider provider; - - @Embedded - private UID uid; - - private LocalDateTime lastLogin; - - // 최초 로그인 날짜 - private LocalDateTime dateJoined; - - @Lob - private String extraData; - - @Column(length = 1000) - private String profileImageUrl; - - public SocialAccount(OAuth2Provider provider, String uid, LocalDateTime lastLogin, LocalDateTime dateJoined, String extraData) { - this.provider = provider; - this.uid = new UID(uid); - this.lastLogin = lastLogin; - this.dateJoined = dateJoined; - this.extraData = extraData; - } - - public SocialAccount(OAuth2UserInfo userInfo) { - this.provider = userInfo.getProvider(); - this.uid = new UID(userInfo.getId()); - this.lastLogin = LocalDateTime.now(); - this.dateJoined = LocalDateTime.now(); - this.profileImageUrl = userInfo.getImageUrl(); - try { - this.extraData = new ObjectMapper().writeValueAsString(userInfo.getExtraData()); - } catch (JsonProcessingException ignored) {} - } - - - public String getUid() { - return uid.getValue(); - } - - public OAuth2Provider getOAuth2Provider() { - return provider; - } - - public SocialAccount setLastLoginTime(LocalDateTime time) { - this.lastLogin = time; - return this; - } -} diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountRepository.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountRepository.java deleted file mode 100644 index 8d28ac12..00000000 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountRepository.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialAccount; - -import com.inhabas.api.auth.domain.oauth2.OAuth2Provider; -import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID; -import org.springframework.data.jpa.repository.JpaRepository; - -import java.util.Optional; - -public interface SocialAccountRepository extends JpaRepository { - - Optional findByUidAndProvider(UID uid, OAuth2Provider provider); -} diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountService.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountService.java deleted file mode 100644 index 58ad9a26..00000000 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountService.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialAccount; - -import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo; - -public interface SocialAccountService { - - /** - * OAuth2 인증이 정상적으로 완료된 {@code SocialAccount} 의 정보를 db에 저장한다. - * @param oAuth2UserInfo 인증 완료 후 가공된 소셜계정 정보 - */ - void updateSocialAccountInfo(OAuth2UserInfo oAuth2UserInfo); -} diff --git a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountServiceImpl.java b/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountServiceImpl.java deleted file mode 100644 index f4c38117..00000000 --- a/module-auth/src/main/java/com/inhabas/api/auth/domain/oauth2/socialAccount/SocialAccountServiceImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialAccount; - -import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID; -import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo; -import lombok.RequiredArgsConstructor; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.time.LocalDateTime; - -@Service -@RequiredArgsConstructor -public class SocialAccountServiceImpl implements SocialAccountService { - - private final SocialAccountRepository socialAccountRepository; - - @Override - @Transactional - public void updateSocialAccountInfo(OAuth2UserInfo oAuth2UserInfo) { - - SocialAccount socialAccount = socialAccountRepository - .findByUidAndProvider(new UID(oAuth2UserInfo.getId()), oAuth2UserInfo.getProvider()) - .orElse(new SocialAccount(oAuth2UserInfo)) - .setLastLoginTime(LocalDateTime.now()); - - socialAccountRepository.save(socialAccount); - } -} diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountRepositoryTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountRepositoryTest.java deleted file mode 100644 index 7c8cb790..00000000 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountRepositoryTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialaccount; - -import com.inhabas.api.auth.domain.oauth2.OAuth2Provider; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccount; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccountRepository; -import com.inhabas.api.auth.domain.oauth2.socialAccount.type.UID; -import com.inhabas.api.auth.testAnnotation.DefaultDataJpaTest; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.dao.DataIntegrityViolationException; - -import javax.persistence.EntityNotFoundException; -import java.time.LocalDateTime; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertThrows; - - -@DefaultDataJpaTest -public class SocialAccountRepositoryTest { - - @Autowired - private SocialAccountRepository socialAccountRepository; - - @Test - @DisplayName("소셜 계정을 uid 와 provider 로 조회한다.") - public void findBySocialAccountByUidAndProvider() { - //given - SocialAccount socialAccount = - new SocialAccount(OAuth2Provider.GOOGLE, "1234", LocalDateTime.now(), LocalDateTime.now(), ""); - socialAccountRepository.save(socialAccount); - - //when - SocialAccount find = socialAccountRepository.findByUidAndProvider(new UID("1234"), OAuth2Provider.GOOGLE) - .orElseThrow(EntityNotFoundException::new); - - //then - assertThat(find.getUid()).isEqualTo("1234"); - assertThat(find.getProvider()).isEqualTo(OAuth2Provider.GOOGLE); - } - - @Test - @DisplayName("소셜 계정은 uid와 provider 가 unique 한 조합이어야 한다.") - public void failToSaveTheSameSocialAccount() { - //given - SocialAccount socialAccount = - new SocialAccount(OAuth2Provider.GOOGLE, "1234", LocalDateTime.now(), LocalDateTime.now(), ""); - socialAccountRepository.save(socialAccount); - - //when - assertThrows(DataIntegrityViolationException.class, - () -> socialAccountRepository.save(new SocialAccount(OAuth2Provider.GOOGLE, "1234", LocalDateTime.now(), LocalDateTime.now(), ""))); - } -} diff --git a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountServiceTest.java b/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountServiceTest.java deleted file mode 100644 index fefab2dd..00000000 --- a/module-auth/src/test/java/com/inhabas/api/auth/domain/oauth2/socialaccount/SocialAccountServiceTest.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.inhabas.api.auth.domain.oauth2.socialaccount; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.inhabas.api.auth.domain.oauth2.OAuth2Provider; -import com.inhabas.api.auth.domain.oauth2.userInfo.GoogleOAuth2UserInfo; -import com.inhabas.api.auth.domain.oauth2.userInfo.OAuth2UserInfo; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccount; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccountRepository; -import com.inhabas.api.auth.domain.oauth2.socialAccount.SocialAccountServiceImpl; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; - -import java.time.LocalDateTime; -import java.util.Map; -import java.util.Optional; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.BDDMockito.given; -import static org.mockito.BDDMockito.then; -import static org.mockito.Mockito.times; - -@ExtendWith(MockitoExtension.class) -public class SocialAccountServiceTest { - - @InjectMocks - private SocialAccountServiceImpl socialAccountService; - - @Mock - private SocialAccountRepository socialAccountRepository; - - private final ObjectMapper objectMapper = new ObjectMapper(); - - - @DisplayName("처음 소셜로그인 시도할 때, 새로운 소셜계정 객체를 생성한다.") - @Test - @SuppressWarnings({"unchecked"}) - public void saveSocialAccountInfo() throws JsonProcessingException { - //given - Map attributes = objectMapper.readValue("{\n" + - " \"sub\" : \"1234567889\",\n" + - " \"name\" : \"유동현\",\n" + - " \"given_name\" : \"동현\",\n" + - " \"family_name\" : \"유\",\n" + - " \"picture\" : \"https://lh3.googleusercontent.com/a/AATXAJzeE07A14_4sjavMyRvRpuG7gcVa5O8imNA37pe=s96-c\",\n" + - " \"email\" : \"my@gmail.com\",\n" + - " \"email_verified\" : true,\n" + - " \"locale\" : \"ko\"\n" + - "}", Map.class); - OAuth2UserInfo userInfo = new GoogleOAuth2UserInfo(attributes); - - given(socialAccountRepository.findByUidAndProvider(any(), any())) - .willReturn(Optional.empty()); // 소셜로그인이 처음이다. - - //when - socialAccountService.updateSocialAccountInfo(userInfo); - - //then - then(socialAccountRepository).should(times(1)).findByUidAndProvider(any(), any()); - then(socialAccountRepository).should(times(1)).save(any()); - } - - @DisplayName("소셜로그인 시도가 첫번째가 아니면, 마지막 로그인 시간만 수정한다.") - @Test - @SuppressWarnings({"unchecked"}) - public void updateSocialAccountInfo() throws JsonProcessingException { - //given - String extraData = "{\n" + - " \"sub\" : \"1234567889\",\n" + - " \"name\" : \"유동현\",\n" + - " \"given_name\" : \"동현\",\n" + - " \"family_name\" : \"유\",\n" + - " \"picture\" : \"https://lh3.googleusercontent.com/a/AATXAJzeE07A14_4sjavMyRvRpuG7gcVa5O8imNA37pe=s96-c\",\n" + - " \"email\" : \"my@gmail.com\",\n" + - " \"email_verified\" : true,\n" + - " \"locale\" : \"ko\"\n" + - "}"; - Map attributes = objectMapper.readValue(extraData, Map.class); - OAuth2UserInfo userInfo = new GoogleOAuth2UserInfo(attributes); - - SocialAccount existAccount = new SocialAccount(OAuth2Provider.GOOGLE, userInfo.getId(), LocalDateTime.of(2020, 1, 1, 12, 0, 0), - LocalDateTime.of(2020, 1, 1, 13, 0, 0), extraData); - given(socialAccountRepository.findByUidAndProvider(any(), any())) - .willReturn(Optional.of(existAccount)); // 소셜로그인을 전에 한 적이 있다. - - //when - socialAccountService.updateSocialAccountInfo(userInfo); - - //then - then(socialAccountRepository).should(times(1)).findByUidAndProvider(any(), any()); - then(socialAccountRepository).should(times(1)).save(any()); - } -}