diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt index 7f38b32a42..631d4b93d0 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/repository/AuthenticationUserRepository.kt @@ -13,17 +13,22 @@ class AuthenticationUserRepository( private val namedParameterJdbcTemplate: NamedParameterJdbcTemplate, ) { /** - * @param name + * @param name name of user + * @param source source of user * @return user or null if no results have been found */ - fun findByName(name: String): User? { + fun findByNameAndSource(name: String, source: String): User? { val record = namedParameterJdbcTemplate.queryForList( - "SELECT * FROM save_cloud.user WHERE name = :name", - mapOf("name" to name) + "SELECT * FROM save_cloud.user WHERE name = :name AND source = :source", + mapOf("name" to name, "source" to source) ).singleOrNull() - .orNotFound { - "There is no user with name $name" - } + ?: namedParameterJdbcTemplate.queryForList( + "SELECT * FROM save_cloud.user WHERE id = (select user_id from save_cloud.original_login where name = :name AND source = :source)", + mapOf("name" to name, "source" to source) + ).singleOrNull() + .orNotFound { + "There is no user with name $name and source $source" + } return record.toUserEntity() } diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/ConvertingAuthenticationManager.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/ConvertingAuthenticationManager.kt index 5cc040b1ef..6d0fe31254 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/ConvertingAuthenticationManager.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/security/ConvertingAuthenticationManager.kt @@ -4,6 +4,7 @@ import com.saveourtool.save.authservice.service.AuthenticationUserDetailsService import com.saveourtool.save.authservice.utils.AuthenticationDetails import com.saveourtool.save.authservice.utils.IdentitySourceAwareUserDetails import com.saveourtool.save.authservice.utils.extractUserNameAndIdentitySource +import com.saveourtool.save.utils.AUTH_SEPARATOR import org.springframework.beans.factory.annotation.Autowired import org.springframework.security.authentication.BadCredentialsException @@ -33,7 +34,8 @@ class ConvertingAuthenticationManager( */ override fun authenticate(authentication: Authentication): Mono = if (authentication is UsernamePasswordAuthenticationToken) { val (name, identitySource) = authentication.extractUserNameAndIdentitySource() - authenticationUserDetailsService.findByUsername(name) + val nameAndSource = "$name$AUTH_SEPARATOR$identitySource" + authenticationUserDetailsService.findByUsername(nameAndSource) .cast() .filter { it.identitySource == identitySource diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/service/AuthenticationUserDetailsService.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/service/AuthenticationUserDetailsService.kt index c710b6f5af..98f840ff64 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/service/AuthenticationUserDetailsService.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/service/AuthenticationUserDetailsService.kt @@ -2,6 +2,7 @@ package com.saveourtool.save.authservice.service import com.saveourtool.save.authservice.repository.AuthenticationUserRepository import com.saveourtool.save.authservice.utils.getIdentitySourceAwareUserDetails +import com.saveourtool.save.utils.AUTH_SEPARATOR import org.springframework.context.annotation.Primary import org.springframework.security.core.userdetails.ReactiveUserDetailsService import org.springframework.security.core.userdetails.UserDetails @@ -18,10 +19,13 @@ class AuthenticationUserDetailsService( private val authenticationUserRepository: AuthenticationUserRepository, ) : ReactiveUserDetailsService { /** - * @param username + * @param userNameAndSource * @return IdentitySourceAwareUserDetails retrieved from UserDetails */ - override fun findByUsername(username: String): Mono = { - authenticationUserRepository.findByName(username) - }.toMono().getIdentitySourceAwareUserDetails(username) + override fun findByUsername(userNameAndSource: String): Mono { + val (name, source) = userNameAndSource.split(AUTH_SEPARATOR) + return { + authenticationUserRepository.findByNameAndSource(name, source) + }.toMono().getIdentitySourceAwareUserDetails(name) + } } diff --git a/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt b/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt index 0d9112d3a3..f8e1dde3dd 100644 --- a/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt +++ b/save-backend/src/test/kotlin/com/saveourtool/save/backend/security/BasicSecurityTest.kt @@ -43,7 +43,7 @@ class BasicSecurityTest { @BeforeEach fun setUp() { - whenever(authenticationUserRepository.findByName("user")).thenReturn( + whenever(authenticationUserRepository.findByNameAndSource("user", "basic")).thenReturn( User("user", null, "ROLE_USER", "basic").apply { id = 99 } diff --git a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/utils/Constants.kt b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/utils/Constants.kt index 0f4d766227..cf0bfcd3e0 100644 --- a/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/utils/Constants.kt +++ b/save-cloud-common/src/commonMain/kotlin/com/saveourtool/save/utils/Constants.kt @@ -64,3 +64,8 @@ const val AUTHORIZATION_SOURCE = "X-Authorization-Source" */ @Suppress("NON_EXPORTABLE_TYPE") const val DEFAULT_SETUP_SH_TIMEOUT_MILLIS: Long = 60_000L + +/** + * Separator for name and source of user + */ +const val AUTH_SEPARATOR = "@SAVE@"