update RemoteAuthenticationManager
- authenticate add default event on success and error - processToken nicer catch - makeSimpleGrantedAuthorities to add ROLE_ to role in
This commit is contained in:
@@ -10,9 +10,13 @@ import ltd.hlaeja.security.user.RemoteAuthentication
|
|||||||
import ltd.hlaeja.security.user.RemoteUserDetail
|
import ltd.hlaeja.security.user.RemoteUserDetail
|
||||||
import ltd.hlaeja.service.AccountRegistryService
|
import ltd.hlaeja.service.AccountRegistryService
|
||||||
import ltd.hlaeja.util.toAuthenticationRequest
|
import ltd.hlaeja.util.toAuthenticationRequest
|
||||||
|
import org.springframework.context.ApplicationEventPublisher
|
||||||
import org.springframework.security.authentication.AuthenticationServiceException
|
import org.springframework.security.authentication.AuthenticationServiceException
|
||||||
import org.springframework.security.authentication.ReactiveAuthenticationManager
|
import org.springframework.security.authentication.ReactiveAuthenticationManager
|
||||||
|
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent
|
||||||
|
import org.springframework.security.authentication.event.AuthenticationSuccessEvent
|
||||||
import org.springframework.security.core.Authentication
|
import org.springframework.security.core.Authentication
|
||||||
|
import org.springframework.security.core.AuthenticationException
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
import org.springframework.security.core.authority.SimpleGrantedAuthority
|
||||||
import org.springframework.stereotype.Component
|
import org.springframework.stereotype.Component
|
||||||
import reactor.core.publisher.Mono
|
import reactor.core.publisher.Mono
|
||||||
@@ -23,21 +27,29 @@ private val log = KotlinLogging.logger {}
|
|||||||
class RemoteAuthenticationManager(
|
class RemoteAuthenticationManager(
|
||||||
private val accountRegistryService: AccountRegistryService,
|
private val accountRegistryService: AccountRegistryService,
|
||||||
private val publicJwtService: PublicJwtService,
|
private val publicJwtService: PublicJwtService,
|
||||||
|
private val publisher: ApplicationEventPublisher,
|
||||||
) : ReactiveAuthenticationManager {
|
) : ReactiveAuthenticationManager {
|
||||||
|
|
||||||
override fun authenticate(
|
override fun authenticate(
|
||||||
authentication: Authentication,
|
authentication: Authentication,
|
||||||
): Mono<Authentication> = accountRegistryService.authenticate(authentication.toAuthenticationRequest())
|
): Mono<Authentication> = accountRegistryService.authenticate(authentication.toAuthenticationRequest())
|
||||||
.map(::processToken)
|
.map(::processToken)
|
||||||
|
.doOnNext { publisher.publishEvent(AuthenticationSuccessEvent(it)) }
|
||||||
|
.doOnError { ex ->
|
||||||
|
if (ex is AuthenticationException) {
|
||||||
|
publisher.publishEvent(AuthenticationFailureBadCredentialsEvent(authentication, ex))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun processToken(
|
private fun processToken(
|
||||||
response: ltd.hlaeja.library.accountRegistry.Authentication.Response,
|
response: ltd.hlaeja.library.accountRegistry.Authentication.Response,
|
||||||
): Authentication = try {
|
): Authentication = try {
|
||||||
publicJwtService.verify(response.token) { claims -> makeRemoteAuthentication(claims) }
|
publicJwtService.verify(response.token) { claims -> makeRemoteAuthentication(claims) }
|
||||||
} catch (e: JwtException) {
|
} catch (e: JwtException) {
|
||||||
"An error occurred while processing token: ${e.message}".let {
|
throw "An error occurred while processing token: ${e.message}"
|
||||||
|
.let {
|
||||||
log.error(e) { it }
|
log.error(e) { it }
|
||||||
throw AuthenticationServiceException(it, e)
|
AuthenticationServiceException(it, e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +65,7 @@ class RemoteAuthenticationManager(
|
|||||||
claims: Jws<Claims>,
|
claims: Jws<Claims>,
|
||||||
): MutableList<SimpleGrantedAuthority> = (claims.payload["role"] as String)
|
): MutableList<SimpleGrantedAuthority> = (claims.payload["role"] as String)
|
||||||
.split(",")
|
.split(",")
|
||||||
.map { SimpleGrantedAuthority(it) }
|
.map { SimpleGrantedAuthority("ROLE_$it") }
|
||||||
.toMutableList()
|
.toMutableList()
|
||||||
|
|
||||||
private fun makeRemoteUserDetail(
|
private fun makeRemoteUserDetail(
|
||||||
|
|||||||
Reference in New Issue
Block a user