Files
hlaeja-management/src/main/kotlin/ltd/hlaeja/util/WebClientCalls.kt
Swordsteel 9f6d7066b7 Add remote authentication for users
- add RemoteReactiveAuthenticationManager
- add RemoteAuthentication
- add RemoteUserDetail
- add AccountRegistryService
- add WebClientCalls.kt with accountRegistryAuthenticate
- add Helper.kt with logCall
- add Mapping.kt toAuthenticationRequest
2025-01-17 14:08:13 +01:00

25 lines
1.2 KiB
Kotlin

package ltd.hlaeja.util
import ltd.hlaeja.library.accountRegistry.Authentication
import ltd.hlaeja.property.AccountRegistryProperty
import org.springframework.http.HttpStatus.LOCKED
import org.springframework.http.HttpStatus.NOT_FOUND
import org.springframework.http.HttpStatus.UNAUTHORIZED
import org.springframework.security.authentication.BadCredentialsException
import org.springframework.security.authentication.LockedException
import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.web.reactive.function.client.WebClient
import reactor.core.publisher.Mono
fun WebClient.accountRegistryAuthenticate(
request: Authentication.Request,
property: AccountRegistryProperty,
): Mono<Authentication.Response> = post()
.uri("${property.url}/authenticate".also(::logCall))
.bodyValue(request)
.retrieve()
.onStatus(LOCKED::equals) { throw LockedException("Account is locked") }
.onStatus(UNAUTHORIZED::equals) { throw BadCredentialsException("Invalid credentials") }
.onStatus(NOT_FOUND::equals) { throw UsernameNotFoundException("User not found") }
.bodyToMono(Authentication.Response::class.java)