Skip to content

Commit

Permalink
Fixed user renamed (#2351)
Browse files Browse the repository at this point in the history
* Fixed user renamed

Co-authored-by: Andrey Kuleshov <[email protected]>
  • Loading branch information
Cheshiriks and orchestr7 authored Jul 19, 2023
1 parent 72453c0 commit 480574c
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -33,7 +34,8 @@ class ConvertingAuthenticationManager(
*/
override fun authenticate(authentication: Authentication): Mono<Authentication> = if (authentication is UsernamePasswordAuthenticationToken) {
val (name, identitySource) = authentication.extractUserNameAndIdentitySource()
authenticationUserDetailsService.findByUsername(name)
val nameAndSource = "$name$AUTH_SEPARATOR$identitySource"
authenticationUserDetailsService.findByUsername(nameAndSource)
.cast<IdentitySourceAwareUserDetails>()
.filter {
it.identitySource == identitySource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<UserDetails> = {
authenticationUserRepository.findByName(username)
}.toMono().getIdentitySourceAwareUserDetails(username)
override fun findByUsername(userNameAndSource: String): Mono<UserDetails> {
val (name, source) = userNameAndSource.split(AUTH_SEPARATOR)
return {
authenticationUserRepository.findByNameAndSource(name, source)
}.toMono().getIdentitySourceAwareUserDetails(name)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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@"

0 comments on commit 480574c

Please sign in to comment.