Skip to content

Commit

Permalink
moved /new to BackendService
Browse files Browse the repository at this point in the history
  • Loading branch information
nulls committed Jul 20, 2023
1 parent 0264903 commit 1853285
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.saveourtool.save.gateway.service

import com.saveourtool.save.authservice.utils.IdentitySourceAwareUserDetails
import com.saveourtool.save.entities.User
import com.saveourtool.save.gateway.config.ConfigurationProperties
import com.saveourtool.save.utils.IdentitySourceAwareUserDetailsMixin

import com.fasterxml.jackson.databind.ObjectMapper
import org.springframework.http.MediaType
import org.springframework.http.codec.json.Jackson2JsonEncoder
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.jackson2.CoreJackson2Module
Expand Down Expand Up @@ -64,4 +66,21 @@ class BackendService(
.map {
objectMapper.readValue(it.body, UserDetails::class.java)
}

/**
* Saves a new [User] in DB
*
* @param user
* @return empty [Mono]
*/
fun createNew(user: User): Mono<Void> = webClient.post()
.uri("/internal/users/new")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(user)
.retrieve()
.onStatus({ it.is4xxClientError }) {
Mono.error(ResponseStatusException(it.statusCode()))
}
.toBodilessEntity()
.then()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@ package com.saveourtool.save.gateway.utils

import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.User
import com.saveourtool.save.gateway.config.ConfigurationProperties
import com.saveourtool.save.gateway.service.BackendService
import com.saveourtool.save.info.UserStatus

import org.slf4j.LoggerFactory
import org.springframework.http.MediaType
import org.springframework.security.core.Authentication
import org.springframework.security.web.server.WebFilterExchange
import org.springframework.security.web.server.authentication.ServerAuthenticationSuccessHandler
import org.springframework.web.reactive.function.client.WebClient
import org.springframework.web.server.ResponseStatusException
import reactor.core.publisher.Mono

/**
* [ServerAuthenticationSuccessHandler] that sends user data to backend on successful login
*/
class StoringServerAuthenticationSuccessHandler(
configurationProperties: ConfigurationProperties,
private val backendService: BackendService,
) : ServerAuthenticationSuccessHandler {
private val logger = LoggerFactory.getLogger(javaClass)
private val webClient = WebClient.create(configurationProperties.backend.url)

override fun onAuthenticationSuccess(
webFilterExchange: WebFilterExchange,
Expand All @@ -37,16 +33,7 @@ class StoringServerAuthenticationSuccessHandler(
role = Role.VIEWER.asSpringSecurityRole()
}

return webClient.post()
.uri("/internal/users/new")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(user)
.retrieve()
.onStatus({ it.is4xxClientError }) {
Mono.error(ResponseStatusException(it.statusCode()))
}
.toBodilessEntity()
.then()
return backendService.createNew(user)
}
}

Expand Down

0 comments on commit 1853285

Please sign in to comment.