|
| 1 | +package entry.dsm.gitauth.equusgithubauth.global.oauth.service |
| 2 | + |
| 3 | +import entry.dsm.gitauth.equusgithubauth.domain.user.entity.User |
| 4 | +import entry.dsm.gitauth.equusgithubauth.domain.user.entity.enums.UserRole |
| 5 | +import entry.dsm.gitauth.equusgithubauth.domain.user.entity.repository.UserRepository |
| 6 | +import entry.dsm.gitauth.equusgithubauth.domain.user.presentation.dto.response.TokenResponse |
| 7 | +import entry.dsm.gitauth.equusgithubauth.global.oauth.OAuth2UserInfo |
| 8 | +import entry.dsm.gitauth.equusgithubauth.global.security.auth.CustomOauth2UserDetails |
| 9 | +import entry.dsm.gitauth.equusgithubauth.global.security.auth.GoogleUserDetails |
| 10 | +import entry.dsm.gitauth.equusgithubauth.global.security.jwt.JwtTokenProvider |
| 11 | +import org.springframework.security.oauth2.client.userinfo.DefaultOAuth2UserService |
| 12 | +import org.springframework.security.oauth2.client.userinfo.OAuth2UserRequest |
| 13 | +import org.springframework.security.oauth2.core.OAuth2AuthenticationException |
| 14 | +import org.springframework.security.oauth2.core.user.OAuth2User |
| 15 | +import org.springframework.stereotype.Service |
| 16 | + |
| 17 | +@Service |
| 18 | +class CustomOauth2UserService( |
| 19 | + private val userRepository: UserRepository, |
| 20 | + private val jwtTokenProvider: JwtTokenProvider |
| 21 | +) : DefaultOAuth2UserService() { |
| 22 | + |
| 23 | + override fun loadUser(userRequest: OAuth2UserRequest): OAuth2User { |
| 24 | + val oAuth2User = super.loadUser(userRequest) |
| 25 | + val provider = userRequest.clientRegistration.registrationId |
| 26 | + |
| 27 | + |
| 28 | + val oAuth2UserInfo: OAuth2UserInfo = when(provider) { |
| 29 | + "google" -> GoogleUserDetails(oAuth2User.attributes) |
| 30 | + else -> throw OAuth2AuthenticationException("지원하지 않는 provider: $provider") |
| 31 | + } |
| 32 | + |
| 33 | + val providerId = oAuth2UserInfo.getProviderId() |
| 34 | + val loginId = "$provider$providerId" |
| 35 | + val email = oAuth2UserInfo.getEmail() |
| 36 | + val name = oAuth2UserInfo.getName() |
| 37 | + |
| 38 | + |
| 39 | + val user = userRepository.findByLoginId(loginId) ?: run { |
| 40 | + User( |
| 41 | + loginId = loginId, |
| 42 | + email = email, |
| 43 | + password = "", // OAuth는 비밀번호가 필요 없음 |
| 44 | + name = name, |
| 45 | + provider = provider, |
| 46 | + providerId = providerId, |
| 47 | + role = UserRole.USER |
| 48 | + ).also { userRepository.save(it) } |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + val tokenResponse: TokenResponse = jwtTokenProvider.generateToken(loginId) |
| 53 | + |
| 54 | + // OAuth2User의 attributes에 토큰 정보를 추가하여 클라이언트 전달에 사용 |
| 55 | + val updatedAttributes = oAuth2User.attributes.toMutableMap().apply { |
| 56 | + put("accessToken", tokenResponse.accessToken) |
| 57 | + put("accessTokenExpiration", tokenResponse.accessTokenExpiration.toString()) |
| 58 | + put("refreshToken", tokenResponse.refreshToken) |
| 59 | + put("refreshTokenExpiration", tokenResponse.refreshTokenExpiration.toString()) |
| 60 | + } |
| 61 | + |
| 62 | + return CustomOauth2UserDetails(user, updatedAttributes) |
| 63 | + } |
| 64 | +} |
0 commit comments