fix: address PR review comments for OAuth2 implementation #32
+1
-2
@@ -46,8 +46,7 @@ dependencies {
|
||||
|
||||
implementation(libs.spring.grpc.starter)
|
||||
implementation(libs.grpc.services)
|
||||
|
||||
implementation("org.springframework.boot:spring-boot-starter-oauth2-client:3.4.3")
|
||||
implementation(libs.spring.boot.starter.oauth2.client)
|
||||
|
||||
runtimeOnly(libs.micrometer.registry.prometheus)
|
||||
runtimeOnly(libs.h2)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.project.movienight.adapters.security
|
||||
|
||||
import com.project.movienight.application.ports.input.security.OAuth2UserInfo
|
||||
import com.project.movienight.application.ports.output.IdGenerator
|
||||
import com.project.movienight.application.ports.output.UserRepositoryPort
|
||||
import com.project.movienight.domain.model.User
|
||||
@@ -37,7 +38,6 @@ class CustomOAuth2UserService(
|
||||
}
|
||||
|
||||
private fun findOrCreateUser(userInfo: OAuth2UserInfo): User {
|
||||
// Сначала ищем по provider + provider_id (основной способ для OAuth2)
|
||||
val existingUser = userRepository.findByProviderAndProviderId(
|
||||
userInfo.getProvider(),
|
||||
userInfo.getProviderId()
|
||||
@@ -47,11 +47,9 @@ class CustomOAuth2UserService(
|
||||
log.debug("User found by provider: {}", userInfo.getProvider())
|
||||
existingUser
|
||||
} else {
|
||||
// Проверяем нет ли пользователя с таким email (связывание аккаунтов)
|
||||
val userByEmail = userRepository.findByEmail(userInfo.getEmail())
|
||||
|
||||
if (userByEmail != null) {
|
||||
// Пользователь существует, обновляем его OAuth2 данными
|
||||
log.debug("Linking OAuth2 account to existing user: {}", userInfo.getEmail())
|
||||
userRepository.saveWithOAuth2(userByEmail, userInfo.getProvider(), userInfo.getProviderId())
|
||||
} else {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.project.movienight.adapters.security
|
||||
|
||||
import com.project.movienight.application.ports.input.security.OAuth2UserInfo
|
||||
|
||||
class GoogleOAuth2UserInfo(
|
||||
private val attributes: Map<String, Any>
|
||||
) : OAuth2UserInfo {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.project.movienight.adapters.security
|
||||
|
||||
import com.project.movienight.application.ports.input.security.OAuth2UserInfo
|
||||
import org.springframework.security.oauth2.core.OAuth2AuthenticationException
|
||||
import org.springframework.security.oauth2.core.user.OAuth2User
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.springframework.security.core.GrantedAuthority
|
||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||
import org.springframework.security.core.userdetails.UserDetails
|
||||
import org.springframework.security.oauth2.core.user.OAuth2User
|
||||
import java.util.*
|
||||
import java.util.UUID
|
||||
|
||||
class UserPrincipal(
|
||||
private val user: User,
|
||||
|
||||
@@ -1,22 +1,26 @@
|
||||
package com.project.movienight.adapters.security
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
import com.project.movienight.application.ports.input.security.OAuth2UserInfo
|
||||
|
||||
class VkOAuth2UserInfo(
|
||||
private val attributes: Map<String, Any>
|
||||
) : OAuth2UserInfo {
|
||||
|
||||
override fun getProviderId(): String {
|
||||
val response = attributes["response"] as? List<Map<String, Any>>
|
||||
return response?.firstOrNull()?.get("id")?.toString() ?: ""
|
||||
return (attributes["response"] as? List<*>)
|
||||
?.firstOrNull()
|
||||
?.let { it as? Map<*, *> }
|
||||
?.get("id")
|
||||
?.toString() ?: ""
|
||||
}
|
||||
|
||||
override fun getEmail(): String = attributes["email"] as? String ?: ""
|
||||
override fun getEmail(): String = attributes["email"]?.toString() ?: ""
|
||||
|
||||
override fun getName(): String {
|
||||
val response = attributes["response"] as? List<Map<String, Any>>
|
||||
val first = response?.firstOrNull()
|
||||
val firstName = first?.get("first_name") as? String ?: ""
|
||||
val lastName = first?.get("last_name") as? String ?: ""
|
||||
val response = attributes["response"] as? List<*>
|
||||
val first = response?.firstOrNull() as? Map<*, *>
|
||||
val firstName = first?.get("first_name")?.toString() ?: ""
|
||||
val lastName = first?.get("last_name")?.toString() ?: ""
|
||||
return "$firstName $lastName".trim()
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.project.movienight.adapters.security
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
import com.project.movienight.application.ports.input.security.OAuth2UserInfo
|
||||
|
||||
class YandexOAuth2UserInfo(
|
||||
private val attributes: Map<String, Any>
|
||||
) : OAuth2UserInfo {
|
||||
@@ -8,11 +9,14 @@ class YandexOAuth2UserInfo(
|
||||
override fun getProviderId(): String = attributes["id"]?.toString() ?: ""
|
||||
|
||||
override fun getEmail(): String {
|
||||
val emails = attributes["emails"] as? List<Map<String, String>>
|
||||
return emails?.firstOrNull()?.get("value") ?: ""
|
||||
return (attributes["emails"] as? List<*>)
|
||||
?.firstOrNull()
|
||||
?.let { it as? Map<*, *> }
|
||||
?.get("value")
|
||||
?.toString() ?: ""
|
||||
}
|
||||
|
||||
override fun getName(): String = attributes["display_name"] as? String ?: ""
|
||||
override fun getName(): String = attributes["display_name"]?.toString() ?: ""
|
||||
|
||||
override fun getProvider(): String = "yandex"
|
||||
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.project.movienight.adapters.security
|
||||
package com.project.movienight.application.ports.input.security
|
||||
|
||||
interface OAuth2UserInfo {
|
||||
fun getProviderId(): String
|
||||
Reference in New Issue
Block a user